Skip to content

Commit

Permalink
chore: #1294: Import pdps directly from file.
Browse files Browse the repository at this point in the history
  • Loading branch information
zabeen committed Apr 27, 2024
1 parent fb6dc94 commit 89cec57
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 20 deletions.
8 changes: 6 additions & 2 deletions Atlas.ManualTesting.Common/Services/FileReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ namespace Atlas.ManualTesting.Common.Services
{
public interface IFileReader<T>
{
Task<IReadOnlyCollection<T>> ReadAllLines(string delimiter, string filePath);
Task<IReadOnlyCollection<T>> ReadAllLines(string delimiter, string filePath, bool hasHeaderRecord = true);
IAsyncEnumerable<T> ReadAsync(string delimiter, string filePath);
}

public class FileReader<T> : IFileReader<T>
{
public async Task<IReadOnlyCollection<T>> ReadAllLines(string delimiter, string filePath)
public async Task<IReadOnlyCollection<T>> ReadAllLines(
string delimiter,
string filePath,
bool hasHeaderRecord = true)
{
FileChecks(filePath);

Expand All @@ -19,6 +22,7 @@ public async Task<IReadOnlyCollection<T>> ReadAllLines(string delimiter, string
using var csv = new CsvReader(reader);

csv.Configuration.Delimiter = delimiter;
csv.Configuration.HasHeaderRecord = hasHeaderRecord;
csv.Configuration.HeaderValidated = null;
csv.Configuration.MissingFieldFound = null;
csv.Configuration.TypeConverterOptionsCache.GetOptions<string>().NullValues.Add("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private static void RegisterDatabaseServices(this IServiceCollection services, F
Func<IServiceProvider, ValidationSearchSettings> fetchValidationSearchSettings)
{
services.AddScoped<ISubjectInfoImporter, SubjectInfoImporter>();
services.AddScoped<IFileReader<ImportedSubject>, FileReader<ImportedSubject>>();
services.AddScoped(typeof(IFileReader<>), typeof(FileReader<>));
services.AddScoped<ITestDonorExporter, TestDonorExporter>();
services.AddScoped<ISearchRequester, SearchRequester>();

Expand Down
10 changes: 5 additions & 5 deletions Atlas.MatchPrediction.Test.Validation/Models/HomeworkRequest.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using Atlas.Common.Public.Models.GeneticData.PhenotypeInfo.TransferModels;
using System.Collections.Generic;

namespace Atlas.MatchPrediction.Test.Validation.Models
{
public class HomeworkRequest
{
/// <summary>
/// Each string should have format: "PatientId,DonorId"
/// Path where homework set file should be read from.
/// Make sure to escape the backslashes in the path.
/// </summary>
public IEnumerable<string> PatientDonorPairs { get; set; }
public string InputPath { get; set; }

/// <summary>
/// Path where results should be written to.
Expand All @@ -17,9 +17,9 @@ public class HomeworkRequest
public string ResultsPath { get; set; }

/// <summary>
/// Used to identify the result set.
/// Include the file extension, e.g., "A.csv".
/// </summary>
public string HomeworkSetName { get; set; }
public string SetFileName { get; set; }

/// <summary>
/// Loci set to `true` will be included in the analysis.
Expand Down
8 changes: 8 additions & 0 deletions Atlas.MatchPrediction.Test.Validation/Models/SubjectIdPair.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Atlas.MatchPrediction.Test.Validation.Models
{
internal class SubjectIdPair
{
public string PatientId { get; set; }
public string DonorId { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Atlas.Common.Utils.Extensions;
using Atlas.ManualTesting.Common.Services;
using Atlas.MatchPrediction.Test.Validation.Data.Models.Homework;
using Atlas.MatchPrediction.Test.Validation.Data.Repositories.Homework;
using Atlas.MatchPrediction.Test.Validation.Models;
Expand All @@ -18,25 +20,40 @@ public interface IHomeworkRequestProcessor

internal class HomeworkRequestProcessor : IHomeworkRequestProcessor
{
private const string FileDelimiter = ",";
private readonly IHomeworkSetRepository setRepository;
private readonly IFileReader<SubjectIdPair> fileReader;
private readonly IPatientDonorPairRepository pdpRepository;
private readonly IPatientDonorPairProcessor pdpProcessor;

public HomeworkRequestProcessor(
IHomeworkSetRepository setRepository,
IFileReader<SubjectIdPair> fileReader,
IPatientDonorPairRepository pdpRepository,
IPatientDonorPairProcessor pdpProcessor)
{
this.setRepository = setRepository;
this.fileReader = fileReader;
this.pdpRepository = pdpRepository;
this.pdpProcessor = pdpProcessor;
}

/// <inheritdoc />
public async Task<int> StoreHomeworkRequest(HomeworkRequest request)
{
var setId = await setRepository.Add(request.HomeworkSetName, request.ResultsPath, request.MatchLoci.MatchLociToString());
await pdpRepository.BulkInsert(request.PatientDonorPairs.Select(pdp => MapToDatabaseModel(pdp, setId)).ToList());
var subjectIdPairs = await fileReader.ReadAllLines(
FileDelimiter,
Path.Combine(request.InputPath, request.SetFileName),
hasHeaderRecord: false);

if (subjectIdPairs.IsNullOrEmpty())
{
throw new ArgumentException($"No patient-donor pairs found in file {request.SetFileName}.");
}

var setId = await setRepository.Add(request.SetFileName, request.ResultsPath, request.MatchLoci.MatchLociToString());

await pdpRepository.BulkInsert(subjectIdPairs.Select(ids => MapToDatabaseModel(ids, setId)).ToList());
return setId;
}

Expand Down Expand Up @@ -65,19 +82,12 @@ public async Task StartOrContinueHomeworkRequest(int homeworkSetId)
}
}

private static PatientDonorPair MapToDatabaseModel(string patientDonorPair, int homeworkSetId)
private static PatientDonorPair MapToDatabaseModel(SubjectIdPair ids, int homeworkSetId)
{
var ids = patientDonorPair.Split(',');

if (ids.Length != 2)
{
throw new ArgumentException($"{nameof(patientDonorPair)} must contain exactly two ids separated by a comma; instead found {patientDonorPair}.");
}

return new PatientDonorPair
{
PatientId = ids[0],
DonorId = ids[1],
PatientId = ids.PatientId,
DonorId = ids.DonorId,
HomeworkSet_Id = homeworkSetId
};
}
Expand Down

0 comments on commit 89cec57

Please sign in to comment.