Commit ef03c1a9 authored by cnguyen2's avatar cnguyen2
Browse files

Add search by symbol, fullname and annotationId

parent f3ada804
Pipeline #164552 passed with stages
in 9 minutes and 33 seconds
......@@ -5,7 +5,7 @@ import java.util.Set;
import io.quarkus.runtime.annotations.RegisterForReflection;
@RegisterForReflection
public class FlybaseGene {
public class DrosophilaGene {
private String id;
private String taxanomyId;
private String type;
......@@ -15,7 +15,7 @@ public class FlybaseGene {
private String transcriptType;
private Set<String> transcriptIds;
public FlybaseGene(String id, String taxanomyId, String type, String symbol,
public DrosophilaGene(String id, String taxanomyId, String type, String symbol,
String fullname, String annotationId, String transcriptType,
Set<String> transcriptIds) {
super();
......
package de.unigoettingen.ibeetlebase.geneinfo.repository;
import java.util.Collection;
import de.unigoettingen.ibeetlebase.geneinfo.model.DrosophilaGene;
public interface DrosophilaGeneRepository extends Repository<DrosophilaGene> {
Collection<DrosophilaGene> getBySymbol(String symbol);
Collection<DrosophilaGene> getByFullname(String fullname);
Collection<DrosophilaGene> getByAnnotationId(String annotationId);
}
\ No newline at end of file
package de.unigoettingen.ibeetlebase.geneinfo.repository;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toCollection;
import static java.util.stream.Collectors.toMap;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import de.unigoettingen.ibeetlebase.geneinfo.model.DrosophilaGene;
import de.unigoettingen.ibeetlebase.geneinfo.model.Gene;
import de.unigoettingen.ibeetlebase.geneinfo.service.FlyBase;
import io.quarkus.runtime.Startup;
@Startup
@ApplicationScoped
public class InMemoryDrosophilaGeneRepository implements DrosophilaGeneRepository {
@Inject
FlyBase service;
private Map<String, DrosophilaGene> mapFromId;
private Map<String, Collection<DrosophilaGene>> mapFromSymbol;
private Map<String, Collection<DrosophilaGene>> mapFromFullname;
private Map<String, Collection<DrosophilaGene>> mapFromAnnotationId;
@Override
public DrosophilaGene get(String id) {
return mapFromId.get(id);
}
@Override
public Collection<DrosophilaGene> get() {
return mapFromId.values();
}
@Override
public Collection<DrosophilaGene> getBySymbol(String symbol) {
return mapFromSymbol.getOrDefault(symbol, Collections.emptyList());
}
@Override
public Collection<DrosophilaGene> getByFullname(String fullname) {
return mapFromFullname.getOrDefault(fullname, Collections.emptyList());
}
@Override
public Collection<DrosophilaGene> getByAnnotationId(String annotationId) {
return mapFromAnnotationId.getOrDefault(annotationId, Collections.emptyList());
}
@PostConstruct
void init() {
loadData();
}
private void loadData() {
mapFromId = service.getDB().entrySet()
.stream()
.collect(toMap(Map.Entry::getKey,
entry -> convertToFlybaseGene(entry.getValue())));
mapFromSymbol = mapFromId.values()
.stream()
.collect(groupingBy(DrosophilaGene::getSymbol, toCollection(LinkedList::new)));
mapFromFullname = mapFromId.values()
.stream()
.collect(groupingBy(DrosophilaGene::getFullname, toCollection(LinkedList::new)));
mapFromAnnotationId = mapFromId.values()
.stream()
.collect(groupingBy(DrosophilaGene::getAnnotationId, toCollection(LinkedList::new)));
}
private DrosophilaGene convertToFlybaseGene(Gene gene) {
Map<String, Collection<String>> info = gene.getInformation();
return new DrosophilaGene(gene.getGeneID(),
collectionToString(organismToTaxonomyId(info.get("organism"))),
collectionToString(info.get("gene_type")),
collectionToString(info.get("gene_symbol")),
collectionToString(info.get("gene_fullname")),
collectionToString(info.get("annotation_ID")),
collectionToString(info.get("transcript_Type")),
info.get("transcript_ID").stream().collect(Collectors.toSet()));
}
private Collection<String> organismToTaxonomyId(Collection<String> organisms) {
return organisms.stream().map(o -> {
switch (o) {
case "Dana":
return "7217";
case "Dmel":
return "7227";
case "Dvir":
return "7244";
case "Dpse":
return "7237";
case "Dsim":
return "7240";
}
return null;
}).collect(Collectors.toList());
}
private String collectionToString(Collection<String> items) {
return items.stream().collect(Collectors.joining(", "));
}
}
package de.unigoettingen.ibeetlebase.geneinfo.repository;
import static java.util.stream.Collectors.toMap;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.PostConstruct;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import de.unigoettingen.ibeetlebase.geneinfo.model.Gene;
import de.unigoettingen.ibeetlebase.geneinfo.model.TriboliumGene;
import de.unigoettingen.ibeetlebase.geneinfo.service.Tribolium;
import io.quarkus.runtime.Startup;
@Startup
@ApplicationScoped
public class InMemoryTriboliumGeneRepository implements TriboliumGeneRepository {
@Inject
Tribolium service;
private Map<String, TriboliumGene> mapFromId;
@Override
public TriboliumGene get(String id) {
return mapFromId.get(id);
}
@Override
public Collection<TriboliumGene> get() {
return mapFromId.values();
}
@PostConstruct
void init() {
loadData();
}
private void loadData() {
mapFromId = service.getDB().entrySet()
.stream()
.collect(toMap(Map.Entry::getKey,
entry -> convertToTriboliumGene(entry.getValue())));
}
private TriboliumGene convertToTriboliumGene(Gene gene) {
Map<String, Collection<String>> info = gene.getInformation();
return new TriboliumGene(gene.getGeneID(),
"7070",
collectionToString(info.get("seqname")),
collectionToString(info.get("source")),
collectionToString(info.get("feature")),
collectionToString(info.get("start")),
collectionToString(info.get("end")),
collectionToString(info.get("score")),
collectionToString(info.get("strand")),
collectionToString(info.get("frame")),
collectionToString(info.get("locus_tag")));
}
private String collectionToString(Collection<String> items) {
return items.stream().collect(Collectors.joining(", "));
}
}
package de.unigoettingen.ibeetlebase.geneinfo.repository;
import java.util.Collection;
public interface Repository<T> {
T get(String id);
Collection<T> get();
}
package de.unigoettingen.ibeetlebase.geneinfo.repository;
import de.unigoettingen.ibeetlebase.geneinfo.model.TriboliumGene;
public interface TriboliumGeneRepository extends Repository<TriboliumGene>{
}
package de.unigoettingen.ibeetlebase.geneinfo.resource;
import static java.util.stream.Collectors.toList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.inject.Inject;
import javax.ws.rs.GET;
......@@ -14,63 +16,97 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.eclipse.microprofile.metrics.annotation.Metered;
import org.eclipse.microprofile.openapi.annotations.Operation;
import org.eclipse.microprofile.openapi.annotations.enums.ParameterIn;
import org.eclipse.microprofile.openapi.annotations.enums.SchemaType;
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.ExampleObject;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.Parameter;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import de.unigoettingen.ibeetlebase.geneinfo.model.FlybaseGene;
import de.unigoettingen.ibeetlebase.geneinfo.model.Gene;
import de.unigoettingen.ibeetlebase.geneinfo.model.DrosophilaGene;
import de.unigoettingen.ibeetlebase.geneinfo.model.TriboliumGene;
import de.unigoettingen.ibeetlebase.geneinfo.service.FlyBase;
import de.unigoettingen.ibeetlebase.geneinfo.service.OrthoAPI;
import de.unigoettingen.ibeetlebase.geneinfo.service.Tribolium;
import de.unigoettingen.ibeetlebase.geneinfo.repository.DrosophilaGeneRepository;
import de.unigoettingen.ibeetlebase.geneinfo.repository.TriboliumGeneRepository;
@Path("/")
@Produces(MediaType.APPLICATION_JSON)
//@QuarkusMain
public class GeneResource { //implements QuarkusApplication {
// private static final String VERSION_STRING = "/{dataversion : [0-9]{6}|current}/v1";
public class GeneResource {
@Inject
FlyBase fb;
@Inject
Tribolium tb;
DrosophilaGeneRepository dGeneRepo;
@Inject
OrthoAPI oa;
TriboliumGeneRepository tGeneRepo;
@GET
@Metered
@Path("/flybase/genes/{id: FBgn[0-9]{7}}")
@Operation(summary = "Get information for a fly gene.")
public FlybaseGene getFlybaseGene(
public DrosophilaGene getFlybaseGene(
@Parameter(
description = "Flybase gene identifier in format FBgn[0-9]{7}.",
example = "FBgn0000015")
@PathParam("id") String id) {
return Optional.ofNullable(fb.retrieve(id)).map(this::convertToFlybaseGene).orElse(null);
return dGeneRepo.get(id);
}
@GET
@Metered
@Path("/flybase/genes")
@Operation(summary = "Get information for a list of fly genes")
public List<FlybaseGene> getFlybaseGenes(
@APIResponse(
responseCode = "200",
description = "A list of found genes.",
content = @Content(
mediaType = "application/json",
schema = @Schema(type = SchemaType.ARRAY, implementation = DrosophilaGene.class)))
@APIResponse(
responseCode = "400",
description = "Zero or more than one query params are provided.")
public Response getFlybaseGenes(
@Parameter(
in = ParameterIn.QUERY,
description = "Flybase gene identifiers in format FBgn[0-9]{7}",
examples = @ExampleObject(name = "FBgn0000015", value = "FBgn0000015"))
@QueryParam("ids") Set<String> ids) {
return ids.stream()
.filter(id -> checkFBgnID(id))
.map(id -> fb.retrieve(id))
.filter(gene -> gene != null)
.map(this::convertToFlybaseGene)
.collect(Collectors.toList());
description = "Flybase gene identifiers in format FBgn[0-9]{7}")
@QueryParam("ids") Set<String> ids,
@QueryParam("symbol") String symbol,
@Parameter(
in = ParameterIn.QUERY,
example = "knirps")
@QueryParam("fullname") String fullname,
@QueryParam("annotationId") String annotationId) {
long paramCnt = Stream.of(symbol, fullname, annotationId).filter(p -> p != null).count();
if (paramCnt > 0 && ids.size() > 0 || ids.size() == 0 && paramCnt != 1) {
return Response.status(Status.BAD_REQUEST).build();
}
Collection<DrosophilaGene> dGenes;
if (!ids.isEmpty()) {
dGenes = ids.stream()
.filter(id -> checkFBgnID(id))
.map(id -> dGeneRepo.get(id))
.filter(gene -> gene != null)
.collect(Collectors.toList());
} else if (symbol != null) {
dGenes = dGeneRepo.getBySymbol(symbol);
} else if (fullname != null) {
dGenes = dGeneRepo.getByFullname(fullname);
} else if (annotationId != null) {
dGenes = dGeneRepo.getByAnnotationId(annotationId);
} else {
dGenes = Collections.emptyList();
}
return Response.ok(dGenes.stream().collect(toList())).build();
}
......@@ -82,8 +118,8 @@ public class GeneResource { //implements QuarkusApplication {
@Parameter(
description = "Tribolium gene identifier in format TC[0-9]{6}}.",
example = "TC001906")
@PathParam("id") String tc) {
return Optional.ofNullable(tb.retrieve(tc)).map(this::convertToTriboliumGene).orElse(null);
@PathParam("id") String id) {
return tGeneRepo.get(id);
}
......@@ -99,9 +135,8 @@ public class GeneResource { //implements QuarkusApplication {
@QueryParam("ids") Set<String> ids) {
return ids.stream()
.filter(id -> checkTCID(id))
.map(id -> tb.retrieve(id))
.map(id -> tGeneRepo.get(id))
.filter(gene -> gene != null)
.map(this::convertToTriboliumGene)
.collect(Collectors.toList());
}
......@@ -122,101 +157,4 @@ public class GeneResource { //implements QuarkusApplication {
private boolean checkTCID(final String tc) {
return tc.matches("TC[0-9]{6}");
}
/**
* REST Endpoint that returns all flybase gene identifiers and their according gene names
* @param dataversion Version ID of the requested data - Required so older versions can be requested in the future
* @return All flybase gene identifiers and their according gene names
*/
/*
@GET
@Metered
@Path("/flybase/identifiers")
public Map<String, String> getAllFlybaseIDs() {
return fb.getAllIDs();
}
*/
/**
* REST Endpoint that returns all tribolium gene identifiers and their according gene names
* @param dataversion Version ID of the requested data - Required so older versions can be requested in the future
* @return All TC gene identifiers and their according gene names
*/
/*
@GET
@Metered
@Path("/tribolium/identifiers")
public Map<String, String> getAllTriboliumIDs() {
return tb.getAllIDs();
}
*/
/**
* This function (requiring Quarkus 1.4.2) is called when the application is started
* If "orthoapi" is given as a command line argument in dev mode, the OrthoDB API calls are executed
* @param args The command line arguments
* @return The exit status (0)
*/
/*
@Override
public int run(String... args) throws Exception {
// Calls the relevant OrthoDB API methods for all genes
if (args.length > 0 && args[0].equals("list_file")) {
//Should write all identifiers to a file.
}
Quarkus.waitForExit();
return 0;
}
*/
private FlybaseGene convertToFlybaseGene(Gene gene) {
Map<String, Collection<String>> info = gene.getInformation();
return new FlybaseGene(gene.getGeneID(),
collectionToString(organismToTaxonomyId(info.get("organism"))),
collectionToString(info.get("gene_type")),
collectionToString(info.get("gene_symbol")),
collectionToString(info.get("gene_fullname")),
collectionToString(info.get("annotation_ID")),
collectionToString(info.get("transcript_Type")),
info.get("transcript_ID").stream().collect(Collectors.toSet()));
}
private TriboliumGene convertToTriboliumGene(Gene gene) {
Map<String, Collection<String>> info = gene.getInformation();
return new TriboliumGene(gene.getGeneID(),
"7070",
collectionToString(info.get("seqname")),
collectionToString(info.get("source")),
collectionToString(info.get("feature")),
collectionToString(info.get("start")),
collectionToString(info.get("end")),
collectionToString(info.get("score")),
collectionToString(info.get("strand")),
collectionToString(info.get("frame")),
collectionToString(info.get("locus_tag")));
}
private Collection<String> organismToTaxonomyId(Collection<String> organisms) {
return organisms.stream().map(o -> {
switch (o) {
case "Dana":
return "7217";
case "Dmel":
return "7227";
case "Dvir":
return "7244";
case "Dpse":
return "7237";
case "Dsim":
return "7240";
}
return null;
}).collect(Collectors.toList());
}
private String collectionToString(Collection<String> items) {
return items.stream().collect(Collectors.joining(", "));
}
}
\ No newline at end of file
package de.unigoettingen.ibeetlebase.geneinfo.service;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.LinkedList;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
......@@ -26,7 +27,7 @@ public class FlyBase implements GeneSource {
* Fills the database with information from the gene file by calling the according method (only if the database is empty)
* @return The database containing all information from the FlyBase gene file
*/
private Map<String, Gene> getDB() {
public Map<String, Gene> getDB() {
if (this.database == null) {
int[] column_ind = new int[]{0,1,2,3,4,5,6,7};
String[] column_des = new String[]{"organism", "gene_type", "gene_ID", "gene_symbol", "gene_fullname",
......
package de.unigoettingen.ibeetlebase.geneinfo.service;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.LinkedList;
import java.util.Map;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
......@@ -27,7 +28,7 @@ public class Tribolium implements GeneSource {
* Fills the database with information from the gene file by calling the according method (only if the database is empty)
* @return The database containing all information from the tribolium gene file
*/
private Map<String, Gene> getDB() {
public Map<String, Gene> getDB() {
if (this.database == null) {
String[] column_des = new String[]{"seqname", "source", "feature", "start", "end", "score", "strand", "frame"};
this.database = readGeneFile(column_des);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment