使用biopython操作gff文件

时间:2014-09-09 11:57:47

标签: python bioinformatics biopython

我有一个GFF文件,这是一个限制为9列的文件。我的Gff文件如下所示:

chr1    GenBank region  1   2821361 .   +   1   ID=CP000253.1
chr1    S-MART  utr5    313 516     .   +   .   ID=CP000253.1|+313..516
chr1    GenBank gene    517 1878    .   +   1   ID=SAOUHSC_00001

.........等等。

问题陈述:

现在,我想合并满足条件的行。条件是第i行的第5列值应该等于i + 1行的第4列减1。

所以最终的结果应该是

chr1    GenBank region  1   2821361 .   +   1   ID=CP000253.1
chr1    predict TU      313 1878    .   +   1   ID=SAOUHSC_00001

为此,我编写了以下程序:

from BCBio import GFF
from Bio.SeqFeature import SeqFeature, FeatureLocation

in_file = "infile.gff"
out_file = "outfile.gff"

limit_info = dict(
        gff_type = ['CDS','exon','gene','mRNA','operon','rRNA','tRNA','utr3','utr5'])
new_qualifiers = {"source": "prediction","ID": "CP000253.1"}
new_sub_qualifiers = {"source": "prediction"}
new_top_feature = SeqFeature(FeatureLocation(0, 2821361), type="genomeRegion", strand=1,
                         qualifiers=new_qualifiers)
i=0

in_handle = open(in_file)
for rec in GFF.parse(in_handle, limit_info=limit_info):
    for i in range(10):
        if rec.features[i].location.end == rec.features[i+1].location.start :
            # print rec.features[i]
            new_top_feature.sub_features[i] =     
[SeqFeature(FeatureLocation(rec.features[i].location.start ,  
rec.features[i+1].location.end ,strand=rec.features[i].strand),  
type="Transcription_unit",  qualifiers=new_sub_qualifiers)]             

in_handle.close()

rec.features = [new_top_feature]

with open(out_file, "w") as out_handle:
    GFF.write([rec], out_handle)

我收到以下错误:

/usr/lib/python2.7/dist-packages/Bio/SeqFeature.py:171: BiopythonDeprecationWarning: Rather using f.sub_features, f.location should be a CompoundFeatureLocation
  BiopythonDeprecationWarning)
Traceback (most recent call last):
  File "/home/nkumar/workplacekepler/random/src/limit.py", line 26, in <module>
    new_top_feature.sub_features[i] = [SeqFeature(FeatureLocation(rec.features[i].location.start , rec.features[i+1].location.end ,strand=rec.features[i].strand), type="Transcription_unit",  qualifiers=new_sub_qualifiers)]
IndexError: list assignment index out of range

即使它是一个超出范围的索引错误,我也无法弄清楚,出了什么问题?

in_handle = open(in_file)
for rec in GFF.parse(in_handle, limit_info=limit_info):
    for i in range(10):        
        if rec.features[i].location.end == rec.features[i+1].location.start :
            print 1          
        else:
            print rec.features[i]            
in_handle.close()

这个功能完美,可以打印所有功能。

1 个答案:

答案 0 :(得分:0)

您将new_top_feature定义为:

type: genomeRegion
location: [0:2821361](+)
qualifiers: 
    Key: ID, Value: CP000253.1
    Key: source, Value: prediction

但它没有子功能

>>> print new_top_feature.sub_features
[]
因此,

new_top_feature.sub_features是一个空列表。您无法直接分配到空列表:

>>> a = []
>>> a[0] = 3
Traceback (most recent call last):
  File "<input>", line 1, in <module>
IndexError: list assignment index out of range

这就是你在做什么

new_top_feature.sub_features[i] =  .....

要将数据添加到此列表,您应该使用append而不是索引。如果您需要按照顺序填写给定位置的列表,您可以创建一个用零填充的足够大小的列表,然后在它们到来时将值分配给位置。