Skip to content

Commit

Permalink
Merge branch 'main' of github.com:michaeljon/fasterqc.net
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljon committed May 20, 2022
2 parents b311874 + eb1a733 commit deb97b8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 5 deletions.
9 changes: 4 additions & 5 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/net6.0/Ovation.FasterQC.Net.dll",
"args": [
"-v",
"-d",
"-l",
"1000000",
"10",
"-f",
"sam",
"bam",
"-i",
"./tmp/in3257_2_S1.sorted.sam",
"./tmp/in3257_2_S1.sorted.bam",
"-o",
"./tmp/bob.json",
"-m",
"MeanQualityDistribution"
"BasicStatistics"
],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://aka.ms/VSCode-CS-LaunchJson-Console
Expand Down
93 changes: 93 additions & 0 deletions Models/BamAlignment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Text;

namespace Ovation.FasterQC.Net
{
Expand Down Expand Up @@ -37,4 +40,94 @@ public class BamAlignment

public byte[] qual { get; set; } = null!;
}

[SuppressMessage("Code style", "IDE1006", Justification = "Names correspond to BAM structure field names")]
public class BamOptionalElement
{
public char[] tag { get; set; }

public char val_type { get; set; }

public object value { get; set; } = null!;

public BamOptionalElement(byte[] block, ref int offset)
{
tag = Encoding.ASCII.GetChars(block, offset, 2); offset += 2;
val_type = Encoding.ASCII.GetChars(block, offset, 1)[0]; offset += 1;

// consume the rest
switch (val_type)
{
case 'A': offset += 1; break;

// byte
case 'c': offset += 1; break;
case 'C': offset += 1; break;

// short
case 's': offset += 2; break;
case 'S': offset += 2; break;

// int
case 'i': offset += 4; break;
case 'I': offset += 4; break;

// float
case 'f': offset += 4; break;

// null-terminated string
case 'Z':
while (block[offset++] != 0) ;
break;

// null-terminated hex digit pairs
case 'H':
while (block[offset++] != 0) ;
break;

// array of stuff
case 'B':
var subtype = Encoding.ASCII.GetChars(block, offset, 1)[0]; offset += 1;
var length = BitConverter.ToUInt32(new Span<byte>(block, offset, 4)); offset += 4;

// consume the stuff
for (var element = 0; element < length; element++)
{
switch (subtype)
{
// byte
case 'c': offset += 1; break;
case 'C': offset += 1; break;

// short
case 's': offset += 2; break;
case 'S': offset += 2; break;

// int
case 'i': offset += 4; break;
case 'I': offset += 4; break;

// float
case 'f': offset += 4; break;
}
}

break;
}
}

public override string ToString()
{
var sb = new StringBuilder();

sb.Append("tag: ");
sb.Append(tag[0]);
sb.Append(tag[1]);

sb.Append(", type: ");
sb.Append(val_type);

return sb.ToString();
}
}
}
6 changes: 6 additions & 0 deletions Readers/BamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,12 @@ private BamAlignment ReadSequence()
{
bamAlignment.qual = Array.Empty<byte>();
}
offset += (int)bamAlignment.l_seq;

while (offset < block_size)
{
_ = new BamOptionalElement(block, ref offset);
}

return bamAlignment;
}
Expand Down

0 comments on commit deb97b8

Please sign in to comment.