-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDNAFileProcessor.java
More file actions
333 lines (276 loc) · 11.1 KB
/
Copy pathDNAFileProcessor.java
File metadata and controls
333 lines (276 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/********************************************************************************
* Written by: Mark Bonner *
* *
* This program will perform various computations on genome sequence *
* files. The genome input file must be of FASTA format, with the accessionID *
* at the top line and the entire genome sequence following immediately *
* on the next line. Output files will be created for complementation, *
* transcription, and translation computations. *
* *
* Computations performed: *
* 1. Count individual DNA nucleotides *
* 2. Compute and return the GC content of the genome *
* 3. Complement and reverse a genome *
* 4. Transcribe DNA sequence to RNA *
* 5. Translate RNA sequence to proteins *
* 6. Calculate the protein mass of the protein sequence *
* 7. Count Hamming Distance (point mutations) between two DNA sequences *
* *
* This program is continually being updated with new computations. *
* *
********************************************************************************/
import java.util.*;
import java.io.*;
public class DNAFileProcessor {
public static void main(String[] args) throws FileNotFoundException {
Scanner console = new Scanner(System.in);
prompt();
// Get file FASTA file contents
HashMap<String, String> info = new HashMap<>(getFile(console));
String fileName = info.get("fileName");
String accessionID = info.get("ID");
String dna = info.get("genome");
System.out.println("\nComputations performed on " + accessionID);
// Counting DNA Nucleotides
System.out.println("\nNucleotide count in genome: ");
countNucleotides(accessionID, dna);
// Computing GC Content
System.out.println("\nGuanine/Cytosine content of the DNA: ");
computingGCContent(dna);
// Complementing a strand of DNA
System.out.println("\nReverse Complemented DNA: ");
reverseComplement(fileName, accessionID, dna);
// Transcribe DNA into RNA
System.out.println("\nDNA Transcribed to RNA: ");
transcription(fileName, accessionID, dna);
// Translating RNA into Protein
System.out.println("\nRNA Sequence Translated into Proteins: ");
String rna = transcription(dna);
String protein = translateRNAIntoProtein(fileName, accessionID, rna);
// Calculating Protein Mass
System.out.println("\nTotal monoisotopic mass of protein string: ");
proteinMass(protein);
// Counting Point Mutations
System.out.println("\n\nEnter y/n if you would like to count point mutations "
+ "between two DNA sequences of equal length: ");
if (console.nextLine().equalsIgnoreCase("y")) {
countingPointMutations(dna, accessionID, console);
}
}
// Get FASTA file input
public static HashMap<String, String> getFile(Scanner console) throws FileNotFoundException {
HashMap<String, String> info = new HashMap<>();
// Ask the user for the name of the file they wish to process
String inputName = console.nextLine();
// Open the file by appending .fasta to the postfix
Scanner fileScanner = new Scanner(new File(inputName + ".fasta"));
// Copy accessionID from genome sequence
String accessionID = fileScanner.nextLine();
// Copy entire DNA sequence into a variable
String dna = "";
while (fileScanner.hasNextLine()) {
dna += fileScanner.nextLine();
}
info.put("fileName", inputName);
info.put("ID", accessionID);
info.put("genome", dna);
return info;
}
/* Given: A protein string P.
* Return: The total weight of P.
*/
public static void proteinMass(String p) throws FileNotFoundException {
Scanner protein = new Scanner(p);
char[] proteinString = protein.next().toCharArray();
// constructing the data structure for the Monoisotopic Mass Table
HashMap<Character, Double> massTable = buildMassTable();
double weight = 0.0;
/* iterate through each amino acid in the protein string and get the
* monoisotopic mass for that amino acid from the data structure
*/
for (int i = 0; i < proteinString.length; i++) {
weight += massTable.getOrDefault(proteinString[i], 0.0);
}
System.out.printf("%.3f", weight);
System.out.print(" Da");
protein.close();
}
/* method to create a data structure for each amino acid
* and it's corresponding monoisotopic mass
*/
public static HashMap<Character, Double> buildMassTable() throws FileNotFoundException {
Scanner file = new Scanner(new File("monoisotopic_mass_table.txt"));
HashMap<Character, Double> massTable = new HashMap<>();
while (file.hasNext()) {
Character aminoAcid = file.next().charAt(0);
Double mass = file.nextDouble();
massTable.put(aminoAcid, mass);
}
return massTable;
}
/* Given: Two DNA strings s and t of equal length.
* Return: The Hamming distance dH(s,t).
*/
public static void countingPointMutations(String dna1, String ID1, Scanner console) throws FileNotFoundException {
System.out.println("Input file name of second DNA sequence, following the guide above: ");
// Get file FASTA file contents
HashMap<String, String> info = new HashMap<>(getFile(console));
//String fileName = info.get("fileName");
String ID2 = info.get("ID");
String dna2 = info.get("genome");
int hammingDistance = 0;
char[] array1 = new char[dna1.length()];
char[] array2 = new char[dna2.length()];
// copy each nucleobase from the DNA string into an array
for (int i = 0; i < dna1.length(); i++) {
array1[i] = dna1.charAt(i);
array2[i] = dna2.charAt(i);
}
/* interate through each element of the arrays and compare the nucleobases,
* if the nucleobases match, then increment the hammingDistance variable
*/
for (int n = 0; n < array1.length; n++) {
if (array1[n] != array2[n]) {
hammingDistance++;
}
}
System.out.println("\nPoint Mutations between given DNA sequences: ");
System.out.println(ID1 + "\n" + ID2);
System.out.println(hammingDistance);
}
/* Given: A DNA string dna.
* Return: The GC-content of the given dna sequence.
*/
public static void computingGCContent(String dna) {
/* count each guanine and cytosine nucleobase in the DNA string and
* print out the GC-content percentage
*/
int count = 0;
int length = dna.length();
for (int i = 0; i < length - 1; i++) {
if (dna.charAt(i) == 'G' || dna.charAt(i) == 'C') {
count++;
}
}
double GCPercentage = (double) count / length * 100;
System.out.printf("%.6f", GCPercentage);
System.out.println("%");
}
/* Given: A DNA string dna.
* Return: The reverse complement dna^c of dna.
*/
public static void reverseComplement(String fileName, String id, String dna) throws FileNotFoundException {
PrintStream outputFile = new PrintStream(new File(fileName + "_OUTPUT_COMPLEMENT.txt"));
// building a data structure for the complement of each DNA symbol
HashMap<Character, Character> complement = new HashMap<>();
complement.put('A', 'T');
complement.put('T', 'A');
complement.put('C', 'G');
complement.put('G', 'C');
String reverseComplement = "";
/* begin the for-loop from the end of the given DNA string
* and complement each symbol into the reverse complement DNA string. *
*/
for (int i = dna.length() - 1; i >= 0; i--) {
reverseComplement += complement.get(dna.charAt(i));
}
outputFile.print(id + "\n" + reverseComplement);
if (reverseComplement.length() < 101) {
System.out.println("***output file created***");
System.out.println(reverseComplement);
} else {
System.out.println("***output file created***");
}
}
/* Given: A DNA string dna.
* Return: The transcribed RNA string of given dna.
*/
public static void transcription(String fileName, String id, String dna) throws FileNotFoundException {
PrintStream outputFile = new PrintStream(new File(fileName + "_OUTPUT_TRANSCRIPTION.txt"));
String rna = dna.replace('T', 'U');
outputFile.print(id + "\n" + rna);
if (rna.length() < 101) {
System.out.println("***output file created***");
System.out.println(rna);
} else {
System.out.println("***output file created***");
}
}
public static String transcription(String dna) {
String rna = dna.replace('T', 'U');
return rna;
}
/* Given: An RNA string rna corresponding to a strand of mRNA.
* Return: The protein string encoded by rna.
*/
public static String translateRNAIntoProtein(String fileName, String id, String rna) throws FileNotFoundException {
PrintStream outputFile = new PrintStream(new File(fileName + "_OUTPUT_RNA_TO_PROTEIN.txt"));
HashMap<String, String> rnaCodonTable = buildRNACodonStructure();
String[] arr = new String[rna.length() / 3];
// copy all codons in the RNA string into an array
for (int i = 0, start = 0, end = 3; i < arr.length; i++, start += 3, end += 3) {
arr[i] = rna.substring(start, end);
}
String proteinString = "";
// use the RNA Codon Table data structure to encode each codon into it's corresponding amino acid
for (int i = 0; i < arr.length; i++) {
proteinString += rnaCodonTable.get(arr[i]);
}
outputFile.print(id + "\n" + proteinString);
outputFile.close();
if (proteinString.length() < 101) {
System.out.println("***output file created***");
System.out.print(proteinString + "\n");
} else {
System.out.println("***output file created***");
}
return proteinString;
}
/* this method uses an input file containing the amino acid alphabet
* and it's corresponding codons to build a data structure for encoding
*/
public static HashMap<String, String> buildRNACodonStructure() throws FileNotFoundException {
Scanner table = new Scanner(new File("RNA_codon_table.txt"));
HashMap<String, String> rnaCodonTable = new HashMap<String, String>();
while (table.hasNext()) {
String rnaString = table.next();
String aminoAcid = table.next();
rnaCodonTable.put(rnaString, aminoAcid);
}
return rnaCodonTable;
}
/* Given: A DNA string dna.
* Return: Four integer counts for the respective number of times
* that the symbols 'A', 'C', 'G', and 'T' occur in dna.
*/
public static void countNucleotides(String id, String dna) {
int[] array = new int[4];
/* loop through each char in the DNA string and
* increment the corresponding element of the array
*/
for (int i = 0; i < dna.length(); i++) {
if (dna.charAt(i) == 'A') {
array[0]++;
} else if (dna.charAt(i) == 'C') {
array[1]++;
} else if (dna.charAt(i) == 'G') {
array[2]++;
} else {
array[3]++;
}
}
System.out.println(array[0] + " Adenine");
System.out.println(array[1] + " Cytosine");
System.out.println(array[2] + " Guanine");
System.out.println(array[3] + " Thymine");
}
// Initial prompt to the user with instructions
public static void prompt() {
System.out.println("This program will perform various computations on a genome sequence of FASTA format.\n");
System.out.println("Please follow these steps before continuing:\n");
System.out.println("1. Move the FASTA file you wish to process into the same folder as this program.\n");
System.out.println("2. Copy only the name of the FASTA file--be sure to exclude the .fasta file extension.\n");
System.out.println("3. Paste the name of the file you wish to process "
+ "then press enter (use CTRL + V to paste if right-click does not work): ");
}
}