Advertisement
Guest User

Untitled

a guest
Mar 16th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.95 KB | None | 0 0
  1. use strict;
  2. use warnings;
  3.  
  4. # usage: script.pl {motif_width} {dnafile}
  5.  
  6. my $width = shift(@ARGV);
  7.  
  8. my $dna;
  9. while (<>) {
  10.     next if /^[#>]/;
  11.  
  12.     $dna .= $_;
  13. }
  14.  
  15. $dna =~ s/[\s\d]//g;
  16.  
  17. # Initialize counts for motif positions
  18. my %mot = map { $_ => [ (0) x $width ] } qw( A C G T );
  19.  
  20. # Initialize background counts
  21. my %bg = map { $_ => 0 } qw( A C G T );
  22.  
  23. # Generate random start site in the sequence
  24. # for motif to start from
  25. my $ms = int(rand(length($dna)-$width));
  26.  
  27. # Within a motif, count the bases at the positions
  28. for my $pos (0..length($dna)-1) {
  29.     my $base = substr($dna, $pos, 1);
  30.     if ($pos >= $ms && $pos < $ms + $width) {
  31.         ++$mot{$base}[$pos-$ms]
  32.             if exists($mot{$base});
  33.     } else {
  34.         ++$bg{$base}
  35.            if exists($bg{$base});
  36.     }
  37. }
  38.  
  39. for my $base (qw( A C G T )) {
  40.    print "$base @{$mot{$base}}\n";
  41. }
  42.  
  43. print "\n";
  44.  
  45. for my $base (qw( A C G T )) {
  46.    print "bg$base = $bg{$base}\n";
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement