Guest User

Untitled

a guest
Feb 19th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.54 KB | None | 0 0
  1. /* CS121 A'11: Virtual Southern Blot
  2.  *
  3.  * java SouthernBlot <sequence file name> <restriction enzyme> <probe file name>
  4.  *
  5.  * Hannah Provenza 388257
  6.  *
  7.  */
  8.  
  9.  
  10. public class SouthernBlot {
  11.     /* genFilm:
  12.      *  String sequence: the sequence to be examined
  13.      *  String enzyme: the restriction enzyme
  14.      *  String probe: the probe to be matched
  15.      *
  16.      *  returns a film represented a boolean array in which the ith
  17.      *  element is true if there is a restriction fragment with
  18.      *  log(size) equal to i that is the complement of the probe.
  19.      */
  20.      public static boolean[] genFilm(String s, String enzyme, String probe) {
  21.         s = s.toUpperCase();
  22.         enzyme = enzyme.toUpperCase();
  23.         probe = probe.toUpperCase();
  24.         String[] fragments = restrictFragments(s, enzyme);
  25.         Utility.gelElectrophoresis(fragments);
  26.         String probeReversed = convertProbe(probe);
  27.         int k = ((int)Math.log10(fragments[0].length()) + 1);
  28.         boolean[] film = new boolean[k];
  29.     // gelElectrophoresis has LONGEST string first, so we're going to count DOWN on film, where longest is last, and UP on fragments, where longest is first.
  30.         int c = k - 1; // c is the index of film
  31.         /*while (c >= 0){
  32.     // Establish a count up through fragments.
  33.             for (int x = 0; x < fragments.length; x++){
  34.     // Establish a count up through fragments[x].
  35.                 for (int y = 0; y < fragments[x].length(); y++){
  36.     // Change the boolean for film
  37.                     if (enzymeMatchAt(fragments[x], probeReversed, y)){
  38.                         film[c] = true;
  39.                         break;
  40.                     }
  41.                 }
  42.     // Establish a condition for changing the count through film.
  43.                 if (fragments[x+1].length() < Math.pow(10, c+1)){ //if the next fragment is small enough to fit into the next smallest film slot, proceed to the next film slot.
  44.           c--;
  45.                 }
  46.                 if (x+1 >= fragments.length){
  47.                     return film;
  48.                 }
  49.             }
  50.         }
  51.         return film;
  52.     */
  53.  
  54.  
  55.     for (int x = 0; x < fragments.length; x++) {
  56.       for (int y = 0; y < fragments[x].length(); y++) {
  57.         if (enzymeMatchAt(fragments[x], probeReversed, y)) {
  58.           film[(int)Math.log10(fragments[x].length())] = true;
  59.         }
  60.       }
  61.     }
  62.  
  63.     return film;
  64.    }
  65.  
  66.  
  67.     /* drawFilm: draw a picture of the film with the largest fragments
  68.      *  at the top and the smallest at the bottom.  Each element in
  69.      *  the film will be represented by colored rectangle, RED for
  70.      *  slots where the corresponding element in the film is true and
  71.      *  BLACK otherwise.
  72.      */
  73.  
  74.     public static void drawFilm(boolean[] film) {
  75.     // YOUR CODE GOES HERE
  76.     }
  77.  
  78.     // YOUR AUXILIARY FUNCTIONS GO HERE
  79.     public static boolean enzymeMatchAt(String s, String enzyme, int start){
  80.         if (s.length() - start < enzyme.length())
  81.             return false;
  82.         for (int i = 0; i < enzyme.length(); i++){
  83.             if (s.charAt(start+i) != enzyme.charAt(i)){
  84.                 return false;
  85.             }
  86.         }
  87.         return true;
  88.     }
  89.  
  90.     public static String[] restrictFragments(String s, String enzyme){
  91.         int a = Utility.countOccurrences(s, enzyme);
  92.         String fragments[] = new String[a+1]; //This will return an array that may be too big in the case that the last few characters in the fragment are the enzyme, but that shouldn't have any consequences for the program.
  93.         for (int y = 0; y < a+1; y++){
  94.             fragments[y] = "";
  95.         }
  96.         int b = 0; // b tracks the slot in fragments
  97.         int i = 0; // i tracks progress through s
  98.         while (i < s.length()){
  99.             if (enzymeMatchAt(s, enzyme, i)){
  100.                 fragments[b] = fragments[b] + enzyme;
  101.                 b++;
  102.                 i = i + enzyme.length();
  103.             } else {
  104.                 fragments[b] = fragments[b] + s.charAt(i);
  105.                 i++;
  106.             }
  107.         }
  108.         return fragments;
  109.     }
  110.     public static String convertProbe(String probe){
  111.         String probeReversed = "";
  112.         int i = 0;
  113.         while(i < probe.length()){
  114.             if (probe.charAt(i) == 'A'){
  115.                 probeReversed = probeReversed + 'T';
  116.             } else {
  117.                 if (probe.charAt(i) == 'T'){
  118.                     probeReversed = probeReversed + 'A';
  119.                 }
  120.             }
  121.             if (probe.charAt(i) == 'C'){
  122.                 probeReversed = probeReversed + 'G';
  123.             } else {
  124.                 if (probe.charAt(i) == 'G'){
  125.                     probeReversed = probeReversed + 'C';
  126.                 }
  127.             }
  128.             i++;
  129.         }
  130.         return probeReversed;
  131.     }
  132.    
  133.  
  134.                
  135.    
  136.     public static void main(String[] args) {
  137.     if (args.length != 3) {
  138.         System.err.println("usage: java SouthernBlot <sequence file name> <restriction enzyme> <probe file name>");
  139.         System.exit(0);
  140.     }
  141.  
  142.     String sequence = Utility.readSequence(args[0]);
  143.     String enzyme = args[1].toUpperCase();
  144.     String probe = Utility.readSequence(args[2]);
  145.  
  146.     boolean[] film = genFilm(sequence, enzyme, probe);
  147.     drawFilm(film);
  148.     }
  149. }
Add Comment
Please, Sign In to add comment