bio python, sequence retrieval
1
0
Entering edit mode
3.8 years ago

I needed to pick up a sequence from a fast file, that fast file contains nearly 200 contigs. I needed to pick up a certain sequence from particular contigs. I have used this command but this command is not proper, I got the following error. Please help me out.

  from Bio import SeqIO
    with open("outfile2.txt","w") as f:
     for seq in SeqIO.parse("RI_solani.fa","fasta"):
            chrs={}
            chrs[seq.id] = seq.seq
            f.writestrseq.id) + "\n")
            f.write(str(chrs['KB317696.1'][0:70]))
            f.write(str(chrs['KB317696.1'][70:140]) + "\n")

     KeyError                                  Traceback (most recent call last)
 <ipython-input-23-e727c4ed6266> in <module>
  5                 chrs[seq.id] = seq.seq
  6                 f.writestrseq.id) + "\n")
  --> 7                 f.write(str(chrs['KB317696.1'][0:70]))
  8                 f.write(str(chrs['KB317696.1'][70:140]) + "\n")

 KeyError: 'KB317696.1'
python biopython • 1.1k views
ADD COMMENT
3
Entering edit mode
3.8 years ago

Biopython documentation is very clear.

for seq in SeqIO.parse("RI_solani.fa","fasta")
    if seq.id == "KB317696.1":
        seq.seq = seq.seq[0:70]
        SeqIO.write(seq, "test_out.fa","fasta")

If you want more slices,

out_file = open("test_out.fa", "w")

for seq in SeqIO.parse("RI_solani.fa","fasta"):

    if seq.id == "KB317696.1":

        out_file.write(">" + seq.id + "\n")
        out_file.write( str(seq.seq[0:3]) + "\n")
        out_file.write( str(seq.seq[5:9]) + "\n")

out_file.close()

There are multiple ways of doing it, but from your code, its apparent that you haven't figured out basic python. I would suggest to spend more time in understanding python before jumping to use modules.

ADD COMMENT
0
Entering edit mode

Thank you @geek_y. This helped me a lot.

ADD REPLY

Login before adding your answer.

Traffic: 2711 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6