2016-11-08 1 views
-1

Ich habe Probleme bei der Implementierung von zwei Dateien in diesem Programm. Ich versuche, auf den Inhalt der Datei $Q und zuzugreifen.Suche längste Übereinstimmung zwischen 2 Dateien von Muster

print "Input the K value \n"; 
$k = <>; 
chomp $k; 

print "Input T\n"; 
$t = <>; 
chomp $t; 

%Qkmer =();      
$i = 1; 

$query=' '; 
while ($line=<IN>) { 
chomp($line); 
if ($line=~ m/^>/) { 
next; 
} 
$query=$query.$line; 
$line=~ s/(^|\n)[\n\s]*/$1/g; 

while (length($line) >= $k) { 
    $line =~ m/(.{$k})/; 
    if (! defined $Qkmer{$1}) {#every key not deined as the first match 
    $Qkmer{$1} = $i; 
    } 
    $i++; 
    $line = substr($line, 1, length($line) -1); 
} 
} 

open(MYDATA, '<', "data.txt"); 

while ($line=<MYDATA>) { \ 
    chomp($line); 
    %Skmer =();   # This initializes the hash called Skmer. 
    $j = 1; 

    if ($line=~ m/^>/) { #if the line starts with > 
    next; #start on next line #separated characters 
    } 
    $line=~ s/^\s+|\s+$//g ; #remove all spaces from file 
    while (length($line) >= $k) { 
    $line =~ m/(.{$k})/;#match any k characters and only k characters in dna 
    $Skmer{$1} = $j; #set the key position to $j and increase for each new key 
    $j++; 
    $line = substr($line, 1, length($line) -1); #this removes the first character in the current string 
    } 

    ###(56)###for($Skmerkey(keys %Skmer)){ 
    $i=$Skmer{$Skmerkey}; 
    if(defined $Qkmer($Skmerkey)){ 
     $j=$Qkmer($Skmerkey); 
     } 
     $S1=$line; 
     $S2=$query; 
     @arrayS1= split(//, $S1); 
     @array2= split(//, $S2); 

     $l=0; 
     while($arrayS1[$i-$l] eq $arrayS2[$j-$l]){ 
     $l++; 
     } 
     $start=$i-$l; 
     $m=0; 
     while ($arrayS1[$i+$k+$m] eq $arrayS2[$j+$k+$m]) { 
     $m++; 
     } 
     $length=$l+$k+$m; 
     $match= substr($S1, $start, $length); 

     if($length>$t){ 
     $longest=length($match); 
     print "Longest: $match of length $longest \n"; 
     } 
    } 

}###(83)### 

Die Eingabedateien enthalten nur Buchstabenfolgen. Zum Beispiel:

Datei 1:

ahhtsagnchjgstffhjyfcsghnvzfhg 

File2:

ggujvfbgfgkjfcijjjffcvvafcsghnvzfhgvugxckugcbhfcgh 
ghnvzfhgvugxckHhfgjgcfujvftjbvdtkhvddgjcdgjxdjkfrh 
ajdbvciyqdanvkjghnvzfhgvugxc 

Von einer Übereinstimmung eines Wortes der Länge $k in Datei 1 in Datei 2, überprüfe ich aus, dass Match in Datei 2 nach links und rechts von Wort für weitere Übereinstimmungen. Die endgültige Ausgabe ist die längste Übereinstimmung zwischen Datei 1 und Datei 2 basierend auf $k. Jetzt habe ich ge

Mit diesem Code, erhalte ich einen Syntaxfehler und ich bin nicht suer warum, weil es mir richtig aussieht:

syntax error at testk.pl line 56, near "$Skmerkey(" 
syntax error at testk.pl line 83, near "}" 

Danke.

+1

'$ k' verwendet, aber nicht definiert. 'Warinings verwenden; Verwenden Sie strikt; ' – Mike

+0

Hash'% kmer' immer leer !!! – Mike

+0

Ja, ich habe den Code direkt von meinem Editor eingefügt, also musste ich manuell Leerzeichen hinzufügen, um alles in einen Codeblock zu bekommen. In meinem aktuellen Programm sind die auskommentierten Bereiche nicht auskommentiert. Ich habe die Hashes verwendet, um Bereiche zu unterscheiden, mit denen ich speziell Probleme habe und wahrscheinlich einen anderen Weg implementieren muss. –

Antwort

0
use strict;   # <--- Allways use this 
use warnings;  # <--- and this 
use Data::Dumper; 

my $k=3; 

open(my $IN, '<', "File2"); # use $IN instead of depricated IN 
my $line=0; # line number 
my %kmer; # hash of arrays of all $k-letter "words" line/position 
my @Q;  # rows of Q-file 
while(<$IN>) { 
    chomp; 
    next if /^>/; 
    s/^\s+|\s+$//g; 
    next if !$_; 
    my $pos=0; 
    push @Q, $_; # store source row 
    for(/(?=(.{$k}))/g) { # Capture $k letters. floating window with step 1 symbol 
    push @{$kmer{$_}}, [$line,$pos]; # store row number and position of "word" 
    $pos++; 
    } 
    $line++; 
} 

open($IN, '<', "File1"); 
$line=0; 
while(<$IN>) { # Read S-file 
    chomp; 
    next if /^>/; 
    s/^\s+|\s+$//g; 
    next if !$_; 
    my $pos=0; 
    my $len=length($_); # length of row of S-file 
    my $s=$_;   # Current row of S-file 
    my @ignore=();  # array for store information about match tails 
    for(/(?=(.{$k}))/g) { 
    next if ! $kmer{$_}; # "word" not found try to next 
    for(@{$kmer{$_}}) { # $kmer{word} contains array of lines/positions in Q 
     my($qline, $qpos)[email protected]{$_}; 
#  print "test $qline:$qpos "; 
     if(grep {$_->[0]==$qline && $_->[1]==$qpos } @ignore) { 
     # this line/position already tested and included in found matching 
#  print "Ignore match tail $qline:$qpos\n"; 
     next; 
     } 
     my $j=$k; # $k letters same, test after this point 
     my $qlen=length($Q[$qline]); 
     $j++ while($pos+$j<$len && $qpos+$j<$qlen && 
        substr($s,$pos+$j,1) eq substr($Q[$qline],$qpos+$j,1)); 
     print "MATCH FOUND: S-file line $line pos $pos, Q-file line $qline pos $qpos: ", 
      substr($s,$pos,$j),"\n"; 
     push @ignore, [$qline, $qpos, $j]; # store positions and length of match 
    } 
    } continue { # Continue block works on all loops, include after "next" 
    $pos++; 
    @ignore=grep { # recalculate/filter position and length of all match tails 
        ++$_->[1]; # increment position 
        (--$_->[2]) # decrement length 
        >= $k  # and filter out lengths < $k 
       } @ignore; 
# print Dumper(\@ignore); 
    } 
    $line++; 
} 
Verwandte Themen