Obtendo dados da lista correspondente

0

list.txt

 GETID_17049_knownids_1/2_Confidence_0.625_Length_2532
 GETID_9248_knownids_6/10_Confidence_0.439_Length_2474
 GETID_11084_knownids_3/3_Confidence_0.600_Length_1451
 GETID_15916_knownids_10/11_Confidence_0.324_Length_1825

sample1.txt

>GETID_17049_knownids_1/2_Confidence_0.625_Length_2532
sampletextforsample1
sampletextforsample1
sampletextforsample1
>GETID_18457_knownids_1/2_Confidence_0.625_Length_2532
sample2textforsample1
sample2textforsample1
sample2textforsample1
sample2textforsample1

sample2.txt

>GETID_11084_knownids_3/3_Confidence_0.600_Length_1451
sampletextforsample2
sampletextforsample2
>GETID_67838_knownids_3/3_Confidence_0.600_Length_1451
sample2textforsample2
sample2textforsample2

sample3.txt

>GETID_17049_knownids_1/2_Confidence_0.625_Length_2532
sampletextforsample3
sampletextforsample3
sampletextforsample3
>GETID_15916_knownids_10/11_Confidence_0.324_Length_1825
sample2textforsample3
sample2textforsample3

output.txt

>GETID_17049_knownids_1/2_Confidence_0.625_Length_2532
sampletextforsample1
sampletextforsample1
sampletextforsample1
>GETID_17049_knownids_1/2_Confidence_0.625_Length_2532
sampletextforsample3
sampletextforsample3
sampletextforsample3
>GETID_11084_knownids_3/3_Confidence_0.600_Length_1451
sampletextforsample2
sampletextforsample2
>GETID_15916_knownids_10/11_Confidence_0.324_Length_1825
sample2textforsample3
sample2textforsample3

Eu quero ler cada linha de list.txt (capturando valores entre colchetes (GETID_ {17049} knownids {1/2} _Confidence_1.0_Length_ {2532}) e compare com sample1.txt, sample2.txt, sample3.txt tem várias linhas e imprime o conteúdo (output.txt) desses arquivos quando é combinado com list.txt. A saída deve conter correspondência exata para list.txt. Qualquer ajuda em awk / sed / perl é apreciado.

    
por jack 29.08.2012 / 20:23

2 respostas

1

Se você modificar ligeiramente a solução fornecida por Gilles em esta pergunta (também referido por jw013 ), você pode obter o efeito que você pede, exceto que a ordem é baseada na seqüência de entrada e não é idêntica a output.txt listado em sua pergunta:

awk -v patterns_file=list.txt '
BEGIN {
  while (getline < patterns_file)
    patterns_array[">" $0] = 1
  close(patterns_file)
}
$0 in patterns_array { print; getline; print }
' sample[1-3].txt

Saída:

>GETID_17049_knownids_1/2_Confidence_0.625_Length_2532
sampletextforsample1
>GETID_11084_knownids_3/3_Confidence_0.600_Length_1451
sampletextforsample2
>GETID_17049_knownids_1/2_Confidence_0.625_Length_2532
sampletextforsample3
>GETID_15916_knownids_10/11_Confidence_0.324_Length_1825
sample2textforsample3

Editar

Para fazer isso funcionar com registros de várias linhas, use um separador de registro apropriado ( RS ). No seu caso, defina-o como: greater-than no início do arquivo ( ^> ) ou new-line seguido por greater-than ( \n> ) ou new-line no final do arquivo ( \n$ ) parecem boas escolhas com base na entrada fornecida.

Algo como isso deve funcionar:

awk -v patterns_file=patterns.txt '
BEGIN {
  while (getline < patterns_file) 
    patterns_array[$0] = 1
  close(patterns_file)
  RS="^>|\n>|\n$"
}
$1 in patterns_array { print ">" $0 }
' sample[1-3].txt

Editar 2

Para produzir cada registro apenas uma vez, exclua-o de patterns_array após a saída:

awk -v patterns_file=patterns.txt '
BEGIN {
  while (getline < patterns_file) 
    patterns_array[$0] = 1
  close(patterns_file)
  RS="^>|\n>|\n$"
}
$1 in patterns_array { print ">" $0; delete patterns_array[$1] }
' sample[1-3].txt
    
por 30.08.2012 / 16:04
1

Aqui está uma solução PERL. Ele funcionará para qualquer número de arquivos e espera que o primeiro arquivo seja a lista. Ele também anexará o nome do arquivo ao cabeçalho FASTA.

#!/usr/bin/perl -w
use strict;
my $list=shift;
open(A,$list); 
my %k;
while(<A>){
    ## Remove trailing newline
    chomp;
    if ( /(\d+?)_knownids_(.+?)_.+?(\d+)$/){ 
      ## Concatenate the patterns and save in a hash
      my $pp=join("-", $1,$2,$3);
      $k{PAT}{$pp}=$_;
    }
}
close(A);
## Read each input file
my $name;
for my $f (@ARGV) {
    open(F,$f);
    while(<F>){
       ## Skip empty lines
       next if /^\s*$/;
       ## Is this a FASTA header?
       if ( /^\s*>/){
           ## If this id is in the list, keep it for this file
           if(/(\d+?)_knownids_(.+?)_.+?(\d+)$/){ 
              $name=join("-", $1,$2,$3);
           }
           ## Skip the sequences we are not interested in
           else{$name="foo"}
       }
       ## Collect the sequence
       else {
           if (defined($k{PAT}{$name})) {
           $k{$f}{$name}.=$_;
           }   
       } 
    }
    close(F);
}
## For each unique pattern found in list.txt
foreach my $pat (keys(%{$k{PAT}})) {
    ## For each of the files passed as arguments
    foreach my $file (@ARGV) {
    ## If the pattern was found in that file, print
    if (defined($k{$file}{$pat})) {
          print ">$k{PAT}{$pat}_$file\n";  
          print "$k{$file}{$pat}"
        }
    }
}

Se o script for salvo como compare.pl , faça:

$ ./compare.pl list.txt sample1.txt sample2.txt sample3.txt sampleN.txt

A saída é:

> GETID_11084_knownids_3/3_Confidence_0.600_Length_1451_sample2.txt
sampletextforsample2
> GETID_17049_knownids_1/2_Confidence_0.625_Length_2532_sample1.txt
sampletextforsample1
> GETID_17049_knownids_1/2_Confidence_0.625_Length_2532_sample3.txt
sampletextforsample3
> GETID_15916_knownids_10/11_Confidence_0.324_Length_1825_sample3.txt
sample2textforsample3
    
por 30.08.2012 / 06:21

Tags