Jianguo
Jianguo

Reputation: 89

string match search

one text file like this as query file:

fooLONGcite
GetmoreDATA
stringMATCH
GOODthing

another text file like this as subject file:

sometingfooLONGcite
anyotherfooLONGcite
matchGetmoreDATA
GETGOODthing
brotherGETDATA
CITEMORETHING
TOOLONGSTUFFETC

The expected result will be get the matched string from subject file and then print it out. So, the output should be:

sometingfooLONGcite
anyotherfooLONGcite
matchGetmoreDATA    
GETGOODthing

Here is my perl script. But It doesn't work. Can you help me find where is the problem? Thanks.

#!/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);

Upvotes: 0

Views: 127

Answers (2)

Birei
Birei

Reputation: 36262

As I understand, you look for a match of part of the string, not an exact one. Here a script that does what I think you are looking for:

Content of script.pl. I take into account that file of queries is small because I add all its content to a regex:

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

Run the script:

perl script.pl query.txt subject.txt

And result:

sometingfooLONGcite
anyotherfooLONGcite
matchGetmoreDATA
GETGOODthing

Upvotes: 1

Brian Roach
Brian Roach

Reputation: 76898

Your current code doesn't make a lot of sense; you're even referencing variables you don't assign anything to.

All you need to do is read the first file into a hash, then check each line of the second against that hash.

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

...

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

Upvotes: 0

Related Questions