List of usage examples for org.apache.solr.client.solrj SolrQuery setRows
public SolrQuery setRows(Integer rows)
From source file:edu.vt.vbi.patric.common.DataApiHandler.java
License:Apache License
/** * wrapper function of field facet query * * @param core SolrCore/*from w ww.j a va2 s .co m*/ * @param queryParam query condition * @param filterParam filter condition * @param facetFields comma separated list of fields */ public Map getFieldFacets(SolrCore core, String queryParam, String filterParam, String facetFields) throws IOException { Map<String, Object> res = new HashMap<>(); SolrQuery query = new SolrQuery(); query.setQuery(queryParam); if (filterParam != null) { query.addFilterQuery(filterParam); } query.setRows(0).setFacet(true).setFacetLimit(-1).setFacetMinCount(1) .setFacetSort(FacetParams.FACET_SORT_COUNT).set("json.nl", "map"); query.addFacetField(facetFields.split(",")); List<String> fields = Arrays.asList(facetFields.split(",")); LOGGER.trace("getFieldFacets: [{}] {}", core.getSolrCoreName(), query.toString()); String response = this.solrQuery(core, query); Map resp = jsonParser.readValue(response); Map facet_fields = (Map) ((Map) resp.get("facet_counts")).get("facet_fields"); Map<String, Object> facets = new HashMap<>(); for (String field : fields) { Map values = (Map) facet_fields.get(field); Map<String, Integer> facetValues = new LinkedHashMap<>(); for (Map.Entry<String, Integer> entry : (Iterable<Map.Entry>) values.entrySet()) { facetValues.put(entry.getKey(), entry.getValue()); } facets.put(field, facetValues); } res.put("total", ((Map) resp.get("response")).get("numFound")); res.put("facets", facets); return res; }
From source file:edu.vt.vbi.patric.common.DataApiHandler.java
License:Apache License
/** * wrapper function of pivot facet query * * @param core SolrCore// ww w .ja v a 2s. c o m * @param queryParam query condition * @param filterParam filter condition * @param facetFields comma separated list of fields */ public Map getPivotFacets(SolrCore core, String queryParam, String filterParam, String facetFields) throws IOException { Map<String, Object> res = new HashMap<>(); SolrQuery query = new SolrQuery(); query.setQuery(queryParam); if (filterParam != null) { query.addFilterQuery(filterParam); } query.setRows(0).setFacet(true).setFacetLimit(-1).setFacetMinCount(1) .setFacetSort(FacetParams.FACET_SORT_INDEX); query.addFacetPivotField(facetFields); LOGGER.trace("getPivotFacets: [{}] {}", core.getSolrCoreName(), query.toString()); String response = this.solrQuery(core, query); Map<String, Object> resp = jsonParser.readValue(response); Map facet_fields = (Map) ((Map) resp.get("facet_counts")).get("facet_pivot"); List<Map> values = (List) facet_fields.get(facetFields); Map<String, Object> facet = new LinkedHashMap<>(); for (Map value : values) { String localKey = value.get("value").toString(); List<Map> localValues = (List) value.get("pivot"); Map<String, Integer> pivotValues = new LinkedHashMap<>(); for (Map local : localValues) { pivotValues.put(local.get("value").toString(), (Integer) local.get("count")); } facet.put(localKey, pivotValues); } res.put("total", ((Map) resp.get("response")).get("numFound")); res.put(facetFields, facet); return res; }
From source file:edu.vt.vbi.patric.common.DataApiHandler.java
License:Apache License
public SolrQuery buildSolrQuery(Map<String, String> key, String sorts, String facets, int start, int end, boolean highlight) { SolrQuery query = new SolrQuery(); SolrInterface solr = new SolrInterface(); // Processing key map query.setQuery(StringHelper.stripQuoteAndParseSolrKeywordOperator(key.get("keyword"))); if (key.containsKey("filter") && key.get("filter") != null) { query.addFilterQuery(key.get("filter")); }//from w w w . j av a 2 s . co m if (key.containsKey("filter2") && key.get("filter2") != null) { query.addFilterQuery(key.get("filter2")); } // use SolrJoin if possible if (key.containsKey("join")) { query.addFilterQuery(key.get("join")); } if (key.containsKey("fields") && !key.get("fields").equals("")) { query.addField(key.get("fields")); } // sort conditions if (sorts != null && !sorts.equals("") && !sorts.equals("[]")) { try { JSONArray sorter = (JSONArray) new JSONParser().parse(sorts); for (Object aSort : sorter) { JSONObject jsonSort = (JSONObject) aSort; query.addSort(SolrQuery.SortClause.create(jsonSort.get("property").toString(), jsonSort.get("direction").toString().toLowerCase())); } } catch (ParseException e) { LOGGER.error(e.getMessage(), e); } } // facet conditions if (facets != null && !facets.equals("") && !facets.equals("{}")) { query.setFacet(true).setFacetMinCount(1).setFacetLimit(-1).setFacetSort(FacetParams.FACET_SORT_COUNT) .set("json.nl", "map"); try { JSONObject facetConditions = (JSONObject) new JSONParser().parse(facets); if (facetConditions.containsKey("facet.sort")) { String facetSort = facetConditions.get("facet.sort").toString(); if (facetSort.equals("index")) { query.setFacetSort(FacetParams.FACET_SORT_INDEX); } } if (facetConditions.containsKey("field_facets")) { String[] fieldFacets = facetConditions.get("field_facets").toString().split(","); query.addFacetField(fieldFacets); } if (facetConditions.containsKey("date_range_facets") && !facetConditions.get("date_range_facets").equals("")) { String[] dateRangeFacets = facetConditions.get("date_range_facets").toString().split(","); for (String field : dateRangeFacets) { query.addDateRangeFacet(field, solr.getRangeStartDate(), solr.getRangeEndDate(), solr.getRangeDate()); } } } catch (ParseException e) { LOGGER.error(e.getMessage(), e); } } // start & end query.setStart(start); // setting starting index if (end == -1) { query.setRows(MAX_ROWS); } else { query.setRows(end); } // highlight if (highlight) { query.set("hl", "on").set("hl.fl", "*"); } return query; }
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(/*from ww w. j av a2 s. c o m*/ "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()); 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.common.SolrInterface.java
License:Apache License
public String getProteomicsTaxonIdFromFeatureId(String id) { SolrQuery query = new SolrQuery(); query.setQuery("na_feature_id:" + id); query.setRows(1000000); String experiment_id = ""; // try { // QueryResponse qr = server.query(query); // SolrDocumentList sdl = qr.getResults(); ///* w ww.jav a2s . co m*/ // for (SolrDocument d : sdl) { // for (Entry<String, Object> el : d) { // if (el.getKey().equals("experiment_id")) { // if (experiment_id.length() == 0) // experiment_id = el.getValue().toString(); // else // experiment_id += "##" + el.getValue().toString(); // } // } // } // } // catch (SolrServerException e) { // e.printStackTrace(); // } return experiment_id; }
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 ww .j a v a2s.co m*/ 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 .ja v a 2 s . c o 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 {//from w w w .j a v a 2 s .c o m 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 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)); }/*from w w w . java 2 s. com*/ 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 ww . j ava 2s . com 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()); }