Skip to content

Commit

Permalink
feature: #964: Report discrepant antigen mismatch counts for exercise 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
zabeen committed May 23, 2023
1 parent 8ff7bf3 commit 4d58aa2
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 135 deletions.
22 changes: 20 additions & 2 deletions Atlas.ManualTesting/DependencyInjection/ServiceConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Atlas.Common.Utils.Extensions;
using Atlas.DonorImport.Data.Repositories;
using Atlas.DonorImport.ExternalInterface.Models;
using Atlas.HlaMetadataDictionary.ExternalInterface.Models;
using Atlas.ManualTesting.Common;
using Atlas.ManualTesting.Services;
using Atlas.ManualTesting.Services.Scoring;
Expand Down Expand Up @@ -89,9 +90,26 @@ Func<IServiceProvider, DonorManagementSettings> fetchDonorManagementSettings
services.AddScoped(typeof(IFileReader<>), typeof(FileReader<>));
services.AddScoped<IScoreBatchRequester, ScoreBatchRequester>();
services.AddScoped<IScoreRequestProcessor, ScoreRequestProcessor>();
services.AddScoped<IWmdaResultsComparer, WmdaResultsComparer>();
services.AddScoped<IWmdaDiscrepantResultsReporter, WmdaDiscrepantResultsReporter>();
services.AddScoped<IWmdaResultsTotalMismatchComparer, WmdaResultsTotalMismatchComparer>();
services.AddScoped<IWmdaResultsAntigenMismatchComparer, WmdaResultsAntigenMismatchComparer>();
services.AddScoped<IConvertHlaRequester, ConvertHlaRequester>();
services.AddScoped<IWmdaDiscrepantResultsWriter, WmdaDiscrepantResultsWriter>();

services.AddScoped<IWmdaDiscrepantAlleleResultsReporter, WmdaDiscrepantResultsReporter>(sp =>
{
var resultsComparer = sp.GetService<IWmdaResultsTotalMismatchComparer>();
var cacheProvider = sp.GetService<ITransientCacheProvider>();
var hlaConverter = sp.GetService<IConvertHlaRequester>();
return new WmdaDiscrepantResultsReporter(resultsComparer, cacheProvider, hlaConverter, TargetHlaCategory.PGroup);
});

services.AddScoped<IWmdaDiscrepantAntigenResultsReporter, WmdaDiscrepantResultsReporter>(sp =>
{
var resultsComparer = sp.GetService<IWmdaResultsAntigenMismatchComparer>();
var cacheProvider = sp.GetService<ITransientCacheProvider>();
var hlaConverter = sp.GetService<IConvertHlaRequester>();
return new WmdaDiscrepantResultsReporter(resultsComparer, cacheProvider, hlaConverter, TargetHlaCategory.Serology);
});
}

private static void RegisterDatabaseServices(
Expand Down
45 changes: 38 additions & 7 deletions Atlas.ManualTesting/Functions/WmdaConsensusDatasetFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ namespace Atlas.ManualTesting.Functions
public class WmdaConsensusDatasetFunctions
{
private readonly IScoreRequestProcessor scoreRequestProcessor;
private readonly IWmdaDiscrepantResultsReporter wmdaDiscrepantResultsReporter;
private readonly IWmdaDiscrepantAlleleResultsReporter alleleResultsReporter;
private readonly IWmdaDiscrepantAntigenResultsReporter antigenResultsReporter;
private readonly IWmdaDiscrepantResultsWriter resultsWriter;

public WmdaConsensusDatasetFunctions(
IScoreRequestProcessor scoreRequestProcessor,
IWmdaDiscrepantResultsReporter wmdaDiscrepantResultsReporter)
IWmdaDiscrepantAlleleResultsReporter alleleResultsReporter,
IWmdaDiscrepantAntigenResultsReporter antigenResultsReporter,
IWmdaDiscrepantResultsWriter resultsWriter)
{
this.scoreRequestProcessor = scoreRequestProcessor;
this.wmdaDiscrepantResultsReporter = wmdaDiscrepantResultsReporter;
this.alleleResultsReporter = alleleResultsReporter;
this.antigenResultsReporter = antigenResultsReporter;
this.resultsWriter = resultsWriter;
}

[FunctionName(nameof(ProcessWmdaConsensusDataset_Exercise1))]
Expand Down Expand Up @@ -62,14 +68,32 @@ await scoreRequestProcessor.ProcessScoreRequest(new ScoreRequestProcessorInput
});
}

[FunctionName(nameof(ReportDiscrepantResults_Exercise1))]
public async Task ReportDiscrepantResults_Exercise1(
/// <summary>
/// Report discrepant total mismatches - can be used on results from both exercise 1 and 2.
/// </summary>
[FunctionName(nameof(ReportDiscrepantResults_TotalMismatches))]
public async Task ReportDiscrepantResults_TotalMismatches(
[RequestBodyType(typeof(ReportDiscrepanciesRequest), nameof(ReportDiscrepanciesRequest))]
[HttpTrigger(AuthorizationLevel.Function, "post")]
HttpRequest request)
{
var importAndCompareRequest = JsonConvert.DeserializeObject<ReportDiscrepanciesRequest>(await new StreamReader(request.Body).ReadToEndAsync());
await wmdaDiscrepantResultsReporter.ReportDiscrepantResults(importAndCompareRequest);
var reportRequest = JsonConvert.DeserializeObject<ReportDiscrepanciesRequest>(await new StreamReader(request.Body).ReadToEndAsync());
var report = await alleleResultsReporter.ReportDiscrepantResults(reportRequest);
await resultsWriter.WriteToFile(BuildDiscrepantResultsFilePath(reportRequest.ResultsFilePath, "total"), report);
}

/// <summary>
/// Report discrepant antigen mismatches - can only be used on results from exercise 2.
/// </summary>
[FunctionName(nameof(ReportDiscrepantResults_AntigenMismatches))]
public async Task ReportDiscrepantResults_AntigenMismatches(
[RequestBodyType(typeof(ReportDiscrepanciesRequest), nameof(ReportDiscrepanciesRequest))]
[HttpTrigger(AuthorizationLevel.Function, "post")]
HttpRequest request)
{
var reportRequest = JsonConvert.DeserializeObject<ReportDiscrepanciesRequest>(await new StreamReader(request.Body).ReadToEndAsync());
var report = await antigenResultsReporter.ReportDiscrepantResults(reportRequest);
await resultsWriter.WriteToFile(BuildDiscrepantResultsFilePath(reportRequest.ResultsFilePath, "antigen"), report);
}

private static ScoringCriteria BuildThreeLocusScoringCriteria()
Expand All @@ -80,5 +104,12 @@ private static ScoringCriteria BuildThreeLocusScoringCriteria()
LociToExcludeFromAggregateScore = new List<Locus>()
};
}

private static string BuildDiscrepantResultsFilePath(string resultsPath, string mismatchCountKeyword)
{
return
$"{Path.GetDirectoryName(resultsPath)}/" +
$"{Path.GetFileNameWithoutExtension(resultsPath)}-{mismatchCountKeyword}-discrepancies.txt";
}
}
}
28 changes: 25 additions & 3 deletions Atlas.ManualTesting/Models/WmdaConsensusResultsFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Atlas.MatchingAlgorithm.Client.Models.Scoring;
using System.Collections.Generic;
using System.Linq;
using Atlas.Common.Public.Models.GeneticData;

namespace Atlas.ManualTesting.Models
{
Expand All @@ -13,6 +14,13 @@ public class WmdaConsensusResultsFile
public string MismatchCountAtB { get; set; }
public string MismatchCountAtDrb1 { get; set; }

public IDictionary<Locus, string> TotalMismatchCounts => new Dictionary<Locus, string>
{
{ Locus.A, MismatchCountAtA },
{ Locus.B , MismatchCountAtB },
{ Locus.Drb1 , MismatchCountAtDrb1 }
};

/// <summary>
/// Empty constructor needed for reading results from files
/// </summary>
Expand Down Expand Up @@ -43,6 +51,20 @@ public class WmdaConsensusResultsFileSetTwo : WmdaConsensusResultsFile
public string AntigenMismatchCountAtB { get; set; }
public string AntigenMismatchCountAtDrb1 { get; set; }

public IDictionary<Locus, string> AntigenMismatchCounts => new Dictionary<Locus, string>
{
{ Locus.A, AntigenMismatchCountAtA },
{ Locus.B , AntigenMismatchCountAtB },
{ Locus.Drb1 , AntigenMismatchCountAtDrb1 }
};

/// <summary>
/// Empty constructor needed for reading results from files
/// </summary>
public WmdaConsensusResultsFileSetTwo()
{
}

public WmdaConsensusResultsFileSetTwo(string patientId, string donorId, ScoringResult result) : base(patientId, donorId, result)
{
static string CountAntigenMismatches(LocusSearchResult locusResult)
Expand All @@ -61,9 +83,9 @@ static string CountAntigenMismatches(LocusSearchResult locusResult)

public override string ToString()
{
return $"{PatientId};{DonorId};" +
$"{MismatchCountAtA};{AntigenMismatchCountAtA};" +
$"{MismatchCountAtB};{AntigenMismatchCountAtB};" +
return $"{PatientId};{DonorId};" +
$"{MismatchCountAtA};{AntigenMismatchCountAtA};" +
$"{MismatchCountAtB};{AntigenMismatchCountAtB};" +
$"{MismatchCountAtDrb1};{AntigenMismatchCountAtDrb1}";
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Atlas.ManualTesting.Services.WmdaConsensusResults
{
public interface IWmdaDiscrepantResultsWriter
{
Task WriteToFile(string discrepantResultsFilePath, DiscrepantResultsReport report);
}

internal class WmdaDiscrepantResultsWriter : IWmdaDiscrepantResultsWriter
{
public async Task WriteToFile(string discrepantResultsFilePath, DiscrepantResultsReport report)
{
var contents = BuildFileContents(report.AnalysedDiscrepancies);

var headerText =
"Locus;" +
"Patient HLA Typing;Donor HLA Typing;" +
"Consensus Count;Atlas Count;" +
$"([Patient HLA-Donor HLA] Common {report.CommonHlaCategory} [Common {report.CommonHlaCategory} Count]);" +
"PDP Count;PDP Ids";

await File.WriteAllLinesAsync(discrepantResultsFilePath, new[] { headerText }.Concat(contents));
}

private static IEnumerable<string> BuildFileContents(IEnumerable<AnalysedDiscrepancy> analysedDiscrepancies)
{
static string HlaMetadataToString(CommonHlaMetadataInfo info)
{
if (!info.CommonHlaMetadata.Any())
{
return "_";
}

const int maxLength = 24;
var concatHlaMetadata = ConcatenateStrings(info.CommonHlaMetadata);
var formattedString = concatHlaMetadata.Length > maxLength ? $"{concatHlaMetadata[..maxLength]}..." : concatHlaMetadata;
return $"([{info.PatientHla}-{info.DonorHla}] {formattedString} [{info.CommonHlaMetadata.Count}])";
}

static string ConcatenateStrings(IEnumerable<string> strings) => string.Join(",", strings);

return analysedDiscrepancies.Select(d =>
$"{d.Locus};" +
$"{d.PatientHla.Position1} + {d.PatientHla.Position2};" +
$"{d.DonorHla.Position1} + {d.DonorHla.Position2};" +
$"{d.MismatchedCounts.First().ConsensusMismatchCount};" +
$"{d.MismatchedCounts.First().AtlasMismatchCount};" +
$"{HlaMetadataToString(d.CommonHlaMetadata.Position1)} + {HlaMetadataToString(d.CommonHlaMetadata.Position2)};" +
$"{d.MismatchedCounts.Count};" +
$"{ConcatenateStrings(d.MismatchedCounts.Select(mc => $"{mc.PatientId}:{mc.DonorId}"))}"
);
}
}
}
Loading

0 comments on commit 4d58aa2

Please sign in to comment.