-
Notifications
You must be signed in to change notification settings - Fork 5
/
Example.java
249 lines (224 loc) · 8.07 KB
/
Example.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
package parasail;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.String;
import java.lang.StringBuilder;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.List;
import java.util.Scanner;
class Example {
public static int open;
public static int gap;
public static Matrix matrix;
public static boolean do_cigar;
private static class AlignTask implements Runnable {
private Sequence s1;
private Sequence s2;
private Result result;
public AlignTask(Sequence s1, Sequence s2) {
this.s1 = s1;
this.s2 = s2;
}
@Override
public void run() {
result = Aligner.sw(s1.getData(), s2.getData(), Example.open, Example.gap, Example.matrix);
System.out.println(toString() + " = " + result.getScore());
result.delete();
}
public String toString() {
String n1 = s1.getName();
String n2 = s2.getName();
StringBuilder ret = new StringBuilder();
if (n1.length() > 33) {
ret.append(n1.substring(0,15));
ret.append("...");
ret.append(n1.substring(n1.length()-15));
}
else {
ret.append(n1);
}
ret.append(" <-> ");
if (n2.length() > 33) {
ret.append(n2.substring(0,15));
ret.append("...");
ret.append(n2.substring(n2.length()-15));
}
else {
ret.append(n2);
}
return ret.toString();
}
}
private static class CigarTask implements Runnable {
private Sequence s1;
private Sequence s2;
private Result result;
public CigarTask(Sequence s1, Sequence s2) {
this.s1 = s1;
this.s2 = s2;
}
@Override
public void run() {
result = Aligner.sw_trace(s1.getData(), s2.getData(), Example.open, Example.gap, Example.matrix);
System.out.println(toString() + " = " + result.getCigar(s1.getData(), s2.getData(), Example.matrix).decode());
result.delete();
}
public String toString() {
String n1 = s1.getName();
String n2 = s2.getName();
StringBuilder ret = new StringBuilder();
if (n1.length() > 33) {
ret.append(n1.substring(0,15));
ret.append("...");
ret.append(n1.substring(n1.length()-15));
}
else {
ret.append(n1);
}
ret.append(" <-> ");
if (n2.length() > 33) {
ret.append(n2.substring(0,15));
ret.append("...");
ret.append(n2.substring(n2.length()-15));
}
else {
ret.append(n2);
}
return ret.toString();
}
}
private static class Sequence {
private String name;
private String data;
public Sequence(String name, String data) {
this.name = name;
this.data = data;
}
public String getName() {
return name;
}
public String getData() {
return data;
}
}
public static ArrayList<Sequence> parse_fasta(String filename) {
ArrayList<Sequence> sequences = new ArrayList<Sequence>();
boolean first = true;
String name = new String();
String last_name = new String();
String current = new String();
try (Scanner sc = new Scanner(new File(filename))) {
while (sc.hasNextLine()) {
String line = sc.nextLine().trim();
if (line.charAt(0) == '>') {
last_name = name;
name = line;
if (first) {
first = false;
}
else {
sequences.add(new Sequence(last_name,current));
current = "";
}
} else {
current += line;
}
}
sequences.add(new Sequence(name,current));
}
catch (FileNotFoundException e){
e.printStackTrace();
}
return sequences;
}
public static void print_help() {
System.out.println("parasail.Example [options] filename.fasta");
System.out.println("Options:");
System.out.println(" -t number of threads");
System.out.println(" -o gap open penalty");
System.out.println(" -e gap extension penalty");
System.out.println(" -m matrix name");
System.out.println(" -c (no arg), print cigar");
System.out.println(" -h this help");
}
public static void main(String[] args) {
Example.open = 10;
Example.gap = 1;
Example.matrix = Matrix.blosum62;
ArrayList<Sequence> sequences = null;
int threads = 1;
String filename = null;
for (int i = 0; i < args.length; i++) {
switch (args[i].charAt(0)) {
case '-':
if (args[i].charAt(1) == 'h') {
print_help();
System.exit(0);
}
else if (args[i].charAt(1) == 'c') {
Example.do_cigar = true;
}
else {
if (args.length-1 == i) {
throw new IllegalArgumentException("Expected arg after: "+args[i]);
}
if (args[i].length() != 2) {
throw new IllegalArgumentException("Not a valid argument: "+args[i]);
}
if (args[i].charAt(1) == 't') {
threads = Integer.parseInt(args[i+1]);
}
else if (args[i].charAt(1) == 'o') {
Example.open = Integer.parseInt(args[i+1]);
}
else if (args[i].charAt(1) == 'e') {
Example.gap = Integer.parseInt(args[i+1]);
}
else if (args[i].charAt(1) == 'm') {
Example.matrix = Matrix.lookup(args[i+1]);
}
else if (args[i].charAt(1) == 'm') {
Example.matrix = Matrix.lookup(args[i+1]);
}
else {
throw new IllegalArgumentException("Not a valid argument: "+args[i]);
}
i++;
}
break;
default:
filename = args[i];
break;
}
}
if (null == filename) {
print_help();
System.exit(0);
}
System.out.println(" filename: " + filename);
System.out.println(" threads: " + threads);
System.out.println(" gap open: " + Example.open);
System.out.println("gap extend: " + Example.gap);
System.out.println(" matrix: " + Example.matrix.getName());
sequences = parse_fasta(filename);
ExecutorService executor = Executors.newFixedThreadPool(threads);
for (int i = 0; i < sequences.size(); i++) {
for (int j = i+1; j < sequences.size(); j++) {
Runnable worker = null;
if (Example.do_cigar) {
worker = new CigarTask(sequences.get(i), sequences.get(j));
}
else {
worker = new AlignTask(sequences.get(i), sequences.get(j));
}
executor.execute(worker);
}
}
executor.shutdown();
while (!executor.isTerminated()) {
}
System.out.println("Finished all threads");
}
}