Search in sources :

Example 6 with SampleSequencingObjectJoin

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

the class SampleServiceImplTest method testMergeSamples.

@Test
public void testMergeSamples() {
    // For every sample in toMerge, the service should:
    // 1. call SequenceFileRepository to get the sequence files in that
    // sample,
    // 2. call SequenceFileRepository to add the sequence files to
    // mergeInto,
    // 3. call SampleRepository to persist the sample as deleted.
    final int SIZE = 3;
    Sample s = s(1L);
    Project project = p(1L);
    Sample[] toMerge = new Sample[SIZE];
    SequenceFile[] toMerge_sf = new SequenceFile[SIZE];
    SequencingObject[] toMerge_so = new SequencingObject[SIZE];
    SampleSequencingObjectJoin[] s_so_joins = new SampleSequencingObjectJoin[SIZE];
    SampleSequencingObjectJoin[] s_so_original = new SampleSequencingObjectJoin[SIZE];
    ProjectSampleJoin[] p_s_joins = new ProjectSampleJoin[SIZE];
    List<Sample> mergeSamples = new ArrayList<>();
    for (long i = 0; i < SIZE; i++) {
        int p = (int) i;
        toMerge[p] = s(i + 2);
        mergeSamples.add(toMerge[p]);
        toMerge_sf[p] = sf(i + 2);
        toMerge_so[p] = so(i + 2);
        s_so_joins[p] = new SampleSequencingObjectJoin(s, toMerge_so[p]);
        p_s_joins[p] = new ProjectSampleJoin(project, toMerge[p], true);
        List<Join<Project, Sample>> projectSampleJoins = new ArrayList<>();
        projectSampleJoins.add(p_s_joins[p]);
        List<SampleSequencingObjectJoin> sampleSeqObjectJoins = new ArrayList<>();
        SampleSequencingObjectJoin join = new SampleSequencingObjectJoin(toMerge[p], toMerge_so[p]);
        sampleSeqObjectJoins.add(join);
        s_so_original[p] = join;
        when(ssoRepository.getSequencesForSample(toMerge[p])).thenReturn(null);
        when(ssoRepository.getSequencesForSample(toMerge[p])).thenReturn(sampleSeqObjectJoins);
        when(ssoRepository.save(s_so_joins[p])).thenReturn(s_so_joins[p]);
        when(ssoRepository.readObjectForSample(toMerge[p], toMerge_so[p].getId())).thenReturn(join);
        when(psjRepository.getProjectForSample(toMerge[p])).thenReturn(projectSampleJoins);
        // for deletion
        when(psjRepository.readSampleForProject(project, toMerge[p])).thenReturn(p_s_joins[p]);
    }
    List<Join<Project, Sample>> joins = new ArrayList<>();
    joins.add(new ProjectSampleJoin(project, s, true));
    when(psjRepository.getProjectForSample(s)).thenReturn(joins);
    Sample saved = sampleService.mergeSamples(project, s, mergeSamples);
    verify(psjRepository).getProjectForSample(s);
    for (int i = 0; i < SIZE; i++) {
        verify(ssoRepository).getSequencesForSample(toMerge[i]);
        verify(ssoRepository).save(s_so_joins[i]);
        verify(ssoRepository).delete(s_so_original[i]);
        verify(sampleRepository).delete(toMerge[i].getId());
        verify(psjRepository).getProjectForSample(toMerge[i]);
        verify(psjRepository).delete(p_s_joins[i]);
    }
    assertEquals("The saved sample should be the same as the sample to merge into.", s, saved);
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ArrayList(java.util.ArrayList) Join(ca.corefacility.bioinformatics.irida.model.joins.Join) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) 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) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) Test(org.junit.Test)

Example 7 with SampleSequencingObjectJoin

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

the class SampleSequenceFilesControllerTest method testAddNewSequenceFilePairToSampleMismatchedRunIDs.

@Test(expected = IllegalArgumentException.class)
public void testAddNewSequenceFilePairToSampleMismatchedRunIDs() throws IOException {
    Project p = TestDataFactory.constructProject();
    Sample s = TestDataFactory.constructSample();
    SequenceFilePair pair = TestDataFactory.constructSequenceFilePair();
    SampleSequencingObjectJoin sso = new SampleSequencingObjectJoin(s, pair);
    SequenceFileResource resource1 = new SequenceFileResource();
    SequenceFileResource resource2 = new SequenceFileResource();
    resource1.setMiseqRunId(1L);
    resource2.setMiseqRunId(2L);
    Path f1 = Files.createTempFile(null, null);
    Path f2 = Files.createTempFile(null, null);
    MockMultipartFile mmf1 = new MockMultipartFile("filename1", "filename1", "blurgh1", FileCopyUtils.copyToByteArray(f1.toFile()));
    MockMultipartFile mmf2 = new MockMultipartFile("filename2", "filename2", "blurgh2", FileCopyUtils.copyToByteArray(f2.toFile()));
    MockHttpServletResponse response = new MockHttpServletResponse();
    when(sampleService.getSampleForProject(p, s.getId())).thenReturn(new ProjectSampleJoin(p, s, true));
    when(sequencingObjectService.createSequencingObjectInSample(any(SequenceFilePair.class), Matchers.eq(s))).thenReturn(sso);
    controller.addNewSequenceFilePairToSample(s.getId(), mmf1, resource1, mmf2, resource2, response);
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) ProjectSampleJoin(ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin) Project(ca.corefacility.bioinformatics.irida.model.project.Project) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) SequenceFileResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.sequencefile.SequenceFileResource) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 8 with SampleSequencingObjectJoin

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

the class SampleSequenceFilesControllerTest method testAddNewSequenceFilePairToSample.

@Test
public void testAddNewSequenceFilePairToSample() throws IOException {
    Sample s = TestDataFactory.constructSample();
    String file1Name = "file1_R1_01.fastq.gz";
    String file2Name = "file2_R2_01.fastq.gz";
    SequenceFilePair pair = TestDataFactory.constructSequenceFilePair();
    Iterator<SequenceFile> iterator = pair.getFiles().iterator();
    SequenceFile sf1 = iterator.next();
    SequenceFile sf2 = iterator.next();
    sf1.setFile(Paths.get(file1Name));
    sf2.setFile(Paths.get(file2Name));
    sf1.setId(3245L);
    SampleSequencingObjectJoin sso = new SampleSequencingObjectJoin(s, pair);
    SequenceFileResource resource1 = new SequenceFileResource();
    SequenceFileResource resource2 = new SequenceFileResource();
    Path f1 = Files.createTempFile(null, null);
    Path f2 = Files.createTempFile(null, null);
    MockMultipartFile mmf1 = new MockMultipartFile(file1Name, file1Name, "blurgh1", FileCopyUtils.copyToByteArray(f1.toFile()));
    MockMultipartFile mmf2 = new MockMultipartFile(file2Name, file2Name, "blurgh2", FileCopyUtils.copyToByteArray(f2.toFile()));
    MockHttpServletResponse response = new MockHttpServletResponse();
    // mock out the service calls
    when(sampleService.read(s.getId())).thenReturn(s);
    when(sequencingObjectService.createSequencingObjectInSample(any(SequenceFilePair.class), Matchers.eq(s))).thenReturn(sso);
    ModelMap modelMap = controller.addNewSequenceFilePairToSample(s.getId(), mmf1, resource1, mmf2, resource2, response);
    verify(sampleService).read(s.getId());
    verify(sequencingObjectService).createSequencingObjectInSample(any(SequenceFilePair.class), Matchers.eq(s));
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertNotNull("Object should not be null", o);
    assertTrue("Object should be an instance of SequenceFilePair", o instanceof SequenceFilePair);
    SequenceFilePair returnVal = (SequenceFilePair) o;
    Link selfCollection = returnVal.getLink(Link.REL_SELF);
    Link sampleRC = returnVal.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);
    Link sampleSequenceFiles = returnVal.getLink(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);
    String sampleLocation = "http://localhost/api/samples/" + s.getId();
    String pairLocation = sampleLocation + "/pairs/" + pair.getId();
    assertEquals("Pair location should be correct", pairLocation, selfCollection.getHref());
    assertEquals("Sample location should be correct", sampleLocation, sampleRC.getHref());
    assertEquals("Sequence file location should be correct", sampleLocation + "/sequenceFiles", sampleSequenceFiles.getHref());
    String sequenceFileLocation1 = pairLocation + "/files/" + sf1.getId();
    String sequenceFileLocation2 = pairLocation + "/files/" + sf2.getId();
    String[] sequenceFileLocs = new String[] { sequenceFileLocation1, sequenceFileLocation2 };
    String locationHeader = response.getHeader(HttpHeaders.LOCATION);
    assertEquals("The location header should have the self rel", pairLocation, locationHeader);
    Iterator<SequenceFile> filesIterator = returnVal.getFiles().iterator();
    for (int i = 0; i < 2; i++) {
        SequenceFile returnedFile = filesIterator.next();
        Link self = returnedFile.getLink(Link.REL_SELF);
        assertNotNull("Self link should not be null", self);
        assertEquals("Self reference should be correct", sequenceFileLocs[i], self.getHref());
    }
    assertEquals("HTTP status must be CREATED", HttpStatus.CREATED.value(), response.getStatus());
    Files.delete(f1);
    Files.delete(f2);
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ModelMap(org.springframework.ui.ModelMap) SequenceFileResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.sequencefile.SequenceFileResource) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Link(org.springframework.hateoas.Link) Test(org.junit.Test)

Example 9 with SampleSequencingObjectJoin

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

the class SampleSequenceFilesControllerTest method testAddNewSequenceFileToSample.

@Test
public void testAddNewSequenceFileToSample() throws IOException {
    Sample s = TestDataFactory.constructSample();
    SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();
    SequenceFile sf = so.getSequenceFile();
    SampleSequencingObjectJoin sso = new SampleSequencingObjectJoin(s, so);
    SequenceFileResource resource = new SequenceFileResource();
    Path f = Files.createTempFile(null, null);
    MockMultipartFile mmf = new MockMultipartFile("filename", "filename", "blurgh", FileCopyUtils.copyToByteArray(f.toFile()));
    MockHttpServletResponse response = new MockHttpServletResponse();
    when(sampleService.read(s.getId())).thenReturn(s);
    when(sequencingObjectService.createSequencingObjectInSample(any(SingleEndSequenceFile.class), Matchers.eq(s))).thenReturn(sso);
    when(sequencingObjectService.read(so.getId())).thenReturn(so);
    ModelMap modelMap = controller.addNewSequenceFileToSample(s.getId(), mmf, resource, response);
    verify(sampleService).read(s.getId());
    verify(sampleService, times(1)).read(s.getId());
    verify(sequencingObjectService).createSequencingObjectInSample(any(SingleEndSequenceFile.class), Matchers.eq(s));
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertNotNull("object must not be null", o);
    assertTrue("object must be a SequenceFile", o instanceof SequenceFile);
    SequenceFile sfr = (SequenceFile) o;
    assertEquals("response must have CREATED status", HttpStatus.CREATED.value(), response.getStatus());
    Link self = sfr.getLink(Link.REL_SELF);
    Link sampleSequenceFiles = sfr.getLink(RESTSampleSequenceFilesController.REL_SAMPLE_SEQUENCE_FILES);
    Link sample = sfr.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);
    String sampleLocation = "http://localhost/api/samples/" + s.getId();
    String sequenceFileLocation = sampleLocation + "/unpaired/" + so.getIdentifier() + "/files/" + sf.getId();
    assertNotNull("self reference must exist", self);
    assertEquals("self reference must be correct", sequenceFileLocation, self.getHref());
    assertNotNull("sequence files link must exist", sampleSequenceFiles);
    assertEquals("sequence files location must be correct", sampleLocation + "/sequenceFiles", sampleSequenceFiles.getHref());
    assertNotNull("sample link must exist", sample);
    assertEquals("sample location must be correct", sampleLocation, sample.getHref());
    Files.delete(f);
}
Also used : Path(java.nio.file.Path) MockMultipartFile(org.springframework.mock.web.MockMultipartFile) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) 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) SequenceFileResource(ca.corefacility.bioinformatics.irida.web.assembler.resource.sequencefile.SequenceFileResource) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Link(org.springframework.hateoas.Link) Test(org.junit.Test)

Example 10 with SampleSequencingObjectJoin

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

the class SampleSequenceFilesControllerTest method testGetSampleSequenceFiles.

@Test
public void testGetSampleSequenceFiles() throws IOException {
    Sample s = TestDataFactory.constructSample();
    SingleEndSequenceFile so = TestDataFactory.constructSingleEndSequenceFile();
    SampleSequencingObjectJoin r = new SampleSequencingObjectJoin(s, so);
    List<SampleSequencingObjectJoin> relationships = Lists.newArrayList(r);
    // mock out the service calls
    when(sampleService.read(s.getId())).thenReturn(s);
    when(sequencingObjectService.getSequencingObjectsForSample(s)).thenReturn(relationships);
    ModelMap modelMap = controller.getSampleSequenceFiles(s.getId());
    // verify that the service calls were used.
    verify(sampleService).read(s.getId());
    verify(sequencingObjectService).getSequencingObjectsForSample(s);
    Object o = modelMap.get(RESTGenericController.RESOURCE_NAME);
    assertTrue(o instanceof ResourceCollection);
    @SuppressWarnings("unchecked") ResourceCollection<SequenceFile> resources = (ResourceCollection<SequenceFile>) o;
    assertNotNull(resources);
    assertEquals(1, resources.size());
    Link selfCollection = resources.getLink(Link.REL_SELF);
    Link sample = resources.getLink(RESTSampleSequenceFilesController.REL_SAMPLE);
    String sampleLocation = "http://localhost/api/samples/" + s.getId();
    String sequenceFileLocation = sampleLocation + "/unpaired/" + so.getIdentifier() + "/files/" + so.getSequenceFile().getId();
    assertEquals(sampleLocation + "/sequenceFiles", selfCollection.getHref());
    assertEquals(sampleLocation, sample.getHref());
    // confirm that the self rel for an individual sequence file exists
    SequenceFile sfr = resources.iterator().next();
    Link self = sfr.getLink(Link.REL_SELF);
    assertEquals(sequenceFileLocation, self.getHref());
    assertEquals(so.getSequenceFile().getFile(), sfr.getFile());
}
Also used : SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) 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) ResourceCollection(ca.corefacility.bioinformatics.irida.web.assembler.resource.ResourceCollection) 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