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