Search in sources :

Example 1 with UnsupportedReferenceFileContentError

use of ca.corefacility.bioinformatics.irida.exceptions.UnsupportedReferenceFileContentError in project irida by phac-nml.

the class ReferenceFileController method addIndependentReferenceFile.

/**
 * Upload a transient reference file, to be used for a single analysis.
 *
 * @param file     the new reference file
 * @param response the response to write errors to.
 * @param locale   Locale of the current user
 * @return Success message with uploaded file id and name
 * @throws IOException if the new reference file cannot be saved
 */
@RequestMapping("/new")
public Map<String, Object> addIndependentReferenceFile(@RequestParam(value = "file") final MultipartFile file, final HttpServletResponse response, final Locale locale) throws IOException {
    logger.debug("Adding transient reference file for a single pipeline.");
    // Prepare a new reference file using the multipart file supplied by the caller
    final Path temp = Files.createTempDirectory(null);
    final Path target = temp.resolve(file.getOriginalFilename());
    file.transferTo(target.toFile());
    ReferenceFile referenceFile = new ReferenceFile(target);
    try {
        referenceFile = referenceFileService.create(referenceFile);
    } catch (final UnsupportedReferenceFileContentError e) {
        logger.error("User uploaded a reference file that biojava couldn't parse as DNA.", e);
        response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return ImmutableMap.of("error", messageSource.getMessage("projects.meta.reference-file.invalid-content", new Object[] {}, locale));
    }
    // Clean up temporary files
    Files.deleteIfExists(target);
    Files.deleteIfExists(temp);
    return ImmutableMap.of("uploaded-file-id", referenceFile.getId(), "uploaded-file-name", referenceFile.getLabel());
}
Also used : Path(java.nio.file.Path) ReferenceFile(ca.corefacility.bioinformatics.irida.model.project.ReferenceFile) UnsupportedReferenceFileContentError(ca.corefacility.bioinformatics.irida.exceptions.UnsupportedReferenceFileContentError) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with UnsupportedReferenceFileContentError

use of ca.corefacility.bioinformatics.irida.exceptions.UnsupportedReferenceFileContentError in project irida by phac-nml.

the class BioJavaSequenceFileUtilitiesImpl method countSequenceFileLengthInBases.

/**
 * {@inheritDoc}
 */
@Override
public Long countSequenceFileLengthInBases(final Path file) throws UnsupportedReferenceFileContentError {
    Long totalLength = 0L;
    logger.trace("Calculating length for file: " + file);
    try (final InputStream stream = Files.newInputStream(file)) {
        final LinkedHashMap<String, DNASequence> readFastaDNASequence = FastaReaderHelper.readFastaDNASequence(stream);
        for (Entry<String, DNASequence> entry : readFastaDNASequence.entrySet()) {
            logger.trace("Calculating for sequence " + entry.getValue().getAccession());
            int length = entry.getValue().getLength();
            totalLength += length;
        }
    } catch (final CompoundNotFoundError e) {
        logger.error("Cannot handle non-DNA files, or files with ambiguous bases.", e);
        throw new UnsupportedReferenceFileContentError("Cannot handle reference files with non-DNA or ambiguous characters.", e);
    } catch (Throwable e) {
        logger.error("Cannot calculate reference file length " + file, e);
        throw new IllegalArgumentException("Cannot parse reference file " + file, e);
    }
    return totalLength;
}
Also used : DNASequence(org.biojava3.core.sequence.DNASequence) InputStream(java.io.InputStream) UnsupportedReferenceFileContentError(ca.corefacility.bioinformatics.irida.exceptions.UnsupportedReferenceFileContentError) CompoundNotFoundError(org.biojava3.core.exceptions.CompoundNotFoundError)

Example 3 with UnsupportedReferenceFileContentError

use of ca.corefacility.bioinformatics.irida.exceptions.UnsupportedReferenceFileContentError in project irida by phac-nml.

the class ReferenceFileController method addReferenceFileToProject.

/**
 * Add a new reference file to a project.
 *
 * @param projectId The id of the project to add the file to.
 * @param files     {@link List} of {@link MultipartFile} file being uploaded.
 * @param response  {@link HttpServletResponse}
 * @param locale    locale of the logged in user
 * @return Success message if file was successfully uploaded
 */
@RequestMapping("/project/{projectId}/new")
@ResponseBody
public Map<String, String> addReferenceFileToProject(@PathVariable Long projectId, @RequestParam(value = "file") List<MultipartFile> files, HttpServletResponse response, final Locale locale) {
    Project project = projectService.read(projectId);
    logger.debug("Adding reference file to project " + projectId);
    try {
        for (MultipartFile file : files) {
            // Prepare a new reference file using the multipart file supplied by the caller
            Path temp = Files.createTempDirectory(null);
            Path target = temp.resolve(file.getOriginalFilename());
            file.transferTo(target.toFile());
            ReferenceFile referenceFile = new ReferenceFile(target);
            projectService.addReferenceFileToProject(project, referenceFile);
            // Clean up temporary files
            Files.deleteIfExists(target);
            Files.deleteIfExists(temp);
        }
    } catch (final UnsupportedReferenceFileContentError e) {
        logger.error("User uploaded a reference file that biojava couldn't parse as DNA.", e);
        response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return ImmutableMap.of("error_message", messageSource.getMessage("projects.meta.reference-file.invalid-content", new Object[] {}, locale));
    } catch (IOException e) {
        logger.error("Error writing sequence file", e);
        response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return ImmutableMap.of("error_message", messageSource.getMessage("projects.meta.reference-file.unknown-error", new Object[] {}, locale));
    }
    // method so doesn't need to be i18n.
    return ImmutableMap.of("success", "upload complete.");
}
Also used : Path(java.nio.file.Path) Project(ca.corefacility.bioinformatics.irida.model.project.Project) MultipartFile(org.springframework.web.multipart.MultipartFile) ReferenceFile(ca.corefacility.bioinformatics.irida.model.project.ReferenceFile) UnsupportedReferenceFileContentError(ca.corefacility.bioinformatics.irida.exceptions.UnsupportedReferenceFileContentError) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

UnsupportedReferenceFileContentError (ca.corefacility.bioinformatics.irida.exceptions.UnsupportedReferenceFileContentError)3 ReferenceFile (ca.corefacility.bioinformatics.irida.model.project.ReferenceFile)2 Path (java.nio.file.Path)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Project (ca.corefacility.bioinformatics.irida.model.project.Project)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 CompoundNotFoundError (org.biojava3.core.exceptions.CompoundNotFoundError)1 DNASequence (org.biojava3.core.sequence.DNASequence)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1