Skip to content

Commit

Permalink
additional flag cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljon committed May 19, 2022
1 parent 3849d00 commit 9db0c64
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 21 deletions.
6 changes: 5 additions & 1 deletion Models/ReadFlag.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public enum ReadFlag : ushort
/// </summary>
LastSegment = 128,

EmbeddedSegment = FirstSegment | LastSegment,

/// <summary>
/// not primary alignment
/// </summary>
Expand All @@ -65,6 +67,8 @@ public enum ReadFlag : ushort
/// <summary>
/// supplementary alignment (e.g. aligner specific, could be a portion of a split read or a tied region)
/// </summary>
SupplementaryAlignment = 2048
SupplementaryAlignment = 2048,

SecondaryAlignment = NotPrimaryAlignment | SupplementaryAlignment
}
}
98 changes: 78 additions & 20 deletions Modules/AlignmentStatistics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ public class AlignmentStatistics : IQcModule

private ulong nextSegmentReverseComplemented;

private ulong firstSegment;

private ulong lastSegment;

private ulong embeddedSegment;

private ulong unknownIndex;

private ulong primaryAlignment;

private ulong secondaryAlignment;

private ulong nonPrimaryAlignment;

private ulong failedQualityChecks;
Expand Down Expand Up @@ -52,6 +64,7 @@ public void ProcessSequence(Sequence sequence)

var readPair = readLengthHistogram[sequence.Read.Length];

// revisit the logic here based on https://samtools.github.io/hts-specs/SAMv1.pdf
if ((sequence.ReadFlag & ReadFlag.Paired) != 0)
{
paired++;
Expand All @@ -68,41 +81,86 @@ public void ProcessSequence(Sequence sequence)
if ((sequence.ReadFlag & ReadFlag.NextSegmentUnmapped) != 0) nextSegmentUnmapped++;
if ((sequence.ReadFlag & ReadFlag.ReverseComplemented) != 0) reverseComplemented++;
if ((sequence.ReadFlag & ReadFlag.NextSegmentReverseComplemented) != 0) nextSegmentReverseComplemented++;

if ((sequence.ReadFlag & ReadFlag.FirstSegment) != 0) firstSegment++;
if ((sequence.ReadFlag & ReadFlag.LastSegment) != 0) lastSegment++;
if ((sequence.ReadFlag & (ReadFlag.EmbeddedSegment)) == ReadFlag.EmbeddedSegment) embeddedSegment++;
if ((sequence.ReadFlag & (ReadFlag.EmbeddedSegment)) == 0) unknownIndex++;

if ((sequence.ReadFlag & ReadFlag.NotPrimaryAlignment) != 0) nonPrimaryAlignment++;
if ((sequence.ReadFlag & ReadFlag.FailedQualityChecks) != 0) failedQualityChecks++;
if ((sequence.ReadFlag & ReadFlag.OpticalDuplicate) != 0) opticalDuplicate++;
if ((sequence.ReadFlag & ReadFlag.SecondaryAlignment) == 0)
{
primaryAlignment++;
}
if ((sequence.ReadFlag & ReadFlag.SecondaryAlignment) == ReadFlag.SecondaryAlignment)
{
secondaryAlignment++;
}
}

public void Reset()
{
sequenceCount = 0;
}

public object Data => new
public object Data
{
sequenceCount,
paired,
aligned,
alignedAndPaired,
segmentUnmapped,
nextSegmentUnmapped,
reverseComplemented,
nextSegmentReverseComplemented,
nonPrimaryAlignment,
failedQualityChecks,
opticalDuplicate,
alignedBases,
averageReadLength = (double)baseCount / (double)sequenceCount,
histogram = readLengthHistogram
.Select((k, v) => new ulong[] { (ulong)k.Key, k.Value.Paired, k.Value.AlignedAndPaired })
.OrderBy(a => a[0])
};
get
{
var minReadLength = readLengthHistogram.Keys.Min();
var maxReadLength = readLengthHistogram.Keys.Max();

for (var readLength = minReadLength; readLength < maxReadLength; readLength++)
{
if (readLengthHistogram.ContainsKey(readLength) == false)
{
readLengthHistogram.Add(readLength, new ReadPair());
}
}

return new
{
sequenceCount,
paired,
aligned,
alignedAndPaired,
segmentUnmapped,
nextSegmentUnmapped,
reverseComplemented,
nextSegmentReverseComplemented,
firstSegment,
lastSegment,
embeddedSegment,
unknownIndex,
primaryAlignment,
secondaryAlignment,
nonPrimaryAlignment,
failedQualityChecks,
opticalDuplicate,
alignedBases,
averageReadLength = (double)baseCount / (double)sequenceCount,
histogram = new
{
minReadLength,
maxReadLength,
paired = readLengthHistogram
.OrderBy(k => k.Key)
.Select((k, v) => k.Value.Paired),
unpaired = readLengthHistogram
.OrderBy(k => k.Key)
.Select((k, v) => k.Value.AlignedAndPaired)
}
};
}
}

class ReadPair
{
public ulong AlignedAndPaired { get; set; }

public ulong Paired { get; set; }

public ulong AlignedAndPaired { get; set; }
}
}
}

0 comments on commit 9db0c64

Please sign in to comment.