Microcebus has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I have 2 DNA sequences and want to perform a dotplot using a sliding window of a particular size with particular mismatch allowed. The following script produces a text matrix in which "1" stands for a hit.
The problem is, that this code works very slowly with long DNA sequences (>10000). I already use my four CPU cores for calculation but I've no idea how I could further speed it up. Any suggestions are welcome!
### CREATE TWO SAMPLE DNA SEQUENCES ### @nucleotides=('A','T','G','C'); foreach(1..1000) { $seq1.=$nucleotides[int(rand(3))]; } foreach(1..1000) { $seq2.=$nucleotides[int(rand(3))]; } ### SETTINGS FOR THE DOTPLOT ### $window_size=5; $max_mismatch=1; @seq1=split('',$seq1); foreach(1..$window_size) { shift@seq1; } @seq2=split('',$seq2); foreach(1..$window_size) { shift@seq2; } open(OUT,">ID_matrix.txt"); $number_of_windows_1=((length$seq1)-$window_size)+1; $number_of_windows_2=((length$seq2)-$window_size)+1; $time_start=time; foreach$window_no(0..$number_of_windows_1-1) { @seq2_temp=@seq2; if($window_no==0) { $current_window=substr($seq1,0,$window_size); @current_window=split('',$current_window); } else { shift@current_window; $next_character=shift@seq1; push(@current_window,$next_character); } foreach$query_no(0..$number_of_windows_2-1) { if($query_no==0) { $query_window=substr($seq2,0,$window_size); @query_window=split('',$query_window); } else { shift@query_window; $next_character=shift@seq2_temp; push(@query_window,$next_character); } $count_matches=0; foreach(0..$window_size-1) { if($current_window[$_]eq$query_window[$_]) { $count_matches++; last if($_-$count_matches>$max_mismatch); } } if($count_matches>=$window_size-$max_mismatch) { print OUT "1"; } else { print OUT "0"; } } print OUT"\n"; } $time_end=time; $time_used=$time_end-$time_start; close OUT; print"Time used: $time_used seconds.\n"; system("pause"); exit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Speed up DNA dotplot
by jwkrahn (Abbot) on Jul 14, 2011 at 06:35 UTC | |
|
Re: Speed up DNA dotplot
by happy.barney (Friar) on Jul 14, 2011 at 07:57 UTC | |
by happy.barney (Friar) on Jul 14, 2011 at 09:35 UTC | |
|
Re: Speed up DNA dotplot (65% speedup)
by BrowserUk (Patriarch) on Jul 14, 2011 at 14:32 UTC |