¿Cómo obtener el resultado deseado?

Nov 24 2020

Estoy trabajando en un proyecto usando el siguiente comando dentro 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

y está provisto de la siguiente salida:

>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

¿Cómo puedo cambiar mi mando para que me proporciona el formato deseado y sólo el ÚNICA gen id:

>H149xcV_Fge342_r3_h2
>H149xcV_bTr423_r3_h2
>H149xcV_kN893_r3_h2
>H149xcV_DNp021_r3_h2
>H149xcV_JEP3324_r3_h2
>H149xcV_SRt424234_r3_h2

Respuestas

2 M__ Nov 24 2020 at 05:18

es fácil de reemplazar matchcon subpero por favor deje de usar listcomo variable ... mylist está bien.

Esto podría funcionar

   mylist = re.sub(r'H149xcV_\w+_\w+_\w+', gene_id)

de otra manera

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

O de @MaximilianPress

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

Lo anterior funcionará ... Como con todo mi código, nunca lo pruebo ...

pippo1980 Dec 16 2020 at 16:37
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)) 

avísame si funciona, yo también estoy aprendiendo