Why not just use regex capture groups? I believe this would be much more efficient.
I want to extract some base pairs from a FASTA file. There is a given sequence, say marker sequence. Every time this marker sequence occurs in the FASTA file, I want to extract n base pairs to the left of it (before it). Is it possible to do so using biopython? If so, please tell.
Thank you.
PS - I know it seems quite simple. But I am very new to python. And I have to do this using biopython only. So finding it very difficult to understand from the cookbook.
2 answers
Approach that you could take:
- Use Bio Seq IO to parse
- Use regex to match sequence to marker pattern
- If found, use substring to extract target sequence using match index from above regex match
OP wishes to use BioPython. Come to think of it, the way OP has phrased the question makes me think this could possibly be an assignment question.
As mentioned by RamRS, you could use Bio.SeqIO for parsing the FASTA file. Or if you prefer plain strings, from Bio.SeqIO.FastaIO import SimpleFastaParser might be useful?
If you are looking for an exact substring match, both the Python string object and the Biopython Seq (sequence) object both offer a .find(...) method.
If you are looking for a more complicated pattern, then as RamRS suggested the Python regular expression library might be a good choice.
Log in to answer this question.