Question: bio python, sequence retrieval
0
gravatar for Balatheskulter
7 months ago by
Balatheskulter0 wrote:

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'
biopython python • 216 views
ADD COMMENTlink modified 7 months ago • written 7 months ago by Balatheskulter0
3
gravatar for geek_y
7 months ago by
geek_y11k
Barcelona
geek_y11k wrote:

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 COMMENTlink modified 7 months ago by Joe19k • written 7 months ago by geek_y11k

Thank you @geek_y. This helped me a lot.

ADD REPLYlink written 7 months ago by Balatheskulter0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1968 users visited in the last hour
_