This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to write gffutils.feature.Feature object to file

How do you most efficiently write a collection of gffutils.feature.Feature objects to file, so that you can create a gff3 file from a collection of Feature objects? I am trying to create a gff3 file without the ##FASTA part at the bottom, so I think I dont need the actual sequence records. Options I so far have are

  • Writing my own code, converting a Feature object to string. Have to take care with converting the attributes and all its items to an appropriate string representation
  • Converting gffutils.feature.Feature to Bio.SeqIO.SeqRecord objects, and using GFF.write() to create my file
  • I found some (old?) documentation that you could convert at Feature object to string by str(my_feature) or my_feature.str() or my_feature.to_string() but all those didnt work
my_feature = gffutils.feature.feature_from_line('contig_A\texample\texon\t1\t100\t.\t+\t.\tName=seq_A')
str(my_feature)
my_feature.str()
my_feature.to_string()
gffutils python gff

1 answer

str(my_feature) works for me on gffutils v0.10.1:

import gffutils

with open('my.gff3', 'w') as fout:
    my_feature = gffutils.feature.feature_from_line('contig_A\texample\texon\t1\t100\t.\t+\t.\tName=seq_A')
    fout.write(str(my_feature) + '\n')

This issue should be relevant, in case you haven't seen it already.

Excellent, my issue was that I am using Feature objects instantiated by myself using the Feature constructor. When I used floats for certain values in the Feature attributes and then the str() function crashes on converting those floats. When I instantiate a Feature object where all values in the attributes are strings it goes well!

Log in to answer this question.