Extract a part of fasta sequence based on bp coordinates
<p>I have a huge fasta file, but I need to extract only a part of it, If I know the start and end base pair coordinate of my sequence. Also, it should be in fasta format with the length of 60 bp per line.This is my try, please let me know if looks OK and any suggestions to improve it are welcome.</p>

<pre><code>from Bio import SeqIO

inFile = open('full_chr.fa','r')
fw=open("part.fa",'w')
line_width = 60
for record in SeqIO.parse(inFile,'fasta'):
    fw.write("&gt;" + record.id + "\n")
    fww = (str(record.seq[600130000:602000000]) + '\n')
    for i in xrange(0,len(fww),line_width):
        fw.write(str(fww[i:i+line_width]) + '\n')
fw.close()
</code></pre>

<p>It's as easy as:</p>

<pre><code>from Bio import SeqIO


record = SeqIO.read("Chromosome.fas", "fasta")

with open("output.fas", "w") as out:
    SeqIO.write(record[100:500], out, "fasta")
</code></pre>

<p>The <code>SeqIO.write</code> already uses a 60 character length wrapping. If you want to manipulate the line wrap use the <code>FastaWriter</code> object. This is an example for 80 bp lines:</p>

<pre><code>from Bio import SeqIO
from Bio.SeqIO.FastaIO import FastaWriter


record = SeqIO.read("Chromosome.fas", "fasta")

with open("output.fas", "w") as out:
    writer = FastaWriter(out, wrap=80)
    writer.write_header()
    writer.write_record(record[100:500])
    writer.write_footer()
</code></pre>

python bioinformatics biopython fasta