我正在學習python,並試圖在地圖和元組上工作。我從一個解析的文件創建了一個字典,並在另一個文件中解析。我想通過詞典進行迭代,並與來自字典獲得的ID替換已解析的文件的每一行的第一個元素在創建元組時迭代字典
我的字典:
for line in blast_lines:
(transcript,swissProt,identity) = parse_blast(blast_line=line)
transcript_to_protein[transcript] = swissProt
解析該文件中,以及創建一個元組如果該ID
def parse_matrix(matrix_line):
matrixFields = matrix_line.rstrip("\n").split("\t")
protein = matrixFields[0]
if matrixFields[0] in transcript_to_protein:
protein = transcript_to_protein.get(transcript)
matrixFields[0] = protein
return(tuple(matrixFields))
我沒有包括所有的在這裏我的代碼條目存在,因爲我相信我的問題一定是我如何通過迭代,將有從字典作爲第一個元素的值解析文件和字典,但我會包括一切都在底部。
輸入:
爆炸(什麼是存儲在字典)
c1000_g1_i1|m.799 gi|48474761|sp|O94288.1|NOC3_SCHPO 100.00 747 0 0 5 751 1 747 0.0 1506
此行的成績單是c1000_g1_i1,瑞士PROT是O94288.1
矩陣(文件是解析)
c3833_g1_i2 4.00 0.07 16.84 26.37
我想取代第一個字段(matrixFi elds [0]),如果第一個字段中的值與字典中的鍵(transcript)相匹配,則使用swissProt。
我想要的輸出看起來像這樣
Q09748.1 4.00 0.07 16.84 26.37
O60164.1 24.55 116.87 220.53 28.82
C5161_G1_I1 107.49 89.39 26.95 698.97
P36614.1 27.91 72.57 5.56 36.58
P37818.1 82.57 19.03 48.55 258.22
但正在此:
O94423.1 4.00 0.07 16.84 26.37
O94423.1 24.55 116.87 220.53 28.82
C5161_G1_I1 107.49 89.39 26.95 698.97
O94423.1 27.91 72.57 5.56 36.58
O94423.1 82.57 19.03 48.55 258.22
注意如何他們全部的4具有相同的價值,而不是單獨的成績單從字典
Full code:
transcript_to_protein = {};
def parse_blast(blast_line="NA"):
fields = blast_line.rstrip("\n").split("\t")
queryIdString = fields[0]
subjectIdString = fields[1]
identity = fields[2]
queryIds = queryIdString.split("|")
subjectIds = subjectIdString.split("|")
transcript = queryIds[0].upper()
swissProt = subjectIds[3]
base = swissProt.split(".")[0]
return(transcript, swissProt, identity)
blast_output = open("/scratch/RNASeq/blastp.outfmt6")
blast_lines = blast_output.readlines()
for line in blast_lines:
(transcript,swissProt,identity) = parse_blast(blast_line=line)
transcript_to_protein[transcript] = swissProt
def parse_matrix(matrix_line):
matrixFields = matrix_line.rstrip("\n").split("\t")
matrixFields[0] = matrixFields[0].upper()
protein = matrixFields[0]
if matrixFields[0] in transcript_to_protein:
protein = transcript_to_protein.get(transcript)
matrixFields[0] = protein
return(tuple(matrixFields))
def tuple_to_tab_sep(one_tuple):
tab = "\t"
return tab.join(one_tuple)
matrix = open("/scratch/RNASeq/diffExpr.P1e-3_C2.matrix")
newline = "\n"
list_of_de_tuples = map(parse_matrix,matrix.readlines())
list_of_tab_sep_lines = map(tuple_to_tab_sep, list_of_de_tuples)
print(newline.join(list_of_tab_sep_lines))
隨着該修正它仍然打印所有更換領域,而不是通過字典迭代相同的值需要,而不是打印過的最後一個值它們全部 –