Thanks for your reply.
You seem to have a few things mixed up.
First, you need to import SeqIO, and not just Bio. Your code would throw an error at line 2 since SeqIO is not defined.
from Bio import SeqIO
I don't know if it is a feature of jupyter notebook or not but just writing import Bio works as good as what you wrote. Though I would agree just importing SeqIO would be faster than importing the whole biopython package ?
Second, your description and code don't match. You write that you want the first n bases of each read, but take the last n bases ( and use a "end" list.).
I made several tests by printing record[:n] and it was effectively printing the first n bases of the sequence of the record. My description was a bit misleading though, I'm sorry.. I wanted to make my question simpler to understand but what I actually want is to take the first n bases and write them to a file and do the same for the last n bases. So in my first code I had two list, one called end5 (for the first n bases) and one called end3 (for the last n bases).
Third, you are using the .append() method wrongly. Append is a method of a list, so of end. Instead, you want to do:
end.append(record[:n])
That's probably the main reason what it didn't seem to work..
Finally, I don't think there is a good reason here to store everything in a list. That's probably not a problem with your current dataset, but not very memory efficient. I'd suggest writing records out immediately.
So my suggestion would be:
import sys from Bio import SeqIO n = 50 for record in SeqIO.parse(sys.argv[1],"fasta") print(record[:n].format("fasta"))
I've got a file of 1.5million sequences to slice so writting everything to a list was definitely not a good idea but I had no idea how to do without it, so thanks a lot ! I'm still learning python by myself so I make rookie mistakes ..
Save this script as slice_last_50.py or any other name you like and use as:
python slice_last_50.py myinputfile.fasta > myoutputfile.fasta
I eventually figured it would be possible to do that but I must admit I am surprised there is no way to write a multifasta file at once.
Thanks again for your help, I will try that and will come back to accept your answer when I have it working !