Skip to content

Commit

Permalink
chore: #1294: First check if db has existing imputaion results for su…
Browse files Browse the repository at this point in the history
…bject.
  • Loading branch information
zabeen committed May 2, 2024
1 parent 7d97d33 commit dcef5df
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Atlas.MatchPrediction.Test.Validation.Data.Repositories.Homework
public interface IImputationSummaryRepository
{
Task<int> Add(ImputationSummary imputationSummary);
Task<int?> Get(string externalSubjectId);
}

public class ImputationSummaryRepository : IImputationSummaryRepository
Expand Down Expand Up @@ -48,5 +49,19 @@ await using (var connection = new SqlConnection(connectionString))
})).Single();
}
}

/// <inheritdoc />
public async Task<int?> Get(string externalSubjectId)
{
const string sql = $@"
SELECT Id
FROM {nameof(MatchPredictionValidationContext.ImputationSummaries)}
WHERE ExternalSubjectId = @{nameof(externalSubjectId)}";

await using (var connection = new SqlConnection(connectionString))
{
return await connection.QuerySingleOrDefaultAsync<int?>(sql, new { externalSubjectId });
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using Atlas.Common.Sql.BulkInsert;
using Atlas.MatchPrediction.Test.Validation.Data.Context;
using Atlas.MatchPrediction.Test.Validation.Data.Models.Homework;
using Dapper;
using Microsoft.Data.SqlClient;

namespace Atlas.MatchPrediction.Test.Validation.Data.Repositories.Homework
{
public interface ISubjectGenotypeRepository : IBulkInsertRepository<SubjectGenotype>
{
Task<IEnumerable<SubjectGenotype>> Get(int imputationSummaryId);
}

public class SubjectGenotypeRepository : BulkInsertRepository<SubjectGenotype>, ISubjectGenotypeRepository
Expand All @@ -16,5 +19,19 @@ public class SubjectGenotypeRepository : BulkInsertRepository<SubjectGenotype>,
public SubjectGenotypeRepository(string connectionString) : base(connectionString, TableName)
{
}

/// <inheritdoc />
public async Task<IEnumerable<SubjectGenotype>> Get(int imputationSummaryId)
{
const string sql = $@"
SELECT *
FROM {TableName}
WHERE {nameof(SubjectGenotype.ImputationSummary_Id)} = @{nameof(imputationSummaryId)}";

await using (var connection = new SqlConnection(ConnectionString))
{
return await connection.QueryAsync<SubjectGenotype>(sql, new { imputationSummaryId });
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ public async Task Process(PatientDonorPair pdp, string matchLoci, string hlaVers
Impute(pdp, donorResult, matchLoci, hlaVersion, false)
// Then submit matching genotypes request
);

// store matching genotypes after imputation as need the subject genotype ids

await UpdateRecord(pdp);
}

private async Task<SubjectGenotypeResult> CheckPatientHasMissingHla(PatientDonorPair pdp, LociInfo<bool> matchLoci)
Expand Down Expand Up @@ -85,8 +89,8 @@ async Task<SubjectGenotypeResult> GetResult()
string hlaVersion,
bool isPatient)
{
// Only request imputation if subject has not been processed before
// This step should update the cache as well
// Only request imputation if subject has not been processed before.
// This step will update the cache as well.
result.Genotypes ??= await genotypesProcessor.RequestAndSaveImputation(result.SubjectInfo, matchLoci, hlaVersion);

if (isPatient)
Expand All @@ -97,8 +101,6 @@ async Task<SubjectGenotypeResult> GetResult()
{
pdp.DonorImputationCompleted = true;
}

await UpdateRecord(pdp);
}

private async Task UpdateRecord(PatientDonorPair pdp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ internal class SubjectGenotypesProcessor : ISubjectGenotypesProcessor
string hlaVersion)
{
var subjectId = subjectInfo.ExternalId;
var summaryId = await summaryRepo.Get(subjectId);

if (summaryId != null)
{
return await subjectGenotypeRepo.Get(summaryId.Value);
}

var response = await imputationRequester.Request(new HomeworkImputationRequest
{
Expand All @@ -57,8 +63,8 @@ internal class SubjectGenotypesProcessor : ISubjectGenotypesProcessor
return null;
}

var summaryId = await summaryRepo.Add(BuildSummary(subjectId, response.Result!));
var genotypes = SplitIntoGenotypes(response.Result!.GenotypeLikelihoods, summaryId).ToList();
summaryId = await summaryRepo.Add(BuildSummary(subjectId, response.Result!));
var genotypes = SplitIntoGenotypes(response.Result!.GenotypeLikelihoods, summaryId.Value).ToList();
await subjectGenotypeRepo.BulkInsert(genotypes);
return genotypes;
}
Expand Down

0 comments on commit dcef5df

Please sign in to comment.