Search in sources :

Example 26 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class AssemblyFileProcessor method shouldAssemble.

/**
 * Check whether any {@link Project} associated with the
 * {@link SequencingObject} is set to assemble
 *
 * @param object
 *            {@link SequencingObject} to check ot assemble
 * @return true if it should assemble, false otherwise
 */
private boolean shouldAssemble(SequencingObject object) {
    boolean assemble = false;
    SampleSequencingObjectJoin sampleForSequencingObject = ssoRepository.getSampleForSequencingObject(object);
    /*
		 * This is something that should only ever happen in tests, but added
		 * check with a warning
		 */
    if (sampleForSequencingObject != null) {
        List<Join<Project, Sample>> projectForSample = psjRepository.getProjectForSample(sampleForSequencingObject.getSubject());
        assemble = projectForSample.stream().anyMatch(j -> j.getSubject().getAssembleUploads());
    } else {
        logger.warn("Cannot find sample for sequencing object.  Not assembling");
    }
    return assemble;
}
Also used : ProjectSampleJoinRepository(ca.corefacility.bioinformatics.irida.repositories.joins.project.ProjectSampleJoinRepository) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) AnalysisType(ca.corefacility.bioinformatics.irida.model.enums.AnalysisType) FileProcessor(ca.corefacility.bioinformatics.irida.processing.FileProcessor) UserRepository(ca.corefacility.bioinformatics.irida.repositories.user.UserRepository) Logger(org.slf4j.Logger) IridaWorkflowNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.IridaWorkflowNotFoundException) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission) FileProcessorException(ca.corefacility.bioinformatics.irida.processing.FileProcessorException) SequencingObjectRepository(ca.corefacility.bioinformatics.irida.repositories.sequencefile.SequencingObjectRepository) IridaWorkflowsService(ca.corefacility.bioinformatics.irida.service.workflow.IridaWorkflowsService) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) UUID(java.util.UUID) SampleSequencingObjectJoinRepository(ca.corefacility.bioinformatics.irida.repositories.joins.sample.SampleSequencingObjectJoinRepository) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sets(com.google.common.collect.Sets) List(java.util.List) Component(org.springframework.stereotype.Component) User(ca.corefacility.bioinformatics.irida.model.user.User) Builder(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission.Builder) AnalysisSubmissionRepository(ca.corefacility.bioinformatics.irida.repositories.analysis.submission.AnalysisSubmissionRepository) Transactional(org.springframework.transaction.annotation.Transactional) IridaWorkflow(ca.corefacility.bioinformatics.irida.model.workflow.IridaWorkflow) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)

Example 27 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class SistrTypingFileProcessor method shouldTypeWithSISTR.

/**
 * Check whether any {@link Project} associated with the
 * {@link SequencingObject} is set to type with SISTR.
 *
 * @param object {@link SequencingObject} to check to type with SISTR.
 * @return true if it should type with SISTR, false otherwise
 */
private Project.AutomatedSISTRSetting shouldTypeWithSISTR(SequencingObject object) {
    Project.AutomatedSISTRSetting type = Project.AutomatedSISTRSetting.OFF;
    SampleSequencingObjectJoin sampleForSequencingObject = ssoRepository.getSampleForSequencingObject(object);
    /*
		 * This is something that should only ever happen in tests, but added
		 * check with a warning
		 */
    if (sampleForSequencingObject != null) {
        List<Join<Project, Sample>> projectForSample = psjRepository.getProjectForSample(sampleForSequencingObject.getSubject());
        Set<Project.AutomatedSISTRSetting> sistrOptions = projectForSample.stream().map(j -> j.getSubject().getSistrTypingUploads()).collect(Collectors.toSet());
        if (sistrOptions.contains(Project.AutomatedSISTRSetting.AUTO_METADATA)) {
            return Project.AutomatedSISTRSetting.AUTO_METADATA;
        } else if (sistrOptions.contains(Project.AutomatedSISTRSetting.AUTO))
            return Project.AutomatedSISTRSetting.AUTO;
    } else {
        logger.warn("Cannot find sample for sequencing object.  Not typing with SISTR");
    }
    return type;
}
Also used : ProjectSampleJoinRepository(ca.corefacility.bioinformatics.irida.repositories.joins.project.ProjectSampleJoinRepository) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) AnalysisType(ca.corefacility.bioinformatics.irida.model.enums.AnalysisType) FileProcessor(ca.corefacility.bioinformatics.irida.processing.FileProcessor) UserRepository(ca.corefacility.bioinformatics.irida.repositories.user.UserRepository) Logger(org.slf4j.Logger) IridaWorkflowNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.IridaWorkflowNotFoundException) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission) FileProcessorException(ca.corefacility.bioinformatics.irida.processing.FileProcessorException) SequencingObjectRepository(ca.corefacility.bioinformatics.irida.repositories.sequencefile.SequencingObjectRepository) IridaWorkflowsService(ca.corefacility.bioinformatics.irida.service.workflow.IridaWorkflowsService) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) Set(java.util.Set) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) SampleSequencingObjectJoinRepository(ca.corefacility.bioinformatics.irida.repositories.joins.sample.SampleSequencingObjectJoinRepository) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Sets(com.google.common.collect.Sets) List(java.util.List) Component(org.springframework.stereotype.Component) User(ca.corefacility.bioinformatics.irida.model.user.User) Builder(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission.Builder) AnalysisSubmissionRepository(ca.corefacility.bioinformatics.irida.repositories.analysis.submission.AnalysisSubmissionRepository) Transactional(org.springframework.transaction.annotation.Transactional) IridaWorkflow(ca.corefacility.bioinformatics.irida.model.workflow.IridaWorkflow) Project(ca.corefacility.bioinformatics.irida.model.project.Project) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)

Example 28 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class RESTSampleSequenceFilesController method addNewSequenceFileToSample.

/**
 * Add a new {@link SequenceFile} to a {@link Sample}.
 *
 * @param sampleId
 *            the identifier for the {@link Sample}.
 * @param file
 *            the content of the {@link SequenceFile}.
 * @param fileResource
 *            the parameters for the file
 * @param response
 *            the servlet response.
 * @return a response indicating the success of the submission.
 * @throws IOException
 *             if we can't write the file to disk.
 */
@RequestMapping(value = "/api/samples/{sampleId}/sequenceFiles", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ModelMap addNewSequenceFileToSample(@PathVariable Long sampleId, @RequestPart("file") MultipartFile file, @RequestPart(value = "parameters", required = false) SequenceFileResource fileResource, HttpServletResponse response) throws IOException {
    ModelMap modelMap = new ModelMap();
    logger.debug("Adding sequence file to sample " + sampleId);
    logger.trace("Uploaded file size: " + file.getSize() + " bytes");
    // load the sample from the database
    Sample sample = sampleService.read(sampleId);
    logger.trace("Read sample " + sampleId);
    // prepare a new sequence file using the multipart file supplied by the
    // caller
    Path temp = Files.createTempDirectory(null);
    Path target = temp.resolve(file.getOriginalFilename());
    // Changed to MultipartFile.transerTo(File) because it was truncating
    // large files to 1039956336 bytes
    // target = Files.write(target, file.getBytes());
    file.transferTo(target.toFile());
    logger.trace("Wrote temp file to " + target);
    SequenceFile sf;
    SequencingRun miseqRun = null;
    if (fileResource != null) {
        sf = fileResource.getResource();
        Long miseqRunId = fileResource.getMiseqRunId();
        if (miseqRunId != null) {
            miseqRun = miseqRunService.read(miseqRunId);
            logger.trace("Read miseq run " + miseqRunId);
        }
    } else {
        sf = new SequenceFile();
    }
    sf.setFile(target);
    SingleEndSequenceFile singleEndSequenceFile = new SingleEndSequenceFile(sf);
    if (miseqRun != null) {
        singleEndSequenceFile.setSequencingRun(miseqRun);
        logger.trace("Added seqfile to miseqrun");
    }
    // save the seqobject and sample
    SampleSequencingObjectJoin createSequencingObjectInSample = sequencingObjectService.createSequencingObjectInSample(singleEndSequenceFile, sample);
    singleEndSequenceFile = (SingleEndSequenceFile) createSequencingObjectInSample.getObject();
    logger.trace("Created seqfile in sample " + createSequencingObjectInSample.getObject().getId());
    // clean up the temporary files.
    Files.deleteIfExists(target);
    Files.deleteIfExists(temp);
    logger.trace("Deleted temp file");
    // prepare a link to the sequence file itself (on the sequence file
    // controller)
    String objectType = objectLabels.get(SingleEndSequenceFile.class);
    Long sequenceFileId = singleEndSequenceFile.getSequenceFile().getId();
    Link selfRel = linkTo(methodOn(RESTSampleSequenceFilesController.class).readSequenceFileForSequencingObject(sampleId, objectType, singleEndSequenceFile.getId(), sequenceFileId)).withSelfRel();
    // Changed, because sfr.setResource(sf)
    // and sfr.setResource(sampleSequenceFileRelationship.getObject())
    // both will not pass a GET-POST comparison integration test.
    singleEndSequenceFile = (SingleEndSequenceFile) sequencingObjectService.read(singleEndSequenceFile.getId());
    SequenceFile sequenceFile = singleEndSequenceFile.getFileWithId(sequenceFileId);
    // add links to the resource
    sequenceFile.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).getSampleSequenceFiles(sampleId)).withRel(REL_SAMPLE_SEQUENCE_FILES));
    sequenceFile.add(selfRel);
    sequenceFile.add(linkTo(methodOn(RESTProjectSamplesController.class).getSample(sampleId)).withRel(REL_SAMPLE));
    sequenceFile.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).readSequencingObject(sampleId, objectType, singleEndSequenceFile.getId())).withRel(REL_SEQ_OBJECT));
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, sequenceFile);
    // add a location header.
    response.addHeader(HttpHeaders.LOCATION, selfRel.getHref());
    // set the response status.
    response.setStatus(HttpStatus.CREATED.value());
    // respond to the client
    return modelMap;
}
Also used : Path(java.nio.file.Path) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SequencingRun(ca.corefacility.bioinformatics.irida.model.run.SequencingRun) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) Link(org.springframework.hateoas.Link) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 29 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class RESTSampleSequenceFilesController method addNewSequenceFilePairToSample.

/**
 * Add a pair of {@link SequenceFile}s to a {@link Sample}
 *
 * @param sampleId
 *            The {@link Sample} id to add to
 * @param file1
 *            The first multipart file
 * @param fileResource1
 *            The metadata for the first file
 * @param file2
 *            The second multipart file
 * @param fileResource2
 *            the metadata for the second file
 * @param response
 *            a reference to the servlet response.
 * @return Response containing the locations for the created files
 * @throws IOException
 *             if we can't write the files to disk
 */
@RequestMapping(value = "/api/samples/{sampleId}/pairs", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ModelMap addNewSequenceFilePairToSample(@PathVariable Long sampleId, @RequestPart("file1") MultipartFile file1, @RequestPart(value = "parameters1") SequenceFileResource fileResource1, @RequestPart("file2") MultipartFile file2, @RequestPart(value = "parameters2") SequenceFileResource fileResource2, HttpServletResponse response) throws IOException {
    logger.debug("Adding pair of sequence files to sample " + sampleId);
    logger.trace("First uploaded file size: " + file1.getSize() + " bytes");
    logger.trace("Second uploaded file size: " + file2.getSize() + " bytes");
    ModelMap modelMap = new ModelMap();
    // confirm that a relationship exists between the project and the sample
    Sample sample = sampleService.read(sampleId);
    logger.trace("Read sample " + sampleId);
    // create temp files
    Path temp1 = Files.createTempDirectory(null);
    Path target1 = temp1.resolve(file1.getOriginalFilename());
    Path temp2 = Files.createTempDirectory(null);
    Path target2 = temp2.resolve(file2.getOriginalFilename());
    // transfer the files to temp directories
    file1.transferTo(target1.toFile());
    file2.transferTo(target2.toFile());
    // create the model objects
    SequenceFile sf1 = fileResource1.getResource();
    SequenceFile sf2 = fileResource2.getResource();
    sf1.setFile(target1);
    sf2.setFile(target2);
    // get the sequencing run
    SequencingRun sequencingRun = null;
    if (!Objects.equal(fileResource1.getMiseqRunId(), fileResource2.getMiseqRunId())) {
        throw new IllegalArgumentException("Cannot upload a pair of files from different sequencing runs");
    }
    Long runId = fileResource1.getMiseqRunId();
    SequenceFilePair sequenceFilePair = new SequenceFilePair(sf1, sf2);
    if (runId != null) {
        sequencingRun = miseqRunService.read(runId);
        sequenceFilePair.setSequencingRun(sequencingRun);
        logger.trace("Added sequencing run to files" + runId);
    }
    // add the files and join
    SampleSequencingObjectJoin createSequencingObjectInSample = sequencingObjectService.createSequencingObjectInSample(sequenceFilePair, sample);
    // clean up the temporary files.
    Files.deleteIfExists(target1);
    Files.deleteIfExists(temp1);
    Files.deleteIfExists(target2);
    Files.deleteIfExists(temp2);
    logger.trace("Deleted temp files");
    SequencingObject sequencingObject = createSequencingObjectInSample.getObject();
    sequencingObject = addSequencingObjectLinks(sequencingObject, sampleId);
    sequencingObject.add(linkTo(methodOn(RESTSampleSequenceFilesController.class).getSampleSequenceFiles(sampleId)).withRel(REL_SAMPLE_SEQUENCE_FILES));
    // add location header
    response.addHeader(HttpHeaders.LOCATION, sequencingObject.getLink("self").getHref());
    // set the response status.
    response.setStatus(HttpStatus.CREATED.value());
    modelMap.addAttribute(RESTGenericController.RESOURCE_NAME, sequencingObject);
    // respond to the client
    return modelMap;
}
Also used : Path(java.nio.file.Path) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SequencingRun(ca.corefacility.bioinformatics.irida.model.run.SequencingRun) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 30 with SampleSequencingObjectJoin

use of ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin in project irida by phac-nml.

the class ProjectEventHandlerTest method testHandleSequenceFileAddedEventMultipleProjects.

@SuppressWarnings("unchecked")
@Test
public void testHandleSequenceFileAddedEventMultipleProjects() {
    Class<? extends ProjectEvent> clazz = DataAddedToSampleProjectEvent.class;
    Project project = new Project("p1");
    Project project2 = new Project("p2");
    Sample sample = new Sample();
    SequenceFile file = new SequenceFile();
    SingleEndSequenceFile seqObj = new SingleEndSequenceFile(file);
    SampleSequencingObjectJoin join = new SampleSequencingObjectJoin(sample, seqObj);
    when(psjRepository.getProjectForSample(sample)).thenReturn(Lists.newArrayList(new ProjectSampleJoin(project, sample, true), new ProjectSampleJoin(project2, sample, true)));
    when(eventRepository.save(any(ProjectEvent.class))).thenReturn(new DataAddedToSampleProjectEvent(project, sample));
    Object[] args = {};
    MethodEvent methodEvent = new MethodEvent(clazz, join, args);
    handler.delegate(methodEvent);
    ArgumentCaptor<ProjectEvent> captor = ArgumentCaptor.forClass(ProjectEvent.class);
    verify(eventRepository, times(2)).save(captor.capture());
    List<ProjectEvent> allValues = captor.getAllValues();
    Set<Project> projects = Sets.newHashSet(project, project2);
    for (ProjectEvent event : allValues) {
        assertTrue(event instanceof DataAddedToSampleProjectEvent);
        Project eventProject = event.getProject();
        assertTrue(projects.contains(eventProject));
        projects.remove(eventProject);
    }
    verify(projectRepository, times(2)).save(project);
}
Also used : Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) UserRoleSetProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRoleSetProjectEvent) UserRemovedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.UserRemovedProjectEvent) DataAddedToSampleProjectEvent(ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent) ProjectEvent(ca.corefacility.bioinformatics.irida.model.event.ProjectEvent) SampleAddedProjectEvent(ca.corefacility.bioinformatics.irida.model.event.SampleAddedProjectEvent) Test(org.junit.Test)

Aggregations

SampleSequencingObjectJoin (ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)54 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)45 SequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile)30 SingleEndSequenceFile (ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile)30 Test (org.junit.Test)29 Project (ca.corefacility.bioinformatics.irida.model.project.Project)21 SequencingObject (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject)16 ProjectSampleJoin (ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin)14 SequenceFilePair (ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair)11 Path (java.nio.file.Path)9 Transactional (org.springframework.transaction.annotation.Transactional)9 ModelMap (org.springframework.ui.ModelMap)9 ArrayList (java.util.ArrayList)8 Join (ca.corefacility.bioinformatics.irida.model.joins.Join)7 AnalysisSubmission (ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission)7 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)7 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)6 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)5 DataAddedToSampleProjectEvent (ca.corefacility.bioinformatics.irida.model.event.DataAddedToSampleProjectEvent)4 User (ca.corefacility.bioinformatics.irida.model.user.User)4