<p>I developed an answer as follows:</p>
<pre><code>from Bio import SeqIO
from Bio.SeqIO.FastaIO import FastaWriter
import os

os.chdir(&quot;C:/Users/&quot;)

with open(&quot;Spike protein.fasta&quot;, &quot;wt&quot;) as out:
    for seq_record in SeqIO.parse(&quot;Whole genome.fas&quot;, &quot;fasta&quot;):
        out.write(str(seq_record.id) + &quot;\n&quot;)
        out.write(str(seq_record.seq[21563:25384]) + &quot;\n&quot;)
</code></pre>

CC BY-SA 4.0
2020-11-13T14:30:40.240Z
2020-11-13T14:30:40.240Z
Theo Jones
<p>You can use <code>SeqIO</code> for the writing in addition to reading the sequences. This has the advantage that it will always write the correct fasta header, which is not always just the <code>.id</code> if there is whitespace, e.g. (untested)</p>
<pre><code>from Bio import SeqIO

with open(&quot;Spike protein.fasta&quot;, &quot;w&quot;) as out:
    for seq_record in SeqIO.parse(&quot;Whole genome.fas&quot;, &quot;fasta&quot;):
        seq_record.seq = seq_record.seq[21563:25384]
        SeqIO.write(seq_record, out, &quot;fasta&quot;)
</code></pre>
<p>BTW don't put spaces in your filenames, it can lead to issues</p>

CC BY-SA 4.0
2020-11-13T17:31:15.053Z
2020-11-14T17:45:56.350Z
2020-11-14T17:45:56.350Z
Chris_Rands
<p>I am trying to extract a specific sequence from a multifasta file, from each sequence in the aligned file.</p>
<p>The sequences look like this, and there are 32 sequences within the multiFASTA:</p>
<pre><code>&gt;NC_045512.2_Severe_acute_respiratory_syndrome_coronavirus_2_isolate_Wuhan-Hu-1_complete_genome 
ATT AAA GGT TTA TAC CTT CCC AGG TAA CAA ACC AAC CAA CTT TCG ATC TCT TGT AGA TCT GTT CTC TAA ACG AAC TTT 
AAA ATC TGT GTG GCT GTC ACT CGG CTG CAT GCT TAG TGC ACT CAC GCA GTA TAA TTA ATA ACT AAT TAC TGT CGT TGA 
CAG GAC ACG AGT AAC TCG TCT ATC TTC TGC AGG CTG CTT ACG GTT TCG TCC GTG TTG CAG CCG ATC ATC AGC ACA TCT 
AGG TTT CGT CCG GGT GTG ACC GAA AGG TAA GAT GGA GAG CCT TGT CCC TGG TTT CAA CGA GAA AAC ACA CGT CCA ACT 
</code></pre>
<p>Using the following code I can pull the sequence, using coordinates, from a single FASTA file, but how would one iterate this to pull the section (by coordinates) from each sequence within the multiFASTA?</p>
<pre><code>from Bio import SeqIO
from Bio.SeqIO.FastaIO import FastaWriter
import os

os.chdir(&quot;C:/Users/&quot;)

record = SeqIO.read(&quot;Whole genome.fas&quot;, &quot;fasta&quot;)

with open(&quot;Spike protein.fasta&quot;, &quot;wt&quot;) as out:
    writer = FastaWriter(out, wrap = [])
    writer.write_header()
    writer.write_record(record[21563:25384])
    writer.write_footer()
</code></pre>
<p>Thanks in advance!</p>

<fasta><python><covid-19><sars-cov-2><seqio>
Extract sequences from MultiFASTA aligned file, by coordinates
# Extract sequences from MultiFASTA aligned file, by coordinates

(Asked by: username_0 on 2020-11-13)

I am trying to extract a specific sequence from a multifasta file, from each sequence in the aligned file.
The sequences look like this, and there are 32 sequences within the multiFASTA:
>NC_045512.2_Severe_acute_respiratory_syndrome_coronavirus_2_isolate_Wuhan-Hu-1_complete_genome 
ATT AAA GGT TTA TAC CTT CCC AGG TAA CAA ACC AAC CAA CTT TCG ATC TCT TGT AGA TCT GTT CTC TAA ACG AAC TTT 
AAA ATC TGT GTG GCT GTC ACT CGG CTG CAT GCT TAG TGC ACT CAC GCA GTA TAA TTA ATA ACT AAT TAC TGT CGT TGA 
CAG GAC ACG AGT AAC TCG TCT ATC TTC TGC AGG CTG CTT ACG GTT TCG TCC GTG TTG CAG CCG ATC ATC AGC ACA TCT 
AGG TTT CGT CCG GGT GTG ACC GAA AGG TAA GAT GGA GAG CCT TGT CCC TGG TTT CAA CGA GAA AAC ACA CGT CCA ACT 

Using the following code I can pull the sequence, using coordinates, from a single FASTA file, but how would one iterate this to pull the section (by coordinates) from each sequence within the multiFASTA?
from Bio import SeqIO
from Bio.SeqIO.FastaIO import FastaWriter
import os

os.chdir("C:/Users/")

record = SeqIO.read("Whole genome.fas", "fasta")

with open("Spike protein.fasta", "wt") as out:
    writer = FastaWriter(out, wrap = [])
    writer.write_header()
    writer.write_record(record[21563:25384])
    writer.write_footer()

Thanks in advance!

**Tags:** fasta, python, covid-19, sars-cov-2, seqio

---

## 2 answers

### Answer by username_0 on 2020-11-13. Score: 1 | Accepted answer

I developed an answer as follows:
from Bio import SeqIO
from Bio.SeqIO.FastaIO import FastaWriter
import os

os.chdir("C:/Users/")

with open("Spike protein.fasta", "wt") as out:
    for seq_record in SeqIO.parse("Whole genome.fas", "fasta"):
        out.write(str(seq_record.id) + "\n")
        out.write(str(seq_record.seq[21563:25384]) + "\n")


---

### Answer by username_1 on 2020-11-13. Score: 2

You can use SeqIO for the writing in addition to reading the sequences. This has the advantage that it will always write the correct fasta header, which is not always just the .id if there is whitespace, e.g. (untested)
from Bio import SeqIO

with open("Spike protein.fasta", "w") as out:
    for seq_record in SeqIO.parse("Whole genome.fas", "fasta"):
        seq_record.seq = seq_record.seq[21563:25384]
        SeqIO.write(seq_record, out, "fasta")

BTW don't put spaces in your filenames, it can lead to issues


---

