private void WriteCanvasPartitionResults(string outPath)
{
Dictionary<string, bool> starts = new Dictionary<string, bool>();
Dictionary<string, bool> stops = new Dictionary<string, bool>();
foreach (string chr in SegmentationResults.SegmentByChr.Keys)
{
for (int segmentIndex = 0; segmentIndex < SegmentationResults.SegmentByChr[chr].Length; segmentIndex++)
{
Segment segment = SegmentationResults.SegmentByChr[chr][segmentIndex];
starts[chr + ":" + segment.start] = true;
stops[chr + ":" + segment.end] = true;
}
}
Dictionary<string, List<GenomicBin>> ExcludedIntervals = new Dictionary<string, List<GenomicBin>>();
if (!string.IsNullOrEmpty(ForbiddenIntervalBedPath))
{
ExcludedIntervals = CanvasCommon.Utilities.LoadBedFile(ForbiddenIntervalBedPath);
}
using (GzipWriter writer = new GzipWriter(outPath))
{
int segmentNum = -1;
foreach (string chr in StartByChr.Keys)
{
List<GenomicBin> excludeIntervals = null;
if (ExcludedIntervals.ContainsKey(chr)) excludeIntervals = ExcludedIntervals[chr];
int excludeIndex = 0; // Points to the first interval which *doesn't* end before our current position
uint previousBinEnd = 0;
for (int pos = 0; pos < StartByChr[chr].Length; pos++)
{
uint start = StartByChr[chr][pos];
uint end = EndByChr[chr][pos];
bool newSegment = false;
string key = chr + ":" + start;
if (starts.ContainsKey(key))
{
newSegment = true;
}
if (excludeIntervals != null)
{
while (excludeIndex < excludeIntervals.Count && excludeIntervals[excludeIndex].Stop < previousBinEnd) excludeIndex++;
if (excludeIndex < excludeIntervals.Count)
{
// Note: forbiddenZoneMid should never fall inside a bin, becuase these intervals were already excluded
// from consideration during the call to CanvasBin.
int forbiddenZoneMid = (excludeIntervals[excludeIndex].Start + excludeIntervals[excludeIndex].Stop) / 2;
if (previousBinEnd < forbiddenZoneMid && end >= forbiddenZoneMid) newSegment = true;
}
}
if (previousBinEnd > 0 && MaxInterBinDistInSegment >= 0 && previousBinEnd + MaxInterBinDistInSegment < start
&& !newSegment)
{
newSegment = true;
}
if (newSegment) segmentNum++;
writer.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}\t{4}", chr, start, end, ScoreByChr[chr][pos], segmentNum));
previousBinEnd = end;
}
}
}
}