CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
codes can't read pattern text file

 



sulis
New User

Jan 4, 2005, 6:36 PM

Post #1 of 1 (385 views)
codes can't read pattern text file Can't Post

i can't seem to make this code read the pattern text file. there are several patterns in pattern file. another text file i have has to detect every patterns found. It must go through every pattern. I did a foreach loop but can't work.can anyone help? this is my code.

#!/usr/bin/perl

use Benchmark;

$start = new Benchmark;

$CHR_NUMBER = '01';

# Variable $pattern to match

open(PATTERN, "<ptest.txt") or die "Can't open pattern file";
my @pattern = ;


# Variable $PATTERNLENGTH

$PATTERNLENGTH = calculate_length();

$PATTERNLENGTH_LESSONE = $PATTERNLENGTH - 1;

# Variable $original is the file to read from

$original = $CHR_NUMBER.'.fa';


# Variable $output is file to write results to

$output = $CHR_NUMBER.'.txt';

open(OUTPUT, ">",$output,) or die "Can't open $output: $!";

# Variable #EXACT_POSITION to follow the pointer thruout every loop.

$EXACT_POSITION = 0;

# Variable $FIRST_LINE_LENGTH to hold the length of first line (which is to be ignored).

$FIRST_LINE_LENGTH;

# Variable $remaining to hold the remaining 16 characters of a line, as the min. required is 17.

$remaining = "";


open(ORIGINAL, "<",$original) or die "Can't open $original: $!";

# gets file size

$filesize = -s $original;

print ("Filesize:" , $filesize, "\n\n");

# loop till end of file

while ($current_line = <ORIGINAL>)

{

if ($. == 1)

{

# gets length of first line and sets $EXACT_POSITION to go to that position

$FIRST_LINE_LENGTH = length($current_line);

$EXACT_POSITION = $FIRST_LINE_LENGTH;

next;

}

else

{

# cuts off the \n character at the end of string

chomp($current_line);

# EXACT_POSITION must increment because there's the \n character

$EXACT_POSITION++;



# adds $remaining characters infront of the current_line and store in $temp_line before continuing checking.

$temp_line = $remaining.$current_line;

# gets the length of the $temp_line and store in $temp_pos

$temp_length = length($temp_line);

# sets $temp_pos to 0 on every loop

$temp_pos = 0;

# goes into the loop whenever $temp_length has more than 16 chars, less than 16, cannot do checking.

while ($temp_length > $PATTERNLENGTH_LESSONE)

{

# substring out 17 characters the $temp_pos

$temp_seq = substr($temp_line, $temp_pos, $PATTERNLENGTH);


# increment $EXACT_POSITION

$EXACT_POSITION++;

# increment $temp_pos

$temp_pos++;

# decrement $temp_length

$temp_length--;


foreach $pattern (@pattern) {

if ($temp_seq =~ /$pattern/i)

{

if (($temp_pos < $PATTERNLENGTH_LESSONE) && ($. > 2))

{

#minus off the first line cos it is not a position to the researcher and write to output file

print OUTPUT $CHR_NUMBER,"\t",($EXACT_POSITION-$FIRST_LINE_LENGTH-$.+1+1),"\t",$temp_seq, "\n";

}

else

{

print OUTPUT $CHR_NUMBER,"\t",($EXACT_POSITION-$FIRST_LINE_LENGTH-$.+1),"\t",$temp_seq, "\n";

}

}



# store the remainining 16 characters into $remaining

$remaining = substr($temp_line, $temp_pos, $PATTERNLENGTH_LESSONE);

}

}
}

}

close(ORIGINAL);

close(OUTPUT);



$end = new Benchmark;

$diff = timediff($end, $start);

print "\n\nTime taken was ", timestr($diff, 'all'), " seconds.\n";



sub calculate_length()

{

my $chars = 0;

@fields = split(/\]/, $pattern);

foreach $dat (@fields)

{

$pos = index($dat,'[');

if ($pos == -1)

{

$chars = $chars + length($dat);

}

else

{

$chars = $chars + $pos + 1;

}

}

return ($chars);

}

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives