From 9f888513f43d9ef3c3805830dcd743b26381320e Mon Sep 17 00:00:00 2001 From: Stephen Riley Date: Mon, 16 May 2022 18:23:09 -0700 Subject: [PATCH 1/2] apply --- Interfaces/ISequenceReader.cs | 2 +- Models/BamAlignment.cs | 15 ++++++++------- Models/Sequence.cs | 3 ++- Ovation.FasterQC.Net.csproj | 1 + Program.cs | 12 ++++++++---- Readers/BamReader.cs | 2 +- Readers/FastqLineReader.cs | 12 ++++++------ Readers/FastqReader.cs | 4 ++-- Readers/ReaderFactory.cs | 2 +- Utils/CliOptions.cs | 9 ++++----- 10 files changed, 34 insertions(+), 28 deletions(-) diff --git a/Interfaces/ISequenceReader.cs b/Interfaces/ISequenceReader.cs index fe37083..954aa5a 100644 --- a/Interfaces/ISequenceReader.cs +++ b/Interfaces/ISequenceReader.cs @@ -6,7 +6,7 @@ public interface ISequenceReader : IDisposable { int SequencesRead { get; } - bool ReadSequence(out Sequence sequence); + bool ReadSequence(out Sequence? sequence); double ApproximateCompletion { get; } } diff --git a/Models/BamAlignment.cs b/Models/BamAlignment.cs index 23dda7e..28cdc0f 100644 --- a/Models/BamAlignment.cs +++ b/Models/BamAlignment.cs @@ -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; } @@ -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 -} \ No newline at end of file +} diff --git a/Models/Sequence.cs b/Models/Sequence.cs index e9eff94..3c63b88 100644 --- a/Models/Sequence.cs +++ b/Models/Sequence.cs @@ -34,6 +34,7 @@ public Sequence(BamAlignment bamAlignment) Identifier = new ReadOnlyMemory(bamAlignment.read_name).ToArray(); Read = new ReadOnlyMemory(bamAlignment.seq).ToArray(); Quality = new ReadOnlyMemory(bamAlignment.qual).ToArray(); + Blank = Array.Empty(); } public override string ToString() @@ -48,4 +49,4 @@ public override string ToString() return sb.ToString(); } } -} \ No newline at end of file +} diff --git a/Ovation.FasterQC.Net.csproj b/Ovation.FasterQC.Net.csproj index 9eb1d86..77ff3f2 100644 --- a/Ovation.FasterQC.Net.csproj +++ b/Ovation.FasterQC.Net.csproj @@ -3,6 +3,7 @@ Exe net6.0 + enable diff --git a/Program.cs b/Program.cs index d83c842..fb15b01 100644 --- a/Program.cs +++ b/Program.cs @@ -19,7 +19,7 @@ class Program PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; - private TimedSequenceProgressBar progressBar; + private TimedSequenceProgressBar? progressBar; static void Main(string[] args) { @@ -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) @@ -62,6 +64,8 @@ private void Run() var results = new Dictionary() { ["_modules"] = Settings.ModuleNames, + ["_inputFilename"] = Settings.InputFilename, + ["_outputFilename"] = string.IsNullOrWhiteSpace(Settings.OutputFilename) ? "STDOUT" : Settings.OutputFilename, }; foreach (var module in modules) @@ -69,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)) diff --git a/Readers/BamReader.cs b/Readers/BamReader.cs index a2d9d4c..1e3be34 100644 --- a/Readers/BamReader.cs +++ b/Readers/BamReader.cs @@ -40,7 +40,7 @@ public BamReader(string bam) ConsumeHeader(); } - public bool ReadSequence(out Sequence sequence) + public bool ReadSequence(out Sequence? sequence) { try { diff --git a/Readers/FastqLineReader.cs b/Readers/FastqLineReader.cs index de7611b..9c2c264 100644 --- a/Readers/FastqLineReader.cs +++ b/Readers/FastqLineReader.cs @@ -10,7 +10,7 @@ public class FastqLineReader : ISequenceReader { private readonly FileStream inputStream; - private readonly GZipStream gzipStream; + private readonly GZipStream? gzipStream; private readonly BufferedStream bufferedStream; @@ -50,7 +50,7 @@ public FastqLineReader(string fastq, bool gzipped = true) } } - public bool ReadSequence(out Sequence sequence) + public bool ReadSequence(out Sequence? sequence) { try { @@ -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++; diff --git a/Readers/FastqReader.cs b/Readers/FastqReader.cs index 946ee2b..bbdca57 100644 --- a/Readers/FastqReader.cs +++ b/Readers/FastqReader.cs @@ -9,7 +9,7 @@ public class FastqReader : ISequenceReader { private readonly FileStream inputStream; - private readonly GZipStream gzipStream; + private readonly GZipStream? gzipStream; private readonly BufferedStream bufferedStream; @@ -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 diff --git a/Readers/ReaderFactory.cs b/Readers/ReaderFactory.cs index c8c34bf..8b63848 100644 --- a/Readers/ReaderFactory.cs +++ b/Readers/ReaderFactory.cs @@ -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}") }; diff --git a/Utils/CliOptions.cs b/Utils/CliOptions.cs index 6a4a7f0..a9e4b76 100644 --- a/Utils/CliOptions.cs +++ b/Utils/CliOptions.cs @@ -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; } @@ -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 ModuleNames { get; set; } + public IEnumerable ModuleNames { get; set; } = Array.Empty(); - public static CliOptions Settings - { get; set; } + public static CliOptions Settings { get; set; } = null!; public const int UpdatePeriod = 100_000; From aa902c2664cbb635a3a1ec798f1074725b3b33da Mon Sep 17 00:00:00 2001 From: Stephen Riley Date: Tue, 17 May 2022 15:25:42 -0700 Subject: [PATCH 2/2] fix warnings --- Readers/BamReader.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Readers/BamReader.cs b/Readers/BamReader.cs index 1e3be34..5c6f5a0 100644 --- a/Readers/BamReader.cs +++ b/Readers/BamReader.cs @@ -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;