Search in sources :

Example 46 with SampleSequencingObjectJoin

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

the class SequencingObjectServiceImpl method getUniqueSamplesForSequencingObjects.

/**
 * {@inheritDoc}
 */
@PreAuthorize("hasAnyRole('ROLE_ADMIN') or hasPermission(#sequenceFiles, 'canReadSequencingObject')")
@Override
public <T extends SequencingObject> Map<Sample, T> getUniqueSamplesForSequencingObjects(Set<T> sequenceFiles) throws DuplicateSampleException {
    Map<Sample, T> sequenceFilesSampleMap = new HashMap<>();
    for (T seqObj : sequenceFiles) {
        SequenceFile file = seqObj.getFiles().iterator().next();
        SampleSequencingObjectJoin join = ssoRepository.getSampleForSequencingObject(seqObj);
        if (join == null) {
            throw new EntityNotFoundException("No sample associated with sequence file " + seqObj.getClass() + "[id=" + seqObj.getId() + "]");
        } else {
            Sample sample = join.getSubject();
            if (sequenceFilesSampleMap.containsKey(sample)) {
                SequencingObject previousFile = sequenceFilesSampleMap.get(sample);
                throw new DuplicateSampleException("Sequence files " + file + ", " + previousFile + " have the same sample " + sample);
            } else {
                sequenceFilesSampleMap.put(sample, seqObj);
            }
        }
    }
    return sequenceFilesSampleMap;
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFile) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) HashMap(java.util.HashMap) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) EntityNotFoundException(ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) DuplicateSampleException(ca.corefacility.bioinformatics.irida.exceptions.DuplicateSampleException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 47 with SampleSequencingObjectJoin

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

the class SequencingRunServiceImpl method delete.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@PreAuthorize("hasRole('ROLE_ADMIN')")
public void delete(Long id) {
    Set<Sample> referencedSamples = new HashSet<>();
    logger.trace("Getting samples for SequencingRun " + id);
    // Get the Files from the SequencingRun to delete
    SequencingRun read = read(id);
    Set<SequencingObject> findSequencingObjectsForSequencingRun = objectRepository.findSequencingObjectsForSequencingRun(read);
    // For each file in the run
    for (SequencingObject sequencingObject : findSequencingObjectsForSequencingRun) {
        // get the sample the file is in. If the sample is empty when this
        // is complete it will be removed
        SampleSequencingObjectJoin sampleForSequencingObject = ssoRepository.getSampleForSequencingObject(sequencingObject);
        if (sampleForSequencingObject != null) {
            logger.trace("Sample " + sampleForSequencingObject.getSubject().getId() + " is used in this run");
            referencedSamples.add(sampleForSequencingObject.getSubject());
        }
        // Get the analysis submissions this file is included in
        Set<AnalysisSubmission> submissions = submissionRepository.findAnalysisSubmissionsForSequecingObject(sequencingObject);
        // If there are no submissions, we can delete the pair and file
        if (submissions.isEmpty()) {
            logger.trace("Deleting file " + sequencingObject.getId());
            objectRepository.delete(sequencingObject);
        } else {
            logger.trace("Keeping file " + sequencingObject.getId() + " because it's used in an analysis");
            if (sampleForSequencingObject != null) {
                // otherwise we'll just remove it from the sample
                ssoRepository.delete(sampleForSequencingObject);
            }
            sequencingObject.setSequencingRun(null);
            objectRepository.save(sequencingObject);
        }
    }
    // Delete the run
    logger.trace("Deleting SequencingRun " + id);
    super.delete(id);
    // Search if samples are empty. If they are, delete the sample.
    for (Sample sample : referencedSamples) {
        List<SampleSequencingObjectJoin> sequencesForSample = ssoRepository.getSequencesForSample(sample);
        if (sequencesForSample.isEmpty()) {
            logger.trace("Sample " + sample.getId() + " is empty.  Deleting sample");
            sampleRepository.delete(sample.getId());
        }
    }
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) SequencingRun(ca.corefacility.bioinformatics.irida.model.run.SequencingRun) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) AnalysisSubmission(ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) HashSet(java.util.HashSet) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 48 with SampleSequencingObjectJoin

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

the class SampleServiceImpl method addSequencingObjectToSample.

/**
 * Add a {@link SequencingObject} to a {@link Sample} after testing if it
 * exists in a {@link Sample} already
 *
 * @param sample
 *            {@link Sample} to add to
 * @param seqObject
 *            {@link SequencingObject} to add
 * @return a {@link SampleSequencingObjectJoin}
 */
@Transactional
private SampleSequencingObjectJoin addSequencingObjectToSample(Sample sample, SequencingObject seqObject) {
    // the two entities.
    if (ssoRepository.getSampleForSequencingObject(seqObject) != null) {
        throw new EntityExistsException("This sequencefile is already associated with a sample");
    }
    logger.trace("adding " + seqObject.getId() + " to sample " + sample.getId());
    SampleSequencingObjectJoin join = new SampleSequencingObjectJoin(sample, seqObject);
    return ssoRepository.save(join);
}
Also used : EntityExistsException(javax.persistence.EntityExistsException) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) Transactional(org.springframework.transaction.annotation.Transactional)

Example 49 with SampleSequencingObjectJoin

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

the class SampleServiceImpl method removeSequencingObjectFromSample.

/**
 * {@inheritDoc}
 */
@Override
@Transactional
@PreAuthorize("hasPermission(#sample, 'canUpdateSample')")
public void removeSequencingObjectFromSample(Sample sample, SequencingObject object) {
    SampleSequencingObjectJoin readObjectForSample = ssoRepository.readObjectForSample(sample, object.getId());
    ssoRepository.delete(readObjectForSample);
}
Also used : SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) Transactional(org.springframework.transaction.annotation.Transactional)

Example 50 with SampleSequencingObjectJoin

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

the class ProjectSynchronizationService method syncSample.

/**
 * Synchronize a given {@link Sample} to the local installation.
 *
 * @param sample          the {@link Sample} to synchronize. This should have been read
 *                        from a remote api.
 * @param project         The {@link Project} the {@link Sample} belongs in.
 * @param existingSamples A map of samples that have already been synchronized.  These will be checked to see if they've been updated
 */
public void syncSample(Sample sample, Project project, Map<String, Sample> existingSamples) {
    Sample localSample;
    if (existingSamples.containsKey(sample.getRemoteStatus().getURL())) {
        // if the sample already exists check if it's been updated
        localSample = existingSamples.get(sample.getRemoteStatus().getURL());
        // if there's changes, update the sample
        if (checkForChanges(localSample.getRemoteStatus(), sample)) {
            logger.debug("found changes for sample " + sample.getSelfHref());
            // ensure the ids are properly set
            sample = updateIds(localSample, sample);
            sample.getRemoteStatus().setSyncStatus(SyncStatus.UPDATING);
            localSample = sampleService.update(sample);
        }
    } else {
        // if the sample doesn't already exist create it
        sample.getRemoteStatus().setSyncStatus(SyncStatus.UPDATING);
        localSample = sampleService.create(sample);
        projectService.addSampleToProject(project, sample, true);
    }
    // get the local files and organize by their url
    Collection<SampleSequencingObjectJoin> localObjects = objectService.getSequencingObjectsForSample(localSample);
    Map<String, SequencingObject> objectsByUrl = new HashMap<>();
    localObjects.forEach(j -> {
        SequencingObject pair = j.getObject();
        // concatenated it
        if (pair.getRemoteStatus() != null) {
            String url = pair.getRemoteStatus().getURL();
            objectsByUrl.put(url, pair);
        }
    });
    List<SequenceFilePair> sequenceFilePairsForSample = pairRemoteService.getSequenceFilePairsForSample(sample);
    List<ProjectSynchronizationException> syncErrors = new ArrayList<>();
    for (SequenceFilePair pair : sequenceFilePairsForSample) {
        if (!objectsByUrl.containsKey(pair.getRemoteStatus().getURL())) {
            pair.setId(null);
            try {
                syncSequenceFilePair(pair, localSample);
            } catch (ProjectSynchronizationException e) {
                syncErrors.add(e);
            }
        }
    }
    List<SingleEndSequenceFile> unpairedFilesForSample = singleEndRemoteService.getUnpairedFilesForSample(sample);
    for (SingleEndSequenceFile file : unpairedFilesForSample) {
        if (!objectsByUrl.containsKey(file.getRemoteStatus().getURL())) {
            file.setId(null);
            try {
                syncSingleEndSequenceFile(file, localSample);
            } catch (ProjectSynchronizationException e) {
                syncErrors.add(e);
            }
        }
    }
    if (syncErrors.isEmpty()) {
        localSample.getRemoteStatus().setSyncStatus(SyncStatus.SYNCHRONIZED);
    } else {
        localSample.getRemoteStatus().setSyncStatus(SyncStatus.ERROR);
        logger.error("Setting sample " + localSample.getId() + "sync status to ERROR due to sync errors with files");
    }
    sampleService.update(localSample);
}
Also used : SequencingObject(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject) HashMap(java.util.HashMap) Sample(ca.corefacility.bioinformatics.irida.model.sample.Sample) ProjectSynchronizationException(ca.corefacility.bioinformatics.irida.exceptions.ProjectSynchronizationException) ArrayList(java.util.ArrayList) SingleEndSequenceFile(ca.corefacility.bioinformatics.irida.model.sequenceFile.SingleEndSequenceFile) SequenceFilePair(ca.corefacility.bioinformatics.irida.model.sequenceFile.SequenceFilePair) SampleSequencingObjectJoin(ca.corefacility.bioinformatics.irida.model.sample.SampleSequencingObjectJoin)

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