Skip to content

Commit

Permalink
fixing percent complete on read-limited runs
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljon committed May 18, 2022
1 parent 29819cb commit 3aa3af1
Showing 1 changed file with 31 additions and 22 deletions.
53 changes: 31 additions & 22 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using CommandLine;
using Ovation.FasterQC.Net.Modules;
Expand All @@ -11,7 +10,7 @@

namespace Ovation.FasterQC.Net
{
class Program
internal class Program : IDisposable
{
private static readonly JsonSerializerOptions options = new()
{
Expand All @@ -22,17 +21,17 @@ class Program

private TimedSequenceProgressBar? progressBar;

static void Main(string[] args)
private static void Main(string[] args)
{
var parser = new Parser(config =>
Parser? parser = new(config =>
{
config.AutoHelp = true;
config.AutoVersion = true;
config.CaseInsensitiveEnumValues = true;
}
);

parser.ParseArguments<CliOptions>(args)
_ = parser.ParseArguments<CliOptions>(args)
.WithParsed(o =>
{
Settings = o;
Expand All @@ -42,8 +41,8 @@ static void Main(string[] args)

private void Run()
{
using var sequenceReader = ReaderFactory.Create(Settings);
var modules = ModuleFactory.Create(Settings);
using ISequenceReader? sequenceReader = ReaderFactory.Create(Settings);
IEnumerable<IQcModule>? modules = ModuleFactory.Create(Settings);

Console.Error.WriteLine($"Running modules:\n {string.Join("\n ", Settings.ModuleNames)}");

Expand All @@ -54,7 +53,7 @@ private void Run()
{
ArgumentNullException.ThrowIfNull(sequence);

foreach (var module in modules)
foreach (IQcModule? module in modules)
{
module.ProcessSequence(sequence);
}
Expand All @@ -64,35 +63,25 @@ private void Run()
{
if (sequenceReader.SequencesRead % UpdatePeriod == 0)
{
var approximateCompletion = sequenceReader.ApproximateCompletion;

// if we're limiting the number of reads then the reader's
// approximation will be incorrect (it's based on file positions),
// so we'll do the math ourselves
if (Settings.ReadLimit < ulong.MaxValue)
{
approximateCompletion = 100.0 * (double)sequenceReader.SequencesRead / (double)Settings.ReadLimit;
}

Console.Error.WriteLine($"{sequenceReader.SequencesRead.WithSsiUnits()} sequences completed ({approximateCompletion:0.0}%)");
WritePercentComplete(sequenceReader.ApproximateCompletion, sequenceReader.SequencesRead);
}
});
}

var results = new Dictionary<string, object>()
Dictionary<string, object>? results = new()
{
["_modules"] = Settings.ModuleNames,
["_inputFilename"] = Settings.InputFilename,
["_outputFilename"] = string.IsNullOrWhiteSpace(Settings.OutputFilename) ? "STDOUT" : Settings.OutputFilename,
};

foreach (var module in modules)
foreach (IQcModule? module in modules)
{
results[module.Name] = module.Data;
}

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

if (string.IsNullOrWhiteSpace(Settings.OutputFilename))
{
Expand All @@ -103,5 +92,25 @@ private void Run()
File.WriteAllText(Settings.OutputFilename, JsonSerializer.Serialize(results, options));
}
}

private static void WritePercentComplete(double reportedCompletion, ulong sequencesRead)
{
double approximateCompletion = reportedCompletion;

// if we're limiting the number of reads then the reader's
// approximation will be incorrect (it's based on file positions),
// so we'll do the math ourselves
if (Settings.ReadLimit < ulong.MaxValue)
{
approximateCompletion = 100.0 * sequencesRead / Settings.ReadLimit;
}

Console.Error.WriteLine($"{sequencesRead.WithSsiUnits()} sequences completed ({approximateCompletion:0.0}%)");
}

public void Dispose()
{
throw new NotImplementedException();
}
}
}

0 comments on commit 3aa3af1

Please sign in to comment.