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