List of usage examples for org.apache.solr.client.solrj SolrQuery setFields
public SolrQuery setFields(String... fields)
From source file:edu.ucsb.nceas.mdqengine.solr.SolrIndex.java
License:Open Source License
/** * Get all indexed ids in the solr server. * @return an empty list if there is no index. * @throws SolrServerException/*from w ww.ja v a 2s . c o m*/ */ public List<String> getSolrIds() throws SolrServerException, IOException { List<String> list = new ArrayList<String>(); SolrQuery query = new SolrQuery(IDQUERY); query.setRows(Integer.MAX_VALUE); query.setFields(ID); QueryResponse response = solrClient.query(query); SolrDocumentList docs = response.getResults(); if (docs != null) { for (SolrDocument doc : docs) { String identifier = (String) doc.getFieldValue(ID); list.add(identifier); } } return list; }
From source file:edu.vt.vbi.patric.common.FASTAHelper.java
License:Apache License
public static String getFASTASequence(PortletRequest request, List<String> featureIds, String type) throws IOException { StringBuilder fasta = new StringBuilder(); SolrQuery query = new SolrQuery("feature_id:(" + StringUtils.join(featureIds, " OR ") + ")"); query.setFields( "feature_id,patric_id,alt_locus_tag,refseq_locus_tag,annotation,gi,product,genome_id,genome_name,na_sequence,aa_sequence"); query.setRows(featureIds.size());/*from w w w . j a v a2 s . com*/ DataApiHandler dataApi = new DataApiHandler(request); LOGGER.trace("getFASTASequence: [{}] {}", SolrCore.FEATURE.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.FEATURE, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<GenomeFeature> features = dataApi.bindDocuments((List<Map>) respBody.get("docs"), GenomeFeature.class); for (GenomeFeature feature : features) { if (type.equals("dna") || type.equals("both")) { fasta.append(">"); if (feature.getAnnotation().equals("PATRIC")) { if (feature.hasPatricId()) { fasta.append(feature.getPatricId()).append("|"); } } else if (feature.getAnnotation().equals("RefSeq")) { if (feature.getGi() > 0) { fasta.append("gi|").append(feature.getGi()).append("|"); } } if (feature.hasRefseqLocusTag()) { fasta.append(feature.getRefseqLocusTag()).append("|"); } if (feature.hasAltLocusTag()) { fasta.append(feature.getAltLocusTag()).append("|"); } if (feature.hasProduct()) { fasta.append(" ").append(feature.getProduct()); } fasta.append(" [").append(feature.getGenomeName()).append(" | ").append(feature.getGenomeId()) .append("]"); fasta.append("\n"); if (feature.hasNaSequence()) { fasta.append(StringHelper.chunk_split(feature.getNaSequence(), 60, "\n")); } } if (type.equals("protein") || type.equals("both")) { fasta.append(">"); if (feature.getAnnotation().equals("PATRIC")) { if (feature.hasPatricId()) { fasta.append(feature.getPatricId()).append("|"); } } else if (feature.getAnnotation().equals("RefSeq")) { if (feature.getGi() > 0) { fasta.append("gi|").append(feature.getGi()).append("|"); } } if (feature.hasRefseqLocusTag()) { fasta.append(feature.getRefseqLocusTag()).append("|"); } if (feature.hasAltLocusTag()) { fasta.append(feature.getAltLocusTag()).append("|"); } if (feature.hasProduct()) { fasta.append(" ").append(feature.getProduct()); } fasta.append(" [").append(feature.getGenomeName()).append(" | ").append(feature.getGenomeId()) .append("]"); fasta.append("\n"); if (feature.hasAaSequence()) { fasta.append(StringHelper.chunk_split(feature.getAaSequence(), 60, "\n")); } } } return fasta.toString(); }
From source file:edu.vt.vbi.patric.portlets.CompareRegionViewer.java
License:Apache License
@SuppressWarnings("unchecked") private void printTrackList(ResourceRequest request, ResourceResponse response) throws IOException { String contextType = request.getParameter("cType"); String contextId = request.getParameter("cId"); String pinFeatureSeedId = request.getParameter("feature"); // pin feature String windowSize = request.getParameter("window"); // window size int _numRegion = Integer.parseInt(request.getParameter("regions")); // number of genomes to compare int _numRegion_buffer = 10; // number of genomes to use as a buffer in case that PATRIC has no genome data, // which was retrieved from API String _key = ""; DataApiHandler dataApi = new DataApiHandler(request); // if pin feature is not given, retrieve from the database based on na_feature_id if (pinFeatureSeedId == null && (contextType != null && contextType.equals("feature") && contextId != null)) { GenomeFeature feature = dataApi.getFeature(contextId); pinFeatureSeedId = feature.getPatricId(); }/*w w w. j av a2s . com*/ if (pinFeatureSeedId != null && !pinFeatureSeedId.equals("") && windowSize != null) { CRResultSet crRS = null; try { SAPserver sapling = new SAPserver("http://servers.nmpdr.org/pseed/sapling/server.cgi"); crRS = new CRResultSet(pinFeatureSeedId, sapling.compared_regions(pinFeatureSeedId, _numRegion + _numRegion_buffer, Integer.parseInt(windowSize) / 2)); long pk = (new Random()).nextLong(); _key = "" + pk; Gson gson = new Gson(); SessionHandler.getInstance().set(SessionHandler.PREFIX + pk, gson.toJson(crRS, CRResultSet.class)); SessionHandler.getInstance().set(SessionHandler.PREFIX + "_windowsize" + pk, windowSize); } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); } JSONObject trackList = new JSONObject(); JSONArray tracks = new JSONArray(); JSONObject trStyle = new JSONObject(); trStyle.put("className", "feature5"); trStyle.put("showLabels", false); trStyle.put("label", "function( feature ) { return feature.get('patric_id'); }"); JSONObject trHooks = new JSONObject(); trHooks.put("modify", "function(track, feature, div) { div.style.backgroundColor = ['red','#1F497D','#938953','#4F81BD','#9BBB59','#806482','#4BACC6','#F79646'][feature.get('phase')];}"); // query genome metadata SolrQuery query = new SolrQuery("genome_id:(" + StringUtils.join(crRS.getGenomeIds(), " OR ") + ")"); query.setFields( "genome_id,genome_name,isolation_country,host_name,disease,collection_date,completion_date"); query.setRows(_numRegion + _numRegion_buffer); String apiResponse = dataApi.solrQuery(SolrCore.GENOME, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Genome> patricGenomes = dataApi.bindDocuments((List<Map>) respBody.get("docs"), Genome.class); int count_genomes = 1; if (crRS.getGenomeNames().size() > 0) { for (Integer idx : crRS.getTrackMap().keySet()) { if (count_genomes > _numRegion) { break; } CRTrack crTrack = crRS.getTrackMap().get(idx); Genome currentGenome = null; for (Genome genome : patricGenomes) { if (genome.getId().equals(crTrack.getGenomeID())) { currentGenome = genome; } } if (currentGenome != null) { count_genomes++; crRS.addToDefaultTracks(crTrack); JSONObject tr = new JSONObject(); tr.put("style", trStyle); tr.put("hooks", trHooks); tr.put("type", "FeatureTrack"); tr.put("tooltip", "<div style='line-height:1.7em'><b>{patric_id}</b> | {refseq_locus_tag} | {alt_locus_tag} | {gene}<br>{product}<br>{type}:{start}...{end} ({strand_str})<br> <i>Click for detail information</i></div>"); tr.put("urlTemplate", "/portal/portal/patric/CompareRegionViewer/CRWindow?action=b&cacheability=PAGE&mode=getTrackInfo&key=" + _key + "&rowId=" + crTrack.getRowID() + "&format=.json"); tr.put("key", crTrack.getGenomeName()); tr.put("label", "CR" + idx); tr.put("dataKey", _key); JSONObject metaData = new JSONObject(); if (currentGenome.getIsolationCountry() != null) { metaData.put("Isolation Country", currentGenome.getIsolationCountry()); } if (currentGenome.getHostName() != null) { metaData.put("Host Name", currentGenome.getHostName()); } if (currentGenome.getDisease() != null) { metaData.put("Disease", currentGenome.getDisease()); } if (currentGenome.getCollectionDate() != null) { metaData.put("Collection Date", currentGenome.getCollectionDate()); } if (currentGenome.getCompletionDate() != null) { metaData.put("Completion Date", currentGenome.getCompletionDate()); } tr.put("metadata", metaData); tracks.add(tr); } } } trackList.put("tracks", tracks); JSONObject facetedTL = new JSONObject(); JSONArray dpColumns = new JSONArray(); dpColumns.addAll(Arrays.asList("key", "Isolation Country", "Host Name", "Disease", "Collection Date", "Completion Date")); facetedTL.put("displayColumns", dpColumns); facetedTL.put("type", "Faceted"); facetedTL.put("escapeHTMLInData", false); trackList.put("trackSelector", facetedTL); trackList.put("defaultTracks", crRS.getDefaultTracks()); response.setContentType("application/json"); trackList.writeJSONString(response.getWriter()); response.getWriter().close(); } else { response.getWriter().write("{}"); } }
From source file:edu.vt.vbi.patric.portlets.CompareRegionViewer.java
License:Apache License
/** * Retrieves features that can be mapped by PSEED peg ID. This is used for CompareRegionViewer to map * features each other.//from ww w. j a v a2 s . co m * * @param IDs IDs * @return list of features (na_feature_id, pseed_id, source_id, start, end, strand, na_length, aa_length, product, genome_name, accession) */ private Map<String, GenomeFeature> getPSeedMapping(ResourceRequest request, String IDs) throws IOException { Map<String, GenomeFeature> result = new HashMap<>(); DataApiHandler dataApi = new DataApiHandler(request); SolrQuery query = new SolrQuery("patric_id:(" + IDs + ")"); query.setFields( "feature_id,patric_id,alt_locus_tag,start,end,strand,feature_type,product,gene,refseq_locus_tag,genome_name,accession"); query.setRows(1000); String apiResponse = dataApi.solrQuery(SolrCore.FEATURE, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<GenomeFeature> features = dataApi.bindDocuments((List<Map>) respBody.get("docs"), GenomeFeature.class); for (GenomeFeature feature : features) { result.put(feature.getPatricId(), feature); } return result; }
From source file:edu.vt.vbi.patric.portlets.CompPathwayMap.java
License:Apache License
@Override protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException { response.setContentType("text/html"); SiteHelper.setHtmlMetaElements(request, response, "Comparative Pathway Map"); DataApiHandler dataApi = new DataApiHandler(request); String pk = request.getParameter("param_key") != null ? request.getParameter("param_key") : ""; String dm = request.getParameter("display_mode") != null ? request.getParameter("display_mode") : ""; String cType = request.getParameter("context_type") != null ? request.getParameter("context_type") : ""; String map = request.getParameter("map") != null ? request.getParameter("map") : ""; String algorithm = request.getParameter("algorithm") != null ? request.getParameter("algorithm") : ""; String cId = request.getParameter("context_id") != null ? request.getParameter("context_id") : ""; String ec_number = request.getParameter("ec_number") != null ? request.getParameter("ec_number") : ""; String feature_id = request.getParameter("feature_id") != null ? request.getParameter("feature_id") : ""; String ec_names = ""; int occurrences = 0; String genomeId = ""; String taxonId = ""; String pathway_name = ""; String pathway_class = ""; String definition = ""; int patricGenomeCount = 0; int brc1GenomeCount = 0; int refseqGenomeCount = 0; // get attributes try {// ww w . j a v a 2s. com SolrQuery query = new SolrQuery("pathway_id:" + map); query.setFields("pathway_name,pathway_class,ec_description"); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); if (!sdl.isEmpty()) { Map doc = sdl.get(0); definition = doc.get("ec_description").toString(); pathway_name = doc.get("pathway_name").toString(); pathway_class = doc.get("pathway_class").toString(); } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } try { if (cType != null && cType.equals("taxon")) { taxonId = cId; } else if (cType != null && cType.equals("genome")) { genomeId = cId; } if (pk != null && !pk.isEmpty()) { Map<String, String> key = jsonReader .readValue(SessionHandler.getInstance().get(SessionHandler.PREFIX + pk)); if (key != null && key.containsKey("genomeId") && !key.get("genomeId").equals("")) { genomeId = key.get("genomeId"); } else if (key != null && key.containsKey("taxonId") && !key.get("taxonId").equals("")) { taxonId = key.get("taxonId"); } if (key != null && key.containsKey("feature_id") && !key.get("feature_id").equals("")) { feature_id = key.get("feature_id"); } } SolrQuery query = new SolrQuery("*:*"); query.setRows(dataApi.MAX_ROWS); if (!genomeId.equals("")) { if (genomeId.contains(",")) { query.addFilterQuery("genome_id:(" + StringUtils.join(genomeId.split(","), " OR ") + ")"); query.setRows(genomeId.split(",").length); } else { query.addFilterQuery("genome_id:" + genomeId); } } if (!taxonId.equals("")) { query.addFilterQuery("taxon_lineage_ids:" + taxonId); } LOGGER.debug("counting features: {}", query.toString()); String apiResponse = dataApi.solrQuery(SolrCore.GENOME, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Genome> genomes = dataApi.bindDocuments((List<Map>) respBody.get("docs"), Genome.class); for (Genome genome : genomes) { if (genome.hasPatricCds()) { patricGenomeCount++; } // if (doc.get("brc1_cds") != null && !doc.get("brc1_cds").toString().equals("0")) { // brc1GenomeCount++; // } if (genome.hasRefseqCds()) { refseqGenomeCount++; } } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } // TODO: implement with solr if (dm != null && dm.equals("ec")) { SolrQuery query = new SolrQuery("ec_number:(" + ec_number + ") AND pathway_id:(" + map + ")"); LOGGER.debug("[{}]:{}", SolrCore.PATHWAY_REF.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { ec_names = (String) doc.get("ec_description"); occurrences = (Integer) doc.get("occurrence"); } } else if (dm != null && dm.equals("feature")) { // q=pathway_id:00051&fq={!join%20from=ec_number%20to=ec_number%20fromIndex=pathway}feature_id:PATRIC.235.14.JMSA01000002.CDS.537.665.fwd+AND+pathway_id:00051 SolrQuery query = new SolrQuery("pathway_id:" + map); query.addFilterQuery(SolrCore.PATHWAY.getSolrCoreJoin("ec_number", "ec_number", "feature_id:(" + feature_id + ") AND pathway_id:(" + map + ")")); LOGGER.debug("[{}]:{}", SolrCore.PATHWAY_REF.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { ec_number = (String) doc.get("ec_number"); ec_names = (String) doc.get("ec_description"); occurrences = (Integer) doc.get("occurrence"); } } ///// request.setAttribute("cType", cType); request.setAttribute("cId", cId); request.setAttribute("taxonId", taxonId); request.setAttribute("genomeId", genomeId); request.setAttribute("algorithm", algorithm); request.setAttribute("map", map); request.setAttribute("dm", dm); request.setAttribute("pk", pk); request.setAttribute("feature_id", feature_id); request.setAttribute("ec_number", ec_number); request.setAttribute("ec_names", ec_names); request.setAttribute("occurrences", occurrences); request.setAttribute("taxongenomecount_patric", patricGenomeCount); request.setAttribute("taxongenomecount_brc1", brc1GenomeCount); request.setAttribute("taxongenomecount_refseq", refseqGenomeCount); request.setAttribute("definition", definition); request.setAttribute("pathway_name", pathway_name); request.setAttribute("pathway_class", pathway_class); PortletRequestDispatcher prd = getPortletContext() .getRequestDispatcher("/WEB-INF/jsp/comp_pathway_map.jsp"); prd.include(request, response); }
From source file:edu.vt.vbi.patric.portlets.CompPathwayMap.java
License:Apache License
private void processKeggMap(ResourceRequest request, ResourceResponse response) throws IOException { Map<String, String> key = new HashMap<>(); DataApiHandler dataApi = new DataApiHandler(request); JSONObject ret = new JSONObject(); JSONObject val = new JSONObject(); try {//w w w. ja v a2 s .c om val = (JSONObject) (new JSONParser()).parse(request.getParameter("val").toString()); } catch (ParseException e) { LOGGER.error(e.getMessage(), e); } String need = val.get("need").toString(); String genomeId = null, taxonId = null, map = null, pk = null; if (val.containsKey("genomeId") && val.get("genomeId") != null && !val.get("genomeId").equals("")) { genomeId = val.get("genomeId").toString(); } if (val.containsKey("taxonId") && val.get("taxonId") != null && !val.get("taxonId").equals("")) { taxonId = val.get("taxonId").toString(); } if (val.containsKey("map") && val.get("map") != null && !val.get("map").equals("")) { map = val.get("map").toString(); } if (val.containsKey("pk") && val.get("pk") != null && !val.get("pk").equals("")) { pk = val.get("pk").toString(); } if (need.equals("all")) { if (genomeId != null) { key.put("genomeId", genomeId); } if (taxonId != null) { key.put("taxonId", taxonId); } if (map != null) { key.put("map", map); } Map<String, String> sessKey = null; if (pk != null && !pk.isEmpty()) { sessKey = jsonReader.readValue(SessionHandler.getInstance().get(SessionHandler.PREFIX + pk)); if (sessKey != null && sessKey.containsKey("genomeId") && !sessKey.get("genomeId").equals("")) { genomeId = sessKey.get("genomeId"); } } List<String> annotations = Arrays.asList("PATRIC", "RefSeq"); // getting coordinates try { JSONArray listCoordinates = new JSONArray(); for (String annotation : annotations) { Set<String> ecNumbers = new HashSet<String>(); // step1. genome_count, feature_count // pathway/select?q=pathway_id:00053+AND+annotation:PATRIC&fq={!join+from=genome_id+to=genome_id+fromIndex=genome}taxon_lineage_ids:83332+AND+genome_status:(complete+OR+wgs) // &rows=0&facet=true&json.facet={stat:{field:{field:ec_number,limit:-1,facet:{genome_count:"unique(genome_id)",gene_count:"unique(feature_id)"}}}} SolrQuery query = new SolrQuery("pathway_id:" + map + " AND annotation:" + annotation); if (taxonId != null) { query.addFilterQuery(SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + taxonId)); } if (genomeId != null) { query.addFilterQuery(SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "genome_id:(" + genomeId.replaceAll(",", " OR ") + ")")); } query.setRows(0).setFacet(true); query.add("json.facet", "{stat:{field:{field:ec_number,limit:-1,facet:{genome_count:\"unique(genome_id)\",gene_count:\"unique(feature_id)\"}}}}"); LOGGER.debug("step 1. [{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); int numFound = (Integer) respBody.get("numFound"); if (numFound > 0) { List<Map> buckets = (List<Map>) ((Map) ((Map) resp.get("facets")).get("stat")) .get("buckets"); Map<String, Map> mapStat = new HashMap<>(); for (Map value : buckets) { if (Integer.parseInt(value.get("count").toString()) > 0) { mapStat.put(value.get("val").toString(), value); ecNumbers.add(value.get("val").toString()); } } // step2. coordinates, occurrence // pathway_ref/select?q=pathway_id:00010+AND+map_type:enzyme%+AND+ec_number:("1.2.1.3"+OR+"1.1.1.1")&fl=ec_number,ec_description,map_location,occurrence if (!ecNumbers.isEmpty()) { query = new SolrQuery("pathway_id:" + map + " AND map_type:enzyme AND ec_number:(" + StringUtils.join(ecNumbers, " OR ") + ")"); query.setFields("ec_number,ec_description,map_location,occurrence"); query.setRows(dataApi.MAX_ROWS); LOGGER.trace("genome_x_y: [{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), query); apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); resp = jsonReader.readValue(apiResponse); respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { String ecNumber = doc.get("ec_number").toString(); Map stat = mapStat.get(ecNumber); if (!stat.get("gene_count").toString().equals("0")) { List<String> locations = (List<String>) doc.get("map_location"); for (String location : locations) { JSONObject coordinate = new JSONObject(); coordinate.put("algorithm", annotation); coordinate.put("description", doc.get("ec_description")); coordinate.put("ec_number", ecNumber); coordinate.put("genome_count", stat.get("genome_count")); String[] loc = location.split(","); coordinate.put("x", loc[0]); coordinate.put("y", loc[1]); listCoordinates.add(coordinate); } } } } } } ret.put("genome_x_y", listCoordinates); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } // get pathways try { SolrQuery query = new SolrQuery("annotation:(" + StringUtils.join(annotations, " OR ") + ")"); if (taxonId != null) { query.addFilterQuery(SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + taxonId)); } if (genomeId != null) { query.addFilterQuery("genome_id:(" + genomeId.replaceAll(",", " OR ") + ")"); } query.setFields("pathway_id,pathway_name,annotation").setRows(dataApi.MAX_ROWS); LOGGER.trace("genome_pathway_x_y: [{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); JSONArray listEnzymes = new JSONArray(); Set<String> hash = new HashSet<>(); for (Map doc : sdl) { // TODO: need to improve this using solr String hashKey = doc.get("pathway_id").toString() + ":" + doc.get("annotation").toString(); if (!hash.contains(hashKey)) { hash.add(hashKey); JSONObject enzyme = new JSONObject(); enzyme.put("algorithm", doc.get("annotation")); enzyme.put("map_id", doc.get("pathway_id")); enzyme.put("map_name", doc.get("pathway_name")); listEnzymes.add(enzyme); } } ret.put("genome_pathway_x_y", listEnzymes); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } // map_ids_in_map try { SolrQuery query = new SolrQuery("pathway_id:" + map + " AND map_type:path"); query.setFields("ec_number,ec_description,map_location").setRows(dataApi.MAX_ROWS); LOGGER.trace("map_ids_in_map: [{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); JSONArray listCoordinates = new JSONArray(); for (Map doc : sdl) { List<String> locations = (List<String>) doc.get("map_location"); for (String location : locations) { JSONObject coordinate = new JSONObject(); coordinate.put("source_id", doc.get("ec_number")); String[] loc = location.split(","); coordinate.put("x", loc[0]); coordinate.put("y", loc[1]); coordinate.put("width", loc[2]); coordinate.put("height", loc[3]); listCoordinates.add(coordinate); } } ret.put("map_ids_in_map", listCoordinates); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } // all coordinates try { SolrQuery query = new SolrQuery("pathway_id:" + map + " AND map_type:enzyme"); query.setFields("ec_number,ec_description,map_location").setRows(dataApi.MAX_ROWS); LOGGER.trace("all_coordinates: [{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); JSONArray listCoordinates = new JSONArray(); for (Map doc : sdl) { List<String> locations = (List<String>) doc.get("map_location"); for (String location : locations) { JSONObject coordinate = new JSONObject(); coordinate.put("ec", doc.get("ec_number")); coordinate.put("description", doc.get("ec_description")); String[] loc = location.split(","); coordinate.put("x", loc[0]); coordinate.put("y", loc[1]); listCoordinates.add(coordinate); } } ret.put("all_coordinates", listCoordinates); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } else { // need: feature_id or ec_number JSONArray coordinates = new JSONArray(); if (need.equals("ec_number")) { try { SolrQuery query = new SolrQuery("*:*"); if (map != null) { query.addFilterQuery("pathway_id:(" + map + ")"); } LOGGER.trace("[{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { List<String> locations = (List<String>) doc.get("map_location"); for (String loc : locations) { JSONObject coordinate = new JSONObject(); coordinate.put("ec_number", doc.get("ec_number")); String[] xy = loc.split(","); coordinate.put("x", xy[0]); coordinate.put("y", xy[1]); coordinates.add(coordinate); } } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } else { // feature String featureIds = null; if (val.containsKey("value") && val.get("value") != null && !val.get("value").equals("")) { featureIds = val.get("value").toString(); } try { SolrQuery query = new SolrQuery("*:*"); if (map != null) { query.addFilterQuery("pathway_id:(" + map + ")"); } if (featureIds != null) { query.addFilterQuery(SolrCore.PATHWAY.getSolrCoreJoin("ec_number", "ec_number", "feature_id:(" + featureIds.replaceAll(",", " OR ") + ")")); } LOGGER.trace("coordinates: [{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { List<String> locations = (List<String>) doc.get("map_location"); for (String loc : locations) { JSONObject coordinate = new JSONObject(); coordinate.put("ec_number", doc.get("ec_number")); String[] xy = loc.split(","); coordinate.put("x", xy[0]); coordinate.put("y", xy[1]); coordinates.add(coordinate); } } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } ret.put("coordinates", coordinates); } response.setContentType("application/json"); ret.writeJSONString(response.getWriter()); }
From source file:edu.vt.vbi.patric.portlets.CompPathwayMap.java
License:Apache License
private void processHeatMap(ResourceRequest request, ResourceResponse response) throws IOException { DataApiHandler dataApi = new DataApiHandler(request); String genomeId, taxonId, algorithm, map; genomeId = request.getParameter("genomeId"); taxonId = request.getParameter("taxonId"); algorithm = request.getParameter("algorithm"); map = request.getParameter("map"); JSONObject json = new JSONObject(); try {/*from w w w .java 2 s .c o m*/ SolrQuery query = new SolrQuery("pathway_id:" + map); if (algorithm != null && !algorithm.equals("")) { query.addFilterQuery("annotation:" + algorithm); } if (taxonId != null && !taxonId.equals("")) { query.addFilterQuery( SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + taxonId)); } if (genomeId != null && !genomeId.equals("")) { query.addFilterQuery(SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "genome_id:(" + genomeId.replaceAll(",", " OR ") + ")")); } query.setRows(dataApi.MAX_ROWS).setFields("genome_id,annotation,ec_number,ec_description") .setFacet(true); // {stat:{field:{field:genome_ec,limit:-1,facet:{gene_count:\"unique(feature_id)\"}}}} query.add("json.facet", "{stat:{field:{field:genome_ec,limit:-1}}}}"); LOGGER.debug("heatmap step 1: {}", query.toString()); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); int numFound = (Integer) respBody.get("numFound"); if (numFound > 0) { List<Map> buckets = (List<Map>) ((Map) ((Map) resp.get("facets")).get("stat")).get("buckets"); respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); Map<String, Integer> mapStat = new HashMap<>(); for (Map value : buckets) { if (Integer.parseInt(value.get("count").toString()) > 0) { mapStat.put(value.get("val").toString(), (Integer) value.get("count")); } } JSONArray items = new JSONArray(); for (Map doc : sdl) { final JSONObject item = new JSONObject(); item.put("genome_id", doc.get("genome_id")); item.put("algorithm", doc.get("annotation")); item.put("ec_number", doc.get("ec_number")); item.put("ec_name", doc.get("ec_description")); Integer count = mapStat.get(doc.get("genome_id") + "_" + doc.get("ec_number")); item.put("gene_count", String.format("%02x", count)); // 2-digit hex string items.add(item); } // if data exceeds limit, keep going int i = 1; while (sdl.size() == 25000) { query.setStart(25000 * i); apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query); resp = jsonReader.readValue(apiResponse); respBody = (Map) resp.get("response"); sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { final JSONObject item = new JSONObject(); item.put("genome_id", doc.get("genome_id")); item.put("algorithm", doc.get("annotation")); item.put("ec_number", doc.get("ec_number")); item.put("ec_name", doc.get("ec_description")); Integer count = mapStat.get(doc.get("genome_id") + "_" + doc.get("ec_number")); item.put("gene_count", String.format("%02x", count)); // 2-digit hex string items.add(item); } i++; } json.put("data", items); } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } try { SolrQuery query = new SolrQuery("*:*"); if (taxonId != null && !taxonId.equals("")) { query.addFilterQuery( SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + taxonId)); } if (genomeId != null && !genomeId.equals("")) { query.addFilterQuery(SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "genome_id:(" + genomeId.replaceAll(",", " OR ") + ")")); } if (algorithm != null && !algorithm.equals("")) { switch (algorithm) { case "PATRIC": query.addFilterQuery("patric_cds:[1 TO *]"); break; case "RefSeq": query.addFilterQuery("refseq_cds:[1 TO *]"); break; case "BRC1": query.addFilterQuery("brc1_cds:[1 TO *]"); break; } } query.setFields("genome_id,genome_name").setRows(dataApi.MAX_ROWS).addSort("genome_name", SolrQuery.ORDER.asc); LOGGER.debug("step 2: [{}] {}", SolrCore.GENOME.getSolrCoreName(), query.toString()); String apiResponse = dataApi.solrQuery(SolrCore.GENOME, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Genome> genomes = dataApi.bindDocuments((List<Map>) respBody.get("docs"), Genome.class); JSONArray items = new JSONArray(); for (Genome genome : genomes) { JSONObject item = new JSONObject(); item.put("genome_id", genome.getId()); item.put("genome_name", genome.getGenomeName()); items.add(item); } json.put("genomes", items); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } response.setContentType("application/json"); json.writeJSONString(response.getWriter()); }
From source file:edu.vt.vbi.patric.portlets.CompPathwayTable.java
License:Apache License
@SuppressWarnings("unchecked") private JSONObject processPathwayTab(DataApiHandler dataApi, String pathwayClass, String pathwayId, String ecNumber, String annotation, String contextType, String contextId) throws PortletException, IOException { JSONObject jsonResult = new JSONObject(); SolrQuery query = new SolrQuery("*:*"); if (pathwayClass != null && !pathwayClass.equals("")) { query.addFilterQuery("pathway_class:" + pathwayClass); }//from w w w . j a v a 2s. c o m if (pathwayId != null && !pathwayId.equals("")) { query.addFilterQuery("pathway_id:" + pathwayId); } if (ecNumber != null && !ecNumber.equals("")) { query.addFilterQuery("ec_number:" + ecNumber); } if (annotation != null && !annotation.equals("")) { query.addFilterQuery("annotation:" + annotation); } if (contextType.equals("genome")) { query.addFilterQuery( SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "genome_id:" + contextId)); } else if (contextType.equals("taxon")) { query.addFilterQuery( SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + contextId)); } JSONArray items = new JSONArray(); int count_total = 0; int count_unique = 0; try { Set<String> listPathwayIds = new HashSet<>(); Map<String, JSONObject> uniquePathways = new HashMap<>(); // get pathway stat query.setRows(0).setFacet(true); query.add("json.facet", "{stat:{field:{field:pathway_id,limit:-1,facet:{genome_count:\"unique(genome_id)\",gene_count:\"unique(feature_id)\",ec_count:\"unique(ec_number)\",genome_ec:\"unique(genome_ec)\"}}}}"); LOGGER.trace("[{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query); Map resp = jsonReader.readValue(apiResponse); List<Map> buckets = (List<Map>) ((Map) ((Map) resp.get("facets")).get("stat")).get("buckets"); Map<String, Map> mapStat = new HashMap<>(); for (Map value : buckets) { mapStat.put((String) value.get("val"), value); listPathwayIds.add((String) value.get("val")); } if (!listPathwayIds.isEmpty()) { // get pathway list SolrQuery pathwayQuery = new SolrQuery( "pathway_id:(" + StringUtils.join(listPathwayIds, " OR ") + ")"); pathwayQuery.setFields("pathway_id,pathway_name,pathway_class"); pathwayQuery.setRows(Math.max(dataApi.MAX_ROWS, listPathwayIds.size())); LOGGER.trace("[{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), pathwayQuery); apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, pathwayQuery); resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { String aPathwayId = doc.get("pathway_id").toString(); Map stat = mapStat.get(aPathwayId); if (!uniquePathways.containsKey(aPathwayId) && !stat.get("genome_count").toString().equals("0")) { JSONObject item = new JSONObject(); item.put("pathway_id", aPathwayId); item.put("pathway_name", doc.get("pathway_name")); item.put("pathway_class", doc.get("pathway_class")); float genome_ec = Float.parseFloat(stat.get("genome_ec").toString()); float genome_count = Float.parseFloat(stat.get("genome_count").toString()); float ec_count = Float.parseFloat(stat.get("ec_count").toString()); float gene_count = Float.parseFloat(stat.get("gene_count").toString()); float ec_cons = 0; float gene_cons = 0; if (genome_count > 0 && ec_count > 0) { ec_cons = genome_ec / genome_count / ec_count * 100; gene_cons = gene_count / genome_count / ec_count; } item.put("ec_cons", ec_cons); item.put("ec_count", ec_count); item.put("gene_cons", gene_cons); item.put("gene_count", gene_count); item.put("genome_count", genome_count); item.put("algorithm", annotation); uniquePathways.put(aPathwayId, item); } } for (Map.Entry<String, JSONObject> pathway : uniquePathways.entrySet()) { items.add(pathway.getValue()); } count_total = uniquePathways.entrySet().size(); count_unique = count_total; } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } // Wrapping jsonResult try { jsonResult.put("total", count_total); jsonResult.put("results", items); jsonResult.put("unique", count_unique); } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); } return jsonResult; }
From source file:edu.vt.vbi.patric.portlets.CompPathwayTable.java
License:Apache License
@SuppressWarnings("unchecked") private JSONObject processEcNumberTab(DataApiHandler dataApi, String pathwayClass, String pathwayId, String ecNumber, String annotation, String contextType, String contextId) throws PortletException, IOException { JSONObject jsonResult = new JSONObject(); SolrQuery query = new SolrQuery("*:*"); if (pathwayClass != null && !pathwayClass.equals("")) { query.addFilterQuery("pathway_class:" + pathwayClass); }// www .ja v a 2s. c o m if (pathwayId != null && !pathwayId.equals("")) { query.addFilterQuery("pathway_id:" + pathwayId); } if (ecNumber != null && !ecNumber.equals("")) { query.addFilterQuery("ec_number:" + ecNumber); } if (annotation != null && !annotation.equals("")) { query.addFilterQuery("annotation:" + annotation); } if (contextType.equals("genome")) { query.addFilterQuery( SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "genome_id:" + contextId)); } else if (contextType.equals("taxon")) { query.addFilterQuery( SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + contextId)); } JSONArray items = new JSONArray(); int count_total = 0; int count_unique = 0; try { Set<String> listPathwayIds = new HashSet<>(); Set<String> listEcNumbers = new HashSet<>(); // get pathway stat query.setRows(0).setFacet(true); query.add("json.facet", "{stat:{field:{field:pathway_ec,limit:-1,facet:{genome_count:\"unique(genome_id)\",gene_count:\"unique(feature_id)\",ec_count:\"unique(ec_number)\"}}}}"); LOGGER.trace("[{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query); Map resp = jsonReader.readValue(apiResponse); List<Map> buckets = (List<Map>) ((Map) ((Map) resp.get("facets")).get("stat")).get("buckets"); Map<String, Map> mapStat = new HashMap<>(); for (Map value : buckets) { if (!value.get("genome_count").toString().equals("0")) { mapStat.put(value.get("val").toString(), value); String[] pathway_ec = value.get("val").toString().split("_"); listPathwayIds.add(pathway_ec[0]); listEcNumbers.add(pathway_ec[1]); } } // get pathway list SolrQuery pathwayQuery = new SolrQuery("*:*"); if (!listPathwayIds.isEmpty()) { pathwayQuery.setQuery("pathway_id:(" + StringUtils.join(listPathwayIds, " OR ") + ")"); pathwayQuery.setFields("pathway_id,pathway_name,pathway_class,ec_number,ec_description"); pathwayQuery.setRows(Math.max(dataApi.MAX_ROWS, listPathwayIds.size())); LOGGER.trace("[{}] {}", SolrCore.PATHWAY_REF.getSolrCoreName(), pathwayQuery); apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, pathwayQuery); resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { String aPathwayId = doc.get("pathway_id").toString(); String aEcNumber = doc.get("ec_number").toString(); Map stat = mapStat.get(aPathwayId + "_" + aEcNumber); if (stat != null && !stat.get("genome_count").toString().equals("0")) { JSONObject item = new JSONObject(); item.put("pathway_id", aPathwayId); item.put("pathway_name", doc.get("pathway_name")); item.put("pathway_class", doc.get("pathway_class")); float genome_count = Float.parseFloat(stat.get("genome_count").toString()); float gene_count = Float.parseFloat(stat.get("gene_count").toString()); item.put("ec_name", doc.get("ec_description")); item.put("ec_number", doc.get("ec_number")); item.put("gene_count", gene_count); item.put("genome_count", genome_count); item.put("algorithm", annotation); items.add(item); } } count_total = items.size(); count_unique = listEcNumbers.size(); } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } // Wrapping jsonResult jsonResult.put("total", count_total); jsonResult.put("results", items); jsonResult.put("unique", count_unique); return jsonResult; }
From source file:edu.vt.vbi.patric.portlets.CompPathwayTable.java
License:Apache License
@SuppressWarnings("unchecked") private JSONObject processGeneTab(DataApiHandler dataApi, String pathwayClass, String pathwayId, String ecNumber, String annotation, String contextType, String contextId) throws PortletException, IOException { JSONObject jsonResult = new JSONObject(); SolrQuery query = new SolrQuery("*:*"); if (pathwayClass != null && !pathwayClass.equals("")) { query.addFilterQuery("pathway_class:" + pathwayClass); }/*from w w w. jav a2 s .com*/ if (pathwayId != null && !pathwayId.equals("")) { query.addFilterQuery("pathway_id:" + pathwayId); } if (ecNumber != null && !ecNumber.equals("")) { query.addFilterQuery("ec_number:" + ecNumber); } if (annotation != null && !annotation.equals("")) { query.addFilterQuery("annotation:" + annotation); } if (contextType.equals("genome")) { query.addFilterQuery( SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "genome_id:" + contextId)); } else if (contextType.equals("taxon")) { query.addFilterQuery( SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_id", "taxon_lineage_ids:" + contextId)); } JSONArray items = new JSONArray(); int count_total = 0; int count_unique = 0; try { Set<String> listFeatureIds = new HashSet<>(); query.setFields("pathway_id,pathway_name,feature_id,ec_number,ec_description"); query.setRows(dataApi.MAX_ROWS); LOGGER.trace("[{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query); String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query); Map resp = jsonReader.readValue(apiResponse); Map respBody = (Map) resp.get("response"); List<Map> sdl = (List<Map>) respBody.get("docs"); Map<String, Map> mapStat = new HashMap<>(); for (Map doc : sdl) { mapStat.put(doc.get("feature_id").toString(), doc); listFeatureIds.add(doc.get("feature_id").toString()); } int i = 1; while (sdl.size() == 25000) { query.setStart(25000 * i); LOGGER.trace("[{}] {}, iter={}", SolrCore.PATHWAY.getSolrCoreName(), query, i); apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query); resp = jsonReader.readValue(apiResponse); respBody = (Map) resp.get("response"); sdl = (List<Map>) respBody.get("docs"); for (Map doc : sdl) { mapStat.put(doc.get("feature_id").toString(), doc); listFeatureIds.add(doc.get("feature_id").toString()); } i++; } // get pathway list if (!listFeatureIds.isEmpty()) { SolrQuery featureQuery = new SolrQuery( "feature_id:(" + StringUtils.join(listFeatureIds, " OR ") + ")"); featureQuery.setFields( "genome_name,genome_id,accession,alt_locus_tag,refseq_locus_tag,patric_id,feature_id,gene,product"); featureQuery.setRows(Math.max(dataApi.MAX_ROWS, listFeatureIds.size())); LOGGER.trace("[{}] {}, {}", SolrCore.FEATURE.getSolrCoreName(), featureQuery, listFeatureIds.size()); apiResponse = dataApi.solrQuery(SolrCore.FEATURE, featureQuery); resp = jsonReader.readValue(apiResponse); respBody = (Map) resp.get("response"); List<GenomeFeature> features = dataApi.bindDocuments((List<Map>) respBody.get("docs"), GenomeFeature.class); for (GenomeFeature feature : features) { String featureId = feature.getId(); Map stat = mapStat.get(featureId); JSONObject item = new JSONObject(); item.put("genome_name", feature.getGenomeName()); item.put("genome_id", feature.getGenomeId()); item.put("accession", feature.getAccession()); item.put("feature_id", feature.getId()); item.put("alt_locus_tag", feature.getAltLocusTag()); item.put("refseq_locus_tag", feature.getRefseqLocusTag()); item.put("algorithm", annotation); item.put("patric_id", feature.getPatricId()); item.put("gene", feature.getGene()); item.put("product", feature.getProduct()); item.put("ec_name", stat.get("ec_description")); item.put("ec_number", stat.get("ec_number")); item.put("pathway_id", stat.get("pathway_id")); item.put("pathway_name", stat.get("pathway_name")); items.add(item); } i = 1; while (features.size() == 25000) { featureQuery.setStart(25000 * i); LOGGER.trace("[{}] {}, iter={}", SolrCore.FEATURE.getSolrCoreName(), featureQuery, i); apiResponse = dataApi.solrQuery(SolrCore.FEATURE, featureQuery); resp = jsonReader.readValue(apiResponse); respBody = (Map) resp.get("response"); features = dataApi.bindDocuments((List<Map>) respBody.get("docs"), GenomeFeature.class); for (GenomeFeature feature : features) { String featureId = feature.getId(); Map stat = mapStat.get(featureId); JSONObject item = new JSONObject(); item.put("genome_name", feature.getGenomeName()); item.put("genome_id", feature.getGenomeId()); item.put("accession", feature.getAccession()); item.put("feature_id", feature.getId()); item.put("alt_locus_tag", feature.getAltLocusTag()); item.put("refseq_locus_tag", feature.getRefseqLocusTag()); item.put("algorithm", annotation); item.put("patric_id", feature.getPatricId()); item.put("gene", feature.getGene()); item.put("product", feature.getProduct()); item.put("ec_name", stat.get("ec_description")); item.put("ec_number", stat.get("ec_number")); item.put("pathway_id", stat.get("pathway_id")); item.put("pathway_name", stat.get("pathway_name")); items.add(item); } i++; } count_total = items.size(); count_unique = count_total; } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } // Wrapping jsonResult jsonResult.put("total", count_total); jsonResult.put("results", items); jsonResult.put("unique", count_unique); return jsonResult; }