-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC_VCF前処理.cs
534 lines (516 loc) · 20.4 KB
/
C_VCF前処理.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
namespace SELDLA
{
class C_VCF前処理
{
static int cntSplitLines;
public C_VCF前処理()
{
initialize();
}
public void initialize()
{
}
public void cleanupVcf(string filename, int opt_dp, int opt_gq, double opt_nonzerorate, string opt_o)
{
StreamWriter writer = new StreamWriter(opt_o + "_clean.txt");
List<string> cleaned = GetRecordsFromFile(filename).AsParallel().AsOrdered().Select(x => cleanupRow(x, opt_dp, opt_gq, opt_nonzerorate)).ToList();
Console.WriteLine("input datas: " + cleaned.Count());
//cleaned = cleaned.Where(x => x != null).OrderBy(x => x.Split("\t")[0]).ThenBy(x => ParseInt(x.Split("\t")[1])).ToList(); //split時にsortしているから不要
cleaned = cleaned.Where(x => x != null).ToList();
Console.WriteLine("cleaned datas: " + cleaned.Count());
foreach (string str in cleaned)
{
writer.WriteLine(str);
}
writer.Close();
}
private static int ParseInt(string str)
{
int result;
int.TryParse(str, out result);
return result;
}
///<summary>こんな関数作らなくても、File.ReadLines(filename)で置き換えられるみたい。</summary>
static IEnumerable<string> GetRecordsFromFile(string filename)
{
using (var streamReader = new StreamReader(filename))
{
string str;
while ((str = streamReader.ReadLine()) != null)
{
yield return str;
}
}
}
public static string cleanupRow(string line, int opt_dp, int opt_gq, double opt_nonzerorate)
{
bool viewflag = true;
string result = "";
if (line.StartsWith("##"))
{
viewflag = false;
}
else if (line.StartsWith("#"))
{
result = line;
}
else
{
string[] arr1 = line.Split("\t");
result = arr1[0];
if (arr1[3].Length == 1 && arr1[4].Length == 1)
{
string[] descript = arr1[8].Split(":");
int pos_dp = 0;
int pos_gq = 0;
for (int i = 1; i < 7; i++)
{
result += "\t" + arr1[i];
}
result += "\t.\t" + arr1[8];
for (int i = 0; i < descript.Length; i++)
{
if (descript[i] == "DP")
{
pos_dp = i;
//Console.WriteLine("DP" + i);
}
else if (descript[i] == "GQ")
{
pos_gq = i;
//Console.WriteLine("GQ" + i);
}
}
int numofnonzero = 0;
for (int i = 9; i < arr1.Length; i++)
{
string[] value_pearson = arr1[i].Split(":");
if ((pos_dp == 0 || (value_pearson.Length > pos_dp && value_pearson[pos_dp] != "." && Int32.Parse(value_pearson[pos_dp]) >= opt_dp)) &&
(pos_gq == 0 || (value_pearson.Length > pos_gq && value_pearson[pos_gq] != "." && Int32.Parse(value_pearson[pos_gq]) >= opt_gq)))
{
string tempres = "";
if (value_pearson[0] == "0/0")
{
tempres = "0";
numofnonzero++;
}
else if (value_pearson[0] == "0/1" || value_pearson[0] == "0|1" || value_pearson[0] == "1|0")
{
tempres = "1";
numofnonzero++;
}
else if (value_pearson[0] == "1/1" || value_pearson[0] == "1|1")
{
tempres = "2";
numofnonzero++;
}
else
{
tempres = "-1";
}
result += "\t" + tempres;
}
else
{
result += "\t-1";
}
}
if (numofnonzero < (arr1.Length - 9) * opt_nonzerorate)
{
viewflag = false;
}
}
else
{
viewflag = false;
}
}
if (viewflag)
{
return result;
}
else
{
return null;
}
}
/// <summary>
/// 出力はファイルで家系ごとに出力している
/// </summary>
/// <param name="input_clean"></param>
/// <param name="opt_o"></param>
/// <param name="inputfamily"></param>
/// <param name="opt_p"></param>
/// <param name="opt_b"></param>
/// <param name="mode"></param>
/// <param name="needSort"></param>
public void F_家系ごとにVCFを分割(string input_clean, string opt_o, string inputfamily, double opt_p, double opt_b, string mode, bool needSort)
{
//家系情報を読み込む
StreamReader file = new StreamReader(inputfamily);
string line;
List<string[]> families = new List<string[]>();
while ((line = file.ReadLine()) != null)
{
string[] temp = line.Split("\t");
if ((mode == "crossbreed" || mode == "duploid") && temp.Length >= 3)
{
families.Add(temp);
}
else if ((mode == "haploid" || mode == "selfpollination") && temp.Length >= 2)
{
families.Add(temp);
}
}
file.Close();
Console.WriteLine("families: " + families.Count);
//分割開始
StreamReader vcffile = new StreamReader(input_clean);
string[] header = null;
int counter = 0;
while ((line = vcffile.ReadLine()) != null)
{
counter++;
if (counter == 1)
{
header = line.Split("\t"); break;
}
}
vcffile.Close();
int num_fam = 0;
foreach (string[] fam in families)
{
num_fam++;
Console.WriteLine("Separating data of family " + num_fam);
Dictionary<int, int> idord = new Dictionary<int, int>();
for (int j = 0; j < fam.Length; j++)
{
//vcfに親の情報が無い場合でも頑張って動かすために、その時はfamily.txtファイルに親の名前として-1を入力してあればOK
if ((mode == "crossbreed" || mode == "duploid") && (j == 0 || j == 1) && fam[j] == "-1")
{
idord.Add(j, -1);
}
else if ((mode == "haploid" || mode == "selfpollination") && j == 0 && fam[j] == "-1")
{
idord.Add(j, -1);
}
else //以下は普通に親もきちんとある場合
{
for (int i = 9; i < header.Length; i++)
{
if (fam[j] == header[i])
{
idord.Add(j, i);
}
}
if (!idord.ContainsKey(j))
{
throw new System.Exception("The corresponding ID of " + fam[j] + " does not exist in the VCF.");
}
}
}
StreamWriter writer = new StreamWriter(opt_o + "_split_" + num_fam + ".txt");
writer.Write("#chr\tpos");
if (mode == "crossbreed" || mode == "duploid")
{
for (int j = 2; j < fam.Length; j++)
{
writer.Write("\t" + header[idord[j]]);
}
}
else
{ //haploid
for (int j = 1; j < fam.Length; j++)
{
writer.Write("\t" + header[idord[j]]);
}
}
writer.WriteLine("");
List<bool> countdatas;
if (mode == "crossbreed")
{
countdatas = File.ReadLines(input_clean).Skip(1).AsParallel().AsOrdered().Select(x => x.Split("\t")).Select(f => splitVcfRowCrossHap(f, idord, opt_p, opt_b))
.Where(x => x != null).AsSequential().Select(x => { writer.WriteLine(x); return true; }).ToList();
}
else if (mode == "duploid")
{
countdatas = File.ReadLines(input_clean).Skip(1).AsParallel().AsOrdered().Select(x => x.Split("\t")).Select(f => splitVcfRowDup(f, idord, opt_p, opt_b))
.Where(x => x != null).AsSequential().Select(x => { writer.WriteLine(x); return true; }).ToList();
}
else if (mode == "haploid")
{
countdatas = File.ReadLines(input_clean).Skip(1).AsParallel().AsOrdered().Select(x => x.Split("\t")).Select(f => splitVcfRowHap(f, idord, opt_p, opt_b))
.Where(x => x != null).AsSequential().Select(x => { writer.WriteLine(x); return true; }).ToList();
}
else //if (mode == "selfpollination")
{
countdatas = File.ReadLines(input_clean).Skip(1).AsParallel().AsOrdered().Select(x => x.Split("\t")).Select(f => splitVcfRowSelfPoll(f, idord, opt_p, opt_b))
.Where(x => x != null).AsSequential().Select(x => { writer.WriteLine(x); return true; }).ToList();
}
writer.Close();
Console.WriteLine("family " + num_fam + " datas: " + countdatas.Count);
if (needSort)
{
Console.WriteLine("Sorting... This step needs memory larger than the input vcf. If you don't have enough memory, you have to sort input vcf using sort commnad of Linux.");
StreamWriter writer2 = new StreamWriter(opt_o + "_sorted" + "_split_" + num_fam + ".txt");
File.ReadLines(opt_o + "_split_" + num_fam + ".txt").Take(1).Select(x => { writer2.WriteLine(x); return true; }).ToList();
File.ReadLines(opt_o + "_split_" + num_fam + ".txt").Skip(1).AsParallel().AsOrdered().Select(x => x.Split("\t")).OrderBy(x => x[0]).ThenBy(x => Int32.Parse(x[1]))
.AsSequential().Select(x =>
{
writer2.Write(x[0]);
for(int i = 1; i < x.Length; i++)
{
writer2.Write("\t" + x[i]);
}
writer2.WriteLine();
return true;
}).ToList();
writer2.Close();
}
}
}
public static string splitVcfRowCrossHap(string[] vals, Dictionary<int, int> idord, double opt_p, double opt_b)
{
Interlocked.Increment(ref cntSplitLines);
if (cntSplitLines % 100000 == 0) { Console.WriteLine("splited " + cntSplitLines + " lines"); }
string result = "";
bool viewflag = true;
if ((idord[0]!=-1 && vals[idord[0]] != "1") || (idord[1]!=-1 && vals[idord[1]] != "-1")) { viewflag = false; }
if (viewflag)
{
int n0 = 0;
int n1 = 0;
int n2 = 0;
int n = 0;
for (int i = 2; i < idord.Count; i++)
{
n++;
if (vals[idord[i]] == "0")
{
n0++;
}
else if (vals[idord[i]] == "1")
{
n1++;
}
else if (vals[idord[i]] == "2")
{
n2++;
}
}
if ((n0 + n2) < n * opt_p || n0 < n * opt_b || n2 < n * opt_b)
{
viewflag = false;
}
else
{
result = vals[0] + "\t" + vals[1];
for (int i = 2; i < idord.Count; i++)
{
if (vals[idord[i]] == "0" || vals[idord[i]] == "-1")
{
result += "\t" + vals[idord[i]];
}
else if (vals[idord[i]] == "2")
{
result += "\t1";
}
else
{
result += "\t-1";
}
}
}
}
if (!viewflag)
{
result = null;
}
return result;
}
public static string splitVcfRowHap(string[] vals, Dictionary<int, int> idord, double opt_p, double opt_b)
{
Interlocked.Increment(ref cntSplitLines);
if (cntSplitLines % 100000 == 0) { Console.WriteLine("splited " + cntSplitLines + " lines"); }
string result = "";
bool viewflag = true;
if (idord[0]!=-1 && vals[idord[0]] != "1") { viewflag = false; }
if (viewflag)
{
int n0 = 0;
int n1 = 0;
int n2 = 0;
int n = 0;
for (int i = 1; i < idord.Count; i++)
{
n++;
if (vals[idord[i]] == "0")
{
n0++;
}
else if (vals[idord[i]] == "1")
{
n1++;
}
else if (vals[idord[i]] == "2")
{
n2++;
}
}
if ((n0 + n2) < n * opt_p || n0 < n * opt_b || n2 < n * opt_b)
{
viewflag = false;
}
else
{
result = vals[0] + "\t" + vals[1];
for (int i = 1; i < idord.Count; i++)
{
if (vals[idord[i]] == "0" || vals[idord[i]] == "-1")
{
result += "\t" + vals[idord[i]];
}
else if (vals[idord[i]] == "2")
{
result += "\t1";
}
else
{
result += "\t-1";
}
}
}
}
if (!viewflag)
{
result = null;
}
return result;
}
public static string splitVcfRowDup(string[] vals, Dictionary<int, int> idord, double opt_p, double opt_b)
{
Interlocked.Increment(ref cntSplitLines);
if (cntSplitLines % 100000 == 0) { Console.WriteLine("splited " + cntSplitLines + " lines"); }
string result = "";
bool viewflag = true;
if ((idord[0]!=-1 && vals[idord[0]] != "1") || (idord[1]!=-1 && !(vals[idord[1]] == "0" || vals[idord[1]] == "2"))) { viewflag = false; }
if (viewflag)
{
int n0 = 0;
int n1 = 0;
int n2 = 0;
int n = 0;
for (int i = 2; i < idord.Count; i++)
{
n++;
if (vals[idord[i]] == "0")
{
n0++;
}
else if (vals[idord[i]] == "1")
{
n1++;
}
else if (vals[idord[i]] == "2")
{
n2++;
}
}
if (n1 < n * opt_b
|| (vals[idord[1]] == "2" && (n2 < n * opt_b || (n1 + n2) < n * opt_p))
|| (vals[idord[1]] == "0" && (n0 < n * opt_b || (n1 + n0) < n * opt_p)))
{
viewflag = false;
}
else
{
result = vals[0] + "\t" + vals[1];
for (int i = 2; i < idord.Count; i++)
{
if (vals[idord[i]] == "1" || vals[idord[i]] == "-1")
{
result += "\t" + vals[idord[i]];
}
else if ((vals[idord[1]] == "2" && vals[idord[i]] == "2") || (vals[idord[1]] == "0" && vals[idord[i]] == "0"))
{
result += "\t0";
}
else
{
result += "\t-1";
}
}
}
}
if (!viewflag)
{
result = null;
}
return result;
}
public static string splitVcfRowSelfPoll(string[] vals, Dictionary<int, int> idord, double opt_p, double opt_b)
{
Interlocked.Increment(ref cntSplitLines);
if (cntSplitLines % 100000 == 0) { Console.WriteLine("splited " + cntSplitLines + " lines"); }
string result = "";
bool viewflag = true;
if (idord[0]!=-1 && vals[idord[0]] != "1") { viewflag = false; } //idord[0]==-1は親のVCFなしのはず
if (viewflag)
{
int n0 = 0;
int n1 = 0;
int n2 = 0;
int n = 0;
for (int i = 1; i < idord.Count; i++)
{
n++;
if (vals[idord[i]] == "0")
{
n0++;
}
else if (vals[idord[i]] == "1")
{
n1++;
}
else if (vals[idord[i]] == "2")
{
n2++;
}
}
if ((n0 + n1 + n2) < n * opt_p || (n0 + n2) < n * opt_b || n1 < n * opt_b)
{
viewflag = false;
}
else
{
result = vals[0] + "\t" + vals[1];
for (int i = 1; i < idord.Count; i++)
{
if (vals[idord[i]] == "0" || vals[idord[i]] == "-1")
{
result += "\t" + vals[idord[i]];
}
else if (vals[idord[i]] == "2")
{
result += "\t0";
}
else
{
result += "\t1";
}
}
}
}
if (!viewflag)
{
result = null;
}
return result;
}
}
}