
Quero usar grep
para pesquisar o padrão de um arquivo em um segundo. Meu arquivo de padrão é algo como:
K02217
K07448
KO8980
O arquivo para pesquisar é:
>aai:AARI_24510 proP; proline/betaine transporter; K03762 MFS transporter, MHS family, proline/betaine transporter
>aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
>aai:AARI_28260 hypothetical protein
>aai:AARI_29060 ABC drug resistance transporter, inner membrane subunit; K09686 antibiotic transport system permease protein
>aai:AARI_29070 ABC drug resistance transporter, ATP-binding subunit (EC:3.6.3.-); K09687 antibiotic transport system ATP-binding protein
>aai:AARI_29650 hypothetical protein
>aai:AARI_32480 iron-siderophore ABC transporter ATP-binding subunit (EC:3.6.3.-); K02013 iron complex transport system ATP-binding protein [EC:3.6.3.34]
>aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system protein
O comando que tentei é:
fgrep --file=pattern.txt file.txt >> output.txt
Isso imprime as linhas do arquivo.txt onde o padrão é encontrado. Preciso que ele imprima também uma coluna com o padrão encontrado. Então, algo como:
K07448 mrr; restriction system protein Mrr; K07448 restriction system
K02217 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
Alguém pode me sugerir como fazer?
Responder1
Se você não se importa com uma coluna extra com um número, você pode usar join
e grep
para fazer isso.
$ join <(grep -of patterns.txt file.txt | nl) \
<(grep -f patterns.txt file.txt | nl)
1 KO3322 proteinaseK (KO3322)
2 KO3435 Xxxxx KO3435;folding factor
3 KO3435 Yyyyy KO3435,xxxx
Responder2
Você pode usar um loop de shell:
$ while read pat; do
grep "$pat" file |
while read match do
echo -e "$pat\t$match"
done
done < patterns
KO3435 Xxxxx KO3435;folding factor
KO3435 Yyyyy KO3435,xxxx
KO3322 proteinaseK (KO3322)
Eu testei executando isso no arquivo simples UniProt para humanos (625M) e usando 1000 IDs UniProt como padrões. Demorou cerca de 6 minutos no meu laptop Pentium i7. Demorou cerca de 35 segundos quando procurei apenas 100 padrões.
Conforme apontado nos comentários abaixo, você pode tornar isso um pouco mais rápido ignorando echo
e usando grep
as opções --label
e -H
:
$ while read pat; do
grep "$pat" --label="$pat" -H < file
done < patterns
Executar isso em seus arquivos de exemplo produz:
$ while read pat; do
grep "$pat" --label="$pat" -H < kegg.annotations;
done < allKO.IDs.txt > test1
terdon@oregano foo $ cat test1
K02217:>aai:AARI_26600 ferritin-like protein; K02217 ferritin [EC:1.16.3.1]
K07448:>aai:AARI_33320 mrr; restriction system protein Mrr; K07448 restriction system protein
Responder3
Você pode usarack:
$ ack "$(tr '\n' '|' < pattern.txt | sed -e 's/.$//')" --print0 --output='$& $_' file.txt
KO3322 proteinaseK (KO3322)
KO3435 Xxxxx KO3435;folding factor
KO3435 Yyyyy KO3435,xxxx