Skip to content

Commit

Permalink
mroe
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sikina committed Aug 18, 2024
1 parent b0ad0a2 commit 4c61647
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import edu.harvard.hms.dbmi.avillach.hpds.processing.AbstractProcessor;
import edu.harvard.hms.dbmi.avillach.hpds.processing.AsyncResult;
import edu.harvard.hms.dbmi.avillach.hpds.processing.HpdsProcessor;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
Expand Down Expand Up @@ -69,22 +71,32 @@ public long distance() {

@Override
public void runQuery(Query query, AsyncResult asyncResult) {
Set<Integer> recordedJourneys = new HashSet<>();
for (String concept : query.getAnyRecordOf()) {
LOG.info("Creating user journeys for concept {}", concept);
Optional<PhenoCube<?>> maybeCube = abstractProcessor.nullableGetCube(concept);
if (maybeCube.isEmpty()) {
continue;

}

public void writeJourney(Query query) {
try {
String tmpPath = File.createTempFile(System.nanoTime() + "journey", ".tsv").getAbsolutePath();
Set<Integer> recordedJourneys = new HashSet<>();
for (String concept : query.getAnyRecordOf()) {
LOG.info("Creating user journeys for concept {}", concept);
Optional<PhenoCube<?>> maybeCube = abstractProcessor.nullableGetCube(concept);
if (maybeCube.isEmpty()) {
continue;
}
maybeCube.get().keyBasedIndex().stream()
.filter(Predicate.not(recordedJourneys::contains))
.map(id -> new PatientJourney(concept, id))
.map(this::updateJourney)
.sorted((a, b) -> -Long.compare(b.distance(), a.distance()))
.peek(j -> recordedJourneys.add(j.patient))
.limit(100)
.forEach(journey -> write(tmpPath, journey));
LOG.info("Done creating user journeys for concept {} to {}", concept, tmpPath);
}
maybeCube.get().keyBasedIndex().stream()
.filter(Predicate.not(recordedJourneys::contains))
.map(id -> new PatientJourney(concept, id))
.map(this::updateJourney)
.sorted((a, b) -> -Long.compare(b.distance(), a.distance()))
.peek(j -> recordedJourneys.add(j.patient))
.limit(100)
.forEach(journey -> write(asyncResult.getTempFilePath(), journey));
LOG.info("Done creating user journeys for concept {} to {}", concept, asyncResult.getTempFilePath());
} catch (IOException e) {
LOG.error("Can't create temp file", e);
return;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import javax.ws.rs.core.Response.Status;

import edu.harvard.hms.dbmi.avillach.hpds.data.query.ResultType;
import edu.harvard.hms.dbmi.avillach.hpds.processing.timeseries.ObservationDistanceProcessor;
import edu.harvard.hms.dbmi.avillach.hpds.service.filesharing.FileSharingService;
import edu.harvard.hms.dbmi.avillach.hpds.service.util.Paginator;
import edu.harvard.hms.dbmi.avillach.hpds.service.util.QueryDecorator;
Expand Down Expand Up @@ -46,7 +47,8 @@ public class PicSureService implements IResourceRS {
@Autowired
public PicSureService(QueryService queryService, TimelineProcessor timelineProcessor, CountProcessor countProcessor,
VariantListProcessor variantListProcessor, AbstractProcessor abstractProcessor,
Paginator paginator, FileSharingService fileSystemService, QueryDecorator queryDecorator
Paginator paginator, FileSharingService fileSystemService, QueryDecorator queryDecorator,
ObservationDistanceProcessor observationDistanceProcessor
) {
this.queryService = queryService;
this.timelineProcessor = timelineProcessor;
Expand All @@ -56,6 +58,7 @@ public PicSureService(QueryService queryService, TimelineProcessor timelineProce
this.paginator = paginator;
this.fileSystemService = fileSystemService;
this.queryDecorator = queryDecorator;
this.observationDistanceProcessor = observationDistanceProcessor;
Crypto.loadDefaultKey();
}

Expand All @@ -78,6 +81,7 @@ public PicSureService(QueryService queryService, TimelineProcessor timelineProce
private final FileSharingService fileSystemService;

private final QueryDecorator queryDecorator;
private final ObservationDistanceProcessor observationDistanceProcessor;

private static final String QUERY_METADATA_FIELD = "queryMetadata";
private static final int RESPONSE_CACHE_SIZE = 50;
Expand Down Expand Up @@ -275,6 +279,11 @@ private Optional<String> roundTripUUID(String uuid) {
public Response writeQueryResult(
@RequestBody() Query query, @PathParam("dataType") String datatype
) {
log.info("Writing {}", datatype);
if ("journey".equals(datatype)) {
observationDistanceProcessor.writeJourney(query);
return Response.status(400, "good luck!").build();
}
if (roundTripUUID(query.getPicSureId()).map(id -> !id.equalsIgnoreCase(query.getPicSureId())).orElse(false)) {
return Response
.status(400, "The query pic-sure ID is not a UUID")
Expand Down

0 comments on commit 4c61647

Please sign in to comment.