/// <summary>
/// Outputs the copy number calls to a text file.
/// </summary>
private static void WriteVariants(IReadOnlyCollection <List <CanvasSegment> > segments, PloidyInfo ploidy, GenomeMetadata genome,
BgzipOrStreamWriter writer, bool isPedigreeInfoSupplied = true, int?denovoQualityThreshold = null)
{
var nSamples = segments.Count;
foreach (GenomeMetadata.SequenceMetadata chromosome in genome.Sequences)
{
for (int segmentIndex = 0; segmentIndex < segments.First().Count; segmentIndex++)
{
var firstSampleSegment = segments.First()[segmentIndex];
if (!isPedigreeInfoSupplied && segments.Select(sample => sample[segmentIndex].Filter == "PASS").Any() && segments.Count > 1)
{
firstSampleSegment.Filter = "PASS";
}
if (!firstSampleSegment.Chr.Equals(chromosome.Name, StringComparison.OrdinalIgnoreCase))
{
continue;
}
var referenceCopyNumbers = segments.Select(segment => ploidy?.GetReferenceCopyNumber(segment[segmentIndex]) ?? 2).ToList();
var currentSegments = segments.Select(x => x[segmentIndex]).ToList();
var cnvTypes = new List <CnvType>();
for (int sampleIndex = 0; sampleIndex < nSamples; sampleIndex++)
{
cnvTypes.Add(currentSegments[sampleIndex].GetCnvType(referenceCopyNumbers[sampleIndex]));
}
CnvType cnvType;
if (cnvTypes.TrueForAll(x => x == CnvType.Reference))
{
cnvType = CnvType.Reference;
}
else if (cnvTypes.TrueForAll(x => x == CnvType.Reference | x == CnvType.Loss))
{
cnvType = CnvType.Loss;
}
else if (cnvTypes.TrueForAll(x => x == CnvType.Reference | x == CnvType.Gain))
{
cnvType = CnvType.Gain;
}
else if (cnvTypes.TrueForAll(x => x == CnvType.Reference | x == CnvType.LossOfHeterozygosity))
{
cnvType = CnvType.LossOfHeterozygosity;
}
else
{
cnvType = CnvType.ComplexCnv;
}
WriteInfoField(writer, firstSampleSegment, cnvType, denovoQualityThreshold, isMultisample: segments.Count > 1);
// FORMAT field
if (segments.Count == 1)
{
WriteSingleSampleInfo(writer, firstSampleSegment);
}
else
{
WriteFormatField(writer, currentSegments);
}
}
}
}