-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
264 lines (227 loc) · 7.39 KB
/
Main.java
File metadata and controls
264 lines (227 loc) · 7.39 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
import java.util.*;
import java.util.concurrent.*;
import java.io.*;
import org.apache.commons.io.*;
class Vcf {
/**
This class handles one vcf sample: parsing, computing hamming distances,
saving distance matrix, compute and save collision probabilities.
*/
static class Handler implements Callable<float[]> {
private static final int totalUsers = 2504;
private final String name;
private final List<String> chunk;
private final Matrix m;
public Handler(String name, List<String> chunk){
this.name = name;
this.chunk = chunk;
this.m = new Matrix(totalUsers);
}
/**
Given a row of values, computes all pair-wise hamming distances and
updates the matrix. This is called for every line so it can increase
distances in the matrix by 1 at most.
*/
private void hamming(int[] values){
for(int i=0; i<values.length; i++){
for(int j=i+1; j<values.length; j++){
if (values[i] != values[j])
m.set(i, j, m.get(i,j)+1);
}
}
}
/**
Parses a vcf line and produces 2504 values in {0,1,2} representing
the three possible states of a SNP:
0) no mutation, 1) single mutation (on either side), 2) double mutation.
*/
private int[] parse(String s) throws Exception {
String[] fields = s.split("\t");
int[] values = new int[totalUsers];
for(int i=0; i<totalUsers; i++){
String f = fields[i+9];
if(f.equals("0|0")) {values[i] = 0;}
else if(f.equals("0|1")) {values[i] = 1;}
else if(f.equals("1|0")) {values[i] = 1;}
else if(f.equals("1|1")) {values[i] = 2;}
else {throw new Exception("field: "+f);}
}
return values;
}
public float[] call() throws Exception {
float[] probs = null;
for(String line : chunk)
hamming(parse(line));
m.save(name);
probs = m.pCollisionPerUser();
return probs;
}
}
/**
Filters away lines which are not SNP or that are multiallelic SNP.
*/
static class SNPIterator implements Iterator<String> {
private final Iterator<String> it;
private String line;
private boolean goodLine(String line){
return line.contains("VT=SNP") && !line.contains("MULTI_ALLELIC");
}
public SNPIterator(Iterator<String> it){
this.it = it;
this.line = null;
}
public boolean hasNext(){
if(line != null){return true;}
else {
while(it.hasNext()){
line = it.next();
if(goodLine(line)) return true;
}
return false;
}
}
public String next(){
if(hasNext()){
String res = line;
line = null;
return res;
} else {
throw new NoSuchElementException();
}
}
}
}
/**
This class contains the commands that are called from the main.
*/
class Commands {
/**
call as: hamminger chr22.vcf 100 3
produces: chr22_K100_N{1...n} and chr22_K100_prob
Parses the vcf file chr22.vcf and splits it in contiguous samples of size 100.
For each sample computes all the pair-wise hamming distances for all the 2504 users.
For each sample i, the distance matrix is saved to chr22_K100_N$i.
The probability of collision for each user is saved to chr22_K100_prob, one line per sample.
Last argument is the number of cores to use concurrently.
*/
public static void hamminger(String[] args) throws Exception {
final String srcFileName = args[0]; // chr22.vcf
final int chunkSize = Integer.parseInt(args[1]); // size of genome or k
final int nCores = Integer.parseInt(args[2]);
final Pool pool = new Pool(nCores, nCores * 2);
final BufferedReader br = new BufferedReader(new FileReader(srcFileName));
final Iterator<String> it = new Vcf.SNPIterator(new LineIterator(br));
final ChunkIterator<String> cit = new ChunkIterator<String>(chunkSize, it);
final List<Future<float[]>> result = new ArrayList<Future<float[]>>();
int chunkCnt = 0;
while (cit.hasNext()){
chunkCnt++;
String dstFileName = srcFileName.substring(0,srcFileName.length() -4)
+"_K"+chunkSize+"_N"+chunkCnt;
// submit blocks so that we don't parse more than we can handle
result.add(pool.submit(new Vcf.Handler(dstFileName, cit.next())));
}
br.close();
pool.shutdown();
Util.log("Finished reading input.");
String dstFileName = srcFileName.substring(0,srcFileName.length() -4)+"_K"+chunkSize+"_probs";
BufferedWriter bw = new BufferedWriter(new FileWriter(dstFileName));
for(Future<float[]> f : result){
float[] a = f.get(); // blocks
bw.write(Util.arrayToCsv(a)+"\n");
}
bw.close();
}
/**
merger old/chr22_K100_N1 10
merges the distances of old/chr22_K100_N{1..10} into old/chr22_K1000_N1
merges the distances of old/chr22_K100_N{11..20} into old/chr22_K1000_N2
and so on.
*/
public static void merger(String[] args) throws Exception {
boolean strict = true;
String tmp = args[0].substring(0,args[0].lastIndexOf("_"));
int chunkSize = Integer.parseInt(tmp.substring(tmp.lastIndexOf("_")+2));
String base = tmp.substring(0,tmp.lastIndexOf("_"));
final int nFiles = Integer.parseInt(args[1]);
String srcFile = base +"_K"+ chunkSize + "_N";
String dstFile = base +"_K"+ chunkSize*nFiles + "_N";
File f = null;
int cnt = 1;
Matrix m = new Matrix(srcFile+cnt);
cnt++;
String probFile = base +"_K"+ chunkSize*nFiles +"_probs";
BufferedWriter bw = new BufferedWriter(new FileWriter(probFile));
while((f = new File(srcFile+cnt)).exists()){
if (cnt % nFiles == 0){
String file = dstFile+(cnt/nFiles);
m.save(file);
bw.write(Util.arrayToCsv(m.pCollisionPerUser())+"\n");
m = new Matrix(srcFile+cnt);
} else {
m.sum(new Matrix(srcFile+cnt));
}
cnt++;
}
if(!strict){
String file = dstFile+(cnt/nFiles);
m.save(file);
bw.write(Util.arrayToCsv(m.pCollisionPerUser())+"\n");
}
bw.close();
}
/**
collisioner chr22_K100_probs
Takes a file where each line contains the collision probability of all users,
different lines are produced by different samples, all of the same size.
For each user (for each column), computes average over samples and saves it to
chr22_K100_probs_avg
Additionally prints some statistics over all probability of collision of all users:
[average, variance, minimum, 1st quartile, median, 3rd quartile, maximum]
*/
public static void collisioner(String[] args) throws Exception {
final String srcFileName = args[0];
float[][] data = Util.arrayOfCsv(srcFileName);
// average per column (each column is a user)
float[] usersAvg = new float[data[0].length];
for(int r=0; r<data.length; r++){
for(int c=0; c<data[0].length; c++){
usersAvg[c] += data[r][c];
}
}
for(int c=0; c<data[0].length; c++){
usersAvg[c] /= data.length;
}
String dstFileName = srcFileName + "_avg";
BufferedWriter bw = new BufferedWriter(new FileWriter(dstFileName));
bw.write(Util.arrayToCsv(usersAvg)+"\n");
bw.close();
List<Float> probs = new ArrayList<Float>();
for(int r=0; r<data.length; r++){
for(int c=0; c<data[0].length; c++){
probs.add(data[r][c]);
}
}
List<Float> stat = Util.stat(probs);
System.out.println(Arrays.toString(stat.toArray()));
}
}
/**
This is the entry point that simply chooses which command to execute.
*/
class Main {
public static void main(String[] args) throws Exception {
String[] newArgs = Arrays.copyOfRange(args,1,args.length);
switch(args[0]){
case "hamming" :
Commands.hamminger(newArgs);
break;
case "merge" :
Commands.merger(newArgs);
break;
case "collision" :
Commands.collisioner(newArgs);
break;
}
}
}