Python 在创建元组时迭代字典

Python 在创建元组时迭代字典,python,dictionary,tuples,Python,Dictionary,Tuples,我正在学习python,并尝试使用map和tuple。我已经从一个解析过的文件创建了一个字典,现在正在另一个文件中解析。我希望遍历字典,并用从字典中获取的ID替换解析文件每行的第一个元素 我的字典: for line in blast_lines: (transcript,swissProt,identity) = parse_blast(blast_line=line) transcript_to_protein[transcript] = swissProt 在文件中进行解

我正在学习python,并尝试使用map和tuple。我已经从一个解析过的文件创建了一个字典,现在正在另一个文件中解析。我希望遍历字典,并用从字典中获取的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))
我没有在这里包含所有代码,因为我确信我的问题一定是如何迭代解析文件和字典,但我将在底部包含所有内容

投入:

blast(字典中存储的内容)

对于这一行,成绩单是c1000_g1_i1,瑞士的prot是O94288.1

矩阵(正在分析的文件)

如果第一个字段中的值与字典中的键(转录本)匹配,我将尝试用字典中的swissProt替换第一个字段(matrixFields[0])

我想要一个像这样的输出

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个都具有相同的值,而不是字典中的单个成绩单

完整代码:

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))

首先,
parse_blast()
中有一个bug-它没有返回元组
(转录本、swissProt、标识)
,而是返回
(转录本、基、标识)
,并且
base
不包含缺少的信息

更新

其次,
parse_matrix()
中还有一个bug。从文件中读取的第一个字段没有丢失的信息,但是,当
matrixFields[0]
位于
transcript\u to\u protein
字典中时,它会将这些信息放入返回的元组中


仅仅解决一个问题本身并不能解决问题。

似乎问题可能出在parseblast函数中。 用于线路

因此,主语应该是gi | 48474761 | sp | O94288.1 | NOC3 | u SCHPO

然后

swissProt = subjectIds[3]
swissProt将是O94288.1,函数将使用它进一步拆分。在线

base = swissProt.split(".")[0]

最终结果是swissprot将是094288,而不是| O94288.1,这似乎是您所期望的。我建议在单行输入上测试该函数,直到获得所需的输出。错误出现在我的字典调用中,因为我想将matrixFields[0]与字典中的成绩单匹配,所以我尝试使用
if matrixFields[0]搜索字典在转录到蛋白质中:
,但需要指定字段

trasncript = matrixfields[0]
if transcript in transcript_to_protein:
        protein = transcript_to_protein.get(transcript)

更正后,它仍然为所有替换的字段打印相同的值,而不是遍历字典,而不是为所有字段打印最后一个值。对于单行,它工作正常,问题是它只是为所有行打印相同的swissprot id,而不是与字典中的键匹配的行
swissProt = subjectIds[3]
base = swissProt.split(".")[0]
trasncript = matrixfields[0]
if transcript in transcript_to_protein:
        protein = transcript_to_protein.get(transcript)