diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/controller/contigalias/ContigAliasController.java b/src/main/java/uk/ac/ebi/eva/contigalias/controller/contigalias/ContigAliasController.java index 757c4828..fb3053d0 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/controller/contigalias/ContigAliasController.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/controller/contigalias/ContigAliasController.java @@ -322,6 +322,21 @@ public ResponseEntity>> getSequencesBySeq } + @ApiOperation(value = "Get a list of chromosomes using their MD5 checksum. ", + notes = "Given a chromosome's MD5 checksum, this endpoint will return a list of chromosomes that are " + + "associated with the MD5 checksum provided. This endpoint returns a list containing one or more chromosomes. " + + "It also accepts two additional parameters (page and size) to control pagination of results. " + + "If the page number and/or page size are invalid then an HTTP status code of 416 is returned by this endpoint.") + @GetMapping(value = "chromosomes/md5checksum/{md5Checksum}", produces = "application/json") + public ResponseEntity>> getSequencesByMD5Checksum( + @PathVariable @ApiParam(value = "MD5 Checksum of chromosome Eg: 7b6e06758e53927330346e9e7cc00cce") String md5Checksum, + @RequestParam(required = false, name = "page") @ApiParam(value = PAGE_NUMBER_DESCRIPTION) Integer pageNumber, + @RequestParam(required = false, name = "size") @ApiParam(value = PAGE_SIZE_DESCRIPTION) Integer pageSize) { + PageRequest pageRequest = createPageRequest(pageNumber, pageSize); + PagedModel> pagedModel = handler.getSequencesByMD5Checksum(md5Checksum, pageRequest); + return createAppropriateResponseEntity(pagedModel); + } + private void linkPagedModelGetAssemblyByAuthority( String accession, String authority, PagedModel pagedModel) { ResponseEntity>> method; diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/controller/contigalias/ContigAliasHandler.java b/src/main/java/uk/ac/ebi/eva/contigalias/controller/contigalias/ContigAliasHandler.java index 11f07306..a19d2841 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/controller/contigalias/ContigAliasHandler.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/controller/contigalias/ContigAliasHandler.java @@ -165,6 +165,11 @@ public PagedModel> getSequencesByName( return generatePagedModelFromPage(createSequencePage(chrPage), sequenceAssembler); } + public PagedModel> getSequencesByMD5Checksum(String md5Checksum, Pageable request) { + Page chrPage = chromosomeService.getChromosomesByMD5Checksum(md5Checksum, request); + return generatePagedModelFromPage(createSequencePage(chrPage), sequenceAssembler); + } + private Page createSequencePage(Page page) { return new PageImpl<>(page.getContent().stream().map(e -> (SequenceEntity)e) .collect(Collectors.toList()), page.getPageable(), page.getTotalElements()); diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/repo/ChromosomeRepository.java b/src/main/java/uk/ac/ebi/eva/contigalias/repo/ChromosomeRepository.java index 5979af07..3ac551a0 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/repo/ChromosomeRepository.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/repo/ChromosomeRepository.java @@ -88,6 +88,8 @@ Page findChromosomeEntitiesByAssemblyInsdcAccessionOrAssemblyR Page findChromosomeEntitiesByUcscName(String ucscName, Pageable request); + Page findChromosomeEntitiesByMd5checksum(String md5Checksum, Pageable request); + long countChromosomeEntitiesByInsdcAccession(String insdcAccession); long countChromosomeEntitiesByRefseq(String refseq); diff --git a/src/main/java/uk/ac/ebi/eva/contigalias/service/ChromosomeService.java b/src/main/java/uk/ac/ebi/eva/contigalias/service/ChromosomeService.java index 9fd11976..dbb8d092 100644 --- a/src/main/java/uk/ac/ebi/eva/contigalias/service/ChromosomeService.java +++ b/src/main/java/uk/ac/ebi/eva/contigalias/service/ChromosomeService.java @@ -173,6 +173,11 @@ public Page getChromosomesByEnaNameAndAssembly( return injectAssemblyIntoChromosomes(page, assembly); } + public Page getChromosomesByMD5Checksum(String md5Checksum, Pageable request) { + Page chrPage = repository.findChromosomeEntitiesByMd5checksum(md5Checksum, request); + return stripChromosomesAndScaffoldsFromAssembly(chrPage); + } + private Page injectAssemblyIntoChromosomes(Page page, AssemblyEntity assembly) { if (page != null && page.getTotalElements() > 0) { page.forEach(it -> it.setAssembly(assembly)); diff --git a/src/test/java/uk/ac/ebi/eva/contigalias/service/ChromosomeServiceIntegrationTest.java b/src/test/java/uk/ac/ebi/eva/contigalias/service/ChromosomeServiceIntegrationTest.java index 475cc8bd..d7198086 100644 --- a/src/test/java/uk/ac/ebi/eva/contigalias/service/ChromosomeServiceIntegrationTest.java +++ b/src/test/java/uk/ac/ebi/eva/contigalias/service/ChromosomeServiceIntegrationTest.java @@ -25,14 +25,16 @@ import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.test.context.ActiveProfiles; - +import uk.ac.ebi.eva.contigalias.entities.AssemblyEntity; import uk.ac.ebi.eva.contigalias.entities.ChromosomeEntity; import uk.ac.ebi.eva.contigalias.entitygenerator.AssemblyGenerator; import uk.ac.ebi.eva.contigalias.entitygenerator.ChromosomeGenerator; import uk.ac.ebi.eva.contigalias.repo.ChromosomeRepository; import java.util.Collections; +import java.util.Comparator; import java.util.List; +import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -115,6 +117,58 @@ void testUpdateMD5ChecksumForAllChromosomesInAssembly() { } + @Test + void testGetChromosomesByMD5Checksum() { + String testMD5Checksum = "test-MD5-checksum"; + + AssemblyEntity assemblyEntity = AssemblyGenerator.generate(); + ChromosomeEntity chromosomeWithMD5 = ChromosomeGenerator.generate(assemblyEntity); + chromosomeWithMD5.setMd5checksum(testMD5Checksum); + chromosomeRepository.save(chromosomeWithMD5); + + Page chrPage = service.getChromosomesByMD5Checksum(testMD5Checksum, Pageable.unpaged()); + List chromosomeList = chrPage.getContent(); + + assertEquals(1, chromosomeList.size()); + assertChromosomeIdenticalToEntity(chromosomeWithMD5, chromosomeList.get(0)); + assertEquals(testMD5Checksum, chromosomeList.get(0).getMd5checksum()); + assertEquals(assemblyEntity.getInsdcAccession(), chromosomeList.get(0).getAssembly().getInsdcAccession()); + } + + @Test + void testGetMultipleChromosomesByMD5ChecksumInDifferentAssemblies() { + String testMD5Checksum = "test-MD5-checksum"; + + AssemblyEntity assemblyEntity1 = AssemblyGenerator.generate(); + assemblyEntity1.setInsdcAccession("assembly1"); + ChromosomeEntity chromosomeWithMD51 = ChromosomeGenerator.generate(assemblyEntity1); + chromosomeWithMD51.setInsdcAccession("chromosome1"); + chromosomeWithMD51.setMd5checksum(testMD5Checksum); + chromosomeRepository.save(chromosomeWithMD51); + + AssemblyEntity assemblyEntity2 = AssemblyGenerator.generate(); + assemblyEntity2.setInsdcAccession("assembly2"); + ChromosomeEntity chromosomeWithMD52 = ChromosomeGenerator.generate(assemblyEntity2); + chromosomeWithMD52.setInsdcAccession("chromosome2"); + chromosomeWithMD52.setMd5checksum(testMD5Checksum); + chromosomeRepository.save(chromosomeWithMD52); + + Page chrPage = service.getChromosomesByMD5Checksum(testMD5Checksum, Pageable.unpaged()); + + List chromosomeList = chrPage.getContent().stream() + .sorted(Comparator.comparing(c -> c.getInsdcAccession())) + .collect(Collectors.toList()); + assertEquals(2, chromosomeList.size()); + + assertEquals(testMD5Checksum, chromosomeList.get(0).getMd5checksum()); + assertChromosomeIdenticalToEntity(chromosomeWithMD51, chromosomeList.get(0)); + assertEquals("assembly1", chromosomeList.get(0).getAssembly().getInsdcAccession()); + + assertEquals(testMD5Checksum, chromosomeList.get(1).getMd5checksum()); + assertChromosomeIdenticalToEntity(chromosomeWithMD52, chromosomeList.get(1)); + assertEquals("assembly2", chromosomeList.get(1).getAssembly().getInsdcAccession()); + } + void assertChromosomePageIdenticalToEntity(Page page) { assertNotNull(page); assertTrue(page.getTotalElements() > 0); @@ -129,4 +183,12 @@ void assertChromosomeIdenticalToEntity(ChromosomeEntity chromosomeEntity) { assertEquals(entity.getEnaSequenceName(), chromosomeEntity.getEnaSequenceName()); } + void assertChromosomeIdenticalToEntity(ChromosomeEntity chromosomeEntity1, ChromosomeEntity chromosomeEntity2) { + assertEquals(chromosomeEntity1.getGenbankSequenceName(), chromosomeEntity2.getGenbankSequenceName()); + assertEquals(chromosomeEntity1.getInsdcAccession(), chromosomeEntity2.getInsdcAccession()); + assertEquals(chromosomeEntity1.getRefseq(), chromosomeEntity2.getRefseq()); + assertEquals(chromosomeEntity1.getUcscName(), chromosomeEntity2.getUcscName()); + assertEquals(chromosomeEntity1.getEnaSequenceName(), chromosomeEntity2.getEnaSequenceName()); + } + }