<p>its easy replace <code>match</code> with <code>sub</code> but please stop using <code>list</code> as a variable ... mylist is fine.</p>
<p>This might work</p>
<pre><code>   mylist = re.sub(r'H149xcV_\w+_\w+_\w+', gene_id)
</code></pre>
<p>otherwise</p>
<pre><code>myregex = re.compile('_\w+\s+.*') 
fastaid = myregex.sub('', myfile)
</code></pre>
<p>OR from @MaximilianPress</p>
<pre><code>myregex2 = re.compile('_\w+\n') # or myregex2 = re.compile('_\w+$')
fastaid2 = myregex.sub('\n', myfile) # or fasaid2 = myregex.sub('', myfile)
</code></pre>
<p>The above will work ... As with all my code I never proof it ..</p>

CC BY-SA 4.0
2020-11-23T22:57:14.840Z
Hi @andnowmywatchbegins thats easy just use dictionary to remove the duplicates ... its been answered several times on the site. Please remember accept/upvote stuff.
M__
CC BY-SA 4.0
2020-11-23T22:18:54.220Z
2020-11-23T22:57:57.713Z
2020-11-23T22:57:57.713Z
M__
<pre><code>from Bio import SeqIO
import sys
import re 

unique = []
fasta_file = (sys.argv[1])
for myfile in SeqIO.parse(fasta_file, &quot;fasta&quot;):
    if len(myfile) &gt; 250:
        gene_id = myfile.id
        if gene.id not in unique:
            unique.append(gene.id)
            mylist = re.match(r&quot;H149xcV_\w+_\w+_\w+&quot;, gene_id)
            print (&quot;&gt;&quot;+mylist.group(0)) 
</code></pre>
<p>let me know if it works I am learning too</p>

CC BY-SA 4.0
2020-12-16T09:37:39.423Z
2020-12-16T09:37:39.423Z
pippo1980
<p>I am working on a project using the following command within <code>nano</code>:</p>
<pre><code>from Bio import SeqIO
import sys
import re 


    fasta_file = (sys.argv[1])
    for myfile in SeqIO.parse(fasta_file, &quot;fasta&quot;):
      if len(myfile) &gt; 250:
       gene_id = myfile.id
       mylist = re.match(r&quot;H149xcV_\w+_\w+_\w+&quot;, gene_id)
       print (&quot;&gt;&quot;+mylist.group(0))   # edited from list to mylist
</code></pre>
<p>and its providing with the following outout:</p>
<pre><code>&gt;H149xcV_Fge342_r3_h2_d1
&gt;H149xcV_bTr423_r3_h2_d1
&gt;H149xcV_kN893_r3_h2_d1
&gt;H149xcV_DNp021_r3_h2_d1
&gt;H149xcV_JEP3324_r3_h2_d1
&gt;H149xcV_SRt424234_r3_h2_d1
</code></pre>
<p>How can I change my command so that it provides me with the desired format and only the <strong>UNIQUE</strong> gene id's:</p>
<pre><code>&gt;H149xcV_Fge342_r3_h2
&gt;H149xcV_bTr423_r3_h2
&gt;H149xcV_kN893_r3_h2
&gt;H149xcV_DNp021_r3_h2
&gt;H149xcV_JEP3324_r3_h2
&gt;H149xcV_SRt424234_r3_h2
</code></pre>

<python><phylogenetics><fasta><phylogeny>
How to obtain desired output?
# How to obtain desired output?

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

I am working on a project using the following command within nano:
from Bio import SeqIO
import sys
import re 


    fasta_file = (sys.argv[1])
    for myfile in SeqIO.parse(fasta_file, "fasta"):
      if len(myfile) > 250:
       gene_id = myfile.id
       mylist = re.match(r"H149xcV_\w+_\w+_\w+", gene_id)
       print (">"+mylist.group(0))   # edited from list to mylist

and its providing with the following outout:
>H149xcV_Fge342_r3_h2_d1
>H149xcV_bTr423_r3_h2_d1
>H149xcV_kN893_r3_h2_d1
>H149xcV_DNp021_r3_h2_d1
>H149xcV_JEP3324_r3_h2_d1
>H149xcV_SRt424234_r3_h2_d1

How can I change my command so that it provides me with the desired format and only the UNIQUE gene id's:
>H149xcV_Fge342_r3_h2
>H149xcV_bTr423_r3_h2
>H149xcV_kN893_r3_h2
>H149xcV_DNp021_r3_h2
>H149xcV_JEP3324_r3_h2
>H149xcV_SRt424234_r3_h2

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

---

## 2 answers

### Answer by username_1 on 2020-11-23. Score: 2 | Accepted answer

its easy replace match with sub but please stop using list as a variable ... mylist is fine.
This might work
   mylist = re.sub(r'H149xcV_\w+_\w+_\w+', gene_id)

otherwise
myregex = re.compile('_\w+\s+.*') 
fastaid = myregex.sub('', myfile)

OR from @MaximilianPress
myregex2 = re.compile('_\w+\n') # or myregex2 = re.compile('_\w+$')
fastaid2 = myregex.sub('\n', myfile) # or fasaid2 = myregex.sub('', myfile)

The above will work ... As with all my code I never proof it ..

> **Comments:**
> **username_1**: Hi @andnowmywatchbegins thats easy just use dictionary to remove the duplicates ... its been answered several times on the site. Please remember accept/upvote stuff.  

---

### Answer by username_2 on 2020-12-16. Score: 0

from Bio import SeqIO
import sys
import re 

unique = []
fasta_file = (sys.argv[1])
for myfile in SeqIO.parse(fasta_file, "fasta"):
    if len(myfile) > 250:
        gene_id = myfile.id
        if gene.id not in unique:
            unique.append(gene.id)
            mylist = re.match(r"H149xcV_\w+_\w+_\w+", gene_id)
            print (">"+mylist.group(0)) 

let me know if it works I am learning too


---

