Search in sources :

Example 96 with SequenceFile

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

the class ExportUploadService method uploadSubmission.

/**
 * Upload an {@link NcbiExportSubmission}'s files and submission xml to the
 * configured ftp site
 *
 * @param submission
 *            The {@link NcbiExportSubmission} to upload
 * @param xml
 *            The submission xml to upload
 * @return true/false if upload was successful
 * @throws UploadException
 *             if the upload failed
 */
public NcbiExportSubmission uploadSubmission(NcbiExportSubmission submission, String xml) throws UploadException {
    FTPClient client = null;
    try {
        client = getFtpClient();
        // create submission directory name
        String directoryName = submission.getId().toString() + "-" + new Date().getTime();
        // cd to submission base directory
        if (!client.changeWorkingDirectory(baseDirectory)) {
            throw new UploadException("Couldn't change to base directory " + baseDirectory + " : " + client.getReplyString());
        }
        // create new submission directory
        if (!client.makeDirectory(directoryName)) {
            throw new UploadException("Couldn't create new upload directory " + directoryName + " : " + client.getReplyString());
        }
        // cd to submission directory
        if (!client.changeWorkingDirectory(directoryName)) {
            throw new UploadException("Couldn't change to upload directory " + directoryName + " : " + client.getReplyString());
        }
        // set the directory saved
        String directoryPath = baseDirectory + "/" + directoryName;
        submission.setDirectoryPath(directoryPath);
        // upload submission.xml file
        uploadString(client, "submission.xml", xml);
        // upload biosample files
        for (NcbiBioSampleFiles bsFile : submission.getBioSampleFiles()) {
            // upload single end files
            for (SingleEndSequenceFile file : bsFile.getFiles()) {
                // Just using file IDs as the basename for uploaded files to
                // avoid accidentally sending sensitive sample names to NCBI
                String filename = file.getSequenceFile().getId() + ".fastq";
                uploadPath(client, filename, file.getSequenceFile().getFile());
            }
            // upload paired end files
            for (SequenceFilePair pair : bsFile.getPairs()) {
                // upload forward
                SequenceFile file = pair.getForwardSequenceFile();
                // Just using file IDs as the basename for uploaded files to
                // avoid accidentally sending sensitive sample names to NCBI
                String filename = file.getId() + ".fastq";
                uploadPath(client, filename, file.getFile());
                // upload reverse
                file = pair.getReverseSequenceFile();
                filename = file.getId() + ".fastq";
                uploadPath(client, filename, file.getFile());
            }
        }
        // create submit.ready file
        uploadString(client, "submit.ready", "");
    } catch (IOException e) {
        logger.error("Error in upload", e);
        throw new UploadException("Could not upload run", e);
    } finally {
        disconnectFtpCient(client);
    }
    return submission;
}
Also used : SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) NcbiBioSampleFiles(ca.corefacility.bioinformatics.irida.model.export.NcbiBioSampleFiles) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) UploadException(ca.corefacility.bioinformatics.irida.exceptions.UploadException) IOException(java.io.IOException) FTPClient(org.apache.commons.net.ftp.FTPClient) Date(java.util.Date) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)

Example 97 with SequenceFile

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

the class SequencingObjectServiceImpl method getUniqueSamplesForSequencingObjects.

/**
 * {@inheritDoc}
 */
@PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasPermission(#sequenceFiles, 'canReadSequencingObject')")
@Override
public <T extends SequencingObject> Map<Sample, T> getUniqueSamplesForSequencingObjects(Set<T> sequenceFiles) throws DuplicateSampleException {
    Map<Sample, T> sequenceFilesSampleMap = new HashMap<>();
    for (T seqObj : sequenceFiles) {
        SequenceFile file = seqObj.getFiles().iterator().next();
        SampleSequencingObjectJoin join = ssoRepository.getSampleForSequencingObject(seqObj);
        if (join == null) {
            throw new EntityNotFoundException("No sample associated with sequence file " + seqObj.getClass() + "[id=" + seqObj.getId() + "]");
        } else {
            Sample sample = join.getSubject();
            if (sequenceFilesSampleMap.containsKey(sample)) {
                SequencingObject previousFile = sequenceFilesSampleMap.get(sample);
                throw new DuplicateSampleException("Sequence files " + file + ", " + previousFile + " have the same sample " + sample);
            } else {
                sequenceFilesSampleMap.put(sample, seqObj);
            }
        }
    }
    return sequenceFilesSampleMap;
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) HashMap(java.util.HashMap) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) DuplicateSampleException(ca.corefacility.bioinformatics.irida.exceptions.DuplicateSampleException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 98 with SequenceFile

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

the class SamplesController method createSequenceFileInSample.

/**
 * Create a {@link SequenceFile} and add it to a {@link Sample}
 *
 * @param file
 *            {@link MultipartFile}
 * @param sample
 *            {@link Sample} to add the file to.
 * @throws IOException
 */
private void createSequenceFileInSample(MultipartFile file, Sample sample) throws IOException {
    SequenceFile sequenceFile = createSequenceFile(file);
    sequencingObjectService.createSequencingObjectInSample(new SingleEndSequenceFile(sequenceFile), sample);
}
Also used : SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)

Example 99 with SequenceFile

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

the class SequenceFileController method downloadSequenceFileImages.

/**
 * Get images specific for individual sequence files.
 *
 * @param sequencingObjectId ID for the {@link SequencingObject}
 * @param sequenceFileId     Id for the {@link SequenceFile}
 * @param type               The type of image to get.
 * @param response           {@link HttpServletResponse}
 * @param thumb              Whether to scale the image for a thumbnail
 * @throws IOException if we can't write the image out to the response.
 */
@RequestMapping(value = "/sequenceFiles/img/{sequencingObjectId}/file/{sequenceFileId}/{type}")
public void downloadSequenceFileImages(@PathVariable Long sequencingObjectId, @PathVariable Long sequenceFileId, @PathVariable String type, HttpServletResponse response, @RequestParam(defaultValue = "false") boolean thumb) throws IOException {
    SequencingObject sequencingObject = sequencingObjectService.read(sequencingObjectId);
    SequenceFile file = sequencingObject.getFileWithId(sequenceFileId);
    AnalysisFastQC fastQC = analysisService.getFastQCAnalysisForSequenceFile(sequencingObject, file.getId());
    if (fastQC != null) {
        byte[] chart = new byte[0];
        if (type.equals(IMG_PERBASE)) {
            chart = fastQC.getPerBaseQualityScoreChart();
        } else if (type.equals(IMG_PERSEQUENCE)) {
            chart = fastQC.getPerSequenceQualityScoreChart();
        } else if (type.equals(IMG_DUPLICATION_LEVEL)) {
            chart = fastQC.getDuplicationLevelChart();
        } else {
            throw new EntityNotFoundException("Image not found");
        }
        if (thumb) {
            BufferedImage image = ImageIO.read(new ByteArrayInputStream(chart));
            BufferedImage thumbnail = Scalr.resize(image, Scalr.Method.QUALITY, Scalr.Mode.AUTOMATIC, 160, Scalr.OP_ANTIALIAS);
            ImageIO.write(thumbnail, "png", response.getOutputStream());
        } else {
            response.getOutputStream().write(chart);
        }
    }
    response.setContentType(MediaType.IMAGE_PNG_VALUE);
    response.flushBuffer();
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) ByteArrayInputStream(java.io.ByteArrayInputStream) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) AnalysisFastQC(ca.corefacility.bioinformatics.irida.model.workflow.analysis.AnalysisFastQC) BufferedImage(java.awt.image.BufferedImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 100 with SequenceFile

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

the class SequenceFileController method downloadSequenceFile.

/**
 * Downloads a sequence file.
 *
 * @param sequencingObjectId ID for the {@link SequencingObject}
 * @param sequenceFileId     Id for the {@link SequenceFile}
 * @param response           {@link HttpServletResponse}
 * @throws IOException if we can't write the file to the response.
 */
@RequestMapping("/sequenceFiles/download/{sequencingObjectId}/file/{sequenceFileId}")
public void downloadSequenceFile(@PathVariable Long sequencingObjectId, @PathVariable Long sequenceFileId, HttpServletResponse response) throws IOException {
    SequencingObject sequencingObject = sequencingObjectService.read(sequencingObjectId);
    SequenceFile sequenceFile = sequencingObject.getFileWithId(sequenceFileId);
    Path path = sequenceFile.getFile();
    response.setHeader("Content-Disposition", "attachment; filename=\"" + sequenceFile.getLabel() + "\"");
    Files.copy(path, response.getOutputStream());
    response.flushBuffer();
}
Also used : Path(java.nio.file.Path) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

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