This is a test version of Biostars. For the public version, visit https://www.biostars.org.
bio python, sequence retrieval

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 answer

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.

Log in to answer this question.