List of usage examples for org.apache.solr.client.solrj SolrQuery add
public ModifiableSolrParams add(String name, String... val)
From source file:edu.unc.lib.dl.search.solr.util.FacetFieldUtil.java
License:Apache License
private void addMultivaluedFacetValue(MultivaluedHierarchicalFacet facet, SolrQuery solrQuery) { StringBuilder filterQuery = new StringBuilder(); String solrFieldName = solrSettings.getFieldName(facet.getFieldName()); filterQuery.append(solrFieldName).append(":").append(SolrSettings.sanitize(facet.getSearchValue())) .append(",*"); solrQuery.addFilterQuery(filterQuery.toString()); solrQuery.add("f." + solrFieldName + ".facet.prefix", facet.getPivotValue()); }
From source file:edu.unc.lib.dl.search.solr.util.FacetFieldUtil.java
License:Apache License
public void addDefaultFacetPivot(String fieldKey, Class<?> facetClass, SolrQuery solrQuery) { String solrFieldName = solrSettings.getFieldName(fieldKey); if (CutoffFacet.class.equals(facetClass)) { solrQuery.add("f." + solrFieldName + ".facet.prefix", "1,"); } else if (MultivaluedHierarchicalFacet.class.equals(facetClass)) { solrQuery.add("f." + solrFieldName + ".facet.prefix", "^"); }/* ww w .jav a 2 s .com*/ }
From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java
License:Apache License
public void getChildrenCounts(List<BriefObjectMetadata> resultList, AccessGroupSet accessGroups, String countName, String queryAddendum, SolrQuery baseQuery) { long startTime = System.currentTimeMillis(); if (resultList == null || resultList.size() == 0) return;//from w w w .java 2 s . c o m String ancestorPathField = solrSettings.getFieldName(SearchFieldKeys.ANCESTOR_PATH.name()); SolrQuery solrQuery; if (baseQuery == null) { // Create a base query since we didn't receive one solrQuery = new SolrQuery(); StringBuilder query = new StringBuilder("*:*"); try { // Add access restrictions to query addAccessRestrictions(query, accessGroups); } catch (AccessRestrictionException e) { // If the user doesn't have any access groups, they don't have access to anything, return null. LOG.error(e.getMessage()); return; } solrQuery.setStart(0); solrQuery.setRows(0); solrQuery.setQuery(query.toString()); } else { // Starting from a base query solrQuery = baseQuery.getCopy(); // Make sure we aren't returning any normal results solrQuery.setRows(0); // Remove all facet fields so we are only getting ancestor path if (solrQuery.getFacetFields() != null) { for (String facetField : solrQuery.getFacetFields()) { solrQuery.removeFacetField(facetField); } } } if (queryAddendum != null) { solrQuery.setQuery(solrQuery.getQuery() + " AND " + queryAddendum); } solrQuery.setFacet(true); solrQuery.setFacetMinCount(1); solrQuery.addFacetField(ancestorPathField); Integer countPageSize; try { countPageSize = new Integer(searchSettings.getProperty("search.facet.countPageSize")); } catch (NumberFormatException e) { countPageSize = 20; } solrQuery.add("f." + ancestorPathField + ".facet.limit", Integer.toString(Integer.MAX_VALUE)); // Sort by value rather than count so that earlier tiers will come first in case the result gets cut off solrQuery.setFacetSort("index"); java.util.Map<Integer, StringBuilder> tierQueryMap = new java.util.HashMap<Integer, StringBuilder>(); java.util.Map<Integer, List<BriefObjectMetadata>> containerMap = new java.util.HashMap<Integer, List<BriefObjectMetadata>>(); // Pare the list of ids we are searching for and assigning counts to down to just containers for (BriefObjectMetadata metadataObject : resultList) { if (metadataObject.getPath() != null && metadataObject.getContentModel() != null && metadataObject.getContentModel().contains(ContentModelHelper.Model.CONTAINER.toString())) { CutoffFacetNode highestTier = metadataObject.getPath().getHighestTierNode(); StringBuilder tierQuery = tierQueryMap.get(highestTier.getTier()); List<BriefObjectMetadata> containerObjects = containerMap.get(highestTier.getTier()); if (tierQuery == null) { tierQuery = new StringBuilder(); tierQueryMap.put(highestTier.getTier(), tierQuery); containerObjects = new ArrayList<BriefObjectMetadata>(); containerMap.put(highestTier.getTier(), containerObjects); } if (tierQuery.length() == 0) { tierQuery.append(ancestorPathField).append(":("); } else { tierQuery.append(" OR "); } tierQuery.append(SolrSettings.sanitize(highestTier.getSearchValue())).append(",*"); containerObjects.add(metadataObject); // If there are a lot of results, then do a partial lookup if (containerObjects.size() >= countPageSize) { tierQuery.append(")"); this.executeChildrenCounts(tierQuery, containerObjects, solrQuery, countName, highestTier.getTier()); LOG.info("Partial query done at " + System.currentTimeMillis() + " (" + (System.currentTimeMillis() - startTime) + ")"); containerMap.remove(highestTier.getTier()); tierQueryMap.remove(highestTier.getTier()); } } } Iterator<java.util.Map.Entry<Integer, StringBuilder>> queryIt = tierQueryMap.entrySet().iterator(); while (queryIt.hasNext()) { java.util.Map.Entry<Integer, StringBuilder> tierQueryEntry = queryIt.next(); tierQueryEntry.getValue().append(')'); this.executeChildrenCounts(tierQueryEntry.getValue(), containerMap.get(tierQueryEntry.getKey()), solrQuery, countName, tierQueryEntry.getKey()); } LOG.info("Child count query done at " + System.currentTimeMillis() + " (" + (System.currentTimeMillis() - startTime) + ")"); }
From source file:edu.unc.lib.dl.ui.service.SolrQueryLayerService.java
License:Apache License
private void executeChildrenCounts(StringBuilder query, List<BriefObjectMetadata> containerObjects, SolrQuery solrQuery, String countName, Integer tier) { String ancestorPathField = solrSettings.getFieldName(SearchFieldKeys.ANCESTOR_PATH.name()); // Remove all ancestor path related filter queries or filter queries from previous count executions, so the counts // won't be cut off if (solrQuery.getFilterQueries() != null) { for (String filterQuery : solrQuery.getFilterQueries()) { if (filterQuery.contains(ancestorPathField)) { solrQuery.removeFilterQuery(filterQuery); }// w w w .j a v a 2 s.c o m } } if (tier != null) { solrQuery.remove("f." + ancestorPathField + ".facet.prefix"); solrQuery.add("f." + ancestorPathField + ".facet.prefix", tier + ","); } solrQuery.addFilterQuery(query.toString()); try { long startTime = System.currentTimeMillis(); QueryResponse queryResponse = this.executeQuery(solrQuery); LOG.info("Query executed in " + (System.currentTimeMillis() - startTime)); assignChildrenCounts(queryResponse.getFacetField(ancestorPathField), containerObjects, countName); } catch (SolrServerException e) { LOG.error("Error retrieving Solr search result request", e); } }
From source file:edu.vt.vbi.patric.cache.DataLandingGenerator.java
License:Apache License
private JSONObject getGenomeCounts() { JSONArray series = new JSONArray(); // TODO: reimplement with json facet using sub faceting SolrQuery queryComplete = new SolrQuery("genome_status:Complete"); SolrQuery queryWGS = new SolrQuery("genome_status:WGS"); queryComplete.setFacet(true).setRows(0).setFacetSort(FacetParams.FACET_SORT_INDEX); //.set("json.facet", "{genome_count:{range:{field:completion_date,start:\"2010-01-01T00:00:00.000Z\",end:\"2016-01-01T00:00:00.000Z\",gap:\"%2B1YEAR\",other:\"before\"}}}"); queryWGS.setFacet(true).setRows(0).setFacetSort(FacetParams.FACET_SORT_INDEX); //.set("json.facet", "{genome_count:{range:{field:completion_date,start:\"2010-01-01T00:00:00.000Z\",end:\"2016-01-01T00:00:00.000Z\",gap:\"%2B1YEAR\",other:\"before\"}}}"); try {//from w w w.j a va2 s . c om Date rangeStartDate = DateUtil.parseDate("2010-01-01'T'00:00:00.000'Z'"); Date rangeEndDate = DateUtil.parseDate("2016-01-01'T'00:00:00.000'Z'"); queryComplete.addDateRangeFacet("completion_date", rangeStartDate, rangeEndDate, "+1YEAR") .add(FacetParams.FACET_RANGE_OTHER, "before"); queryWGS.addDateRangeFacet("completion_date", rangeStartDate, rangeEndDate, "+1YEAR") .add(FacetParams.FACET_RANGE_OTHER, "before"); } catch (java.text.ParseException e) { LOGGER.error(e.getMessage(), e); } try { LOGGER.debug("getGenomeCount: [{}] {}", SolrCore.GENOME.getSolrCoreName(), queryComplete); String apiResponse = dataApi.solrQuery(SolrCore.GENOME, queryComplete); Map resp = jsonReader.readValue(apiResponse); Map completeFacets = (Map) ((Map) ((Map) resp.get("facet_counts")).get("facet_ranges")) .get("completion_date"); LOGGER.debug("getGenomeCount: [{}] {}", SolrCore.GENOME.getSolrCoreName(), queryWGS); apiResponse = dataApi.solrQuery(SolrCore.GENOME, queryWGS); resp = jsonReader.readValue(apiResponse); Map wgsFacets = (Map) ((Map) ((Map) resp.get("facet_counts")).get("facet_ranges")) .get("completion_date"); int countComplete = (Integer) completeFacets.get("before"); int countWGS = (Integer) wgsFacets.get("before"); Map<String, Integer> mapCountComplete = new HashMap<>(); Map<String, Integer> mapCountWGS = new HashMap<>(); List listComplete = (List) completeFacets.get("counts"); for (int i = 0; i < listComplete.size(); i = i + 2) { countComplete = countComplete + (Integer) listComplete.get(i + 1); mapCountComplete.put(((String) listComplete.get(i)).substring(0, 4), countComplete); } List listWGS = (List) wgsFacets.get("counts"); for (int i = 0; i < listWGS.size(); i = i + 2) { String year = ((String) listWGS.get(i)).substring(0, 4); countWGS = countWGS + (Integer) listWGS.get(i + 1); mapCountWGS.put(year, countWGS); JSONObject item = new JSONObject(); item.put("year", Integer.parseInt(year)); item.put("complete", mapCountComplete.get(year)); item.put("wgs", mapCountWGS.get(year)); series.add(item); } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } JSONObject jsonData = new JSONObject(); jsonData.put("chart_title", "Number of Bacterial Genomes"); jsonData.put("data", series); return jsonData; }
From source file:edu.vt.vbi.patric.portlets.CompPathwayMap.java
License:Apache License
private void processEcTable(ResourceRequest request, ResourceResponse response) throws IOException { String genomeId = request.getParameter("genomeId"); String taxonId = request.getParameter("taxonId"); String cType = request.getParameter("cType"); String map = request.getParameter("map"); String algorithm = request.getParameter("algorithm"); String pk = request.getParameter("pk"); Map<String, String> key = null; if (pk != null && !pk.isEmpty()) { key = jsonReader.readValue(SessionHandler.getInstance().get(SessionHandler.PREFIX + pk)); }/*w w w. ja va2 s. co m*/ int count_total = 0; JSONArray results = new JSONArray(); try { Set<String> ecNumbers = new HashSet<>(); DataApiHandler dataApi = new DataApiHandler(request); SolrQuery query = new SolrQuery("pathway_id:" + map + " AND 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 ") + ")")); } if (key != null && key.containsKey("genomeId") && !key.get("genomeId").equals("")) { query.addFilterQuery("genome_id:(" + key.get("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)\"}}}}"); 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("gene_count").toString()) > 0) { mapStat.put(value.get("val").toString(), value); ecNumbers.add(value.get("val").toString()); } } if (!ecNumbers.isEmpty()) { query = new SolrQuery( "pathway_id:" + map + " AND ec_number:(" + StringUtils.join(ecNumbers, " OR ") + ")"); query.setRows(ecNumbers.size()).setFields("ec_number,ec_description,occurrence"); query.addSort("ec_number", SolrQuery.ORDER.asc); apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, query); resp = jsonReader.readValue(apiResponse); Map respBdoby = (Map) resp.get("response"); count_total = (Integer) respBdoby.get("numFound"); List<Map> sdl = (List<Map>) respBdoby.get("docs"); for (Map doc : sdl) { String ecNumber = doc.get("ec_number").toString(); Map stat = mapStat.get(ecNumber); JSONObject item = new JSONObject(); item.put("algorithm", algorithm); item.put("ec_name", doc.get("ec_description")); item.put("ec_number", ecNumber); item.put("occurrence", doc.get("occurrence")); item.put("gene_count", stat.get("gene_count")); item.put("genome_count", stat.get("genome_count")); results.add(item); } } } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } JSONObject jsonResult = new JSONObject(); try { jsonResult.put("total", count_total); jsonResult.put("results", results); } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); } PrintWriter writer = response.getWriter(); jsonResult.writeJSONString(writer); writer.close(); }
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 {//from w w w .j a v a 2 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 {/*w ww . j a v a2s.c om*/ 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); }// w w w.ja va 2 s . c om 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); }// w ww. ja v a 2s.co 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; }