簡體   English   中英

如何使用帶有列表的字典將來自多個文件的輸入合成並打印到輸出中的每個鍵的一行中?

[英]How to use dictionaries with a list to synthesize and print inputs from multiple files into a single line for each key in output?

假設我有一個如下所示的文件:

基因名 實驗1 實驗2
A1BG 0.031474 0.05776
ZNF621 0.091025 0.33516
ARHGAP12 0.97852 0.14098

等等…

另一個看起來像這樣的文件:

基因名稱 基因描述 染色體數目 染色體位置
A1BG α-1B-糖蛋白 19 19q13.43
A2M α-2-巨球蛋白 12 12p13.31
A3M α-3-巨球蛋白 12 12p13.33

等等…

我制作了 2 個字典,一個將基因名稱(鍵)與基因注釋/描述(值)相匹配,另一個將基因名稱(鍵)與基因染色體編號(值)相匹配。

我的目標是制作一個輸出文件,其中我將第一個表格(以實驗已經創造出來了。 所以本質上,這將導致兩個文件中存在的每個基因的輸出文件格式如下。 如果文件中不存在一個基因,則最后 2 個字段應為 NA(如下例中的第二列)

基因名 實驗1 實驗2 基因描述 染色體數目
A1BG 0.03147 0.05776 α-1B-糖蛋白 19
ZNF621 0.091025 0.33516 不適用 不適用

我已按以下方式設置我的字典:

infile = open("human_gene_annotations.txt", "rt")

#separate header
gene_header = infile.readline()

#gene annotation dict
gene_annotations = {}
#use for loop to fill
for line in infile:
    line = line.rstrip()
    information = line.split("\t") 
    gene_annotations[information[0]] = {"Gene Description": information[1]}
#close infile 
infile.close()


#open infile again for second dictionary 
infile = open("human_gene_annotations.txt", "rt")
#separate header
gene_header = infile.readline()

#gene chroms dict
gene_chroms = {}
#use for loop to fill
for line in infile:
    line = line.rstrip()
    info_chrom = line.split("\t") 
    gene_chroms[info_chrom[0]] = {"Chromosome Number": info_chrom[2]}
#close infile 
infile.close()

我已將第一個表(來自實驗的表)中的數據解析為如下列表:

genes = [] 
exp1values = []
exp2values = []

for line in infile:
    line = line.rstrip()
    fields = line.split("\t") # this will split the line we read by tab, thus by "column"
    genes.append(fields[0])
    exp1values.append(fields[1])
    exp2values.append(fields[2])

為什么不為第一個表創建一個字典

我正在使用您用於第二個表的現有代碼塊,只有一個例外。 作為基因描述字典和染色體編號字典的值,我將只存儲數字而不是各自的文本

infile = open("human_gene_annotations.txt", "rt")

#separate header
gene_header = infile.readline()

#gene annotation dict
gene_annotations = {}
#use for loop to fill
for line in infile:
    line = line.rstrip()
    information = line.split("\t")
    gene_annotations[information[0]] = information[1] 
#close infile 
infile.close()


#open infile again for second dictionary 
infile = open("human_gene_annotations.txt", "rt")
#separate header
gene_header = infile.readline()

#gene chroms dict
gene_chroms = {}
#use for loop to fill
for line in infile:
    line = line.rstrip()
    info_chrom = line.split("\t") 
    gene_chroms[info_chrom[0]] = info_chrom[2]
#close infile 
infile.close()

現在對於第一個表,我將為這兩個實驗制作另外 2 個詞典


exp1map= {}
exp2map= {}

for line in infile:
    line = line.rstrip()
    fields = line.split("\t") # this will split the line we read by tab, thus by "column"
    exp1map[fields[0]]=fields[1]
    exp2map[fields[0]]=fields[2]

現在我不知道你到底想要輸出表的輸出,但我假設你想將數據寫入制表符分隔的文件

#Create a unique set of all genes from both table 1 and table 2
all_genes = set().union(*[exp1map.keys(),gene_annotations.keys()])

with open('output', 'w') as f:
   f.write('\t'.join(['Gene.name','Experiment.1','Experiment.2','Gene description','Chromosome location']) + '\n')

   for gene in all_genes:
      exp1=exp1map.get(gene,None)
      exp2=exp2map.get(gene,None)
      desc=gene_annotations.get(gene,None)
      chrom=gene_chroms.get(gene,None)

      f.write('\t'.join([exp1,exp2,desc,chrom]) + '\n')
   

我無法測試我的代碼,因為我沒有數據集,但我認為它解決了您的問題。 如果我需要更多幫助或我犯了錯誤,請告訴我

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM