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());
}
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;
}
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.");
}
Aggregations