Search in sources :

Example 1 with SequenceFile

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile 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 SequenceFile

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

the class SamplesController method createSequenceFile.

/**
 * Private method to move the sequence file into the correct directory and
 * create the {@link SequenceFile} object.
 *
 * @param file
 *            {@link MultipartFile} sequence file uploaded.
 *
 * @return {@link SequenceFile}
 * @throws IOException
 *             Exception thrown if there is an error handling the file.
 */
private SequenceFile createSequenceFile(MultipartFile file) throws IOException {
    Path temp = Files.createTempDirectory(null);
    Path target = temp.resolve(file.getOriginalFilename());
    file.transferTo(target.toFile());
    return new SequenceFile(target);
}
Also used : Path(java.nio.file.Path) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)

Example 3 with SequenceFile

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

the class SamplesController method createSequenceFilePairsInSample.

/**
 * Create {@link SequenceFile}'s then add them as {@link SequenceFilePair}
 * to a {@link Sample}
 *
 * @param pair
 *            {@link List} of {@link MultipartFile}
 * @param sample
 *            {@link Sample} to add the pair to.
 * @throws IOException
 */
private void createSequenceFilePairsInSample(List<MultipartFile> pair, Sample sample) throws IOException {
    SequenceFile firstFile = createSequenceFile(pair.get(0));
    SequenceFile secondFile = createSequenceFile(pair.get(1));
    sequencingObjectService.createSequencingObjectInSample(new SequenceFilePair(firstFile, secondFile), sample);
}
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)

Example 4 with SequenceFile

use of ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile 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 5 with SequenceFile

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

the class SequenceFileController method createDefaultPageInfo.

/**
 * Populates the model with the default information for a file.
 *
 * @param sequenceFileId
 *            Id for the sequence file.
 * @param model
 *            {@link Model}
 */
private void createDefaultPageInfo(Long sequencingObjectId, Long sequenceFileId, Model model) {
    SequencingObject seqObject = sequencingObjectService.read(sequencingObjectId);
    SequenceFile file = seqObject.getFileWithId(sequenceFileId);
    AnalysisFastQC fastQC = analysisService.getFastQCAnalysisForSequenceFile(seqObject, file.getId());
    model.addAttribute("sequencingObject", seqObject);
    model.addAttribute("file", file);
    model.addAttribute("created", dateFormatter.print(file.getCreatedDate(), LocaleContextHolder.getLocale()));
    model.addAttribute("fastQC", fastQC);
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) AnalysisFastQC(ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisFastQC)

Aggregations

SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)111 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)84 Test (org.junit.Test)61 Path (java.nio.file.Path)50 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)39 SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)31 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)25 SequenceFilePair (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)20 Project (ca.corefacility.bioinformatics.irida.model.project.Project)15 AnalysisFastQC (ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisFastQC)15 WithMockUser (org.springframework.security.test.context.support.WithMockUser)13 IOException (java.io.IOException)11 SequencingRun (ca.corefacility.bioinformatics.irida.model.run.SequencingRun)9 ArrayList (java.util.ArrayList)9 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)8 GZIPOutputStream (java.util.zip.GZIPOutputStream)8 Link (org.springframework.hateoas.Link)8 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)7 OutputStream (java.io.OutputStream)7 Before (org.junit.Before)7