Advertisement
Guest User

Untitled

a guest
May 10th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 2.83 KB | None | 0 0
  1. #generate a random DNA strings and print it to file specified by the user.
  2.  
  3. print "What is the name of the output file?\n";
  4. $rstrings = <STDIN>;
  5. open (OUT, ">$rstrings");
  6.  
  7. print "How many nucleotides for the string?\n";
  8. $n = <>;
  9.  
  10. print "How many strings?\n";
  11. $num = <>;
  12.  
  13. $numstring = ''; # start with the empty string;
  14. $i = 0;
  15. $j = 0;
  16.  
  17.  
  18. while ($j < $num) {
  19.  
  20.     while ($i < $n) {
  21.  
  22.    $numstring = int(rand(4)) . $numstring; # generate a new random integer
  23.                                            # between 0 and 3, and concatenate
  24.                                            # it with the existing $numstring,
  25.                                            # assigning the result to $numstring.
  26.     $i++;                                  # increase the value of $i by one.
  27.     }
  28.     $j++;
  29.     $i = 0;
  30.     $dnastring = $numstring;                  # unneeded, but good for clarity.
  31.     $dnastring =~ tr/0123/actg/; # translate the numbers to DNA characters.
  32.     print OUT "$dnastring\n";
  33.  
  34.     $numstring = ''; # start with the empty string;
  35. }
  36.  
  37.  
  38. $dnastring = $numstring;                  # unneeded, but good for clarity.
  39. $dnastring =~ tr/0123/actg/; # translate the numbers to DNA characters.
  40. print OUT "$dnastring\n";
  41. close (OUT);
  42.  
  43.  
  44. open (IN, "$rstrings");
  45. open (OUT, ">LCSavg");
  46.  
  47.  
  48. $line = <IN>;
  49. chomp $line;
  50. @string1 =  split(//, $line);
  51.  
  52. $sum = 0;
  53. $V = 1;
  54. $Im = 0;
  55. $Cm = 0;
  56. $avg = 0;
  57.  
  58. while ($string2 = <IN>) {
  59.  
  60.  
  61.  
  62. chomp $string2;
  63. @string2 =  split(//, $string2);
  64.  
  65. $n = @string1;                 #assigning a list to a scalar just assigns the
  66.                                #number of elements in the list to the scalar.
  67. $m = @string2;
  68.  
  69. for ($i = 1; $i <= $n; $i++) { # Assign the column 0  values and print
  70.                                # String 1  See section 5.2 of Johnson
  71.                                # for loops
  72.    $V[$i][0] = $i * $Im;
  73.    # print OUT "$string1[$i-1]";  # Note the -1 here because array indexes start at 0 (ug!)
  74. }
  75.    # print OUT "\n\n";
  76.  
  77. for ($j = 1; $j <= $m; $j++) { # Assign the row 0 values and print String 2
  78.    $V[0][$j] = $j * $Im;
  79.    # print OUT "$string2[$j-1]";
  80. }
  81.  
  82. for ($i = 1; $i <= $n; $i++) {      # follow the recurrences to fill in the V matrix.
  83.  for ($j = 1; $j <= $m; $j++) {
  84. #   print OUT "$string1[$i-1], $string2[$j-1]\n"; # This is here  for debugging purposes.
  85.    if ($string1[$i-1] eq $string2[$j-1]) {
  86.      $t = $V; }
  87.    else {
  88.     {
  89.       $t = $Cm;
  90.     }
  91.    }
  92.  
  93.   $max = $V[$i-1][$j-1] + $t;  
  94. #  print OUT "For $i, $j, t is $t \n"; # Another debugging line.
  95.   if ($max < $V[$i][$j-1] - 1) {
  96.     $max = $V[$i][$j-1] + $Im;
  97.   }
  98.  
  99.   if ($V[$i-1][$j] - 1 > $max) {
  100.     $max = $V[$i-1][$j] + $Im;
  101.   }
  102.   $V[$i][$j] = $max;
  103.  
  104.     }
  105.  
  106.  
  107. }
  108.      print OUT "String  LCS is $max\n";
  109.  $sum = $sum + $max;
  110. }
  111.  
  112.  
  113.  
  114. $avg = $sum / $num;
  115.  
  116. print OUT "Average of all LCS is$avg\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement