<p>There is nothing wrong with the concept of the code, that is exactly how I would do it. The problem is here:</p>
<pre><code>    else:
output.write(line)
</code></pre>
<p>Corrections</p>
<pre><code>import os
output = 'newseq.fasta'
if os.path.exists(output):
    os.unlink(output) 

for line in fasta:
    if line.startswith('&gt;'): # getting just the header
        header= line.strip().split(&quot;|&quot;) # there are several entries on the header separated by the |
        for name in one:
           if name in header: 
              line = header.insert(0,line.pop(header.index(name))) # insert the name from file1 as the first header entry
        else: # if no match in file1 look at file2
            for name in two:
               if name in header:
                    line = header.insert(0,line.pop(header.index(name))) 

# The indentation must be absolutely correct 
# otherwise the code falls over. Must be one indent from
# the first for statement
    with open(output, 'a') as fout:
        fout.write(line)
</code></pre>
<p>I assume that <code>fasta</code>, <code>one</code> and <code>two</code> are loaded successfully.</p>

CC BY-SA 4.0
2022-11-14T21:21:05.693Z
Thanks :-) @NoraJob upvoted previously.
M__
CC BY-SA 4.0
2022-11-13T16:12:45.023Z
2022-11-13T16:19:09.203Z
2022-11-13T16:19:09.203Z
M__
<p>I want to compare the headers from the fasta file to file1, and if there's a match, reorganize the header and put the match first. If there's no match between fasta file and file1,  look at file2 and if there's a match, put that name first on the header.</p>
<p>The goal is to organize the fasta file header with the similar match listed as the first entry on the header. How can I do this with this code or is there another way to write it in python or biopython?</p>
<p><code>BioPython</code> solutions welcome.</p>
<p>Current files look like this</p>
<p>seqs.fasta:</p>
<pre><code>&gt;ant| bee| fly 
AGCT... 
&gt;bird| eagle| hawk| vulture 
GATCG...
</code></pre>
<p>file1.txt:</p>
<pre><code>mouse 
hawk 
cow
</code></pre>
<p>file2.txt:</p>
<pre><code>crane 
fly
</code></pre>
<p>The output file should look like this, so that the order of the header starts with the match from file 1 or file 2:</p>
<pre><code>&gt; fly| ant| bee 
AGCT... 
&gt; hawk| bird| eagle| vulture 
AGCT... 
</code></pre>
<p>My code is:</p>
<pre class="lang-py prettyprint-override"><code>fasta= open('seq.fasta', 'r')
one= open('file1.txt' , 'r')
two= open('file2.txt' , 'r')
output= open('newseq.fasta', 'w')

for line in fasta:
    if line.startswith('&gt;'): # getting just the header
        header= line.strip().split(&quot;|&quot;) # there are several entries on the header separated by the |
        for name in one:
           if name in header: # looking to see if file1.txt has match in fasta file
              header.insert(0,line.pop(header.index(name))) # insert the name from file1 as the first header entry
        else: # if no match in file1 look at file2
            for name in two:
               if name in header:
                    header.insert(0,line.pop(header.index(name))) # insert the name from file2 as first header entry   
    else:
output.write(line)
</code></pre>

<python><fasta><phylogenetics><phylogeny>
Compare and Reorganize Fasta Headers Python
# Compare and Reorganize Fasta Headers Python

(Asked by: username_0 on 2022-11-09)

I want to compare the headers from the fasta file to file1, and if there's a match, reorganize the header and put the match first. If there's no match between fasta file and file1,  look at file2 and if there's a match, put that name first on the header.
The goal is to organize the fasta file header with the similar match listed as the first entry on the header. How can I do this with this code or is there another way to write it in python or biopython?
BioPython solutions welcome.
Current files look like this
seqs.fasta:
>ant| bee| fly 
AGCT... 
>bird| eagle| hawk| vulture 
GATCG...

file1.txt:
mouse 
hawk 
cow

file2.txt:
crane 
fly

The output file should look like this, so that the order of the header starts with the match from file 1 or file 2:
> fly| ant| bee 
AGCT... 
> hawk| bird| eagle| vulture 
AGCT... 

My code is:
fasta= open('seq.fasta', 'r')
one= open('file1.txt' , 'r')
two= open('file2.txt' , 'r')
output= open('newseq.fasta', 'w')

for line in fasta:
    if line.startswith('>'): # getting just the header
        header= line.strip().split("|") # there are several entries on the header separated by the |
        for name in one:
           if name in header: # looking to see if file1.txt has match in fasta file
              header.insert(0,line.pop(header.index(name))) # insert the name from file1 as the first header entry
        else: # if no match in file1 look at file2
            for name in two:
               if name in header:
                    header.insert(0,line.pop(header.index(name))) # insert the name from file2 as first header entry   
    else:
output.write(line)

**Tags:** python, fasta, phylogenetics, phylogeny

---

## 1 answers

### Answer by username_1 on 2022-11-13. Score: 1 | Accepted answer

There is nothing wrong with the concept of the code, that is exactly how I would do it. The problem is here:
    else:
output.write(line)

Corrections
import os
output = 'newseq.fasta'
if os.path.exists(output):
    os.unlink(output) 

for line in fasta:
    if line.startswith('>'): # getting just the header
        header= line.strip().split("|") # there are several entries on the header separated by the |
        for name in one:
           if name in header: 
              line = header.insert(0,line.pop(header.index(name))) # insert the name from file1 as the first header entry
        else: # if no match in file1 look at file2
            for name in two:
               if name in header:
                    line = header.insert(0,line.pop(header.index(name))) 

# The indentation must be absolutely correct 
# otherwise the code falls over. Must be one indent from
# the first for statement
    with open(output, 'a') as fout:
        fout.write(line)

I assume that fasta, one and two are loaded successfully.

> **Comments:**
> **username_1**: Thanks :-) @NoraJob upvoted previously.  

---

