- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequencePartition.java
More file actions
Latest commit
253 lines (233 loc) · 7.1 KB
/
Copy pathsequencePartition.java
File metadata and controls
253 lines (233 loc) · 7.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
importjava.io.*;
importjava.util.*;
importjava.util.concurrent.ThreadLocalRandom;
/*
Author-
Priyam Saikia
Neha Rani
Version History-
10/18/2019 - Neha Rani - Initial Version
Functionality
This program will partition the sequences in a
file into small fragments
Input
1. string: input file name
2. integer: x = minimum fragment length
3. integer: y = maximum fragment length (y > x)
4. string: output file name
Functions:
1.init
2.loadSequencesFromFile
3.writeToOutfile
4.validateInFile
5.validateOutFile
6.loadFilenames
7.isPosInt
*/
publicclasssequencePartition
{
//Gobal variables
Stringinfilename; // Input filename
Stringoutfilename; // Output filename
ArrayList<String> inputSequences;
ArrayList<String> outputSequences;
intmin;
intmax;
//Constructor
publicsequencePartition(){}
publicstaticvoidmain(Stringargs[]) throwsIOException {
intnumArgs = args.length;
if(numArgs!=4) {
//error check
System.out.println("Invalid number of arguments! Try Again.");
return;
}
// checks
if(!isPosInt(args[1]) || !isPosInt(args[2])){
System.out.println("Please enter positive integer the second and third arguments");
return;
}
if(!validateInFile(args[0])) {
System.out.println("Input file does not exist. Make sure you enter filename with extension.");
return;
}
if(!validateOutFile(args[3])) return;
Stringifile=args[0].trim();
intx=Integer.parseInt(args[1]);
inty=Integer.parseInt(args[2]);
if(x>y){
System.out.println("Value of X should be less than or equal to Y. Try again");
return;
}
// More processing for output filename
Stringofile=args[3].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
ofile+=".txt";
}
//System.out.println(ifile);
//System.out.println(x);
//System.out.println(y);
//System.out.println(ofile);
//call sequencePartition
sequencePartitionse = newsequencePartition();
se.init(ifile, x, y, ofile);
}
//initialises all the sequence and create subsequences according to the rule
publicvoidinit(Stringifile, intx, inty, Stringofile){
min=x;
max=y;
infilename=ifile;
outfilename=ofile;
outputSequences=newArrayList<>();
// Task 1: Load sequences from input file
if(!loadSequencesFromFile()) return;
if(inputSequences.size()==0){
System.out.println("Input file is empty or corrupt. No sequences loaded.");
return;
}
// Task 2: Cut into fragments
for(intl=0; l<inputSequences.size(); l++)
{
Stringseq=inputSequences.get(l);
intrandomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
intseqLen=seq.length();
//System.out.println(randomNum + "is random number");
StringsubSeq="";
for(inti=0;i<seqLen;i++)
{
if(subSeq.length()==randomNum)
{
//System.out.println(randomNum);
outputSequences.add(subSeq);
subSeq="";
randomNum = ThreadLocalRandom.current().nextInt(min, max + 1);
}
subSeq=subSeq+seq.charAt(i);
}
if (subSeq.length()>=min)
{
outputSequences.add(subSeq);
}
}
// Task 3: Write sequences to file
writeToOutfile();
System.out.println("Output written to file: " + outfilename);
}
// Load sequences from input file
publicbooleanloadSequencesFromFile(){
// TO DO: Add more validations for read lines?
inputSequences=newArrayList<>();
try(
BufferedReaderbr = newBufferedReader(newFileReader(infilename))) {
Stringline = br.readLine();
StringcurSeq="";
while (line != null) {
//if(line.matches("[ACGT]+")) inputSequences.add(line);
if(line.matches("[ACGT]+")) curSeq=curSeq+line;
elseif(line.matches("[>]+")){
if(!curSeq.isEmpty()) inputSequences.add(curSeq);
curSeq="";
}
line = br.readLine();
}
}
catch (IOExceptione){
e.printStackTrace();
System.out.println("IO Exception! Error reading sequences from input file!");
returnfalse;
}
returntrue;
}
// write final sequence to file
publicvoidwriteToOutfile(){
try{
PrintWriterwriter = newPrintWriter(outfilename);
for(inti=0;i<outputSequences.size();i++){
writer.println(">");
//writer.println(outputSequences.get(i));
StringcurSeq=outputSequences.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 positive integer
publicstaticbooleanisPosInt(Strings){
if(s.matches("\\d+") && Integer.parseInt(s)>0) returntrue;
elsereturnfalse;
}
// Validates input filename
publicstaticbooleanvalidateInFile(Strings){
ArrayList<String> filenames = loadFilenames();
if(filenames.size()==0 || !filenames.contains(s)) returnfalse;
returntrue;
}
// Validates output filename
publicstaticbooleanvalidateOutFile(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;
}
}