我从一个文件中获得了一些不同的信息,这些信息已经分类到列表中,并希望将它们添加到嵌套字典中。
输入
exon 65419 65433 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 1
exon 65520 65573 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 2
CDS 65565 65573 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 2
exon 69037 71585 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 3
CDS 69037 70005 gene_id "ENSG00000186092"; transcript_id "ENST00000641515"; exon_number 3
exon 69055 70108 gene_id "ENSG00000186092"; transcript_id "ENST00000335137"; exon_number 1
CDS 69091 70005 gene_id "ENSG00000186092"; transcript_id "ENST00000335137"; exon_number 1
期望的输出
{'ENSG00000186092': {'ENST00000335137': {'exon_start': ['69055'],
'exon_stop': ['70108']},
'ENST00000641515': {'exon_start': ['65419', '65520', '69037'],
'exon_stop': ['65433', '65573', '71585']}}}
当前尝试
class Vividict(dict):
def __missing__(self, key):
value = self[key] = type(self)() # retain local pointer to value
return value # faster to return than dict lookup
all_info = Vividict()
for line in infile:
if not line.startswith("##"):
item = line.rstrip().split("\t")
info = item[8].split(";")
geneID = info[0].split(" ")[1]
geneID = geneID.strip('\"')
gtf_t_id = info[1].split(" ")[2]
gtf_t_id = gtf_t_id.strip('\"')
if item[2] == "exon":
num = info[6].split(" ")[2]
start = item[3]
stop = item[4]
if start in all_info[geneID][gtf_t_id]["exon_start"]:
all_info[geneID][gtf_t_id]["exon_start"].append(start)
else:
all_info[geneID][gtf_t_id]["exon_start"] = [start]
if stop in all_info[geneID][gtf_t_id]["exon_stop"]:
all_info[geneID][gtf_t_id]["exon_stop"].append(stop)
else:
all_info[geneID][gtf_t_id]["exon_stop"] = [stop]
当前结果
{'ENSG00000186092': {'ENST00000335137': {'exon_start': ['69055'],
'exon_stop': ['70108']},
'ENST00000641515': {'exon_start': ['69037'],
'exon_stop': ['71585']}}}
python大神给出的解决方案
您的代码可以正常工作,但是当开始/结束值是新值时它会不断初始化
并没有出现在该列表中,它会覆盖它并转到其他条件并使
新列表包含1个元素
class Vividict(dict):
def __missing__(self, key):
value = self[key] = type(self)() # retain local pointer to value
return value # faster to return than dict lookup
all_info = Vividict()
for line in infile:
if not line.startswith("##"):
item = line.rstrip().split("\t")
info = item[8].split(";")
geneID = info[0].split(" ")[1]
geneID = geneID.strip('\"')
gtf_t_id = info[1].split(" ")[2]
gtf_t_id = gtf_t_id.strip('\"')
if item[2] == "exon":
num = info[6].split(" ")[2]
start = item[3]
stop = item[4]
try:
if all_info[geneID][gtf_t_id]["exon_start"]:
all_info[geneID][gtf_t_id]["exon_start"].append(start)
except KeyError:
all_info[geneID][gtf_t_id]["exon_start"] = [start]
try:
if all_info[geneID][gtf_t_id]["exon_stop"]:
all_info[geneID][gtf_t_id]["exon_stop"].append(stop)
except KeyError:
all_info[geneID][gtf_t_id]["exon_stop"] = [stop]
用大写字母拆分字符串,但忽略AAA Python Regex - python我的正则表达式:vendor = "MyNameIsJoe. I'mWorkerInAAAinc." ven = re.split(r'(?<=[a-z])[A-Z]|[A-Z](?=[a-z])', vendor) 以大写字母分割字符串,例如:'我的名字是乔。 I'mWorkerInAAAinc”变成…
字符串文字中的正斜杠表现异常 - python为什么S1和S2在撇号位置方面表现不同?S1="1/282/03/10" S2="4/107/03/10" R1="".join({"N\'" ,S1,"\'" }) R2="".join({"N\'…
查找字符串中的行数 - python我正在创建一个python电影播放器/制作器,我想在多行字符串中找到行数。我想知道是否有任何内置函数或可以编写代码的函数来做到这一点:x = """ line1 line2 """ getLines(x) python大神给出的解决方案 如果换行符是'\n',则nlines …
将pandas数据框转换为唯一元组列表 - python将熊猫数据框转换为唯一元组列表的最有效方法是什么?在下面的代码中,我试图提取包含所有唯一PostalCode和Age的元组列表。from typing import NamedTuple, Sequence, Tuple import pandas as pd data = [["tom", 10, "ab 11"],…
在Flask中测试文件上传 - python我在Flask集成测试中使用Flask-Testing。我有一个表单,该表单具有我要为其编写测试的徽标的文件上传,但是我不断收到错误消息:TypeError: 'str' does not support the buffer interface。我正在使用Python3。我找到的最接近的答案是this,但是它对我不起作用。这是我的许多尝…