-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Luke Sikina
committed
Nov 10, 2023
1 parent
47b827b
commit ca8b278
Showing
6 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
.../main/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/FileSharingService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package edu.harvard.hms.dbmi.avillach.hpds.service.filesharing; | ||
|
||
import edu.harvard.hms.dbmi.avillach.hpds.data.query.Query; | ||
import edu.harvard.hms.dbmi.avillach.hpds.data.query.ResultType; | ||
import edu.harvard.hms.dbmi.avillach.hpds.processing.AsyncResult; | ||
import edu.harvard.hms.dbmi.avillach.hpds.service.QueryService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
|
||
/** | ||
* Used for sharing data. Given a query, this service will write | ||
* phenotypic and genomic data into a directory | ||
*/ | ||
@Service | ||
public class FileSharingService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(FileSharingService.class); | ||
|
||
@Autowired | ||
private QueryService queryService; | ||
|
||
@Autowired | ||
private FileSystemService fileWriter; | ||
|
||
@Autowired | ||
private UploadStatusRepository repository; | ||
|
||
public boolean createPhenotypicData(Query phenotypicQuery) { | ||
AsyncResult result = queryService.getResultFor(phenotypicQuery.getId()); | ||
repository.updatePhenotypicStatus(phenotypicQuery.getId(), UploadStatus.COMPLETE); | ||
return fileWriter.writeResultToFile("phenotypic_data.tsv", phenotypicQuery.getId(), result); | ||
} | ||
|
||
public boolean createGenomicData(Query phenotypicQuery) { | ||
Query genomicQuery = createGenomicDataQuery(phenotypicQuery); | ||
repository.updateGenomicStatus(genomicQuery.getId(), UploadStatus.STARTED); | ||
try { | ||
queryService.runQuery(genomicQuery); | ||
} catch (ClassNotFoundException | IOException e) { | ||
LOG.error("Error running genomic query", e); | ||
repository.updateGenomicStatus(genomicQuery.getId(), UploadStatus.ERROR); | ||
return false; | ||
} | ||
|
||
AsyncResult status = null; | ||
while ( | ||
status == null || // wait for the genomic query to complete | ||
status.status.equals(AsyncResult.Status.PENDING) || | ||
status.status.equals(AsyncResult.Status.RUNNING) | ||
) { | ||
try { | ||
Thread.sleep(Duration.of(30, ChronoUnit.SECONDS)); | ||
} catch (InterruptedException e) { | ||
LOG.error("Error waiting for status: ", e); | ||
} | ||
status = queryService.getStatusFor(genomicQuery.getId()); | ||
} | ||
if (status.status != AsyncResult.Status.SUCCESS) { | ||
LOG.error("Error uploading genomic data"); | ||
repository.updateGenomicStatus(phenotypicQuery.getId(), UploadStatus.ERROR); | ||
return false; | ||
} | ||
|
||
AsyncResult result = queryService.getResultFor(genomicQuery.getId()); | ||
repository.updateGenomicStatus(genomicQuery.getId(), UploadStatus.COMPLETE); | ||
return fileWriter.writeResultToFile("genomic_data.tsv", phenotypicQuery.getId(), result); | ||
} | ||
|
||
private Query createGenomicDataQuery(Query query) { | ||
Query clone = new Query(query); | ||
clone.setExpectedResultType(ResultType.VCF_EXCERPT); | ||
// I need these queries to be unique, but I don't have access to the UUID gen logic in PIC-SURE from here | ||
// That said, I don't have to abide by UUID restrictions, since this ID isn't getting written to the picsure db | ||
// So appending _genomic to the UUID should be fine. | ||
clone.setId(clone.getId() + "_genomic"); | ||
return clone; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...rc/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/FileSystemConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package edu.harvard.hms.dbmi.avillach.hpds.service.filesharing; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.nio.file.Path; | ||
|
||
@Configuration | ||
public class FileSystemConfig { | ||
@Value("${file_sharing_root:/gic_query_results/}") | ||
private String fileSharingRootDir; | ||
|
||
@Value("${enable_file_sharing:false}") | ||
private boolean enableFileSharing; | ||
|
||
@Bean() | ||
Path sharingRoot() { | ||
if (!enableFileSharing) { | ||
return Path.of("/dev/null"); | ||
} | ||
|
||
Path path = Path.of(fileSharingRootDir); | ||
if (!path.toFile().exists()) { | ||
throw new RuntimeException(fileSharingRootDir + " DNE."); | ||
} | ||
|
||
return path; | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...c/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/FileSystemService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package edu.harvard.hms.dbmi.avillach.hpds.service.filesharing; | ||
|
||
import edu.harvard.hms.dbmi.avillach.hpds.processing.AsyncResult; | ||
import org.apache.commons.io.IOUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
@Service | ||
public class FileSystemService { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(FileSystemService.class); | ||
|
||
@Autowired | ||
private Path sharingRoot; | ||
|
||
@Value("${enable_file_sharing:false}") | ||
private boolean enableFileSharing; | ||
|
||
public boolean writeResultToFile(String fileName, String directory, AsyncResult result) { | ||
if (!enableFileSharing) { | ||
LOG.warn("Attempted to write query result to file while file sharing is disabled. No-op."); | ||
return false; | ||
} | ||
|
||
Path dirPath = Path.of(sharingRoot.toString(), directory); | ||
Path filePath = Path.of(sharingRoot.toString(), directory, fileName); | ||
|
||
try { | ||
LOG.info("Writing query {} to file: {}", result.id, filePath); | ||
Files.createDirectory(dirPath); | ||
result.stream.open(); | ||
return Files.copy(result.stream, filePath) > 0; | ||
} catch (IOException e) { | ||
LOG.error("Error writing result.", e); | ||
return false; | ||
} finally { | ||
IOUtils.closeQuietly(result.stream); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
...ce/src/main/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/UploadStatus.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package edu.harvard.hms.dbmi.avillach.hpds.service.filesharing; | ||
|
||
public enum UploadStatus { | ||
STARTED, COMPLETE, ERROR, UNKNOWN; | ||
} |
14 changes: 14 additions & 0 deletions
14
...n/java/edu/harvard/hms/dbmi/avillach/hpds/service/filesharing/UploadStatusRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package edu.harvard.hms.dbmi.avillach.hpds.service.filesharing; | ||
|
||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class UploadStatusRepository { | ||
public void updatePhenotypicStatus(String queryId, UploadStatus status) { | ||
|
||
} | ||
|
||
public void updateGenomicStatus(String queryId, UploadStatus status) { | ||
|
||
} | ||
} |