Search in sources :

Example 26 with SequenceFilePair

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair in project irida by phac-nml.

the class SequenceFilePairConcatenator method concatenateFiles.

/**
 * {@inheritDoc}
 */
@Override
public SequenceFilePair concatenateFiles(List<? extends SequencingObject> toConcatenate, String filename) throws ConcatenateException {
    // create the filenames with F/R for the forward and reverse files
    String forwardName = filename + "_R1.fastq";
    String reverseName = filename + "_R2.fastq";
    Path forwardFile;
    Path reverseFile;
    try {
        // create a temp directory for the new files
        Path tempDirectory = Files.createTempDirectory(null);
        forwardFile = tempDirectory.resolve(forwardName);
        reverseFile = tempDirectory.resolve(reverseName);
        // create temp files
        forwardFile = Files.createFile(forwardFile);
        reverseFile = Files.createFile(reverseFile);
    } catch (IOException e) {
        throw new ConcatenateException("Could not create temporary files", e);
    }
    // for each file concatenate the forward and reverse files
    for (SequencingObject f : toConcatenate) {
        SequenceFilePair pair = (SequenceFilePair) f;
        SequenceFile forwardSequenceFile = pair.getForwardSequenceFile();
        SequenceFile reverseSequenceFile = pair.getReverseSequenceFile();
        appendToFile(forwardFile, forwardSequenceFile);
        appendToFile(reverseFile, reverseSequenceFile);
    }
    // create new SequenceFiles
    SequenceFile forward = new SequenceFile(forwardFile);
    SequenceFile reverse = new SequenceFile(reverseFile);
    // create the new pair
    SequenceFilePair sequenceFilePair = new SequenceFilePair(forward, reverse);
    return sequenceFilePair;
}
Also used : Path(java.nio.file.Path) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) IOException(java.io.IOException) ConcatenateException(ca.corefacility.bioinformatics.irida.exceptions.ConcatenateException)

Example 27 with SequenceFilePair

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair in project irida by phac-nml.

the class SequenceFilePairRemoteRepositoryImpl method setRemoteStatus.

/**
 * {@inheritDoc}
 */
@Override
protected <T extends IridaResourceSupport> T setRemoteStatus(T entity, RemoteAPI api) {
    entity = super.setRemoteStatus(entity, api);
    SequenceFilePair pair = (SequenceFilePair) entity;
    pair.getFiles().forEach(f -> super.setRemoteStatus(f, api));
    return entity;
}
Also used : SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)

Example 28 with SequenceFilePair

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair in project irida by phac-nml.

the class RESTSampleSequenceFilesController method addNewSequenceFilePairToSample.

/**
 * Add a pair of {@link SequenceFile}s to a {@link Sample}
 *
 * @param sampleId
 *            The {@link Sample} id to add to
 * @param file1
 *            The first multipart file
 * @param fileResource1
 *            The metadata for the first file
 * @param file2
 *            The second multipart file
 * @param fileResource2
 *            the metadata for the second file
 * @param response
 *            a reference to the servlet response.
 * @return Response containing the locations for the created files
 * @throws IOException
 *             if we can't write the files to disk
 */
@RequestMapping(value = "/api/samples/{sampleId}/pairs", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ModelMap addNewSequenceFilePairToSample(@PathVariable Long sampleId, @RequestPart("file1") MultipartFile file1, @RequestPart(value = "parameters1") SequenceFileResource fileResource1, @RequestPart("file2") MultipartFile file2, @RequestPart(value = "parameters2") SequenceFileResource fileResource2, HttpServletResponse response) throws IOException {
    logger.debug("Adding pair of sequence files to sample " + sampleId);
    logger.trace("First uploaded file size: " + file1.getSize() + " bytes");
    logger.trace("Second uploaded file size: " + file2.getSize() + " bytes");
    ModelMap modelMap = new ModelMap();
    // confirm that a relationship exists between the project and the sample
    Sample sample = sampleService.read(sampleId);
    logger.trace("Read sample " + sampleId);
    // create temp files
    Path temp1 = Files.createTempDirectory(null);
    Path target1 = temp1.resolve(file1.getOriginalFilename());
    Path temp2 = Files.createTempDirectory(null);
    Path target2 = temp2.resolve(file2.getOriginalFilename());
    // transfer the files to temp directories
    file1.transferTo(target1.toFile());
    file2.transferTo(target2.toFile());
    // create the model objects
    SequenceFile sf1 = fileResource1.getResource();
    SequenceFile sf2 = fileResource2.getResource();
    sf1.setFile(target1);
    sf2.setFile(target2);
    // get the sequencing run
    SequencingRun sequencingRun = null;
    if (!Objects.equal(fileResource1.getMiseqRunId(), fileResource2.getMiseqRunId())) {
        throw new IllegalArgumentException("Cannot upload a pair of files from different sequencing runs");
    }
    Long runId = fileResource1.getMiseqRunId();
    SequenceFilePair sequenceFilePair = new SequenceFilePair(sf1, sf2);
    if (runId != null) {
        sequencingRun = miseqRunService.read(runId);
        sequenceFilePair.setSequencingRun(sequencingRun);
        logger.trace("Added sequencing run to files" + runId);
    }
    // add the files and join
    SampleSequencingObjectJoin createSequencingObjectInSample = sequencingObjectService.createSequencingObjectInSample(sequenceFilePair, sample);
    // clean up the temporary files.
    Files.deleteIfExists(target1);
    Files.deleteIfExists(temp1);
    Files.deleteIfExists(target2);
    Files.deleteIfExists(temp2);
    logger.trace("Deleted temp files");
    SequencingObject sequencingObject = createSequencingObjectInSample.getObject();
    sequencingObject = addSequencingObjectLinks(sequencingObject, sampleId);
    sequencingObject.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).getSampleSequenceFiles(sampleId)).withRel(REL_SAMPLE_SEQUENCE_FILES));
    // add location header
    response.addHeader(HttpHeaders.LOCATION, sequencingObject.getLink("self").getHref());
    // set the response status.
    response.setStatus(HttpStatus.CREATED.value());
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, sequencingObject);
    // respond to the client
    return modelMap;
}
Also used : Path(java.nio.file.Path) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SequencingRun(ca.corefacility.bioinformatics.irida.model.run.SequencingRun) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with SequenceFilePair

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair in project irida by phac-nml.

the class RESTSampleSequenceFilesController method addSequencingObjectLinks.

/**
 * Add the links for a {@link SequencingObject} to its sample, self, to each
 * individual {@link SequenceFile}
 *
 * @param sequencingObject {@link SequencingObject} to enhance
 * @param sampleId         ID of the {@link Sample} for the object
 * @param <T>              The subclass of {@link SequencingObject} being enhanced by this method
 * @return the enhanced {@link SequencingObject}
 */
@SuppressWarnings("unchecked")
public static <T extends SequencingObject> T addSequencingObjectLinks(T sequencingObject, Long sampleId) {
    String objectType = objectLabels.get(sequencingObject.getClass());
    // link to self
    sequencingObject.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).readSequencingObject(sampleId, objectType, sequencingObject.getId())).withSelfRel());
    // link to the sample
    sequencingObject.add(linkTo(methodOn(RESTProjectSamplesController.class).getSample(sampleId)).withRel(RESTSampleSequenceFilesController.REL_SAMPLE));
    // link to the individual files
    for (SequenceFile file : sequencingObject.getFiles()) {
        file.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).readSequenceFileForSequencingObject(sampleId, objectType, sequencingObject.getId(), file.getId())).withSelfRel());
    }
    AnalysisSubmission automatedAssembly = sequencingObject.getAutomatedAssembly();
    if (automatedAssembly != null) {
        sequencingObject.add(linkTo(methodOn(RESTAnalysisSubmissionController.class).getResource(automatedAssembly.getId())).withRel(REL_AUTOMATED_ASSEMBLY));
    }
    AnalysisSubmission sistrTyping = sequencingObject.getSistrTyping();
    if (sistrTyping != null) {
        sequencingObject.add(linkTo(methodOn(RESTAnalysisSubmissionController.class).getResource(sistrTyping.getId())).withRel(REL_SISTR_TYPING));
    }
    // if it's a pair, add forward/reverse links
    if (sequencingObject instanceof SequenceFilePair) {
        sequencingObject = (T) addSequenceFilePairLinks((SequenceFilePair) sequencingObject, sampleId);
    }
    return sequencingObject;
}
Also used : SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)

Example 30 with SequenceFilePair

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair in project irida by phac-nml.

the class SNVPhylAnalysisIT method testSNVPhylSuccess.

/**
 * Tests out successfully executing the SNVPhyl pipeline.
 *
 * @throws Exception
 */
@Test
@WithMockUser(username = "aaron", roles = "ADMIN")
public void testSNVPhylSuccess() throws Exception {
    SequenceFilePair sequenceFilePairA = databaseSetupGalaxyITService.setupSampleSequenceFileInDatabase(1L, sequenceFilePathsA1List, sequenceFilePathsA2List).get(0);
    SequenceFilePair sequenceFilePairB = databaseSetupGalaxyITService.setupSampleSequenceFileInDatabase(2L, sequenceFilePathsB1List, sequenceFilePathsB2List).get(0);
    SequenceFilePair sequenceFilePairC = databaseSetupGalaxyITService.setupSampleSequenceFileInDatabase(3L, sequenceFilePathsC1List, sequenceFilePathsC2List).get(0);
    Map<String, String> parameters = ImmutableMap.of("snv-abundance-ratio", "0.75", "minimum-read-coverage", "2", "filter-density-threshold", "2", "filter-density-window-size", "3");
    waitForFilesToSettle(sequenceFilePairA, sequenceFilePairB, sequenceFilePairC);
    AnalysisSubmission submission = databaseSetupGalaxyITService.setupPairSubmissionInDatabase(Sets.newHashSet(sequenceFilePairA, sequenceFilePairB, sequenceFilePairC), referenceFilePath, parameters, snvPhylWorkflow.getWorkflowIdentifier());
    completeSubmittedAnalyses(submission.getId());
    submission = analysisSubmissionRepository.findOne(submission.getId());
    assertEquals("analysis state should be completed.", AnalysisState.COMPLETED, submission.getAnalysisState());
    Analysis analysisPhylogenomics = submission.getAnalysis();
    assertEquals("Should have generated a phylogenomics pipeline analysis type.", AnalysisType.PHYLOGENOMICS, analysisPhylogenomics.getAnalysisType());
    assertEquals("the phylogenomics pipeline should have 8 output files.", 8, analysisPhylogenomics.getAnalysisOutputFiles().size());
    @SuppressWarnings("resource") String matrixContent = new Scanner(analysisPhylogenomics.getAnalysisOutputFile(MATRIX_KEY).getFile().toFile()).useDelimiter("\\Z").next();
    assertTrue("snpMatrix should be the same but is \"" + matrixContent + "\"", com.google.common.io.Files.equal(outputSnvMatrix1.toFile(), analysisPhylogenomics.getAnalysisOutputFile(MATRIX_KEY).getFile().toFile()));
    assertNotNull("file should have tool provenance attached.", analysisPhylogenomics.getAnalysisOutputFile(MATRIX_KEY).getCreatedByTool());
    @SuppressWarnings("resource") String snpTableContent = new Scanner(analysisPhylogenomics.getAnalysisOutputFile(TABLE_KEY).getFile().toFile()).useDelimiter("\\Z").next();
    assertTrue("snpTable should be the same but is \"" + snpTableContent + "\"", com.google.common.io.Files.equal(outputSnvTable1.toFile(), analysisPhylogenomics.getAnalysisOutputFile(TABLE_KEY).getFile().toFile()));
    assertNotNull("file should have tool provenance attached.", analysisPhylogenomics.getAnalysisOutputFile(TABLE_KEY).getCreatedByTool());
    @SuppressWarnings("resource") String vcf2coreContent = new Scanner(analysisPhylogenomics.getAnalysisOutputFile(CORE_KEY).getFile().toFile()).useDelimiter("\\Z").next();
    assertTrue("vcf2core should be the same but is \"" + vcf2coreContent + "\"", com.google.common.io.Files.equal(vcf2core1.toFile(), analysisPhylogenomics.getAnalysisOutputFile(CORE_KEY).getFile().toFile()));
    assertNotNull("file should have tool provenance attached.", analysisPhylogenomics.getAnalysisOutputFile(CORE_KEY).getCreatedByTool());
    // only check size of mapping quality file due to samples output in random order
    assertTrue("the mapping quality file should not be empty.", Files.size(analysisPhylogenomics.getAnalysisOutputFile(QUALITY_KEY).getFile()) > 0);
    @SuppressWarnings("resource") String filterStatsContent = new Scanner(analysisPhylogenomics.getAnalysisOutputFile(STATS_KEY).getFile().toFile()).useDelimiter("\\Z").next();
    assertTrue("filterStats should be the same but is \"" + filterStatsContent + "\"", com.google.common.io.Files.equal(filterStats1.toFile(), analysisPhylogenomics.getAnalysisOutputFile(STATS_KEY).getFile().toFile()));
    assertNotNull("file should have tool provenance attached.", analysisPhylogenomics.getAnalysisOutputFile(STATS_KEY).getCreatedByTool());
    @SuppressWarnings("resource") String snvAlignContent = new Scanner(analysisPhylogenomics.getAnalysisOutputFile(ALIGN_KEY).getFile().toFile()).useDelimiter("\\Z").next();
    assertTrue("snvAlign should be the same but is \"" + snvAlignContent + "\"", com.google.common.io.Files.equal(snvAlign1.toFile(), analysisPhylogenomics.getAnalysisOutputFile(ALIGN_KEY).getFile().toFile()));
    assertNotNull("file should have tool provenance attached.", analysisPhylogenomics.getAnalysisOutputFile(ALIGN_KEY).getCreatedByTool());
    // only test to make sure the files have a valid size since PhyML uses a
    // random seed to generate the tree (and so changes results)
    assertTrue("the phylogenetic tree file should not be empty.", Files.size(analysisPhylogenomics.getAnalysisOutputFile(TREE_KEY).getFile()) > 0);
    assertTrue("the phylogenetic tree stats file should not be empty.", Files.size(analysisPhylogenomics.getAnalysisOutputFile(TREE_KEY).getFile()) > 0);
    // try to follow the phylogenomics provenance all the way back to the
    // upload tools
    final List<ToolExecution> toolsToVisit = Lists.newArrayList(analysisPhylogenomics.getAnalysisOutputFile(TREE_KEY).getCreatedByTool());
    assertFalse("file should have tool provenance attached.", toolsToVisit.isEmpty());
    boolean foundReadsInputTool = false;
    boolean foundReferenceInputTool = false;
    // one where you upload the reads.
    while (!toolsToVisit.isEmpty()) {
        final ToolExecution ex = toolsToVisit.remove(0);
        toolsToVisit.addAll(ex.getPreviousSteps());
        if (ex.isInputTool()) {
            final Map<String, String> params = ex.getExecutionTimeParameters();
            logger.debug("Input tool has " + params);
            foundReferenceInputTool |= params.containsKey("files.NAME") && params.get("files.NAME").contains("reference") && params.get("file_type").contains("fasta");
            foundReadsInputTool |= params.get("file_type").contains("fastq");
        }
    }
    assertTrue("Should have found both reads and reference input tools.", foundReadsInputTool && foundReferenceInputTool);
}
Also used : SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) Scanner(java.util.Scanner) ToolExecution(ca.corefacility.bioinformatics.irida.model.workflow.analysis.ToolExecution) Analysis(ca.corefacility.bioinformatics.irida.model.workflow.analysis.Analysis) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Aggregations

SequenceFilePair (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)59 Test (org.junit.Test)33 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)28 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)23 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)22 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)19 IridaWorkflow (ca.corefacility.bioinformatics.irida.model.workflow.IridaWorkflow)13 Project (ca.corefacility.bioinformatics.irida.model.project.Project)12 Analysis (ca.corefacility.bioinformatics.irida.model.workflow.analysis.Analysis)12 WithMockUser (org.springframework.security.test.context.support.WithMockUser)12 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)11 Path (java.nio.file.Path)11 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)9 History (com.github.jmchilton.blend4j.galaxy.beans.History)8 User (ca.corefacility.bioinformatics.irida.model.user.User)7 HistoriesClient (com.github.jmchilton.blend4j.galaxy.HistoriesClient)7 ArrayList (java.util.ArrayList)7 IridaWorkflowNotFoundException (ca.corefacility.bioinformatics.irida.exceptions.IridaWorkflowNotFoundException)5 ReferenceFile (ca.corefacility.bioinformatics.irida.model.project.ReferenceFile)5 ImmutableMap (com.google.common.collect.ImmutableMap)5