DNATranslation.java
资源名称:DNA.rar [点击查看]
上传用户:along415
上传日期:2018-01-31
资源大小:3k
文件大小:16k
源码类别:
DNA
开发平台:
Java
- import java.util.Scanner;
- public class DNATranslation {
- /**
- * accepts a DNA strand and reports the protein generated,
- * if any, from the DNA strand
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO accepts a DNA strand and reports the protein generated,
- // if any, from the DNA strand
- //REQUIRES : The input to contain only valid, upper-case,
- // DNA nucleotide base letters (A, C, G, and T).
- // No input line will exceed 255 characters in length.
- // There will be no blank lines or spaces in the input.
- //EFFECTS : Accepts a DNA strand and reports the protein generated,
- // if any, from the DNA strand
- // The string ``*** No translatable DNA found ***'' should be output
- // when an input DNA strand does not translate into a valid protein.
- Scanner scanner = new Scanner(System.in);
- System.out.println("Enter the DNA strands,end with '*' :");
- String input = scanner.next();
- String output = "";
- while(input.equals("*") == false){
- input = input.replaceAll("T", "U");
- input = valid1(input);
- if(! input.equals("*** No translatable DNA found ***")&& input.length() % 3 == 0){
- int proteinQuantity = input.length() / 3;
- char [] DNAArray = new char[input.length()];
- for(int i = 0; i < input.length(); i++)
- DNAArray[i] = input.charAt(i);
- for(int i = 1; i < proteinQuantity; i++){
- // translate the DNA strands into protein
- switch(DNAArray[i * 3]){
- case 'U':{
- switch (DNAArray[i * 3 + 1]){
- case 'U':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Phe-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Phe-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Leu-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Leu-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'C':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Ser-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Ser-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Ser-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Ser-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'A':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Tyr-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Tyr-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'G':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Cys-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Cys-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Trp-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- }
- break;
- }
- case 'C':{
- switch (DNAArray[i * 3 + 1]){
- case 'U':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Leu-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Leu-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Leu-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Leu-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'C':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Pro-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Pro-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Pro-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Pro-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'A':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "His-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "His-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Gln-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Gln-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'G':{
- switch(DNAArray[i * 3+ 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Arg-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Arg-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Arg-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Arg-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- }
- break;
- }
- case 'A':{
- switch (DNAArray[i * 3 + 1]){
- case 'U':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Ile-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Ile-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Ile-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Met-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'C':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Thr-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Thr-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Thr-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Thr-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'A':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Asn-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Asn-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Lys-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Lys-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'G':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Ser-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Ser-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Arg-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Arg-" + input.substring(i * 4 + 2,input.length());
- break;
- default:
- input = new String("*** No translatable DNA found ***");
- }
- break;
- }
- }
- break;
- }
- case 'G':{
- switch (DNAArray[i * 3 + 1]){
- case 'U':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Val-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Val-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Val-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Val-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'C':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Ala-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Ala-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Ala-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Ala-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'A':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Asp-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Asp-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Gru-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Gru-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- case 'G':{
- switch(DNAArray[i * 3 + 2]){
- case 'U':
- input = input.substring(0,i * 4 -1) + "Gly-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'C':
- input = input.substring(0,i * 4 -1) + "Gly-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'A':
- input = input.substring(0,i * 4 -1) + "Gly-" + input.substring(i * 4 + 2,input.length());
- break;
- case 'G':
- input = input.substring(0,i * 4 -1) + "Gly-" + input.substring(i * 4 + 2,input.length());
- break;
- }
- break;
- }
- }
- break;
- }
- }
- }
- output += "n" + input.substring(3,input.length()-1);
- }
- else
- output += "n" + "*** No translatable DNA found ***";
- input = scanner.next();
- }
- if(input.equals("*"))
- System.out.println(output);
- }
- private static String valid1 (String a) {
- //EFFECTS : If the DNA strands can produce valid protein sequences, translate
- // it into another strands contains letter 'U','C','G','A',
- // and start with "AUG" ,ends with any of the codons UAA, UAG, or UGA;
- String origin = new String(a);
- for(int i = 0; i < a.length(); i++){
- if(a.substring(i).startsWith("AUG")){
- a = a.substring(i);
- int l = a.length()/3;
- for(int j = 1; j < l; j++){
- String b = a.substring(j * 3);
- if(b.startsWith("UAA")||b.startsWith("UAG")||b.startsWith("UGA"))
- {a = a.substring(0,(j+1) * 3);
- return a;
- }
- }
- }
- }
- return valid2(origin);
- }
- private static String valid2 (String a) {
- //EFFECTS : If the DNA strands can produce valid protein sequences, translate
- // it into another strands contains letter 'U','C','G','A',
- // and start with "AUG" ,ends with any of the codons UAA, UAG, or UGA;
- String origin = new String(a);
- for(int i = 0; i < a.length(); i++){
- if(a.substring(i).startsWith("UAC")){
- a = a.substring(i);
- int l = a.length()/3;
- for(int j = 1; j < l; j++){
- String b = a.substring(j * 3);
- if(b.startsWith("AUU")||b.startsWith("AUC")||b.startsWith("ACU")){
- a = a.substring(0,(j+1) * 3);
- a = a.replaceAll("A", "M");
- a = a.replaceAll("U", "N");
- a = a.replaceAll("G", "P");
- a = a.replaceAll("C", "Q");
- a = a.replaceAll("M", "U");
- a = a.replaceAll("N", "A");
- a = a.replaceAll("P", "C");
- a = a.replaceAll("Q", "G");
- return a;
- }
- }
- }
- }
- return valid3(origin);
- }
- private static String valid3 (String a) {
- //EFFECTS : If the DNA strands can produce valid protein sequences, translate
- // it into another strands contains letter 'U','C','G','A',
- // and start with "AUG" ,ends with any of the codons UAA, UAG, or UGA;
- String fail = new String("*** No translatable DNA found ***");
- String origin = new String(a);
- for(int i = 0; i < a.length(); i++){
- if(a.substring(i).startsWith("AGU")||a.substring(i).startsWith("GAU")||a.substring(i).startsWith("AAU")){
- a = a.substring(i);
- int l = a.length()/3;
- for(int j = 1; j < l; j++){
- String b = a.substring(j * 3);
- if(b.startsWith("GUA")){
- a = a.substring(0,(j+1) * 3);
- StringBuffer c = new StringBuffer(a);
- a = c.reverse().toString();
- return a;
- }
- }
- }
- }
- return valid4(origin);
- }
- private static String valid4 (String a) {
- //EFFECTS : If the DNA strands can produce valid protein sequences, translate
- // it into another strands contains letter 'U','C','G','A',
- // and start with "AUG" ,ends with any of the codons UAA, UAG, or UGA;
- String fail = new String("*** No translatable DNA found ***");
- String origin = new String(a);
- for(int i = 0; i < a.length(); i++){
- if(a.substring(i).startsWith("UCA")||a.substring(i).startsWith("CUA")||a.substring(i).startsWith("UUA")){
- a = a.substring(i);
- int l = a.length()/3;
- for(int j = 1; j < l; j++){
- String b = a.substring(j * 3);
- if(b.startsWith("GUA")){
- a = a.substring(0,(j+1) * 3);;
- StringBuffer c = new StringBuffer(a);
- a = c.reverse().toString();
- a = a.replaceAll("A", "M");
- a = a.replaceAll("U", "N");
- a = a.replaceAll("G", "P");
- a = a.replaceAll("C", "Q");
- a = a.replaceAll("M", "U");
- a = a.replaceAll("N", "A");
- a = a.replaceAll("P", "C");
- a = a.replaceAll("Q", "G");
- return a;
- }
- }
- }
- }
- a = fail;
- return a;
- }
- }