forked from skoren/ArrowGrid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.java
568 lines (510 loc) · 15.6 KB
/
Utils.java
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.PrintStream;
import java.util.regex.Pattern;
import java.lang.ProcessBuilder;
import java.io.FileInputStream;
import java.io.InputStream;
public class Utils {
public static final int MBYTES = 1048576;
public static final int FASTA_LINE_LENGTH = 60;
public static MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
public static final String[] FASTA_ENDS = {"trimmed", "contig", "qual", "fasta", "fas", "fna", "fa", "genome"};
public static final Pattern spaceSplit = Pattern.compile("\\s+");
public static final Pattern tabSplit = Pattern.compile("\\t+");
public static class FastaRecord {
public String header = null;
public String name = null;
public String sequence = null;
public String[] split = null;
public FastaRecord(String h, String s) {
split = h.trim().split("\\s+");
name = split[0];
header = h;
sequence = s;
}
}
public static class FastaReader {
private BufferedReader fileIn = null;
private String header = "";
public FastaReader(String file) throws Exception {
fileIn = Utils.getFile(file, FASTA_ENDS);
}
public FastaRecord readRecord() throws Exception {
String line = null;
StringBuilder fastaSeq = new StringBuilder();
FastaRecord result = null;
while ((line = fileIn.readLine()) != null) {
if (line.startsWith(">")) {
if (header.length() > 0) {
result = new FastaRecord(header, fastaSeq.toString());
header = line.substring(1);
break;
}
header = line.substring(1);
}
else {
fastaSeq.append(line);
}
}
if (result == null && fastaSeq.length() != 0) {
result = new FastaRecord(header, fastaSeq.toString());
}
return result;
}
public void close() throws Exception {
fileIn.close();
}
}
public static class Pair {
public int first;
public double second;
public boolean orient;
public String id;
public Pair(int first, double second) {
this.first = first;
this.second = second;
}
public Pair(int first, double second, boolean orient) {
this.first = first;
this.second = second;
this.orient = orient;
}
public Pair(int first, double second, boolean orient, String id) {
this.first = first;
this.second = second;
this.orient = orient;
this.id = id;
}
}
public enum Translate
{
A("T"),
C("G"),
G("C"),
T("A"),
N("N"),
R("?"),
Y("?"),
K("?"),
M("?"),
S("?"),
W("?"),
D("?"),
V("?"),
H("?"),
B("?");
private String other;
public String getCompliment()
{
if (this == Translate.R) {
double rand = Math.random();
if (rand < 0.5) {
other = "T";
}
else {
other = "C";
}
}
else if (this == Translate.Y) {
double rand = Math.random();
if (rand < 0.5) {
other = "G";
}
else {
other = "A";
}
}
else if (this == Translate.K) {
double rand = Math.random();
if (rand < 0.5) {
other = "A";
}
else {
other = "C";
}
}
else if (this == Translate.M) {
double rand = Math.random();
if (rand < 0.5) {
other = "G";
}
else {
other = "T";
}
}
else if (this == Translate.S) {
double rand = Math.random();
if (rand < 0.5) {
other = "C";
}
else {
other = "G";
}
}
else if (this == Translate.W) {
double rand = Math.random();
if (rand < 0.5) {
other = "T";
}
else {
other = "A";
}
}
else if (this == Translate.D) {
double rand = Math.random()*3;
if (rand < 1) {
other = "T";
}
else if (rand < 2) {
other = "C";
}
else {
other = "A";
}
}
else if (this == Translate.V) {
double rand = Math.random()*3;
if (rand < 1) {
other = "T";
}
else if (rand < 2) {
other = "G";
}
else {
other = "C";
}
}
else if (this == Translate.H) {
double rand = Math.random()*3;
if (rand < 1) {
other = "T";
}
else if (rand < 2) {
other = "G";
}
else {
other = "A";
}
}
else if (this == Translate.B) {
double rand = Math.random()*3;
if (rand < 1) {
other = "G";
}
else if (rand < 2) {
other = "C";
}
else {
other = "A";
}
}
return other;
}
Translate( String other )
{
this.other = other;
}
}
public enum ToProtein
{
GCT("A"),
GCC("A"),
GCA("A"),
GCG("A"),
TTA("L"),
TTG("L"),
CTT("L"),
CTC("L"),
CTA("L"),
CTG("L"),
CGT("R"),
CGC("R"),
CGA("R"),
CGG("R"),
AGA("R"),
AGG("R"),
AAA("K"),
AAG("K"),
AAT("N"),
AAC("N"),
ATG("M"),
GAT("D"),
GAC("D"),
TTT("F"),
TTC("F"),
TGT("C"),
TGC("C"),
CCT("P"),
CCC("P"),
CCA("P"),
CCG("P"),
CAA("Q"),
CAG("Q"),
TCT("S"),
TCC("S"),
TCA("S"),
TCG("S"),
AGT("S"),
AGC("S"),
GAA("E"),
GAG("E"),
ACT("T"),
ACC("T"),
ACA("T"),
ACG("T"),
GGT("G"),
GGC("G"),
GGA("G"),
GGG("G"),
TGG("W"),
CAT("H"),
CAC("H"),
TAT("Y"),
TAC("Y"),
ATT("I"),
ATC("I"),
ATA("I"),
GTT("V"),
GTC("V"),
GTA("V"),
GTG("V"),
TAG("X"),
TGA("X"),
TAA("X");
/*
Ala/A GCU, GCC, GCA, GCG
Leu/L UUA, UUG, CUU, CUC, CUA, CUG
Arg/R CGU, CGC, CGA, CGG, AGA, AGG
Lys/K AAA, AAG
Asn/N AAU, AAC
Met/M AUG
Asp/D GAU, GAC
Phe/F UUU, UUC
Cys/C UGU, UGC
Pro/P CCU, CCC, CCA, CCG
Gln/Q CAA, CAG
Ser/S UCU, UCC, UCA, UCG, AGU, AGC
Glu/E GAA, GAG
Thr/T ACU, ACC, ACA, ACG
Gly/G GGU, GGC, GGA, GGG
Trp/W UGG
His/H CAU, CAC
Tyr/Y UAU, UAC
Ile/I AUU, AUC, AUA
Val/V GUU, GUC, GUA, GUG
START AUG
STOP UAG, UGA, UAA
*/
private String other;
public String getProtein()
{
return other;
}
ToProtein( String other )
{
this.other = other;
}
}
public static BufferedReader getFile(String fileName, String postfix) throws Exception {
String[] arr = new String[1];
arr[0] = postfix;
return getFile(fileName, arr);
}
public static BufferedReader getFile(String fileName, String[] postfix) throws Exception {
BufferedReader bf = null;
bf = new BufferedReader(new InputStreamReader(getFileStream(fileName, postfix, false)));
bf.ready();
return bf;
}
public static BufferedReader getFile(String fileName, String[] postfix, boolean ignoreUknown) throws Exception {
BufferedReader bf = null;
bf = new BufferedReader(new InputStreamReader(getFileStream(fileName, postfix, ignoreUknown)));
bf.ready();
return bf;
}
public static InputStream getFileStream(String fileName, String[] postfix) throws Exception {
return getFileStream(fileName, postfix, false);
}
public static InputStream getFileStream(String fileName, String[] postfix, boolean ignoreUnknown) throws Exception {
InputStream is = null;
if (fileName.endsWith("bz2")) {
// open file as a pipe
System.err.println("Running command " + "bzip2 -dc " + new File(fileName).getAbsolutePath() + " |");
Process p = Runtime.getRuntime().exec("bzip2 -dc " + new File(fileName).getAbsolutePath() + " |");
is = p.getInputStream();
} else if (fileName.endsWith("gz")) {
// open file as a pipe
System.err.println("Runnning comand " + "gzip -dc " + new File(fileName).getAbsolutePath() + " |");
Process p = Runtime.getRuntime().exec("gzip -dc " + new File(fileName).getAbsolutePath() + " |");
is = p.getInputStream();
} else if (fileName.endsWith("zip")) {
System.err.println("Runnning comand " + "unzip -p " + new File(fileName).getAbsolutePath() + " |");
Process p = Runtime.getRuntime().exec("unzip -p " + new File(fileName).getAbsolutePath() + " |");
is = p.getInputStream();
} else {
for (String s : postfix) {
if (fileName.endsWith(s)){
is = new FileInputStream(fileName);
return is;
}
}
if (ignoreUnknown == true) {
is = new FileInputStream(fileName);
return is;
}
System.err.println("Unknown file format " + fileName + " Skipping!");
}
return is;
}
// add new line breaks every FASTA_LINE_LENGTH characters
public static String convertToFasta(String supplied) {
StringBuffer converted = new StringBuffer();
int i = 0;
String[] split = supplied.trim().split("\\s+");
if (split.length > 1) { //process as a qual
int size = 0;
for (i = 0; i < split.length; i++) {
converted.append(split[i]);
size+= split[i].length();
if (i != (split.length - 1)) {
if (size >= FASTA_LINE_LENGTH) {
size = 0;
converted.append("\n");
} else {
converted.append(" ");
}
}
}
} else {
for (i = 0; (i+FASTA_LINE_LENGTH) < supplied.length(); i+= FASTA_LINE_LENGTH) {
converted.append(supplied.substring(i, i+FASTA_LINE_LENGTH));
converted.append("\n");
}
converted.append(supplied.substring(i, supplied.length()));
}
return converted.toString();
}
public static String rc(String supplied) {
StringBuilder st = new StringBuilder();
for (int i = supplied.length() - 1; i >= 0; i--) {
char theChar = supplied.charAt(i);
if (theChar != '-') {
Translate t = Translate.valueOf(Character.toString(theChar).toUpperCase());
st.append(t.getCompliment());
} else {
st.append("-");
}
}
return st.toString();
}
public static void outputFasta(PrintStream out, String fastaSeq, String ID) {
outputFasta(out, fastaSeq, null, ID, ">", null, true);
}
public static void outputFasta(String fastaSeq, String qualSeq, String ID, String fastaSeparator, String qualSeparator, boolean convert) {
outputFasta(System.out, fastaSeq, qualSeq, ID, fastaSeparator, qualSeparator, convert);
}
public static void outputFasta(PrintStream out, String fastaSeq, String qualSeq, String ID, String fastaSeparator, String qualSeparator, boolean convert) {
if (fastaSeq.length() == 0) {
return;
}
if (qualSeq != null && qualSeq.length() != fastaSeq.length()) {
System.err.println("Error length of sequences and fasta for id " + ID + " aren't equal fasta: " + fastaSeq.length() + " qual: " + qualSeq.length());
System.exit(1);
}
out.println(fastaSeparator + ID);
out.println((convert == true ? Utils.convertToFasta(fastaSeq) : fastaSeq));
if (qualSeq != null) {
out.println(qualSeparator + ID);
out.println((convert == true ? Utils.convertToFasta(qualSeq) : qualSeq));
}
}
public static String getUngappedRead(String fasta) {
fasta = fasta.replaceAll("N", "");
fasta = fasta.replaceAll("-", "");
assert(fasta.length() >= 0);
return fasta;
}
public static int countLetterInRead(String fasta, String letter) {
return countLetterInRead(fasta, letter, false);
}
public static int countLetterInRead(String fasta, String letter, boolean caseSensitive) {
String ungapped = Utils.getUngappedRead(fasta);
int len = ungapped.length();
if (len == 0) { return -1; }
int increment = letter.length();
int count = 0;
for (int i = 0; i <= ungapped.length()-increment; i+= increment) {
if (letter.equals(ungapped.substring(i, i+increment)) && caseSensitive) {
count++;
}
if (letter.equalsIgnoreCase(ungapped.substring(i, i+increment)) && !caseSensitive) {
count++;
}
}
return count;
}
public static String toProtein(String genome, boolean isReversed, int frame) {
StringBuilder result = new StringBuilder();
if (isReversed) {
genome = rc(genome);
}
genome = genome.replaceAll("-", "");
for (int i = frame; i < (genome.length() - 3); i += 3) {
String codon = genome.substring(i, i+3);
String protein = ToProtein.valueOf(codon).getProtein();
result.append(protein);
}
return result.toString();
}
public static int checkForEnd(String line, int brackets) {
if (line.startsWith("{")) {
brackets++;
}
if (line.startsWith("}")) {
brackets--;
}
if (brackets == 0) {
return -1;
}
return brackets;
}
public static String getID(String line) {
String ids[] = line.split(":");
int commaPos = ids[1].indexOf(",");
if (commaPos != -1)
return ids[1].substring(1, commaPos).trim();
else
return ids[1].trim();
}
public static String getValue(String line, String key) {
if (line.startsWith(key)) {
return line.split(":")[1];
}
return null;
}
public static int getOvlSize(int readA, int readB, int ahang, int bhang) {
if ((ahang <= 0 && bhang >= 0) || (ahang >= 0 && bhang <= 0)) {
return -1;
}
if (ahang < 0) {
return readA - Math.abs(bhang);
}
else {
return readA - ahang;
}
}
public static int getRangeOverlap(int startA, int endA, int startB, int endB) {
int minA = Math.min(startA, endA);
int minB = Math.min(startB, endB);
int maxA = Math.max(startA, endA);
int maxB = Math.max(startB, endB);
int start = Math.max(minA, minB);
int end = Math.min(maxA, maxB);
return (end-start+1);
}
}