Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- /**
- *This program converts rna to dna & vice versa
- */
- public class DNAtoRNA {
- /**
- *This main method gets user input
- *and calls method the other methods
- *@param args main method arguments
- */
- public static void main(String[] args) {
- Scanner console = new Scanner(System.in);
- boolean toRNA = true;
- System.out.println(" Welcome to the RNA to DNA Converter");
- System.out.println("You may enter R to convert DNA sequences " +
- "to RNA, D to convert RNA sequences to DNA,");
- System.out.println("or Q to quit, and names of the input " +
- "and output files for each conversion. Each line ");
- System.out.println("of the file contains a single sequence " +
- "followed by 0 or more mutation positions.");
- System.out.println();
- boolean isValid = false;
- while (!isValid) {
- System.out.print("Enter R (convert to RNA), D (convert to DNA), or q (quit): ");
- String letterInput = console.next();
- if (letterInput.equalsIgnoreCase("R")) {
- toRNA = true;
- isValid = true;
- }
- if (letterInput.equalsIgnoreCase("D")) {
- toRNA = false;
- isValid = true;
- }
- if (letterInput.equalsIgnoreCase("Q")) {
- isValid = true;
- System.exit(1);
- }
- if (letterInput.length() > 1) {
- isValid = false;
- }
- }
- PrintStream output = null;
- Scanner input = null;
- processFile(toRNA, input, output);
- }
- /**
- *Takes a file as input from
- *the user
- *@param console console that accepts input
- *@return returns the input when called
- */
- public static Scanner getInputScanner(Scanner console) {
- Scanner input = null;
- while (input == null) {
- System.out.print("Enter input file: ");
- String inputName = console.next();
- try {
- input = new Scanner(new File(inputName));
- } catch (FileNotFoundException e) {
- System.out.println("Problem Creating file, try again.");
- }
- }
- return input;
- }
- /**
- *Creates a new file
- *for the output
- *@param console console that accepts input
- *@return returns the output when called
- */
- public static PrintStream getOutputPrintStream(Scanner console) {
- PrintStream output = null;
- while (output == null) {
- System.out.print("Enter output file: ");
- String outputName = console.next();
- try {
- output = new PrintStream(new File(outputName));
- } catch (FileNotFoundException e) {
- System.out.println("File not found. Please try again.");
- }
- }
- return output;
- }
- /**
- *processes the file
- *puts the dna and rna lines
- *in to a line that is passed
- *to the convert methods
- *Then calls the convert methods
- *and presents the result
- *@param toRNA boolean that decides how to convert
- *@param input scanner that takes input
- *@param output printstream thats prints to a new file
- */
- public static void processFile(boolean toRNA, Scanner input, PrintStream output) {
- Scanner console = new Scanner(System.in);
- input = getInputScanner(console);
- output = getOutputPrintStream(console);
- while (input.hasNext()) {
- String line = input.nextLine();
- String toBePrinted = "";
- if (toRNA == true) {
- toBePrinted = convertToRNA(line);
- System.out.print(" ");
- } else {
- toBePrinted = convertToDNA(line);
- }
- output.println(toBePrinted);
- }
- }
- /**
- *converts the input
- *to rna
- *@param line string that contains input
- *@return returns the converted RNA
- */
- public static String convertToRNA(String line) {
- String conversion = "";
- Scanner scan = new Scanner(line);
- String n = scan.next();
- for (int i = 0; i < n.length(); i++) {
- if (n.charAt(i) == ('A')) {
- conversion += "U";
- } else if (n.charAt(i) == 'T') {
- conversion += "A";
- } else if (n.charAt(i) == 'C') {
- conversion += "G";
- } else if (n.charAt(i) == 'G') {
- conversion += "C";
- }
- else {
- throw new IllegalArgumentException();
- }
- }
- while (scan.hasNext()) {
- n = scan.next();
- int mutation;
- try {
- mutation = Integer.parseInt(n);
- } catch (NumberFormatException e) {
- throw new IllegalArgumentException();
- }
- if (conversion.length() > mutation + 1) {
- if (conversion.charAt(mutation) == 'U') {
- conversion = conversion.substring(0, mutation)
- + "C" + conversion.substring(mutation + 1);
- } else if (conversion.charAt(mutation) == 'A') {
- conversion = conversion.substring(0, mutation) +
- "G" + conversion.substring(mutation + 1);
- } else if (conversion.charAt(mutation) == 'G') {
- conversion = conversion.substring(0, mutation)
- + "U" + conversion.substring(mutation + 1);
- } else if (conversion.charAt(mutation) == 'C') {
- conversion = conversion.substring(0, mutation)
- + "A" + conversion.substring(mutation + 1);
- }
- } else if (conversion.length() == mutation + 1) {
- if (conversion.charAt(mutation) == 'U') {
- conversion = conversion.substring(0, mutation) + "C";
- } else if (conversion.charAt(mutation) == 'A') {
- conversion = conversion.substring(0, mutation) + "G";
- } else if (conversion.charAt(mutation) == 'G') {
- conversion = conversion.substring(0, mutation) + "U";
- } else if (conversion.charAt(mutation) == 'C') {
- conversion = conversion.substring(0, mutation) + "A";
- }
- else {
- throw new IllegalArgumentException();
- }
- }
- conversion += " " + mutation;
- }
- scan.close();
- return conversion;
- }
- /**
- *converts input to dna
- *@param line line thats contains input
- *@return returns the converted DNAtoRNA
- */
- public static String convertToDNA(String line) {
- String conversion = "";
- Scanner scan = new Scanner(line);
- String n = scan.next();
- for (int i = 0; i < n.length(); i++) {
- if (n.charAt(i) == ('U')) {
- conversion += "A";
- } else if (n.charAt(i) == 'A') {
- conversion += "T";
- } else if (n.charAt(i) == 'G') {
- conversion += "C";
- } else if (n.charAt(i) == 'C') {
- conversion += "G";
- }
- else {
- throw new IllegalArgumentException();
- }
- }
- while (scan.hasNext()) {
- n = scan.next();
- int mutation;
- try {
- mutation = Integer.parseInt(n);
- } catch (NumberFormatException e) {
- throw new IllegalArgumentException();
- }
- if (conversion.length() > mutation + 1) {
- if (conversion.charAt(mutation) == 'C') {
- conversion = conversion.substring(0, mutation)
- + "T" + conversion.substring(mutation + 1);
- } else if (conversion.charAt(mutation) == 'G') {
- conversion = conversion.substring(0, mutation)
- + "A" + conversion.substring(mutation + 1);
- } else if (conversion.charAt(mutation) == 'A') {
- conversion = conversion.substring(0, mutation)
- + "C" + conversion.substring(mutation + 1);
- } else if (conversion.charAt(mutation) == 'T') {
- conversion = conversion.substring(0, mutation)
- + "G" + conversion.substring(mutation + 1);
- }
- } else if(conversion.length() == mutation + 1){
- if (conversion.charAt(mutation) == 'C') {
- conversion = conversion.substring(0, mutation) + "T";
- } else if (conversion.charAt(mutation) == 'G') {
- conversion = conversion.substring(0, mutation) + "A";
- } else if (conversion.charAt(mutation) == 'A') {
- conversion = conversion.substring(0, mutation) + "C";
- } else if (conversion.charAt(mutation) == 'T') {
- conversion = conversion.substring(0, mutation) + "G";
- }
- else {
- throw new IllegalArgumentException();
- }
- }
- conversion += " " + mutation;
- }
- scan.close();
- return conversion;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement