Commit 61cbb125 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

ReferenceGenomeRestImpl removed

parent c45c81d1
Pipeline #44144 failed with stage
in 9 minutes and 30 seconds
......@@ -2,25 +2,42 @@ package lcsb.mapviewer.api.genomics;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import lcsb.mapviewer.annotation.cache.BigFileCache;
import lcsb.mapviewer.annotation.services.genome.ReferenceGenomeConnectorException;
import lcsb.mapviewer.api.BaseController;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.layout.*;
import lcsb.mapviewer.model.map.layout.ReferenceGenome;
import lcsb.mapviewer.model.map.layout.ReferenceGenomeGeneMapping;
import lcsb.mapviewer.model.map.layout.ReferenceGenomeType;
import lcsb.mapviewer.services.ObjectExistsException;
import lcsb.mapviewer.services.ObjectNotFoundException;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.*;
import lcsb.mapviewer.services.interfaces.IFileService;
import lcsb.mapviewer.services.interfaces.IMiriamService;
import lcsb.mapviewer.services.interfaces.IReferenceGenomeService;
import lcsb.mapviewer.services.utils.ReferenceGenomeExistsException;
@RestController
@RequestMapping(value = "/api/genomics", produces = MediaType.APPLICATION_JSON_VALUE)
......@@ -28,24 +45,20 @@ public class ReferenceGenomeController extends BaseController {
private Logger logger = LogManager.getLogger();
private ReferenceGenomeRestImpl referenceGenomeController;
private IReferenceGenomeService referenceGenomeService;
private IFileService fileService;
private IMiriamService miriamService;
@Autowired
public ReferenceGenomeController(ReferenceGenomeRestImpl referenceGenomeController,
IReferenceGenomeService referenceGenomeService, IFileService fileService, IMiriamService miriamService) {
this.referenceGenomeController = referenceGenomeController;
public ReferenceGenomeController(IReferenceGenomeService referenceGenomeService, IFileService fileService,
IMiriamService miriamService) {
this.referenceGenomeService = referenceGenomeService;
this.fileService = fileService;
this.miriamService = miriamService;
}
@GetMapping(value = "/taxonomies/{organismId}/genomeTypes/{type}/versions/{version}/")
public Map<String, Object> getGenomesByQuery(
@PathVariable(value = "organismId") String organismId,
@PathVariable(value = "type") String type,
public Map<String, Object> getGenomesByQuery(@PathVariable(value = "organismId") String organismId, @PathVariable(value = "type") String type,
@PathVariable(value = "version") String version) throws QueryException {
MiriamData organism;
if (organismId != null && !organismId.isEmpty()) {
......@@ -109,66 +122,181 @@ public class ReferenceGenomeController extends BaseController {
return result;
}
static class RemoteUrl {
public String url;
}
@GetMapping(value = "/taxonomies/{organismId}/genomeTypes/{type}/versions/{version}:getAvailableRemoteUrls")
public List<Map<String, Object>> getRemoteUrls(
@PathVariable(value = "organismId") String organism,
@PathVariable(value = "type") String type,
public List<RemoteUrl> getRemoteUrls(@PathVariable(value = "organismId") String organismId,
@PathVariable(value = "type") ReferenceGenomeType genomeType,
@PathVariable(value = "version") String version) throws QueryException {
return referenceGenomeController.getRemoteUrls(organism, type, version);
if (organismId == null || organismId.isEmpty()) {
throw new QueryException("Unknown taxonomy organism: " + organismId);
}
MiriamData organism = new MiriamData(MiriamType.TAXONOMY, organismId);
String url = referenceGenomeService.getUrlForGenomeVersion(genomeType, organism, version);
List<RemoteUrl> result = new ArrayList<>();
if (url != null) {
RemoteUrl row = new RemoteUrl();
row.url = url;
result.add(row);
}
return result;
}
@GetMapping(value = "/taxonomies/")
public List<Map<String, Object>> getGenomeTaxonomies() throws QueryException {
return referenceGenomeController.getReferenceGenomeTaxonomies();
try {
Set<MiriamData> organisms = new LinkedHashSet<>();
for (ReferenceGenomeType type : ReferenceGenomeType.values()) {
organisms.addAll(referenceGenomeService.getOrganismsByReferenceGenomeType(type));
}
List<Map<String, Object>> result = new ArrayList<>();
for (MiriamData miriamData : organisms) {
result.add(miriamService.serializeMiriamData(miriamData));
}
return result;
} catch (ReferenceGenomeConnectorException e) {
throw new QueryException("Problem with obtaining organism list", e);
}
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PostMapping(value = "/")
public Map<String, Object> addGenome(@RequestBody MultiValueMap<String, Object> formData)
public Object addGenome(@RequestParam(value = "organismId") String organismId, @RequestParam(value = "type") ReferenceGenomeType type,
@RequestParam(value = "version") String version, @RequestParam(value = "url") String url)
throws QueryException, IOException, ReferenceGenomeConnectorException {
return referenceGenomeController.addReferenceGenome(formData);
MiriamData organism;
if (organismId != null && !organismId.isEmpty()) {
organism = new MiriamData(MiriamType.TAXONOMY, organismId);
} else {
throw new QueryException("Unknown taxonomy organism: " + organismId);
}
try {
referenceGenomeService.addReferenceGenome(type, organism, version, url);
return new HashMap<>();
} catch (ReferenceGenomeExistsException e) {
throw new ObjectExistsException(e);
} catch (URISyntaxException e) {
throw new QueryException("Problem wih given uri", e);
}
}
@GetMapping(value = "/")
public List<Map<String, Object>> getDownloaded() {
return referenceGenomeController.getReferenceGenomes();
List<Map<String, Object>> result = new ArrayList<>();
for (ReferenceGenome genome : referenceGenomeService.getDownloadedGenomes()) {
result.add(genomeToMap(genome));
}
return result;
}
static class OrganismType {
public ReferenceGenomeType type;
}
@GetMapping(value = "/taxonomies/{organismId}/genomeTypes/")
public List<Map<String, Object>> getGenomeTaxonomyTypes(
@PathVariable(value = "organismId") String organism) throws QueryException {
return referenceGenomeController.getReferenceGenomeTypes(organism);
public List<OrganismType> getGenomeTaxonomyTypes(@PathVariable(value = "organismId") String organism) throws QueryException {
try {
List<OrganismType> result = new ArrayList<>();
for (ReferenceGenomeType type : ReferenceGenomeType.values()) {
List<MiriamData> organisms = referenceGenomeService.getOrganismsByReferenceGenomeType(type);
for (MiriamData miriamData : organisms) {
if (miriamData.getResource().equals(organism)) {
OrganismType organismType = new OrganismType();
organismType.type = type;
result.add(organismType);
}
}
}
return result;
} catch (ReferenceGenomeConnectorException e) {
throw new QueryException("Problem with obtaining organism list", e);
}
}
static class OrganismTypeVersion {
public String version;
}
@GetMapping(value = "/taxonomies/{organismId}/genomeTypes/{type}/versions/")
public List<Map<String, Object>> getGenomeVersion(
@PathVariable(value = "organismId") String organism,
@PathVariable(value = "type") String type) throws QueryException {
return referenceGenomeController.getReferenceGenomeVersions(organism, type);
public List<OrganismTypeVersion> getGenomeVersion(@PathVariable(value = "organismId") String organismId,
@PathVariable(value = "type") ReferenceGenomeType genomeType) throws QueryException {
if (organismId == null || organismId.isEmpty()) {
throw new QueryException("Unknown taxonomy organism: " + organismId);
}
MiriamData organism = new MiriamData(MiriamType.TAXONOMY, organismId);
try {
List<OrganismTypeVersion> result = new ArrayList<>();
List<String> versions = referenceGenomeService.getAvailableGenomeVersions(genomeType, organism);
for (String string : versions) {
OrganismTypeVersion entry = new OrganismTypeVersion();
entry.version = string;
result.add(entry);
}
return result;
} catch (ReferenceGenomeConnectorException e) {
throw new QueryException("Problem with obtaining version list", e);
}
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@DeleteMapping(value = "/{genomeId}/")
public Map<String, Object> removeGenome(@PathVariable(value = "genomeId") String genomeId)
throws IOException, QueryException {
return referenceGenomeController.removeGenome(genomeId);
public Object removeGenome(@PathVariable(value = "genomeId") Integer genomeId) throws IOException, QueryException {
if (genomeId == null) {
throw new QueryException("Invalid genomeId");
}
ReferenceGenome genome = referenceGenomeService.getReferenceGenomeById(genomeId);
if (genome == null) {
throw new ObjectNotFoundException("Object does not exist");
}
referenceGenomeService.removeGenome(genome);
return new HashMap<>();
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@DeleteMapping(value = "/{genomeId}/geneMapping/{mappingId}/")
public Map<String, Object> removeGeneMapping(
@PathVariable(value = "genomeId") String genomeId,
@PathVariable(value = "mappingId") String mappingId) throws IOException, QueryException {
return referenceGenomeController.removeGeneMapping(genomeId, mappingId);
public Map<String, Object> removeGeneMapping(@PathVariable(value = "genomeId") Integer genomeId, @PathVariable(value = "mappingId") Integer mappingId)
throws IOException, QueryException {
if (genomeId == null) {
throw new QueryException("Invalid genomeId");
}
ReferenceGenome genome = referenceGenomeService.getReferenceGenomeById(genomeId);
if (genome == null) {
throw new ObjectNotFoundException("Genome doesn't exist");
}
ReferenceGenomeGeneMapping geneMapping = null;
for (ReferenceGenomeGeneMapping mapping : genome.getGeneMapping()) {
if (mapping.getId() == mappingId) {
geneMapping = mapping;
}
}
if (geneMapping == null) {
throw new ObjectNotFoundException("Gene mapping doesn't exist");
}
referenceGenomeService.removeReferenceGenomeGeneMapping(geneMapping);
return new HashMap<>();
}
@PreAuthorize("hasAuthority('IS_ADMIN')")
@PostMapping(value = "/{genomeId}/geneMapping/")
public Map<String, Object> addGeneMapping(
@PathVariable(value = "genomeId") String genomeId,
@RequestBody MultiValueMap<String, Object> formData)
@PostMapping(value = "/{genomeId}/geneMapping/", consumes = { MediaType.APPLICATION_FORM_URLENCODED_VALUE })
public Map<String, Object> addGeneMapping(@PathVariable(value = "genomeId") Integer id, @RequestParam String name, @RequestParam String url)
throws QueryException, IOException, ReferenceGenomeConnectorException {
return referenceGenomeController.addGeneMapping(formData, genomeId);
try {
ReferenceGenome genome = referenceGenomeService.getReferenceGenomeById(id);
if (!url.toLowerCase().endsWith("bb")) {
throw new QueryException("Only big bed format files are supported but found: \"" + url + "\".");
}
referenceGenomeService.addReferenceGenomeGeneMapping(genome, name, url);
return new HashMap<>();
} catch (URISyntaxException e) {
throw new QueryException("Problem wih given uri", e);
}
}
}
\ No newline at end of file
package lcsb.mapviewer.api.genomics;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.MultiValueMap;
import lcsb.mapviewer.annotation.cache.BigFileCache;
import lcsb.mapviewer.annotation.services.genome.ReferenceGenomeConnectorException;
import lcsb.mapviewer.api.BaseRestImpl;
import lcsb.mapviewer.api.SerializationUtils;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.layout.*;
import lcsb.mapviewer.services.*;
import lcsb.mapviewer.services.interfaces.IReferenceGenomeService;
import lcsb.mapviewer.services.utils.ReferenceGenomeExistsException;
@Transactional
@Service
public class ReferenceGenomeRestImpl extends BaseRestImpl {
/**
* Default class logger.
*/
private Logger logger = LogManager.getLogger();
/**
* Service that manages reference genomes.
*/
private IReferenceGenomeService referenceGenomeService;
private BigFileCache bigFileCache;
@Autowired
public ReferenceGenomeRestImpl(IReferenceGenomeService referenceGenomeService, BigFileCache bigFileCache) {
this.referenceGenomeService = referenceGenomeService;
this.bigFileCache = bigFileCache;
}
public List<Map<String, Object>> getRemoteUrls(String organismId, String type, String version) throws QueryException {
MiriamData organism;
if (organismId != null && !organismId.isEmpty()) {
organism = new MiriamData(MiriamType.TAXONOMY, organismId);
} else {
throw new QueryException("Unknown taxonomy organism: " + organismId);
}
try {
ReferenceGenomeType genomeType = ReferenceGenomeType.valueOf(type);
String url = referenceGenomeService.getUrlForGenomeVersion(genomeType, organism, version);
List<Map<String, Object>> result = new ArrayList<>();
if (url != null) {
Map<String, Object> row = new TreeMap<>();
row.put("url", url);
result.add(row);
}
return result;
} catch (IllegalArgumentException e) {
throw new QueryException("Cannot find type: " + type);
}
}
private Map<String, Object> genomeToMap(ReferenceGenome genome) {
Map<String, Object> result = new TreeMap<>();
result.put("organism", super.createAnnotation(genome.getOrganism()));
result.put("version", genome.getVersion());
result.put("type", genome.getType());
result.put("downloadProgress", genome.getDownloadProgress());
result.put("sourceUrl", genome.getSourceUrl());
result.put("localUrl", getLocalUrl(genome.getSourceUrl()));
result.put("idObject", genome.getId());
result.put("geneMapping", geneMappingToMaps(genome.getGeneMapping()));
return result;
}
private String getLocalUrl(String sourceUrl) {
String url = null;
try {
url = "../" + bigFileCache.getLocalPathForFile(sourceUrl);
} catch (FileNotFoundException e) {
logger.warn("Cannot find local file", e);
}
return url;
}
private List<Map<String, Object>> geneMappingToMaps(List<ReferenceGenomeGeneMapping> geneMapping) {
List<Map<String, Object>> result = new ArrayList<>();
for (ReferenceGenomeGeneMapping referenceGenomeGeneMapping : geneMapping) {
result.add(geneMappingToMap(referenceGenomeGeneMapping));
}
return result;
}
private Map<String, Object> geneMappingToMap(ReferenceGenomeGeneMapping mapping) {
Map<String, Object> result = new TreeMap<>();
result.put("downloadProgress", mapping.getDownloadProgress());
result.put("localUrl", getLocalUrl(mapping.getSourceUrl()));
result.put("sourceUrl", mapping.getSourceUrl());
result.put("name", mapping.getName());
result.put("idObject", mapping.getId());
return result;
}
public List<Map<String, Object>> getReferenceGenomeTaxonomies() throws QueryException {
try {
Set<MiriamData> organisms = new LinkedHashSet<>();
for (ReferenceGenomeType type : ReferenceGenomeType.values()) {
organisms.addAll(referenceGenomeService.getOrganismsByReferenceGenomeType(type));
}
List<Map<String, Object>> result = new ArrayList<>();
for (MiriamData miriamData : organisms) {
result.add(createAnnotation(miriamData));
}
return result;
} catch (ReferenceGenomeConnectorException e) {
throw new QueryException("Problem with obtaining organism list", e);
}
}
public List<Map<String, Object>> getReferenceGenomeTypes(String organismId) throws QueryException {
try {
Set<MiriamData> organisms = new LinkedHashSet<>();
for (ReferenceGenomeType type : ReferenceGenomeType.values()) {
organisms.addAll(referenceGenomeService.getOrganismsByReferenceGenomeType(type));
}
List<Map<String, Object>> result = new ArrayList<>();
for (MiriamData miriamData : organisms) {
if (miriamData.getResource().equals(organismId)) {
Map<String, Object> entry = new HashMap<>();
entry.put("type", ReferenceGenomeType.UCSC.toString());
result.add(entry);
}
}
return result;
} catch (ReferenceGenomeConnectorException e) {
throw new QueryException("Problem with obtaining organism list", e);
}
}
public List<Map<String, Object>> getReferenceGenomeVersions(String organismId, String type)
throws QueryException {
ReferenceGenomeType genomeType = null;
try {
genomeType = ReferenceGenomeType.valueOf(type);
} catch (IllegalArgumentException e) {
throw new QueryException("Invalid type: " + type);
}
MiriamData organism;
if (organismId != null && !organismId.isEmpty()) {
organism = new MiriamData(MiriamType.TAXONOMY, organismId);
} else {
throw new QueryException("Unknown taxonomy organism: " + organismId);
}
List<String> versions;
try {
versions = referenceGenomeService.getAvailableGenomeVersions(genomeType, organism);
} catch (ReferenceGenomeConnectorException e) {
throw new QueryException("Problem with obtaining version list", e);
}
List<Map<String, Object>> result = new ArrayList<>();
for (String string : versions) {
Map<String, Object> entry = new HashMap<>();
entry.put("version", string);
result.add(entry);
}
return result;
}
public List<Map<String, Object>> getReferenceGenomes() {
List<Map<String, Object>> result = new ArrayList<>();
for (ReferenceGenome genome : referenceGenomeService.getDownloadedGenomes()) {
result.add(genomeToMap(genome));
}
return result;
}
public Map<String, Object> removeGenome(String genomeId) throws IOException, QueryException {
ReferenceGenome genome = referenceGenomeService
.getReferenceGenomeById(SerializationUtils.parseInteger(genomeId, "genomeId"));
if (genome == null) {
throw new ObjectNotFoundException("Object does not exist");
}
referenceGenomeService.removeGenome(genome);
return okStatus();
}
public Map<String, Object> addReferenceGenome(MultiValueMap<String, Object> formData)
throws QueryException, IOException, ReferenceGenomeConnectorException {
String organismId = getFirstValue(formData.get("organismId"));
String type = getFirstValue(formData.get("type"));
String version = getFirstValue(formData.get("version"));
String url = getFirstValue(formData.get("sourceUrl"));
MiriamData organism;
if (organismId != null && !organismId.isEmpty()) {
organism = new MiriamData(MiriamType.TAXONOMY, organismId);
} else {
throw new QueryException("Unknown taxonomy organism: " + organismId);
}
ReferenceGenomeType genomeType = ReferenceGenomeType.valueOf(type);
try {
referenceGenomeService.addReferenceGenome(genomeType, organism, version, url);
return okStatus();
} catch (ReferenceGenomeExistsException e) {
throw new ObjectExistsException(e);
} catch (URISyntaxException e) {
throw new QueryException("Problem wih given uri", e);
}
}
public Map<String, Object> addGeneMapping(MultiValueMap<String, Object> formData, String genomeId)
throws QueryException, IOException, ReferenceGenomeConnectorException {
int id = parseInteger(genomeId, "genomeId");
try {
ReferenceGenome genome = referenceGenomeService.getReferenceGenomeById(id);
String name = getFirstValue(formData.get("name"));
String url = getFirstValue(formData.get("url"));
if (!url.toLowerCase().endsWith("bb")) {
throw new QueryException("Only big bed format files are supported but found: \"" + url + "\".");
}
referenceGenomeService.addReferenceGenomeGeneMapping(genome, name, url);
return okStatus();
} catch (URISyntaxException e) {
throw new QueryException("Problem wih given uri", e);
}
}
public Map<String, Object> removeGeneMapping(String genomeId, String mappingId)
throws IOException, QueryException {
ReferenceGenome genome = referenceGenomeService
.getReferenceGenomeById(SerializationUtils.parseInteger(genomeId, "genomeId"));
if (genome == null) {
throw new ObjectNotFoundException("Genome doesn't exist");
}
int id = SerializationUtils.parseInteger(mappingId, "mappingId");
ReferenceGenomeGeneMapping geneMapping = null;
for (ReferenceGenomeGeneMapping mapping : genome.getGeneMapping()) {
if (mapping.getId() == id) {
geneMapping = mapping;
}
}
if (geneMapping == null) {
throw new ObjectNotFoundException("Gene mapping doesn't exist");
}
referenceGenomeService.removeReferenceGenomeGeneMapping(geneMapping);
return okStatus();
}
}
package lcsb.mapviewer.api.genomics;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({ ReferenceGenomeControllerTest.class })
public class AllGenomicsTests {
}