Search in sources :

Example 1 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class ProjectSamplesController method downloadSamples.

/**
 * Download a set of sequence files from selected samples within a project
 *
 * @param projectId Id for a {@link Project}
 * @param ids       List of ids ofr {@link Sample} within the project
 * @param response  {@link HttpServletResponse}
 * @throws IOException if we fail to read a file from the filesystem.
 */
@RequestMapping(value = "/projects/{projectId}/download/files")
public void downloadSamples(@PathVariable Long projectId, @RequestParam(value = "ids[]") List<Long> ids, HttpServletResponse response) throws IOException {
    Project project = projectService.read(projectId);
    List<Sample> samples = (List<Sample>) sampleService.readMultiple(ids);
    // Add the appropriate headers
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + project.getName() + ".zip\"");
    response.setHeader("Transfer-Encoding", "chunked");
    // storing used file names to ensure we don't have a conflict
    Set<String> usedFileNames = new HashSet<>();
    try (ZipOutputStream outputStream = new ZipOutputStream(response.getOutputStream())) {
        for (Sample sample : samples) {
            Collection<SampleSequencingObjectJoin> sequencingObjectsForSample = sequencingObjectService.getSequencingObjectsForSample(sample);
            for (SampleSequencingObjectJoin join : sequencingObjectsForSample) {
                for (SequenceFile file : join.getObject().getFiles()) {
                    Path path = file.getFile();
                    String fileName = project.getName() + "/" + sample.getSampleName() + "/" + path.getFileName().toString();
                    if (usedFileNames.contains(fileName)) {
                        fileName = handleDuplicate(fileName, usedFileNames);
                    }
                    final ZipEntry entry = new ZipEntry(fileName);
                    // set the file creation time on the zip entry to be
                    // whatever the creation time is on the filesystem
                    final BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
                    entry.setCreationTime(attr.creationTime());
                    entry.setLastModifiedTime(attr.creationTime());
                    outputStream.putNextEntry(entry);
                    usedFileNames.add(fileName);
                    Files.copy(path, outputStream);
                    outputStream.closeEntry();
                }
            }
        }
        outputStream.finish();
    } catch (IOException e) {
        // this generally means that the user has cancelled the download
        // from their web browser; we can safely ignore this
        logger.debug("This *probably* means that the user cancelled the download, " + "but it might be something else, see the stack trace below for more information.", e);
    } catch (Exception e) {
        logger.error("Download failed...", e);
    } finally {
        // close the response outputStream so that we're not leaking
        // streams.
        response.getOutputStream().close();
    }
}
Also used : Path(java.nio.file.Path) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) EntityExistsException(ca.corefacility.bioinformatics.irida.exceptions.EntityExistsException) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) IOException(java.io.IOException) AccessDeniedException(org.springframework.security.access.AccessDeniedException) ConstraintViolationException(javax.validation.ConstraintViolationException) Project(ca.corefacility.bioinformatics.irida.model.project.Project) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) ZipOutputStream(java.util.zip.ZipOutputStream) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes)

Example 2 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class SamplesController method getConcatenatePage.

/**
 * Get the page for concatenating {@link SequencingObject}s in a
 * {@link Sample}
 *
 * @param sampleId
 *            the {@link Sample} to get files for
 * @param model
 *            model for the view
 * @return name of the files concatenate page
 */
@RequestMapping(value = { "/samples/{sampleId}/concatenate", "/projects/{projectId}/samples/{sampleId}/concatenate" }, method = RequestMethod.GET)
public String getConcatenatePage(@PathVariable Long sampleId, Model model) {
    Sample sample = sampleService.read(sampleId);
    model.addAttribute("sampleId", sampleId);
    Collection<SampleSequencingObjectJoin> filePairJoins = sequencingObjectService.getSequencesForSampleOfType(sample, SequenceFilePair.class);
    Collection<SampleSequencingObjectJoin> singleFileJoins = sequencingObjectService.getSequencesForSampleOfType(sample, SingleEndSequenceFile.class);
    List<SequencingObject> filePairs = filePairJoins.stream().map(SampleSequencingObjectJoin::getObject).collect(Collectors.toList());
    // SequenceFile
    model.addAttribute("paired_end", filePairs);
    model.addAttribute("single_end", singleFileJoins);
    model.addAttribute(MODEL_ATTR_SAMPLE, sample);
    model.addAttribute(MODEL_ATTR_CAN_MANAGE_SAMPLE, isProjectManagerForSample(sample));
    model.addAttribute(MODEL_ATTR_ACTIVE_NAV, ACTIVE_NAV_FILES);
    return FILES_CONCATENATE_PAGE;
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)

Example 3 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class CartController method getSequenceFileList.

/**
 * Get {@link SequenceFile}s as a List from a {@link Sample} for JSON serialization
 *
 * @param samples
 *            The {@link Sample} set
 * @return A List<Map<String,Object>> containing the relevant SequenceFile
 *         information
 */
private List<Map<String, Object>> getSequenceFileList(Sample sample) {
    Collection<SampleSequencingObjectJoin> sequencingObjectsForSample = sequencingObjectService.getSequencingObjectsForSample(sample);
    List<Map<String, Object>> sequenceFiles = new ArrayList<>();
    for (SampleSequencingObjectJoin join : sequencingObjectsForSample) {
        for (SequenceFile seq : join.getObject().getFiles()) {
            String objectType = RESTSampleSequenceFilesController.objectLabels.get(join.getObject().getClass());
            String seqFileLoc = linkTo(methodOn(RESTSampleSequenceFilesController.class).readSequenceFileForSequencingObject(sample.getId(), objectType, join.getObject().getId(), seq.getId())).withSelfRel().getHref();
            Map<String, Object> seqMap = ImmutableMap.of("selfRef", seqFileLoc);
            sequenceFiles.add(seqMap);
        }
    }
    return sequenceFiles;
}
Also used : SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)

Example 4 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class SampleServiceImplTest method testRemoveSequenceFileFromSample.

@Test
public void testRemoveSequenceFileFromSample() {
    Sample s = new Sample();
    s.setId(1111L);
    SequenceFile sf = new SequenceFile();
    sf.setId(2222L);
    SingleEndSequenceFile obj = new SingleEndSequenceFile(sf);
    obj.setId(2L);
    SampleSequencingObjectJoin join = new SampleSequencingObjectJoin(s, obj);
    when(ssoRepository.readObjectForSample(s, obj.getId())).thenReturn(join);
    sampleService.removeSequencingObjectFromSample(s, obj);
    verify(ssoRepository).delete(join);
}
Also used : SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) Test(org.junit.Test)

Example 5 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class SampleServiceImplTest method testGetTotalBasesForSampleFailMultipleFastQC.

/**
 * Tests out failing to get the total bases from a sample with one sequence
 * file due to too many FastQC
 *
 * @throws SequenceFileAnalysisException
 */
@Test(expected = SequenceFileAnalysisException.class)
public void testGetTotalBasesForSampleFailMultipleFastQC() throws SequenceFileAnalysisException {
    Sample s1 = new Sample();
    s1.setId(1L);
    SequenceFile sf1 = new SequenceFile();
    sf1.setId(2222L);
    SampleSequencingObjectJoin join = new SampleSequencingObjectJoin(s1, new SingleEndSequenceFile(sf1));
    when(ssoRepository.getSequencesForSample(s1)).thenReturn(Arrays.asList(join));
    sampleService.getTotalBasesForSample(s1);
}
Also used : SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) Test(org.junit.Test)

Aggregations

SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)54 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)45 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)30 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)30 Test (org.junit.Test)29 Project (ca.corefacility.bioinformatics.irida.model.project.Project)21 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)16 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)14 SequenceFilePair (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)11 Path (java.nio.file.Path)9 Transactional (org.springframework.transaction.annotation.Transactional)9 ModelMap (org.springframework.ui.ModelMap)9 ArrayList (java.util.ArrayList)8 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)7 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)5 DataAddedToSampleProjectEvent (ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent)4 User (ca.corefacility.bioinformatics.irida.model.user.User)4