поиск совпадения строк - PullRequest
       1

поиск совпадения строк

0 голосов
/ 02 февраля 2012

один текстовый файл, такой как файл запроса:

fooLONGcite
GetmoreDATA
stringMATCH
GOODthing

другой текстовый файл, подобный этому как файл темы:

sometingfooLONGcite
anyotherfooLONGcite
matchGetmoreDATA
GETGOODthing
brotherGETDATA
CITEMORETHING
TOOLONGSTUFFETC

Ожидаемый результат - получить соответствующую строку из файла темы и затем распечатать ее. Итак, вывод должен быть:

sometingfooLONGcite
anyotherfooLONGcite
matchGetmoreDATA    
GETGOODthing

Вот мой скрипт на Perl. Но это не работает. Можете ли вы помочь мне найти, где проблема? Благодарю.

#!/usr/bin/perl
use strict;

# to check the command line option
if($#ARGV<0){
    printf("Usage: \n <tag> <seq> <outfile>\n");
    exit 1;
}

# to open the given infile file
open(tag, $ARGV[0]) or die "Cannot open the file $ARGV[0]";
open(seq, $ARGV[1]) or die "Cannot open the file $ARGV[1]";

my %seqhash = ();
my $tag_id;
my $tag_seq;
my $seq_id;
my $seq_seq;
my $seq;
my $i = 0;

print "Processing cds seq\n";
#check the seq file
while(<seq>){ 
    my @line = split;
    if($i != 0){
        $seqhash{$seq_seq} = $seq;
        $seq = "";
        print "$seq_seq\n";
    }
    $seq_seq = $line[0];
    $i++;
}

while(<tag>){ 
    my @tagline = split; 
    $tag_seq = $tagline[0];
    $seq = $seqhash{$seq_seq};
    #print "$tag_seq\n";
    print "$seq\n";
    #print output ">$id\n$seq\n";
}
#print "Ending of Processing gff\n";

close(tag);
close(seq);

Ответы [ 2 ]

1 голос
/ 02 февраля 2012

Как я понимаю, вы ищете совпадение части строки, а не точное. Вот скрипт, который делает то, что, я думаю, вы ищете:

Содержание script.pl. Я принимаю во внимание, что файл запросов небольшой, потому что я добавляю все его содержимое в регулярное выражение:

use warnings;
use strict;

## Check arguments.
die qq[Usage: perl $0 <query_file> <subject_file>\n] unless @ARGV == 2;

## Open input files. Abort if found errors.
open my $fh_query, qq[<], shift @ARGV or die qq[Cannot open input file: $!\n];
open my $fh_subject, qq[<], shift @ARGV or die qq[Cannot open input file: $!\n];

## Variable to save a regex with alternations of the content of the 'query' file.
my $query_regex;

{
    ## Read content of the 'query' file in slurp mode.
    local $/ = undef;
    my $query_content = <$fh_query>;

    ## Remove trailing spaces and generate a regex.
    $query_content =~ s/\s+\Z//;
    $query_content =~ s/\n/|/g;
    $query_regex = qr/(?i:($query_content))/;
}

## Read 'subject' file and for each line compare if that line matches with 
## any word of the 'query' file and print in success.
while ( <$fh_subject> ) { 
    if ( m/$query_regex/o ) { 
        print
    }   
}

Запустите скрипт:

perl script.pl query.txt subject.txt

И результат:

sometingfooLONGcite
anyotherfooLONGcite
matchGetmoreDATA
GETGOODthing
0 голосов
/ 02 февраля 2012

Ваш текущий код не имеет большого смысла; вы даже ссылаетесь на переменные, которым ничего не назначаете.

Все, что вам нужно сделать, это прочитать первый файл в хеш, а затем проверить каждую строку второго по этому хешу.

while (my $line = <FILE>)
{
    chomp($line);
    $hash{$line} = 1;
}

...

while (my $line = <FILE2>)
{
    chomp($line);
    if (defined $hash{$line})
    {
        print "$line\n";
    }
}
...