Find Gene Coding (Java String Concept)

 




Part 1: Finding a Gene - Using the Simplified Algorithm

This assignment is to write the code from the lesson from scratch by following the steps below. This will help you see if you really understood how to put the code together, and might identify a part that you did not fully understand. If you get stuck, then you can go back and watch the coding videos that go with this lesson again. We recommend you try this with many of the future Java coding examples before starting programming exercises.

Specifically, you should do the following:

1. Create a new Java project named StringsFirstAssignments. You can put all the classes for this programming exercise in this project.

2. Create a new Java Class named Part1. The following methods go in this class.

3. . Write the method findSimpleGene that has one String parameter dna, representing a string of DNA. This method does the following:

  • Finds the index position of the start codon “ATG”. If there is no “ATG”, return the empty string.
  • Finds the index position of the first stop codon “TAA” appearing after the “ATG” that was found. If there is no such “TAA”, return the empty string.
  • If the length of the substring between the “ATG” and “TAA” is a multiple of 3, then return the substring that starts with that “ATG” and ends with that “TAA”.

4. Write the void method testSimpleGene that has no parameters. You should create five DNA strings. The strings should have specific test cases, such as: DNA with no “ATG”, DNA with no “TAA”, DNA with no “ATG” or “TAA”, DNA with ATG, TAA and the substring between them is a multiple of 3 (a gene), and DNA with ATG, TAA and the substring between them is not a multiple of 3. For each DNA string you should:

  • Print the DNA string.
  • See if there is a gene by calling findSimpleGene with this string as the parameter. If a gene exists following our algorithm above, then print the gene, otherwise print the empty string.
Coding

import java.io.*;
import java.util.Scanner; 
public class TagFinder {

public static String findProtein(String dna) {
        String lowerCaseDna = dna.toLowerCase();
        int start = lowerCaseDna.indexOf("atg");
        if (start == -1) {
            return "";
        }
        int stop = lowerCaseDna.indexOf("tag", start+3);
        if ((stop - start) % 3 == 0) {
            return lowerCaseDna.substring(start, stop+3);
        } 
        stop = lowerCaseDna.indexOf("tga", start+3);
        if((stop-start)%3 == 0){
            return lowerCaseDna.substring(start, stop+3);
          }
        stop = lowerCaseDna.indexOf("taa", start+3);
        if((stop-start)%3 == 0){
            return lowerCaseDna.substring(start, stop+3);
          }
        else {
            return "";
        }
    }
    
public static String findStopCodon(String gene){
        if ((gene.length()%3 == 0)&&(gene.length()!= 0)){
            return gene.substring(gene.length()-3, gene.length());
        } else {
            return "";
        }
    }
public static void main(String [] args){
Scanner myObj = new Scanner(System.in);  // Create a Scanner object
System.out.println("Enter string a:");

String a = myObj.nextLine();  // Read user input
System.out.println("string a is: " + a);  // Output user input

System.out.println("Enter string ap:");

String ap = myObj.nextLine();  // Read user input
System.out.println("string ap is: " + ap);  // Output user input
        //String a = "AATGCTAGTTTAAATCTGA";
        //String ap = "ATGCTAGTTTAAATCTGA".toLowerCase();
        //String a = "ataaactatgttttaaatgt";
       // String ap = "atgttttaa";
       // String a = "acatgataacctaag";
       // String ap = "";
        //String a = "cccatggggtttaaataataataggagagagagagagagttt";
        //String ap = "atggggtttaaataataatag";
        //String a = "atgcctag";
        //String ap = "";
        //String a = "ATGCCCTAG";
        //String ap = "ATGCCCTAG";
        String result = findProtein(a);
        if (ap.equals(result)) {
            System.out.println("success for: " + ap + " length: " + ap.length() + " stopCodon: " + findStopCodon(result));
        }
        else {
            System.out.println("mistake for input: " + a);
            System.out.println("got: " + result);
            System.out.println("not: " + ap);
        }
    }
}



Post a Comment

0 Comments