- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequenceGenerator.java
More file actions
Latest commit
294 lines (283 loc) · 9.17 KB
/
Copy pathsequenceGenerator.java
File metadata and controls
294 lines (283 loc) · 9.17 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
importjava.io.*;
importjava.util.*;
/*
Author-
Priyam Saikia
Neha Rani
Version History-
10/18/2019 - Priyam Saikia - Initial Version
Functionality-
Functions:
*/
publicclasssequenceGenerator{
// **********************************
// VARIABLES
// **********************************
// Parameters input from user
intseqLen; // Length of first sequence
intnumSeq; // Number of total sequences including first
doublefrac_a; // Fraction of nucleotide 'A' in the sequence
doublefrac_c; // Fraction of nucleotide 'C' in the sequence
doublefrac_g; // Fraction of nucleotide 'G' in the sequence
doublefrac_t; // Fraction of nucleotide 'T' in the sequence
doublemutationProbability; // Mutation probability of a any nucleotide. Range:[0-1]
Stringoutfile; // Filename to write output
// Other paramaters required during the run of the program
doublefracSum; // sum of all fractions. Doesn't have to be 1.
StringfirstSeq; // String to store the first sequence
ArrayList<String> kSeqs; // ArrayList to store all k sequences
char[] firstSeqChar; // First sequence as a character array for ease of processing
Stringseparator="----------------------------------------------------------------------"; // Just a separator
//Constructor
publicsequenceGenerator(){}
// Main function - Validation of arguments and init
publicstaticvoidmain(Stringargs[]) throwsIOException{
intnumArgs = args.length;
if(numArgs!=8) {
//error
showError("",0);
return;
}
// also check number and string
if(!isNumber(args[0])){
showError("sequence length",1);
return;
} elseif(!isNumber(args[1])){
showError("fraction for a",1);
return;
} elseif(!isNumber(args[2])){
showError("fraction for c",1);
return;
} elseif(!isNumber(args[3])){
showError("fraction for g",1);
return;
} elseif(!isNumber(args[4])){
showError("fraction for t",1);
return;
} elseif(!isNumber(args[5])){
showError("number of sequences",1);
return;
} elseif(!isDouble(args[6])){
showError("mutation probability",2);
return;
} elseif(!validateFile(args[7])){
// error already shown in validate file function
//showError("output filename",3);
return;
}
// initialize temp variables
intn=Integer.parseInt(args[0]);
inta=Integer.parseInt(args[1]);
intc=Integer.parseInt(args[2]);
intg=Integer.parseInt(args[3]);
intt=Integer.parseInt(args[4]);
intk=Integer.parseInt(args[5]);
doublep=Double.parseDouble(args[6]);
// More processing for output filename
Stringofile=args[7].trim();
String[] toks = ofile.split("\\.");
if(toks.length==1 || (toks.length>1 && !toks[toks.length-1].equals("txt"))){
ofile+=".txt";
} elseif (toks[toks.length-1].equals("txt") && toks[0].isEmpty()){
// empty file - no idea why I have a different check for this - maybe to yell at the user!!!!
ofile+=".txt";
}
//call sequenceGenerator
sequenceGeneratorsg = newsequenceGenerator();
sg.init(n, a, c, g, t, k, p, ofile);
}
// **********************************
// ACTUAL PROCESSING GOES HERE
// **********************************
// Initialize variables and manage process flow
publicvoidinit(intn, inta, intc, intg, intt, intk, doublep, Stringofile){
seqLen=n;
fracSum=a+c+g+t;
frac_a=a/fracSum;
frac_c=c/fracSum;
frac_g=g/fracSum;
frac_t=t/fracSum;
numSeq=k;
mutationProbability=p;
outfile=ofile;
// Task 1: Create first sequence of length n
getFirstSequence();
// Task 2: Create k-1 mutated sequences
createKminus1();
// Task 3: Write output to file
writeToOutfile();
System.out.println(separator);
System.out.println("Output written to file: " + outfile);
}
// Generate first sequence
publicvoidgetFirstSequence(){
firstSeq="";
firstSeqChar=newchar[seqLen];
for(inti=0;i<seqLen;i++){
Stringcurr=generateLetter();
firstSeq=firstSeq+curr;
firstSeqChar[i]=curr.charAt(0);;
}
//System.out.println(firstSeq);
}
// Generate each letter
publicStringgenerateLetter(){
doublenum=0.0;
while(num==0.0){num=Math.random();}
if(num>0.0 && num<=frac_a){
return"A";
} elseif(num>frac_a && num<=(frac_a+frac_c)){
return"C";
} elseif(num>(frac_a+frac_c) && num<=(frac_a+frac_c+frac_g)){
return"G";
} elseif(num>(frac_a+frac_c+frac_g) && num<=1){
return"T";
} else {
// shouldn't be here
// Error???
return"A";
}
}
// Generate k-1 mutated sequences
publicvoidcreateKminus1(){
kSeqs=newArrayList<>();
kSeqs.add(firstSeq);
for(inti=0;i<numSeq-1;i++){
StringcurSeq = "";
for(intj=0;j<seqLen;j++){
curSeq = curSeq + generateMutatedLetter(firstSeqChar[j]);
}
kSeqs.add(curSeq);
//System.out.println(curSeq);
}
}
// Decide if mutation required for a letter and process accordingly
publicStringgenerateMutatedLetter(charc){
Strings=Character.toString(c);
doublenum=0.0;
while(num==0.0){num=Math.random();}
if(num>0.0 && num<=mutationProbability){
//do mutation;
num=0.0;
while(num==0.0){num=Math.random();}
if(num>0.0 && num<=0.5){
// replace
StringnewS=s;
while(s.equals(newS)){newS=generateLetter();}
s=newS;
} else {
// delete
return"";
}
} else {
//no mutation
}
returns;
}
// write sequences to file
publicvoidwriteToOutfile(){
try{
/*
Path file = Paths.get(outfile);
Files.write(file, kSeqs, StandardCharsets.UTF_8);
*/
//System.out.println(outfile);
PrintWriterwriter = newPrintWriter(outfile);
for(inti=0;i<numSeq;i++){
writer.println(">");
//writer.println(kSeqs.get(i));
StringcurSeq=kSeqs.get(i);
intj=0;
while(j+80<curSeq.length()){
writer.println(curSeq.substring(j,j+80));
j=j+80;
}
if(j<curSeq.length()) writer.println(curSeq.substring(j));
}
writer.close();
}
catch (IOExceptione){
e.printStackTrace();
System.out.println("IO Exception! Error writing output to file!");
}
}
// **********************************
// PRE-PROCESSING GOES HERE
// **********************************
// checks if input is a integer and >0
publicstaticbooleanisNumber(Strings){
if(s.matches("\\d+") && Integer.parseInt(s)!=0) returntrue;
elsereturnfalse;
}
// checks if input is a double and >0.0 and <1.0
publicstaticbooleanisDouble(Strings) {
return (s.matches("\\d+(\\.\\d+)?") && Double.parseDouble(s)>0.0 && Double.parseDouble(s)<1.0); //match a number with decimal.
//return str.matches("-?\\d+(\\.\\d+)?"); //match a number with optional '-' and decimal.
}
// Validates filename
publicstaticbooleanvalidateFile(Strings){
String[] toks = s.trim().split("\\.");
if(toks.length>1 && !toks[toks.length-1].equals("txt")){
System.out.println("ERROR: Output file cannot be a "+toks[toks.length-1]+" file. Try again.");
returnfalse;
}
ArrayList<String> filenames = loadFilenames();
if(filenames.size()>0) {
if(filenames.contains(s) || filenames.contains(s+".txt")){
/*
try{
System.out.println("Output file already exists. Do you want to replace it? Y for yes; N for no");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String response;
while(true){
response = bufferedReader.readLine();
if(response.trim().toUpperCase().equals("Y")){
return true;
} else if(response.trim().toUpperCase().equals("N")){
System.out.println("Exiting... Try again.");
return false;
}
System.out.println("Invalid input! Try again.");
}
}
catch(IOException e){
e.printStackTrace();
System.out.println("IO exception in filename. Try again.");
return false;
}
*/
returntrue; // replacing now
}
} elseif(!s.matches("^\\d*[a-zA-Z][a-zA-Z\\d]*$")){
System.out.println("Output file must contain only letters and numbers. Please try again!");
returnfalse;
}
returntrue;
}
// Read all filenames in folder
publicstaticArrayList<String> loadFilenames(){
Filefolder = newFile(System.getProperty("user.dir"));
File[] listOfFiles = folder.listFiles();
ArrayList<String> filenames = newArrayList<>();
for (inti = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
filenames.add(listOfFiles[i].getName());
}
}
returnfilenames;
}
// Display error
publicstaticvoidshowError(Stringinput,intetype){
StringformatError="Format: java hw1-1 <sequence_length> <fraction_for_a> " +
"<fraction_for_c> <fraction_for_g> <fraction_for_t> " +
"<number_of_sequences> <mutation_probability(real_number)> <output_file_name>";
System.out.print("ERROR:");
if(etype==0) {
System.out.println("Invalid number of arguments! Try Again.");
System.out.println(formatError);
}
elseif(etype==1) System.out.println("The "+input+" has to be an integer number>0");
elseif(etype==2) System.out.println("The "+input+" has to be a real number>0.0 AND <1.0");
//else System.out.println("The "+input+" has to be a valid filename");
}
}