rambosauce has asked for the wisdom of the Perl Monks concerning the following question:
Hello and thanks in advance
I'm a perl novice and have some trouble with the following script being inefficient. I have a data input file containing sequences and their positions in the human genome, with each line as a separate sequence. I want to compare the positions of each sequence to a reference file and extract the name of the gene, which is the last column in the reference file. Both files are tab delimited and have a lot of extraneous information. The script goes line by line from the input and for each line searches criteria in the reference line by line. My script works, but as both of these files are large, this can mean runtime of several days. Is there any way to hash one of the files, so that it only iterates N times, vs N*M times, where N and M are line counts for each file? Any other advice would be greatly appreciated.
#!/usr/bin/perl use strict; use warnings; use feature qw( say ); #Take input and output names from command line my $in_qfn = $ARGV[0]; my $out_qfn = $ARGV[1]; my $transcripts_qfn = "/path/HumanTranscriptInfo"; my @transcripts; { open(my $transcripts_fh, "<", $transcripts_qfn) or die("Can't open \"$transcripts_qfn\": $!\n"); while (<$transcripts_fh>) { chomp; push @transcripts, [ split(/\t/, $_) ]; } } { open(my $in_fh, "<", $in_qfn) or die("Can't open \"$in_qfn\": $!\n"); open(my $out_fh, ">", $out_qfn) or die("Can't create \"$out_qfn\": $!\n"); while (<$in_fh>) { chomp; #define what each column is to print later my ($ID, $strand, $chr, $start, $sequence, $quality, $numPositio +ns, $mismatches) = split(/\t/, $_); #define an end position based on sequence length my $end = $start+length($sequence); #define useful info from reference file for my $transcript (@transcripts) { my $ref_chr = $transcript->[0]; my $ref_strand = $transcript->[6]; my $ref_start = $transcript->[3]; my $ref_end = $transcript->[4]; my $info = $transcript->[8]; #now compare values, and if there is a match for strand, chro +mosome, and the start and stop within reference range range, print va +lues if ($chr eq $ref_chr && $strand eq $ref_strand && $end >= $re +f_start && $end <= $ref_end) { say $out_fh join("\t", $ID, $strand, $chr, $start, $end, $ +sequence, $numPositions, $mismatches, $info); } } } } }
|
---|