我正在用python解析这样一个文件: 1bgxt是蛋白质,序列和二级结构,phi角是信息的类型,序列是我想要的一个包含蛋白质的列表中的信息。 关于蛋白质的信息是交替的这一事实给我带来了一些麻烦。 文件示例(它可以有2个以上的蛋白质和2个以上的序列信息):
P1;1bgxt
sequence
MRGMLPLFEPKGRVLLVDGHHLAYRTFHALKGLTTSRGEPVQAVYGFAKSLLKALKEDGDAVIVVFDAKAPSFRH*
P1;1xo1a
sequence
----------RRNLMIVDGTNLGFRFP--------------FASSYVSTIQSLAKSYSARTTIVLGDKG-KSVFR*
P1;1bgxt
secondary structure and phi angle
CPPCCCPPPCPCPCCCCCCCCHHHHCCCCPCCCCCCCPCCCCCCCCHHHHHHHHHHCPCCCCCCCCCCCCCCCCC*
P1;1xo1a
secondary structure and phi angle
----------CCEEEEEEHHHHHCCCC--------------CHHHHHHHHHHHHHHCPEEEEEEECCCP-CCHHH*
以此类推,有更多的序列。你知道吗
我想像这样存储信息:
list = [
["1bgxt",
"MRGMLPLFEPKGRVLLVDGHHLAYRTFHALKGLTTSRGEPVQAVYGFAKSLLKALKEDGDAVIVVFDAKAPSFRH*",
CPPCCCPPPCPCPCCCCCCCCHHHHCCCCPCCCCCCCPCCCCCCCCHHHHHHHHHHCPCCCCCCCCCCCCCCCCC*],
["1xo1a",
"----------RRNLMIVDGTNLGFRFP--------------FASSYVSTIQSLAKSYSARTTIVLGDKG-KSVFR*",
"----------CCEEEEEEHHHHHCCCC--------------CHHHHHHHHHHHHHHCPEEEEEEECCCP-CCHHH*"]
]
我真的很感激你能提供的任何帮助。你知道吗
因此,假设您有一个遵循以下粗略结构的文件:
解析此文件的“诀窍”在于,一次只需按3行一组读取文件,因为文件的结构很容易分解为以下重复周期:
我之所以做出这些基本假设,是因为你在评论中提到:
解析成
dict
首先,我要说的是,您在原始帖子中想要的结构对于解析数据来说可能是次优的,并且您拥有的数据可以巧妙地融入到一个关联数据结构中,比如
dict
。你知道吗假设您有一个类似于上面的文件,名为
sequences.txt
:这将输出如下结构:
如果您想获得列表列表,只需在
proteins
上调用.values()
:直接解析成
list
的list
如果您不想像在最后一段代码中那样使用
dict
,那么这涉及到一些不同的工作,但是您只需要更改几行。你知道吗请注意,给定足够大的数据集,此版本可能需要更多的时间,因为它会在每次需要查找预先存在的蛋白质以将结构数据添加到现有子列表时扫描列表(一个
O(n)
操作),而在字典中,查找蛋白质数据通常是O(1)
。尽管对你有用的东西可能会因你使用数据的目的而有所不同,但我离题了。你知道吗这将输出:
请注意,我不做任何类似于严格的错误检查的事情,为了清晰起见,我尝试使用比惯用代码更具说明性的代码。我也没有尝试过使用类之类的东西来存储数据,或者使用元组或
namedtuple
来存储所有相关的数据,因为这需要对文件进行更多的预处理,以获得可能最小的有用性(元组在Python中是不可变的,所以我不能只修改元组而不创建一个新元组)。你知道吗相关问题 更多 >
编程相关推荐