Skip to content

Commit

Permalink
Merge pull request #1466 from deepsidhu85/fix-set-default-object-null
Browse files Browse the repository at this point in the history
Set Default Sequencing Object/Genome Assembly to null when deleted
  • Loading branch information
ericenns authored Feb 14, 2023
2 parents 492ffd1 + b181bb7 commit 93c9b3a
Show file tree
Hide file tree
Showing 14 changed files with 143 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [Developer]: Added support for cloud based storage. Currently, Microsoft Azure Blob and Amazon AWS S3 are supported. [See PR 1194](https://github.com/phac-nml/irida/pull/1194)
* [Developer]: Updated metadata uploader to use react-router-dom Outlet. [See PR 1464](https://github.com/phac-nml/irida/pull/1464)
* [Developer]: Deprecated "/api/projects/{projectId}/samples/bySequencerId/{seqeuncerId}" in favour of "/api/projects/{projectId}/samples/bySampleName", which accepts a json property "sampleName"
* [Developer]: Fixed bug in setting a `default_sequencing_object and default_genome_assembly to `NULL` for a sample when the default sequencing object or genome assembly were removed. [See PR 1466](https://github.com/phac-nml/irida/pull/1466)

## [22.09.7] - 2022/01/24
* [UI]: Fixed bugs on NCBI Export page preventing the NCBI `submission.xml` file from being properly written. See [PR 1451](https://github.com/phac-nml/irida/pull/1451)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,18 @@ public interface SampleRepositoryCustom {
* @param modifiedDate The new {@link Date}
*/
void updateSampleModifiedDate(Sample sample, Date modifiedDate);

/**
* Update the default sequencing object in a {@link Sample} to NULL.
*
* @param sample The {@link Sample} to update
*/
void removeDefaultSequencingObject(Sample sample);

/**
* Update the default genome assembly in a {@link Sample} to NULL.
*
* @param sample The {@link Sample} to update
*/
void removeDefaultGenomeAssembly(Sample sample);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,19 @@ public void updateSampleModifiedDate(Sample sample, Date modifiedDate) {
query.setParameter(2, sample.getId());
query.executeUpdate();
}

@Transactional
public void removeDefaultSequencingObject(Sample sample) {
Query query = entityManager.createNativeQuery("UPDATE sample SET default_sequencing_object = NULL where id = ?");
query.setParameter(1, sample.getId());
query.executeUpdate();
}


@Transactional
public void removeDefaultGenomeAssembly(Sample sample) {
Query query = entityManager.createNativeQuery("UPDATE sample SET default_genome_assembly = NULL where id = ?");
query.setParameter(1, sample.getId());
query.executeUpdate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -617,11 +617,6 @@ public String deleteSequencingObjectFromSample(Long sampleId, Long sequencingObj
SequencingObject sequencingObject = sequencingObjectService.read(sequencingObjectId);

try {
if (sample.getDefaultSequencingObject() != null
&& sample.getDefaultSequencingObject().getId() == sequencingObjectId) {
sample.setDefaultSequencingObject(null);
sampleService.update(sample);
}
sampleService.removeSequencingObjectFromSample(sample, sequencingObject);
return messageSource.getMessage("server.SampleFiles.removeSequencingObjectSuccess", new Object[] {},
locale);
Expand All @@ -644,11 +639,6 @@ public String deleteGenomeAssemblyFromSample(Long sampleId, Long genomeAssemblyI
GenomeAssembly genomeAssembly = genomeAssemblyService.getGenomeAssemblyForSample(sample, genomeAssemblyId);

try {
if (sample.getDefaultGenomeAssembly() != null
&& sample.getDefaultGenomeAssembly().getId() == genomeAssemblyId) {
sample.setDefaultGenomeAssembly(null);
sampleService.update(sample);
}
genomeAssemblyService.removeGenomeAssemblyFromSample(sample, genomeAssemblyId);
return messageSource.getMessage("server.SampleFiles.removeGenomeAssemblySuccess", new Object[] {}, locale);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.repositories.assembly.GenomeAssemblyRepository;
import ca.corefacility.bioinformatics.irida.repositories.joins.sample.SampleGenomeAssemblyJoinRepository;
import ca.corefacility.bioinformatics.irida.repositories.sample.SampleRepository;
import ca.corefacility.bioinformatics.irida.service.GenomeAssemblyService;
import ca.corefacility.bioinformatics.irida.service.sample.SampleService;

/**
* Service implementation for storing and retrieving {@link GenomeAssembly}
Expand All @@ -27,11 +29,18 @@ public class GenomeAssemblyServiceImpl extends CRUDServiceImpl<Long, GenomeAssem
private static final Logger logger = LoggerFactory.getLogger(GenomeAssemblyServiceImpl.class);
private final SampleGenomeAssemblyJoinRepository sampleGenomeAssemblyJoinRepository;

private final SampleService sampleService;

private SampleRepository sampleRepository;

@Autowired
public GenomeAssemblyServiceImpl(GenomeAssemblyRepository repository,
SampleGenomeAssemblyJoinRepository sampleGenomeAssemblyJoinRepository, Validator validator) {
SampleGenomeAssemblyJoinRepository sampleGenomeAssemblyJoinRepository, Validator validator,
SampleService sampleService, SampleRepository sampleRepository) {
super(repository, validator, GenomeAssembly.class);
this.sampleGenomeAssemblyJoinRepository = sampleGenomeAssemblyJoinRepository;
this.sampleService = sampleService;
this.sampleRepository = sampleRepository;
}

/**
Expand Down Expand Up @@ -85,7 +94,11 @@ public void removeGenomeAssemblyFromSample(Sample sample, Long genomeAssemblyId)
genomeAssemblyId);
if (join != null) {
logger.debug("Removing genome assembly [" + genomeAssemblyId + "] from sample [" + sample.getId() + "]");
sampleGenomeAssemblyJoinRepository.deleteById(join.getId());
sampleGenomeAssemblyJoinRepository.delete(join);
if (sample.getDefaultGenomeAssembly() != null
&& sample.getDefaultGenomeAssembly().getId().equals(genomeAssemblyId)) {
sampleRepository.removeDefaultGenomeAssembly(sample);
}
} else {
logger.trace("Genome assembly [" + genomeAssemblyId + "] is not associated with sample [" + sample.getId()
+ "]. Ignoring.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ public Map<String, List<Long>> getSampleIdsBySampleNameForProjects(List<Long> pr
public void removeSequencingObjectFromSample(Sample sample, SequencingObject object) {
SampleSequencingObjectJoin readObjectForSample = ssoRepository.readObjectForSample(sample, object.getId());
ssoRepository.delete(readObjectForSample);
if (sample.getDefaultSequencingObject() != null
&& sample.getDefaultSequencingObject().getId().equals(object.getId())) {
sampleRepository.removeDefaultSequencingObject(sample);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public synchronized void findMarkedProjectsToSync() {
*
* @param project the {@link Project} to synchronize. This should have been read from a remote api.
*/
private void syncProject(Project project) {
private void syncProject(Project project) throws Exception {
SyncStatus syncType = project.getRemoteStatus().getSyncStatus();

project.getRemoteStatus().setSyncStatus(SyncStatus.UPDATING);
Expand Down Expand Up @@ -305,7 +305,7 @@ private void syncProject(Project project) {
* @return A list of {@link ProjectSynchronizationException}s, empty if no errors.
*/
public List<ProjectSynchronizationException> syncSample(Sample sample, Project project,
Map<String, Sample> existingSamples) {
Map<String, Sample> existingSamples) throws Exception {
Sample localSample;

if (existingSamples.containsKey(sample.getRemoteStatus().getURL())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public Page<ProjectSampleJoin> getSamplesForProjectWithName(Project project, Str
* @param sample {@link Sample} to remove sequences from
* @param object {@link SequencingObject} to remove
*/
public void removeSequencingObjectFromSample(Sample sample, SequencingObject object);
public void removeSequencingObjectFromSample(Sample sample, SequencingObject object) throws Exception;

/**
* Merge multiple samples into one. Merging samples copies the {@link SequenceFile} references from the set of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ public ResponseResource<SequencingObject> addNewSequenceFilePairToSample(@PathVa
description = "Delete the sequencing object from a given sample.", tags = "samples")
@RequestMapping(value = "/api/samples/{sampleId}/{objectType}/{objectId}", method = RequestMethod.DELETE)
public ResponseResource<RootResource> removeSequenceFileFromSample(@PathVariable Long sampleId,
@PathVariable String objectType, @PathVariable Long objectId) {
@PathVariable String objectType, @PathVariable Long objectId) throws Exception {
// load the project, sample and sequence file from the database
Sample s = sampleService.read(sampleId);
SequencingObject seqObject = sequencingObjectService.readSequencingObjectForSample(s, objectId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void hashChangesWithRemovedSample() {

@WithMockUser(username = "admin", roles = "ADMIN")
@Test
public void hashChangesWithRemovedSequencingObject() {
public void hashChangesWithRemovedSequencingObject() throws Exception {
Project project = projectService.read(2L);

Integer originalHash = hashingService.getProjectHash(project);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ public void testSyncProjectsUnauthorized() {
}

@Test
public void testSyncNewSample() {
public void testSyncNewSample() throws Exception {
Sample sample = new Sample();
RemoteStatus sampleStatus = new RemoteStatus("http://sample", api);
sample.setRemoteStatus(sampleStatus);
Expand All @@ -215,7 +215,7 @@ public void testSyncNewSample() {
}

@Test
public void testExistingSample() {
public void testExistingSample() throws Exception {
Sample sample = new Sample();
RemoteStatus sampleStatus = new RemoteStatus("http://sample", api);
sample.setRemoteStatus(sampleStatus);
Expand All @@ -233,7 +233,7 @@ public void testExistingSample() {
}

@Test
public void testSyncSampleNoAssemblies() {
public void testSyncSampleNoAssemblies() throws Exception {
Sample sample = new Sample();
RemoteStatus sampleStatus = new RemoteStatus("http://sample", api);
sample.setRemoteStatus(sampleStatus);
Expand All @@ -253,7 +253,7 @@ public void testSyncSampleNoAssemblies() {
}

@Test
public void testSyncExistingSampleWithDeletedFiles() {
public void testSyncExistingSampleWithDeletedFiles() throws Exception {
Sample sample = new Sample();
RemoteStatus sampleStatus = new RemoteStatus("http://sample", api);
sample.setRemoteStatus(sampleStatus);
Expand All @@ -278,7 +278,7 @@ public void testSyncExistingSampleWithDeletedFiles() {
}

@Test
public void testSyncExistingSampleWithDeletedAssemblies() {
public void testSyncExistingSampleWithDeletedAssemblies() throws Exception {
Sample sample = new Sample();
RemoteStatus sampleStatus = new RemoteStatus("http://sample", api);
sample.setRemoteStatus(sampleStatus);
Expand Down Expand Up @@ -321,7 +321,7 @@ public void testSyncFiles() {
}

@Test
public void testSyncSampleNoFast5() {
public void testSyncSampleNoFast5() throws Exception {
Sample sample = new Sample();
RemoteStatus sampleStatus = new RemoteStatus("http://sample", api);
sample.setRemoteStatus(sampleStatus);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import ca.corefacility.bioinformatics.irida.annotation.ServiceIntegrationTest;
import ca.corefacility.bioinformatics.irida.exceptions.EntityNotFoundException;
import ca.corefacility.bioinformatics.irida.exceptions.SequenceFileAnalysisException;
import ca.corefacility.bioinformatics.irida.model.assembly.GenomeAssembly;
import ca.corefacility.bioinformatics.irida.model.joins.impl.ProjectSampleJoin;
import ca.corefacility.bioinformatics.irida.model.project.Project;
import ca.corefacility.bioinformatics.irida.model.project.ReferenceFile;
Expand All @@ -26,9 +27,11 @@
import ca.corefacility.bioinformatics.irida.model.sample.Sample;
import ca.corefacility.bioinformatics.irida.model.sample.metadata.MetadataEntry;
import ca.corefacility.bioinformatics.irida.model.sample.metadata.ProjectMetadataResponse;
import ca.corefacility.bioinformatics.irida.model.sequenceFile.SequencingObject;
import ca.corefacility.bioinformatics.irida.model.workflow.submission.AnalysisSubmission;
import ca.corefacility.bioinformatics.irida.repositories.joins.sample.SampleGenomeAssemblyJoinRepository;
import ca.corefacility.bioinformatics.irida.service.AnalysisSubmissionService;
import ca.corefacility.bioinformatics.irida.service.GenomeAssemblyService;
import ca.corefacility.bioinformatics.irida.service.ProjectService;
import ca.corefacility.bioinformatics.irida.service.SequencingObjectService;
import ca.corefacility.bioinformatics.irida.service.sample.MetadataTemplateService;
Expand Down Expand Up @@ -63,6 +66,9 @@ public class SampleServiceImplIT {
@Autowired
private MetadataTemplateService metadataTemplateService;

@Autowired
private GenomeAssemblyService genomeAssemblyService;

/**
* Variation in a floating point number to be considered equal.
*/
Expand All @@ -80,6 +86,37 @@ public void testCreateSample() {
assertEquals(sampleName, saved.getSampleName(), "Wrong name was saved.");
}

@Test
@WithMockUser(username = "fbristow", roles = "ADMIN")
public void removeSampleDefaultSequencingObject() throws Exception {
Sample sample = sampleService.read(9L);
SequencingObject sequencingObject = objectService.read(2L);
sample.setDefaultSequencingObject(sequencingObject);
sampleService.update(sample);

assertNotNull(sample.getDefaultSequencingObject());
sampleService.removeSequencingObjectFromSample(sample, sequencingObject);

Sample sampleAfterDelete = sampleService.read(9L);
assertNull(sampleAfterDelete.getDefaultSequencingObject());
}

@Test
@WithMockUser(username = "fbristow", roles = "ADMIN")
public void removeSampleDefaultGenomeAssembly() {
Sample sample = sampleService.read(2L);
GenomeAssembly genomeAssembly = sampleGenomeAssemblyJoinRepository.findBySampleAndAssemblyId(sample.getId(), 2L)
.getObject();
sample.setDefaultGenomeAssembly(genomeAssembly);
sampleService.update(sample);

assertNotNull(sample.getDefaultGenomeAssembly());
genomeAssemblyService.removeGenomeAssemblyFromSample(sample, genomeAssembly.getId());

Sample sampleAfterDelete = sampleService.read(2L);
assertNull(sampleAfterDelete.getDefaultGenomeAssembly());
}

/**
* Straightforward merging of samples all belonging to the same project.
*/
Expand Down
Loading

0 comments on commit 93c9b3a

Please sign in to comment.