Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.49 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. /**
  4. *This program converts rna to dna & vice versa
  5. */
  6. public class DNAtoRNA {
  7. /**
  8. *This main method gets user input
  9. *and calls method the other methods
  10. *@param args main method arguments
  11. */
  12. public static void main(String[] args) {
  13. Scanner console = new Scanner(System.in);
  14.  
  15. boolean toRNA = true;
  16.  
  17. System.out.println(" Welcome to the RNA to DNA Converter");
  18. System.out.println("You may enter R to convert DNA sequences " +
  19. "to RNA, D to convert RNA sequences to DNA,");
  20. System.out.println("or Q to quit, and names of the input " +
  21. "and output files for each conversion. Each line ");
  22. System.out.println("of the file contains a single sequence " +
  23. "followed by 0 or more mutation positions.");
  24. System.out.println();
  25. boolean isValid = false;
  26.  
  27. while (!isValid) {
  28. System.out.print("Enter R (convert to RNA), D (convert to DNA), or q (quit): ");
  29. String letterInput = console.next();
  30. if (letterInput.equalsIgnoreCase("R")) {
  31. toRNA = true;
  32. isValid = true;
  33. }
  34. if (letterInput.equalsIgnoreCase("D")) {
  35. toRNA = false;
  36. isValid = true;
  37. }
  38. if (letterInput.equalsIgnoreCase("Q")) {
  39. isValid = true;
  40. System.exit(1);
  41. }
  42. if (letterInput.length() > 1) {
  43. isValid = false;
  44. }
  45.  
  46. }
  47. PrintStream output = null;
  48. Scanner input = null;
  49. processFile(toRNA, input, output);
  50. }
  51. /**
  52. *Takes a file as input from
  53. *the user
  54. *@param console console that accepts input
  55. *@return returns the input when called
  56. */
  57. public static Scanner getInputScanner(Scanner console) {
  58. Scanner input = null;
  59. while (input == null) {
  60. System.out.print("Enter input file: ");
  61. String inputName = console.next();
  62. try {
  63. input = new Scanner(new File(inputName));
  64. } catch (FileNotFoundException e) {
  65. System.out.println("Problem Creating file, try again.");
  66. }
  67. }
  68. return input;
  69. }
  70. /**
  71. *Creates a new file
  72. *for the output
  73. *@param console console that accepts input
  74. *@return returns the output when called
  75. */
  76. public static PrintStream getOutputPrintStream(Scanner console) {
  77. PrintStream output = null;
  78. while (output == null) {
  79. System.out.print("Enter output file: ");
  80. String outputName = console.next();
  81. try {
  82. output = new PrintStream(new File(outputName));
  83. } catch (FileNotFoundException e) {
  84. System.out.println("File not found. Please try again.");
  85. }
  86. }
  87. return output;
  88. }
  89.  
  90. /**
  91. *processes the file
  92. *puts the dna and rna lines
  93. *in to a line that is passed
  94. *to the convert methods
  95. *Then calls the convert methods
  96. *and presents the result
  97. *@param toRNA boolean that decides how to convert
  98. *@param input scanner that takes input
  99. *@param output printstream thats prints to a new file
  100. */
  101. public static void processFile(boolean toRNA, Scanner input, PrintStream output) {
  102. Scanner console = new Scanner(System.in);
  103. input = getInputScanner(console);
  104. output = getOutputPrintStream(console);
  105.  
  106. while (input.hasNext()) {
  107. String line = input.nextLine();
  108.  
  109. String toBePrinted = "";
  110. if (toRNA == true) {
  111. toBePrinted = convertToRNA(line);
  112. System.out.print(" ");
  113. } else {
  114. toBePrinted = convertToDNA(line);
  115. }
  116. output.println(toBePrinted);
  117. }
  118. }
  119.  
  120. /**
  121. *converts the input
  122. *to rna
  123. *@param line string that contains input
  124. *@return returns the converted RNA
  125. */
  126. public static String convertToRNA(String line) {
  127. String conversion = "";
  128. Scanner scan = new Scanner(line);
  129.  
  130. String n = scan.next();
  131.  
  132.  
  133.  
  134. for (int i = 0; i < n.length(); i++) {
  135. if (n.charAt(i) == ('A')) {
  136. conversion += "U";
  137. } else if (n.charAt(i) == 'T') {
  138. conversion += "A";
  139. } else if (n.charAt(i) == 'C') {
  140. conversion += "G";
  141. } else if (n.charAt(i) == 'G') {
  142. conversion += "C";
  143. }
  144. else {
  145. throw new IllegalArgumentException();
  146. }
  147. }
  148.  
  149. while (scan.hasNext()) {
  150. n = scan.next();
  151. int mutation;
  152. try {
  153. mutation = Integer.parseInt(n);
  154. } catch (NumberFormatException e) {
  155. throw new IllegalArgumentException();
  156. }
  157.  
  158. if (conversion.length() > mutation + 1) {
  159. if (conversion.charAt(mutation) == 'U') {
  160. conversion = conversion.substring(0, mutation)
  161. + "C" + conversion.substring(mutation + 1);
  162. } else if (conversion.charAt(mutation) == 'A') {
  163. conversion = conversion.substring(0, mutation) +
  164. "G" + conversion.substring(mutation + 1);
  165. } else if (conversion.charAt(mutation) == 'G') {
  166. conversion = conversion.substring(0, mutation)
  167. + "U" + conversion.substring(mutation + 1);
  168. } else if (conversion.charAt(mutation) == 'C') {
  169. conversion = conversion.substring(0, mutation)
  170. + "A" + conversion.substring(mutation + 1);
  171. }
  172. } else if (conversion.length() == mutation + 1) {
  173. if (conversion.charAt(mutation) == 'U') {
  174. conversion = conversion.substring(0, mutation) + "C";
  175. } else if (conversion.charAt(mutation) == 'A') {
  176. conversion = conversion.substring(0, mutation) + "G";
  177. } else if (conversion.charAt(mutation) == 'G') {
  178. conversion = conversion.substring(0, mutation) + "U";
  179. } else if (conversion.charAt(mutation) == 'C') {
  180. conversion = conversion.substring(0, mutation) + "A";
  181. }
  182. else {
  183. throw new IllegalArgumentException();
  184. }
  185. }
  186. conversion += " " + mutation;
  187. }
  188. scan.close();
  189. return conversion;
  190. }
  191.  
  192. /**
  193. *converts input to dna
  194. *@param line line thats contains input
  195. *@return returns the converted DNAtoRNA
  196. */
  197. public static String convertToDNA(String line) {
  198. String conversion = "";
  199. Scanner scan = new Scanner(line);
  200.  
  201. String n = scan.next();
  202. for (int i = 0; i < n.length(); i++) {
  203. if (n.charAt(i) == ('U')) {
  204. conversion += "A";
  205. } else if (n.charAt(i) == 'A') {
  206. conversion += "T";
  207. } else if (n.charAt(i) == 'G') {
  208. conversion += "C";
  209. } else if (n.charAt(i) == 'C') {
  210. conversion += "G";
  211. }
  212. else {
  213. throw new IllegalArgumentException();
  214. }
  215. }
  216.  
  217. while (scan.hasNext()) {
  218. n = scan.next();
  219. int mutation;
  220. try {
  221. mutation = Integer.parseInt(n);
  222. } catch (NumberFormatException e) {
  223. throw new IllegalArgumentException();
  224. }
  225. if (conversion.length() > mutation + 1) {
  226. if (conversion.charAt(mutation) == 'C') {
  227. conversion = conversion.substring(0, mutation)
  228. + "T" + conversion.substring(mutation + 1);
  229. } else if (conversion.charAt(mutation) == 'G') {
  230. conversion = conversion.substring(0, mutation)
  231. + "A" + conversion.substring(mutation + 1);
  232. } else if (conversion.charAt(mutation) == 'A') {
  233. conversion = conversion.substring(0, mutation)
  234. + "C" + conversion.substring(mutation + 1);
  235. } else if (conversion.charAt(mutation) == 'T') {
  236. conversion = conversion.substring(0, mutation)
  237. + "G" + conversion.substring(mutation + 1);
  238. }
  239. } else if(conversion.length() == mutation + 1){
  240. if (conversion.charAt(mutation) == 'C') {
  241. conversion = conversion.substring(0, mutation) + "T";
  242. } else if (conversion.charAt(mutation) == 'G') {
  243. conversion = conversion.substring(0, mutation) + "A";
  244. } else if (conversion.charAt(mutation) == 'A') {
  245. conversion = conversion.substring(0, mutation) + "C";
  246. } else if (conversion.charAt(mutation) == 'T') {
  247. conversion = conversion.substring(0, mutation) + "G";
  248. }
  249. else {
  250. throw new IllegalArgumentException();
  251. }
  252. }
  253. conversion += " " + mutation;
  254. }
  255. scan.close();
  256. return conversion;
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement