Skip to content

Commit

Permalink
Merge pull request #2 from stephen-riley/add-null-checking
Browse files Browse the repository at this point in the history
Add null checking
  • Loading branch information
stephen-riley authored May 18, 2022
2 parents e008268 + 6cfe2e2 commit 4e0b684
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Interfaces/ISequenceReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface ISequenceReader : IDisposable
{
int SequencesRead { get; }

bool ReadSequence(out Sequence sequence);
bool ReadSequence(out Sequence? sequence);

double ApproximateCompletion { get; }
}
Expand Down
15 changes: 8 additions & 7 deletions Models/BamAlignment.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Diagnostics.CodeAnalysis;

namespace Ovation.FasterQC.Net
{
#pragma warning disable IDE1006
[SuppressMessage("Code style", "IDE1006", Justification = "Names correspond to BAM structure field names")]
public class BamAlignment
{
public uint block_size { get; set; }
Expand All @@ -27,13 +29,12 @@ public class BamAlignment

public int tlen { get; set; }

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

public uint[] cigar { get; set; }
public uint[] cigar { get; set; } = null!;

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

public byte[] qual { get; set; }
public byte[] qual { get; set; } = null!;
}
#pragma warning restore IDE1006
}
}
3 changes: 2 additions & 1 deletion Models/Sequence.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public Sequence(BamAlignment bamAlignment)
Identifier = new ReadOnlyMemory<byte>(bamAlignment.read_name).ToArray();
Read = new ReadOnlyMemory<byte>(bamAlignment.seq).ToArray();
Quality = new ReadOnlyMemory<byte>(bamAlignment.qual).ToArray();
Blank = Array.Empty<byte>();
}

public override string ToString()
Expand All @@ -48,4 +49,4 @@ public override string ToString()
return sb.ToString();
}
}
}
}
1 change: 1 addition & 0 deletions Ovation.FasterQC.Net.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
Expand Down
10 changes: 6 additions & 4 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Program
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
};

private TimedSequenceProgressBar progressBar;
private TimedSequenceProgressBar? progressBar;

static void Main(string[] args)
{
Expand All @@ -42,14 +42,16 @@ private void Run()
On(Settings.ShowProgress, () => progressBar = new TimedSequenceProgressBar(sequenceReader));
On(Settings.Verbose, () => Console.Error.WriteLine($"Processing {Settings.InputFilename}..."));

while (sequenceReader.ReadSequence(out Sequence sequence))
while (sequenceReader.ReadSequence(out Sequence? sequence))
{
ArgumentNullException.ThrowIfNull(sequence);

foreach (var module in modules)
{
module.ProcessSequence(sequence);
}

On(Settings.ShowProgress, () => progressBar.Update());
On(Settings.ShowProgress, () => progressBar?.Update());
On(Settings.Verbose, () =>
{
if (sequenceReader.SequencesRead % UpdatePeriod == 0)
Expand All @@ -71,7 +73,7 @@ private void Run()
results[module.Name] = module.Data;
}

On(Settings.ShowProgress, () => progressBar.Update(force: true));
On(Settings.ShowProgress, () => progressBar?.Update(force: true));
On(Settings.Verbose, () => Console.Error.WriteLine($"{sequenceReader.SequencesRead.WithSsiUnits()} sequences completed ({sequenceReader.ApproximateCompletion:0.0}%)"));

if (string.IsNullOrWhiteSpace(Settings.OutputFilename))
Expand Down
5 changes: 3 additions & 2 deletions Readers/BamReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public BamReader(string bam)
ConsumeHeader();
}

public bool ReadSequence(out Sequence sequence)
public bool ReadSequence(out Sequence? sequence)
{
try
{
Expand Down Expand Up @@ -106,9 +106,10 @@ private BamAlignment ReadSequence()
var bamAlignment = new BamAlignment
{
block_size = block_size,
refID = BitConverter.ToInt32(block, offset)
};

bamAlignment.refID = BitConverter.ToInt32(block, offset); offset += 4;
offset += 4;
bamAlignment.pos = BitConverter.ToInt32(block, offset) + 1; offset += 4;
bamAlignment.l_read_name = block[offset]; offset += 1;
bamAlignment.mapq = block[offset]; offset += 1;
Expand Down
12 changes: 6 additions & 6 deletions Readers/FastqLineReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class FastqLineReader : ISequenceReader
{
private readonly FileStream inputStream;

private readonly GZipStream gzipStream;
private readonly GZipStream? gzipStream;

private readonly BufferedStream bufferedStream;

Expand Down Expand Up @@ -50,7 +50,7 @@ public FastqLineReader(string fastq, bool gzipped = true)
}
}

public bool ReadSequence(out Sequence sequence)
public bool ReadSequence(out Sequence? sequence)
{
try
{
Expand All @@ -61,10 +61,10 @@ public bool ReadSequence(out Sequence sequence)
return false;
}

var identifier = Encoding.ASCII.GetBytes(streamReader.ReadLine());
var read = Encoding.ASCII.GetBytes(streamReader.ReadLine());
var blank = Encoding.ASCII.GetBytes(streamReader.ReadLine());
var quality = Encoding.ASCII.GetBytes(streamReader.ReadLine());
var identifier = Encoding.ASCII.GetBytes(streamReader.ReadLine() ?? "");
var read = Encoding.ASCII.GetBytes(streamReader.ReadLine() ?? "");
var blank = Encoding.ASCII.GetBytes(streamReader.ReadLine() ?? "");
var quality = Encoding.ASCII.GetBytes(streamReader.ReadLine() ?? "");

sequence = new Sequence(identifier, read, blank, quality);
sequencesRead++;
Expand Down
4 changes: 2 additions & 2 deletions Readers/FastqReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class FastqReader : ISequenceReader
{
private readonly FileStream inputStream;

private readonly GZipStream gzipStream;
private readonly GZipStream? gzipStream;

private readonly BufferedStream bufferedStream;

Expand Down Expand Up @@ -49,7 +49,7 @@ public FastqReader(string fastq, bool gzipped = true)
}
}

public bool ReadSequence(out Sequence sequence)
public bool ReadSequence(out Sequence? sequence)
{
// this is clearly dangerous, instead read a large chunk of the file
// and then walk through it returning only the consumed portion while
Expand Down
2 changes: 1 addition & 1 deletion Readers/ReaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static ISequenceReader Create(CliOptions settings)
{
return settings switch
{
{ Fastq: true } => new FastqLineReader(settings.InputFilename, true),
{ Fastq: true } => new FastqLineReader(settings.InputFilename, settings.Zipped),
{ Bam: true } => new BamReader(settings.InputFilename),
_ => throw new InvalidOperationException($"could not determine file type of {settings.InputFilename}")
};
Expand Down
9 changes: 4 additions & 5 deletions Utils/CliOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ public class CliOptions
public bool ShowProgress { get; set; }

[Option('i', "input", Required = true, HelpText = "Input filename.")]
public string InputFilename { get; set; }
public string InputFilename { get; set; } = null!;

[Option('o', "output", Required = false, HelpText = "Output filename. Defaults to STDOUT.")]
public string OutputFilename { get; set; }
public string OutputFilename { get; set; } = null!;

[Option('b', "bam", Required = false, HelpText = "Assume BAM format.")]
public bool Bam { get; set; }
Expand All @@ -32,10 +32,9 @@ public class CliOptions
public bool Zipped { get; set; }

[Option('m', "modules", Required = true, Min = 1, HelpText = "Space-separated list of modules to run, or 'all'.")]
public IEnumerable<string> ModuleNames { get; set; }
public IEnumerable<string> ModuleNames { get; set; } = Array.Empty<string>();

public static CliOptions Settings
{ get; set; }
public static CliOptions Settings { get; set; } = null!;

public const int UpdatePeriod = 100_000;

Expand Down

0 comments on commit 4e0b684

Please sign in to comment.