-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathExporter.cs
575 lines (509 loc) · 20.7 KB
/
Exporter.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
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
569
570
571
572
573
574
575
using Genometric.GeUtilities.Intervals.Model;
using Genometric.GeUtilities.Intervals.Parsers;
using Genometric.GeUtilities.Intervals.Parsers.Model;
using Genometric.MSPC.CLI.Exporter;
using Genometric.MSPC.CLI.Tests.MockTypes;
using Genometric.MSPC.Core;
using Genometric.MSPC.Core.Model;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Xunit;
namespace Genometric.MSPC.CLI.Tests
{
public class Exporter
{
private readonly string _chr = "chr1";
private readonly char _strand = '.';
private readonly List<Attributes> _attributes;
readonly List<Peak> peaks = new()
{
new Peak(10, 11, 1E-8),
new Peak(100, 110, 1E-8),
new Peak(1000, 1100, 1E-8),
new Peak(10000, 11000, 1E-8),
new Peak(100000, 110000, 1E-8),
new Peak(120000, 130000, 1E-8),
new Peak(150000, 160000, 1E-8),
new Peak(1000000, 1100000, 1E-8),
new Peak(1200000, 1300000, 1E-8),
new Peak(110000000, 120000000, 1E-8),
new Peak(130000000, 140000000, 1E-8),
new Peak(910000000, 920000000, 1E-8),
new Peak(930000000, 940000000, 1E-8)
};
readonly List<string> chrs = new()
{
"chr1", "chr3", "chr5", "chr8", "chr9",
"chr9", "chr9", "chr18", "chr18", "chrX",
"chrX", "chrY", "chrY"
};
private static string Header(bool mspcFormat)
{
if (mspcFormat)
{
return string.Join("\t", new string[]
{
"chr",
"start",
"stop",
"name",
"-1xlog10(p-value)",
"strand",
"xSqrd",
"-1xlog10(Right-Tail Probability)",
"-1xlog10(AdjustedP-value)"
});
}
else
{
return string.Join("\t", new string[]
{
"chr",
"start",
"stop",
"name",
"-1xlog10(p-value)",
"strand"
});
}
}
private string RunMSPCAndExportResultsWithMultiChr()
{
var s0 = new Bed<Peak>();
for (int i = 0; i < peaks.Count; i++)
s0.Add(peaks[i], chrs[i], _strand);
var s1 = new Bed<Peak>();
s1.Add(new Peak(800, 900, 1E-2), "chr5", _strand);
var mspc = new Mspc();
mspc.AddSample(0, s0);
mspc.AddSample(1, s1);
mspc.Run(new Config(
ReplicateType.Biological,
1e-4, 1e-5, 1e-5, 1, 0.05F,
MultipleIntersections.UseLowestPValue));
var path = Path.Join(
Environment.CurrentDirectory,
"MSPCTests_" + new Random().NextDouble().ToString());
new Exporter<Peak>().Export(
_sidfm,
mspc.GetResults(),
mspc.GetConsensusPeaks(),
new Options(path, false, _attributes));
return path;
}
// Sample ID Filename Mapping
private readonly Dictionary<uint, string> _sidfm;
public Exporter()
{
_attributes = new List<Attributes>();
foreach (var att in Enum.GetValues(
typeof(Attributes)).Cast<Attributes>())
_attributes.Add(att);
_sidfm = new Dictionary<uint, string>
{
{ 0, "sA" },
{ 1, "sB" },
{ 2, "sC" }
};
}
private Mspc InitializeMSPC()
{
/// r11 r12
/// Sample 0: --░░░░░░░░░░░-------████████████----------------------------
/// r21 r22 r23
/// Sample 1: ---------████████████████----▒▒▒▒▒▒▒▒---▒▒▒▒▒▒▒▒------------
/// r31 r32 r33
/// Sample 2: ▒▒▒▒---██████████---------------------------------████████--
///
/// Legend: [░░ Background peak], [▒▒ Weak peak], [██ Stringent peak]
var r11 = new Peak(left: 3, right: 13, value: 1e-2, summit: 10, name: "r11");
var r12 = new Peak(left: 21, right: 32, value: 1e-12, summit: 25, name: "r12");
var r21 = new Peak(left: 10, right: 25, value: 1e-8, summit: 20, name: "r21");
var r22 = new Peak(left: 30, right: 37, value: 1e-5, summit: 35, name: "r22");
var r23 = new Peak(left: 41, right: 48, value: 1e-6, summit: 45, name: "r23");
var r31 = new Peak(left: 0, right: 4, value: 1e-6, summit: 2, name: "r31");
var r32 = new Peak(left: 8, right: 17, value: 1e-12, summit: 12, name: "r32");
var r33 = new Peak(left: 51, right: 58, value: 1e-18, summit: 55, name: "r33");
var sA = new Bed<Peak>();
sA.Add(r11, _chr, _strand);
sA.Add(r12, _chr, _strand);
var sB = new Bed<Peak>();
sB.Add(r21, _chr, _strand);
sB.Add(r22, _chr, _strand);
sB.Add(r23, _chr, _strand);
var sC = new Bed<Peak>();
sC.Add(r31, _chr, _strand);
sC.Add(r32, _chr, _strand);
sC.Add(r33, _chr, _strand);
var mspc = new Mspc();
mspc.AddSample(0, sA);
mspc.AddSample(1, sB);
mspc.AddSample(2, sC);
return mspc;
}
private string RunMSPCAndExportResults(
bool includeHeader = false)
{
var mspc = InitializeMSPC();
mspc.Run(new Config(
ReplicateType.Biological,
1e-4, 1e-8, 1e-4, 2, 0.05F,
MultipleIntersections.UseLowestPValue));
var path = Path.Join(
Environment.CurrentDirectory,
"MSPCTests_" + new Random().NextDouble().ToString());
var exporter = new Exporter<Peak>();
var options = new Options(path, includeHeader, _attributes);
exporter.Export(
_sidfm,
mspc.GetResults(),
mspc.GetConsensusPeaks(),
options);
return path;
}
[Fact]
public void CreateOneAndOnlyOneFilePerAttributeInMSPCPeaksFormat()
{
// Arrange & Act
string path = RunMSPCAndExportResults();
foreach (var sampleFolder in Directory.GetDirectories(path))
{
var files = new List<string>();
foreach (var file in Directory.GetFiles(sampleFolder))
files.Add(Path.GetFileName(file));
// Assert
foreach (var att in _attributes)
{
Assert.Contains(
files,
(string p) => { return p.Equals(att + "_mspc_peaks.txt"); });
Assert.True(files.Count(
x => x.Equals(att + "_mspc_peaks.txt")) == 1);
}
}
// Clean up
Directory.Delete(path, true);
}
[Theory]
[InlineData(true, 0, Attributes.Background, 1)]
[InlineData(true, 1, Attributes.Background, 0)]
[InlineData(true, 2, Attributes.Background, 0)]
[InlineData(true, 0, Attributes.Confirmed, 1)]
[InlineData(true, 1, Attributes.Confirmed, 2)]
[InlineData(true, 2, Attributes.Confirmed, 1)]
[InlineData(true, 0, Attributes.Discarded, 0)]
[InlineData(true, 1, Attributes.Discarded, 1)]
[InlineData(true, 2, Attributes.Discarded, 2)]
[InlineData(true, 0, Attributes.FalsePositive, 0)]
[InlineData(true, 1, Attributes.FalsePositive, 0)]
[InlineData(true, 2, Attributes.FalsePositive, 0)]
[InlineData(true, 0, Attributes.Stringent, 1)]
[InlineData(true, 1, Attributes.Stringent, 1)]
[InlineData(true, 2, Attributes.Stringent, 2)]
[InlineData(true, 0, Attributes.TruePositive, 1)]
[InlineData(true, 1, Attributes.TruePositive, 2)]
[InlineData(true, 2, Attributes.TruePositive, 1)]
[InlineData(true, 0, Attributes.Weak, 0)]
[InlineData(true, 1, Attributes.Weak, 2)]
[InlineData(true, 2, Attributes.Weak, 1)]
[InlineData(false, 0, Attributes.Background, 1)]
[InlineData(false, 1, Attributes.Background, 0)]
[InlineData(false, 2, Attributes.Background, 0)]
[InlineData(false, 0, Attributes.Confirmed, 1)]
[InlineData(false, 1, Attributes.Confirmed, 2)]
[InlineData(false, 2, Attributes.Confirmed, 1)]
[InlineData(false, 0, Attributes.Discarded, 0)]
[InlineData(false, 1, Attributes.Discarded, 1)]
[InlineData(false, 2, Attributes.Discarded, 2)]
[InlineData(false, 0, Attributes.FalsePositive, 0)]
[InlineData(false, 1, Attributes.FalsePositive, 0)]
[InlineData(false, 2, Attributes.FalsePositive, 0)]
[InlineData(false, 0, Attributes.Stringent, 1)]
[InlineData(false, 1, Attributes.Stringent, 1)]
[InlineData(false, 2, Attributes.Stringent, 2)]
[InlineData(false, 0, Attributes.TruePositive, 1)]
[InlineData(false, 1, Attributes.TruePositive, 2)]
[InlineData(false, 2, Attributes.TruePositive, 1)]
[InlineData(false, 0, Attributes.Weak, 0)]
[InlineData(false, 1, Attributes.Weak, 2)]
[InlineData(false, 2, Attributes.Weak, 1)]
public void CountNumberOfExportedPeaksInEachSet(
bool mspcFormat, uint sampleID, Attributes attribute, int count)
{
// Arrange & Act
string path = RunMSPCAndExportResults();
string filename =
mspcFormat ?
attribute.ToString() + "_mspc_peaks" :
attribute.ToString();
var sampleFolder =
Array.Find(
Directory.GetDirectories(path),
(string f) =>
{
return f.Contains(_sidfm[sampleID]);
});
var file =
Array.Find(
Directory.GetFiles(sampleFolder),
(string f) =>
{
return Path.GetFileNameWithoutExtension(f).Equals(filename);
});
var bedParser = new BedParser
{
PValueFormat = PValueFormats.minus1_Log10_pValue
};
var parsedSample = bedParser.Parse(file);
// Assert
if (count == 0)
Assert.False(parsedSample.Chromosomes.ContainsKey(_chr));
else
Assert.True(
parsedSample.Chromosomes[_chr].Strands[_strand]
.Intervals.Count == count);
// Clean up
Directory.Delete(path, true);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void WriteHeaderToAllExportedData(bool write)
{
// Arrange & Act & Assert
string path = RunMSPCAndExportResults(write);
foreach (var sampleFolder in Directory.GetDirectories(path))
foreach (var file in Directory.GetFiles(sampleFolder))
using (var reader = new StreamReader(file))
if (Path.GetFileNameWithoutExtension(file).Contains("_mspc_peaks"))
Assert.True(Header(true).Equals(reader.ReadLine()) == write);
else
Assert.True(Header(false).Equals(reader.ReadLine()) == write);
// Clean up
Directory.Delete(path, true);
}
[Theory]
[InlineData(true, true)]
[InlineData(true, false)]
[InlineData(false, true)]
[InlineData(false, false)]
public void WriteHeaderToConsensusPeaksFile(bool mspcFormat, bool write)
{
// Arrange & Act
string line;
string expectedHeader = Header(mspcFormat);
string path = RunMSPCAndExportResults(write);
string filename =
mspcFormat ?
"ConsensusPeaks_mspc_peaks.txt" :
"ConsensusPeaks.bed";
using (var reader = new StreamReader(Path.Join(path, filename)))
line = reader.ReadLine();
// Assert
Assert.True(line.Equals(expectedHeader) == write);
// Clean up
Directory.Delete(path, true);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void AddHeaderBasedOnGivenArg(bool include)
{
// Arrange
static string GetFirstLine(string filename)
{
var reader = new StreamReader(filename);
var line = reader.ReadLine();
reader.Close();
return line;
}
// Act
string path = RunMSPCAndExportResults(include);
foreach (var dir in Directory.GetDirectories(path))
{
foreach (var file in Directory.GetFiles(dir))
{
string potentialHeader;
if (Path.GetFileNameWithoutExtension(file).EndsWith("_mspc_peaks"))
potentialHeader = Header(true);
else if (Path.GetExtension(file) == ".bed")
potentialHeader = Header(false);
else
continue;
// Assert
Assert.True(include == (potentialHeader == GetFirstLine(file)));
}
}
// Clean up
Directory.Delete(path, true);
}
[Theory]
[InlineData(0, Attributes.Background, "chr1", 3, 13, "r11", 2, '.', double.NaN, double.NaN, double.NaN)]
public void CorrectValuesForEachPropertyOfExportedPeakInMSPCPeakFile(
uint sampleID, Attributes attribute,
string chr, int left, int right, string name,
double value, char strand, double xSqrd,
double rtp, double adjustedPValue)
{
// Arrange
string path = RunMSPCAndExportResults();
string expectedLine =
string.Join("\t", new string[]
{
chr,
left.ToString(),
right.ToString(),
name,
value.ToString(),
strand.ToString(),
xSqrd.ToString(),
rtp.ToString(),
adjustedPValue.ToString()
});
string readLine = "";
var sampleFolder =
Array.Find(
Directory.GetDirectories(path),
(string f) =>
{
return f.Contains(_sidfm[sampleID]);
});
var file =
Array.Find(
Directory.GetFiles(sampleFolder),
(string f) =>
{
return Path.GetFileNameWithoutExtension(f)
.Equals(attribute.ToString() + "_mspc_peaks");
});
// Act
using (var reader = new StreamReader(file))
readLine = reader.ReadLine();
// Assert
Assert.Equal(expectedLine, readLine);
// Clean up
Directory.Delete(path, true);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void OutputIsSortedInMSPCPeaksFormat(bool mspcFormat)
{
// Arrange & Act
var path = RunMSPCAndExportResultsWithMultiChr();
string name =
mspcFormat ?
Attributes.Confirmed.ToString() + "_mspc_peaks" :
Attributes.Confirmed.ToString();
// In the exported session folder, first finds the folder that contains
// analysis results for a given sample, then within that folder
// finds the file that contains data belonging to a given attributes.
var filename = Array.Find(
Directory.GetFiles(
Array.Find(
Directory.GetDirectories(path),
(string f) =>
{
return f.Contains(_sidfm[0]);
})),
(string f) =>
{
return Path.GetFileNameWithoutExtension(f).Equals(name);
});
// Assert
int lineCounter = 0;
using (var reader = new StreamReader(filename))
{
for (int i = 0; i < peaks.Count; i++)
{
var line = reader.ReadLine().Split('\t');
Assert.True(
chrs[lineCounter] == line[0] &&
peaks[lineCounter].Left.ToString() == line[1] &&
peaks[lineCounter].Right.ToString() == line[2]);
lineCounter++;
}
}
// Clean up
Directory.Delete(path, true);
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ConsensusPeaksAreSorted(bool mspcFormat)
{
// Arrange & Act
var path = RunMSPCAndExportResultsWithMultiChr();
string filename =
mspcFormat ?
"ConsensusPeaks_mspc_peaks.txt" :
"ConsensusPeaks.bed";
// Assert
int lineCounter = 0;
using (var reader = new StreamReader(Path.Join(path, filename)))
{
for (int i = 0; i < peaks.Count; i++)
{
var line = reader.ReadLine().Split('\t');
Assert.True(
chrs[lineCounter] == line[0] &&
peaks[lineCounter].Left.ToString() == line[1] &&
peaks[lineCounter].Right.ToString() == line[2]);
lineCounter++;
}
}
// Clean up
Directory.Delete(path, true);
}
// This test causes (maybe) deadlock on Github actions
// (Linux and Window environment, runs as expected on mac env).
//[Fact]
#pragma warning disable xUnit1013 // Public method should be marked as test
public void InfForPeaksWithVeryLowPValue()
#pragma warning restore xUnit1013 // Public method should be marked as test
{
// Arrange
string rep1Path = Path.Join(Environment.CurrentDirectory, Main.GetFilename("rep1"));
string rep2Path = Path.Join(Environment.CurrentDirectory, Main.GetFilename("rep2"));
using (var w = new StreamWriter(rep1Path))
w.WriteLine("chr1\t10\t20\tA\t326");
using (var w = new StreamWriter(rep2Path))
w.WriteLine("chr1\t15\t25\tB\t326");
// Act
string outputPath;
var console = new MockConsole();
using (var o = new Orchestrator(console))
{
o.Invoke($"-i {rep1Path} -i {rep2Path} -r bio -w 1e-2 -s 1e-4".Split(' '));
outputPath = o.Config.OutputPath;
}
string rep1Confirmed;
using (var reader = new StreamReader(Path.Join(
outputPath,
Path.GetFileNameWithoutExtension(rep1Path),
Attributes.Confirmed + ".bed")))
{
reader.ReadLine();
rep1Confirmed = reader.ReadLine();
}
string rep2Confirmed;
using (var reader = new StreamReader(Path.Join(
outputPath,
Path.GetFileNameWithoutExtension(rep2Path),
Attributes.Confirmed + ".bed")))
{
reader.ReadLine();
rep2Confirmed = reader.ReadLine();
}
// Assert
Assert.Equal("chr1\t10\t20\tA\tinf", rep1Confirmed);
Assert.Equal("chr1\t15\t25\tB\tinf", rep2Confirmed);
// Clean up
File.Delete(rep1Path);
File.Delete(rep2Path);
Directory.Delete(outputPath, true);
}
}
}