You also only need parse() if your input file contains more than 1 sequence, else you can use SeqIO.read() - this would mean you then didn't need to use the loop necessarily (though this is a more robust/general approach anyway)
Hello
I'm trying to import a fasta sequence using the SeqIO library and i'm having trouble using a method on my sequence - of which I want the console to spit out the resultant mRNA sequence.
- import library: from Bio import SeqIO
- set sequence object: sequence = SeqIO.parse(open(fastaFile),'fasta')
enter the loop in order to access the sequence data (which is now stored as 2 data members .id and .seq in the list/array records): for sequence in record:
This is where i'm having trouble:
>>>for sequence in records: . . . mRNA = record.seq.transcribe() . . . print mRNA
I want to take the data stored in record.id and by using function transcribe(), perform the function and set it equal to a new object value that I can print. Where am I going wrong? Do I need to create a list object mRNA in order to load the record.id?
Thank you
2 answers
There are quite a few things wrong with your code. This isn't tested (just from memory), but should get you closer:
from Bio import SeqIO
records = SeqIO.parse('/path/to/file.fasta', 'fasta') # You don't need open() with SeqIO.parse
for sequence in records:
mrna = sequence.seq.transcribe()
print mrna
This was correct. Thank you! - I was not accessing the sequence member properly.
In case this helps for future reference too, while in the python interpreter, you can use tab autocompletion to see what attributes a particular object has.
e.g.
>>> sequence.seq. #tab tab
Will show you all the following associated methods etc:
sequence.seq.__add__(
sequence.seq.__class__(
sequence.seq.__contains__(
sequence.seq.__delattr__(
sequence.seq.__dict__
sequence.seq.__doc__
sequence.seq.__eq__(
sequence.seq.__format__(
sequence.seq.__getattribute__(
sequence.seq.__getitem__(
sequence.seq.__hash__(
sequence.seq.__init__(
sequence.seq.__le__(
sequence.seq.__len__(
sequence.seq.__lt__(
sequence.seq.__module__
sequence.seq.__ne__(
sequence.seq.__new__(
sequence.seq.__radd__(
sequence.seq.__reduce__(
sequence.seq.__reduce_ex__(
sequence.seq.__repr__(
sequence.seq.__setattr__(
sequence.seq.__sizeof__(
sequence.seq.__str__(
sequence.seq.__subclasshook__(
sequence.seq.__weakref__
sequence.seq._data
sequence.seq._get_seq_str_and_check_alphabet(
sequence.seq.alphabet
sequence.seq.back_transcribe(
sequence.seq.complement(
sequence.seq.count(
sequence.seq.count_overlap(
sequence.seq.endswith(
sequence.seq.find(
sequence.seq.lower(
sequence.seq.lstrip(
sequence.seq.reverse_complement(
sequence.seq.rfind(
sequence.seq.rsplit(
sequence.seq.rstrip(
sequence.seq.split(
sequence.seq.startswith(
sequence.seq.strip(
sequence.seq.tomutable(
sequence.seq.tostring(
sequence.seq.transcribe(
sequence.seq.translate(
sequence.seq.ungap(
sequence.seq.upper(
This is similar to the outcome of using dir(sequence.seq)
Log in to answer this question.
What error are you getting?
Are you sure that
.transcribe()is a method of.id?I'm not getting any errors, it simply doesn't print the object MRNA so I figured my syntax is wrong. And no I don't think .transcribe() is a method of .id. I believe it's a built in method in bio python library Seq
You probably should call transcribe() on the record itself.
record is an array of .id and .seq so I dont think i'm declaring the object mRNA correctly?, Or i'm not passing the .seq/.id data member into the object correctly because that still isn't proper syntax for it to print mRNA.
Thanks
Please add code markup to your post for increased readability. You can do this by selecting the text and clicking the 101010 button. When you compose or edit a post that button is in your toolbar, see image below:
This may conflict with numbering, but spending some effort properly formatting a post will make things much clearer for us.