0
$\begingroup$

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
$\endgroup$
0
2
$\begingroup$

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 ..

$\endgroup$
1
  • $\begingroup$ Hi @andnowmywatchbegins thats easy just use dictionary to remove the duplicates ... its been answered several times on the site. Please remember accept/upvote stuff. $\endgroup$
    – M__
    Nov 23 '20 at 22:57
0
$\begingroup$
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

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.