Skip to content

Commit

Permalink
EVA-3503 Created new endpoint to search contig by MD5 checksum (#127)
Browse files Browse the repository at this point in the history
* add end-point for getting chromosomes using md5checksum
  • Loading branch information
nitin-ebi authored Feb 15, 2024
1 parent edd5c2e commit 7b23066
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,21 @@ public ResponseEntity<PagedModel<EntityModel<SequenceEntity>>> 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<PagedModel<EntityModel<SequenceEntity>>> 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<EntityModel<SequenceEntity>> pagedModel = handler.getSequencesByMD5Checksum(md5Checksum, pageRequest);
return createAppropriateResponseEntity(pagedModel);
}

private void linkPagedModelGetAssemblyByAuthority(
String accession, String authority, PagedModel pagedModel) {
ResponseEntity<PagedModel<EntityModel<AssemblyEntity>>> method;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ public PagedModel<EntityModel<SequenceEntity>> getSequencesByName(
return generatePagedModelFromPage(createSequencePage(chrPage), sequenceAssembler);
}

public PagedModel<EntityModel<SequenceEntity>> getSequencesByMD5Checksum(String md5Checksum, Pageable request) {
Page<ChromosomeEntity> chrPage = chromosomeService.getChromosomesByMD5Checksum(md5Checksum, request);
return generatePagedModelFromPage(createSequencePage(chrPage), sequenceAssembler);
}

private Page<SequenceEntity> createSequencePage(Page<? extends SequenceEntity> page) {
return new PageImpl<>(page.getContent().stream().map(e -> (SequenceEntity)e)
.collect(Collectors.toList()), page.getPageable(), page.getTotalElements());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ Page<ChromosomeEntity> findChromosomeEntitiesByAssemblyInsdcAccessionOrAssemblyR

Page<ChromosomeEntity> findChromosomeEntitiesByUcscName(String ucscName, Pageable request);

Page<ChromosomeEntity> findChromosomeEntitiesByMd5checksumOrderByInsdcAccession(String md5Checksum, Pageable request);

long countChromosomeEntitiesByInsdcAccession(String insdcAccession);

long countChromosomeEntitiesByRefseq(String refseq);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public Page<ChromosomeEntity> getChromosomesByEnaNameAndAssembly(
return injectAssemblyIntoChromosomes(page, assembly);
}

public Page<ChromosomeEntity> getChromosomesByMD5Checksum(String md5Checksum, Pageable request) {
Page<ChromosomeEntity> chrPage = repository.findChromosomeEntitiesByMd5checksumOrderByInsdcAccession(md5Checksum, request);
return stripChromosomesAndScaffoldsFromAssembly(chrPage);
}

private Page<ChromosomeEntity> injectAssemblyIntoChromosomes(Page<ChromosomeEntity> page, AssemblyEntity assembly) {
if (page != null && page.getTotalElements() > 0) {
page.forEach(it -> it.setAssembly(assembly));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ChromosomeEntity> chrPage = service.getChromosomesByMD5Checksum(testMD5Checksum, Pageable.unpaged());
List<ChromosomeEntity> chromosomeList = chrPage.getContent();

assertEquals(1, chromosomeList.size());
assertChromosomesIdentical(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<ChromosomeEntity> chrPage = service.getChromosomesByMD5Checksum(testMD5Checksum, Pageable.unpaged());

List<ChromosomeEntity> chromosomeList = chrPage.getContent().stream()
.sorted(Comparator.comparing(c -> c.getInsdcAccession()))
.collect(Collectors.toList());
assertEquals(2, chromosomeList.size());

assertEquals(testMD5Checksum, chromosomeList.get(0).getMd5checksum());
assertChromosomesIdentical(chromosomeWithMD51, chromosomeList.get(0));
assertEquals("assembly1", chromosomeList.get(0).getAssembly().getInsdcAccession());

assertEquals(testMD5Checksum, chromosomeList.get(1).getMd5checksum());
assertChromosomesIdentical(chromosomeWithMD52, chromosomeList.get(1));
assertEquals("assembly2", chromosomeList.get(1).getAssembly().getInsdcAccession());
}

void assertChromosomePageIdenticalToEntity(Page<ChromosomeEntity> page) {
assertNotNull(page);
assertTrue(page.getTotalElements() > 0);
Expand All @@ -129,4 +183,12 @@ void assertChromosomeIdenticalToEntity(ChromosomeEntity chromosomeEntity) {
assertEquals(entity.getEnaSequenceName(), chromosomeEntity.getEnaSequenceName());
}

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

}

0 comments on commit 7b23066

Please sign in to comment.