/// <summary> /// Calculates distance between contigs for each pair of Full Overlap. /// </summary> /// <param name="validPairedRead">Valid mate pairs which have full Overlap /// and support a particular orientation of contigs.</param> /// <param name="length">Length of forward contig.</param> private static void CalculateDistance(ValidMatePair validPairedRead, long length) { // For reverse read sequence in forward direction. validPairedRead.DistanceBetweenContigs.Add( validPairedRead.PairedRead.MeanLengthOfLibrary - (length - validPairedRead.ForwardReadStartPosition.First()) - (validPairedRead.ReverseReadStartPosition.First() + 1)); validPairedRead.StandardDeviation.Add(validPairedRead.PairedRead.StandardDeviationOfLibrary); validPairedRead.Weight = 1; // For reverse read sequence in reverse complementary direction. validPairedRead.DistanceBetweenContigs.Add( validPairedRead.PairedRead.MeanLengthOfLibrary - (length - validPairedRead.ForwardReadStartPosition[0]) - (validPairedRead.ReverseReadReverseComplementStartPosition[0] + 1)); if (validPairedRead.PairedRead.StandardDeviationOfLibrary != 0) { validPairedRead.StandardDeviation.Add(validPairedRead.PairedRead.StandardDeviationOfLibrary); } else { validPairedRead.StandardDeviation.Add(1); } }
/// <summary> /// Bundles all valid mate pairs in single edge but considering ±3σ limit. /// </summary> /// <param name="contigPairedRead">List of Valid Paired Reads.</param> private static void EdgeBundling(IList <ValidMatePair> contigPairedRead) { int index = 0; List <ValidMatePair> estimatedDistances; while (index < contigPairedRead.Count()) { estimatedDistances = contigPairedRead.Where(distance => (contigPairedRead[index].DistanceBetweenContigs.First() - (3.0 * contigPairedRead[index].StandardDeviation.First()) <= distance.DistanceBetweenContigs.First()) && distance.DistanceBetweenContigs[0] <= (contigPairedRead[index].DistanceBetweenContigs.First() + (3.0 * contigPairedRead[index].StandardDeviation.First()))).ToList(); if (estimatedDistances.Count > 1) { float p = (float)estimatedDistances.Sum(dist => dist.DistanceBetweenContigs[0] / Math.Pow(dist.StandardDeviation[0], 2)); float q = (float)estimatedDistances.Sum(sd => 1 / Math.Pow(sd.StandardDeviation[0], 2)); ValidMatePair distance = new ValidMatePair(); distance.DistanceBetweenContigs.Add(p / q); distance.StandardDeviation.Add((float)(1 / Math.Sqrt(q))); p = (float)estimatedDistances.Sum(dist => dist.DistanceBetweenContigs[1] / Math.Pow(dist.StandardDeviation[1], 2)); q = (float)estimatedDistances.Sum(sd => 1 / Math.Pow(sd.StandardDeviation[1], 2)); distance.DistanceBetweenContigs.Add(p / q); distance.StandardDeviation.Add((float)(1 / Math.Sqrt(q))); foreach (ValidMatePair est in estimatedDistances) { contigPairedRead.Remove(est); } distance.Weight = estimatedDistances.Count; contigPairedRead.Add(distance); index = 0; } else { index++; } } }
/// <summary> /// Further estimates distances using weighted mean. /// and standard deviation by merging valid mate pairs. /// </summary> /// <param name="distances">List of valid mate pairs.</param> private static void CalculateWeigthedEdge(IList <ValidMatePair> distances) { ValidMatePair finalDistance = new ValidMatePair(); finalDistance.DistanceBetweenContigs.Add( distances.Sum(distance => distance.DistanceBetweenContigs[0] * distance.Weight)); finalDistance.StandardDeviation.Add( distances.Sum(distance => distance.StandardDeviation[0] * distance.Weight)); finalDistance.Weight = distances.Sum(distance => distance.Weight); finalDistance.DistanceBetweenContigs[0] /= finalDistance.Weight; finalDistance.StandardDeviation[0] /= finalDistance.Weight; finalDistance.DistanceBetweenContigs.Add( distances.Sum(distance => distance.DistanceBetweenContigs[1] * distance.Weight)); finalDistance.StandardDeviation.Add( distances.Sum(distance => distance.StandardDeviation[0] * distance.Weight)); finalDistance.DistanceBetweenContigs[1] /= finalDistance.Weight; finalDistance.StandardDeviation[1] /= finalDistance.Weight; distances.Clear(); distances.Add(finalDistance); }
/// <summary> /// Further estimates distances using weighted mean. /// and standard deviation by merging valid mate pairs. /// </summary> /// <param name="distances">List of valid mate pairs.</param> private static void CalculateWeigthedEdge(IList<ValidMatePair> distances) { ValidMatePair finalDistance = new ValidMatePair(); finalDistance.DistanceBetweenContigs.Add( distances.Sum(distance => distance.DistanceBetweenContigs[0] * distance.Weight)); finalDistance.StandardDeviation.Add( distances.Sum(distance => distance.StandardDeviation[0] * distance.Weight)); finalDistance.Weight = distances.Sum(distance => distance.Weight); finalDistance.DistanceBetweenContigs[0] /= finalDistance.Weight; finalDistance.StandardDeviation[0] /= finalDistance.Weight; finalDistance.DistanceBetweenContigs.Add( distances.Sum(distance => distance.DistanceBetweenContigs[1] * distance.Weight)); finalDistance.StandardDeviation.Add( distances.Sum(distance => distance.StandardDeviation[0] * distance.Weight)); finalDistance.DistanceBetweenContigs[1] /= finalDistance.Weight; finalDistance.StandardDeviation[1] /= finalDistance.Weight; distances.Clear(); distances.Add(finalDistance); }
/// <summary> /// Bundles all valid mate pairs in single edge but considering ±3σ limit. /// </summary> /// <param name="contigPairedRead">List of Valid Paired Reads.</param> private static void EdgeBundling(IList<ValidMatePair> contigPairedRead) { int index = 0; List<ValidMatePair> estimatedDistances; while (index < contigPairedRead.Count()) { estimatedDistances = contigPairedRead.Where(distance => (contigPairedRead[index].DistanceBetweenContigs.First() - (3.0 * contigPairedRead[index].StandardDeviation.First()) <= distance.DistanceBetweenContigs.First()) && distance.DistanceBetweenContigs[0] <= (contigPairedRead[index].DistanceBetweenContigs.First() + (3.0 * contigPairedRead[index].StandardDeviation.First()))).ToList(); if (estimatedDistances.Count > 1) { float p = (float)estimatedDistances.Sum(dist => dist.DistanceBetweenContigs[0] / Math.Pow(dist.StandardDeviation[0], 2)); float q = (float)estimatedDistances.Sum(sd => 1 / Math.Pow(sd.StandardDeviation[0], 2)); ValidMatePair distance = new ValidMatePair(); distance.DistanceBetweenContigs.Add(p / q); distance.StandardDeviation.Add((float)(1 / Math.Sqrt(q))); p = (float)estimatedDistances.Sum(dist => dist.DistanceBetweenContigs[1] / Math.Pow(dist.StandardDeviation[1], 2)); q = (float)estimatedDistances.Sum(sd => 1 / Math.Pow(sd.StandardDeviation[1], 2)); distance.DistanceBetweenContigs.Add(p / q); distance.StandardDeviation.Add((float)(1 / Math.Sqrt(q))); foreach (ValidMatePair est in estimatedDistances) { contigPairedRead.Remove(est); } distance.Weight = estimatedDistances.Count; contigPairedRead.Add(distance); index = 0; } else { index++; } } }
/// <summary> /// Creates Paired Read Contig Map. /// </summary> /// <param name="forwardContigs">Contigs aligning to forward read.</param> /// <param name="reverseContigs">Contigs aligning to reverse read.</param> /// <param name="pair">Mate Pair.</param> /// <param name="contigMatePairs">Contig mate pair.</param> private static void ContigMatePairMapper( Dictionary<ISequence, IList<ReadMap>> forwardContigs, Dictionary<ISequence, IList<ReadMap>> reverseContigs, MatePair pair, ContigMatePairs contigMatePairs) { foreach (KeyValuePair<ISequence, IList<ReadMap>> forwardContigMaps in forwardContigs) { Dictionary<ISequence, IList<ValidMatePair>> forwardContig; if (!contigMatePairs.TryGetValue(forwardContigMaps.Key, out forwardContig)) { forwardContig = new Dictionary<ISequence, IList<ValidMatePair>>(); contigMatePairs.Add(forwardContigMaps.Key, forwardContig); } foreach (KeyValuePair<ISequence, IList<ReadMap>> reverseContigMaps in reverseContigs) { IList<ValidMatePair> matePairs; if (!forwardContig.TryGetValue(reverseContigMaps.Key, out matePairs)) { matePairs = new List<ValidMatePair>(); forwardContig.Add(reverseContigMaps.Key, matePairs); } foreach (ReadMap forwardMap in forwardContigMaps.Value) { foreach (ReadMap reverseMap in reverseContigMaps.Value) { ValidMatePair validPairedRead = new ValidMatePair(); validPairedRead.PairedRead = pair; validPairedRead.ForwardReadStartPosition.Add(forwardMap.StartPositionOfContig); validPairedRead.ReverseReadStartPosition.Add( reverseMap.StartPositionOfContig + reverseMap.Length - 1); validPairedRead.ReverseReadReverseComplementStartPosition.Add( reverseContigMaps.Key.Count - reverseMap.StartPositionOfContig - 1); matePairs.Add(validPairedRead); } } } } }