Example usage for org.apache.solr.client.solrj SolrQuery setFields

List of usage examples for org.apache.solr.client.solrj SolrQuery setFields

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj SolrQuery setFields.

Prototype

public SolrQuery setFields(String... fields) 

Source Link

Usage

From source file:edu.vt.vbi.patric.portlets.FeaturePropertiesPortlet.java

License:Apache License

protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {

    SiteHelper.setHtmlMetaElements(request, response, "Feature Overview");

    response.setContentType("text/html");
    String cType = request.getParameter("context_type");
    String cId = request.getParameter("context_id");

    if (cType != null && cId != null && cType.equals("feature")) {

        DataApiHandler dataApi = new DataApiHandler(request);
        GenomeFeature feature = dataApi.getPATRICFeature(cId);

        if (feature != null) {
            List<GenomeFeature> listReleateFeatures;
            List<String> listUniprotkbAccessions = null;
            List<Map<String, String>> listUniprotIds = null;
            String refseqLink = null;
            String refseqLocusTag = null;
            Map<String, String> virulenceFactor = null;

            SolrQuery query = new SolrQuery("pos_group:" + feature.getPosGroupInQuote());
            query.setSort("annotation_sort", SolrQuery.ORDER.asc);

            String apiResponse = dataApi.solrQuery(SolrCore.FEATURE, query);

            Map resp = jsonReader.readValue(apiResponse);
            Map respBody = (Map) resp.get("response");

            listReleateFeatures = dataApi.bindDocuments((List<Map>) respBody.get("docs"), GenomeFeature.class);

            // build listUniprotkbAccessions based on GI number
            if (feature.getGi() > 0) {
                query = new SolrQuery("id_value:(" + feature.getGi() + ")");
                query.addFilterQuery("id_type:GI").setRows(dataApi.MAX_ROWS);

                apiResponse = dataApi.solrQuery(SolrCore.ID_REF, query);

                resp = jsonReader.readValue(apiResponse);
                respBody = (Map) resp.get("response");

                List<Map> uniprotList = (List<Map>) respBody.get("docs");

                if (uniprotList.size() > 0) {
                    listUniprotkbAccessions = new ArrayList<>();
                }//www.j a v  a2 s . c o  m

                for (Map doc : uniprotList) {
                    listUniprotkbAccessions.add(doc.get("uniprotkb_accession").toString());
                }
            }

            if (listUniprotkbAccessions != null) {
                query = new SolrQuery(
                        "uniprotkb_accession:(" + StringUtils.join(listUniprotkbAccessions, " OR ") + ")");
                query.setFields("uniprotkb_accession,id_type,id_value");
                query.setRows(1000);

                apiResponse = dataApi.solrQuery(SolrCore.ID_REF, query);

                resp = jsonReader.readValue(apiResponse);
                respBody = (Map) resp.get("response");

                List<Map> sdl = (List<Map>) respBody.get("docs");

                if (!sdl.isEmpty()) {
                    listUniprotIds = new ArrayList<>();
                }

                for (Map doc : sdl) {
                    Map<String, String> uniprot = new HashMap<>();

                    uniprot.put("Accession", doc.get("uniprotkb_accession").toString());
                    uniprot.put("idType", doc.get("id_type").toString());
                    uniprot.put("idValue", doc.get("id_value").toString());

                    listUniprotIds.add(uniprot);
                }
            }

            if (feature.getAnnotation().equals("PATRIC")) {

                if (feature.hasGeneId()) {
                    refseqLink = SiteHelper.getExternalLinks("ncbi_gene").replace("&", "&amp;")
                            + feature.getGeneId();
                }
                refseqLocusTag = feature.getRefseqLocusTag();
            } else if (feature.getAnnotation().equals("RefSeq")) {
                refseqLocusTag = feature.getAltLocusTag();
            }

            query = new SolrQuery("(locus_tag:" + feature.getAltLocusTag()
                    + (feature.hasRefseqLocusTag() ? " OR locus_tag: " + feature.getRefseqLocusTag() : "")
                    + ")");
            query.setFilterQueries("source:PATRIC_VF");
            query.setFields("source,source_id");

            apiResponse = dataApi.solrQuery(SolrCore.SPECIALTY_GENE, query);

            resp = jsonReader.readValue(apiResponse);
            respBody = (Map) resp.get("response");

            List<Map> sdl = (List<Map>) respBody.get("docs");

            for (Map doc : sdl) {
                virulenceFactor = new HashMap<>();

                virulenceFactor.put("source", doc.get("source").toString());
                virulenceFactor.put("sourceId", doc.get("source_id").toString());
            }

            request.setAttribute("feature", feature);
            request.setAttribute("listReleateFeatures", listReleateFeatures);
            request.setAttribute("listUniprotkbAccessions", listUniprotkbAccessions);
            request.setAttribute("listUniprotIds", listUniprotIds);
            request.setAttribute("refseqLink", refseqLink);
            request.setAttribute("refseqLocusTag", refseqLocusTag);
            request.setAttribute("virulenceFactor", virulenceFactor);

            PortletRequestDispatcher prd = getPortletContext()
                    .getRequestDispatcher("/WEB-INF/jsp/overview/feature_properties.jsp");
            prd.include(request, response);
        } else {
            PrintWriter writer = response.getWriter();
            writer.write(" ");
            writer.close();
        }
    } else {
        PrintWriter writer = response.getWriter();
        writer.write("<p>Invalid Parameter - missing context information</p>");
        writer.close();
    }
}

From source file:edu.vt.vbi.patric.portlets.GenomeBrowser.java

License:Apache License

private void printSequenceInfo(ResourceRequest request, ResourceResponse response) throws IOException {

    String sequenceId = request.getParameter("sequence_id");

    DataApiHandler dataApi = new DataApiHandler(request);
    String SequenceString = null;
    SolrQuery query = new SolrQuery("sequence_id:" + sequenceId);
    query.setFields("sequence");

    LOGGER.trace("[{}] {}", SolrCore.SEQUENCE.getSolrCoreName(), query.toString());
    String apiResponse = dataApi.solrQuery(SolrCore.SEQUENCE, query);
    Map resp = jsonReader.readValue(apiResponse);
    Map respBody = (Map) resp.get("response");

    List<GenomeSequence> sequences = dataApi.bindDocuments((List<Map>) respBody.get("docs"),
            GenomeSequence.class);

    for (GenomeSequence sequence : sequences) {
        SequenceString = sequence.getSequence();
    }/*  w w w  .ja  va  2 s  .c om*/

    response.getWriter().println(SequenceString);
}

From source file:edu.vt.vbi.patric.portlets.GlobalSearch.java

License:Apache License

private Map processGlobalSearchFeature(ResourceRequest request) throws IOException {

    DataApiHandler dataApi = new DataApiHandler(request);
    String keyword = request.getParameter("keyword");
    String need = request.getParameter("need");

    SolrQuery query = new SolrQuery(StringHelper.stripQuoteAndParseSolrKeywordOperator(keyword));
    if (need.equals("search")) {
        query.setRows(3).set("hl", "on").set("hl.fl", "*");
    } else {// w ww  .  java 2s .c  om
        query.setRows(dataApi.MAX_ROWS);
    }

    query.setFields(StringUtils.join(DownloadHelper.getFieldsForFeatures(), ","));
    query.addFilterQuery("!feature_type:source");
    query.addFilterQuery("!annotation:BRC1");

    LOGGER.trace("processGlobalSearch: [{}] {}", SolrCore.FEATURE.getSolrCoreName(), query);
    String apiResponse = dataApi.solrQuery(SolrCore.FEATURE, query);

    Map resp = jsonReader.readValue(apiResponse);

    return resp;
}

From source file:edu.vt.vbi.patric.portlets.GlobalSearch.java

License:Apache License

private Map processGlobalSearchGenome(ResourceRequest request) throws IOException {

    DataApiHandler dataApi = new DataApiHandler(request);
    String keyword = request.getParameter("keyword");
    String need = request.getParameter("need");

    SolrQuery query = new SolrQuery(StringHelper.stripQuoteAndParseSolrKeywordOperator(keyword));
    if (need.equals("search")) {
        query.setRows(3).set("hl", "on").set("hl.fl", "*");
    } else {//from   www .  java  2 s .  com
        query.setRows(dataApi.MAX_ROWS);
    }

    query.setFields(StringUtils.join(DownloadHelper.getFieldsForGenomes(), ","));

    LOGGER.trace("processGlobalSearch: [{}] {}", SolrCore.GENOME.getSolrCoreName(), query);
    String apiResponse = dataApi.solrQuery(SolrCore.GENOME, query);

    Map resp = jsonReader.readValue(apiResponse);

    return resp;
}

From source file:edu.vt.vbi.patric.portlets.GlobalSearch.java

License:Apache License

private Map processGlobalSearchTaxonomy(ResourceRequest request) throws IOException {

    DataApiHandler dataApi = new DataApiHandler(request);
    String keyword = request.getParameter("keyword");
    String need = request.getParameter("need");

    SolrQuery query = new SolrQuery(StringHelper.stripQuoteAndParseSolrKeywordOperator(keyword));
    if (need.equals("search")) {
        query.setRows(3).set("hl", "on").set("hl.fl", "*");
    } else {//from  ww  w .j  ava2s .c  o m
        query.setRows(dataApi.MAX_ROWS);
    }

    query.setFields("taxon_id,taxon_name,taxon_rank,genomes");

    LOGGER.trace("processGlobalSearch: [{}] {}", SolrCore.TAXONOMY.getSolrCoreName(), query);
    String apiResponse = dataApi.solrQuery(SolrCore.TAXONOMY, query);

    Map resp = jsonReader.readValue(apiResponse);

    return resp;
}

From source file:edu.vt.vbi.patric.portlets.GlobalSearch.java

License:Apache License

private Map processGlobalSearchExperiment(ResourceRequest request) throws IOException {

    DataApiHandler dataApi = new DataApiHandler(request);
    String keyword = request.getParameter("keyword");
    String need = request.getParameter("need");

    SolrQuery query = new SolrQuery(StringHelper.stripQuoteAndParseSolrKeywordOperator(keyword));
    if (need.equals("search")) {
        query.setRows(3).set("hl", "on").set("hl.fl", "*");
    } else {/* ww w . j a  v  a2s  .co m*/
        query.setRows(dataApi.MAX_ROWS);
    }

    query.setFields("eid,accession,title,description,organism,strain,mutant,timeseries,condition");

    LOGGER.trace("processGlobalSearch: [{}] {}", SolrCore.TRANSCRIPTOMICS_EXPERIMENT.getSolrCoreName(), query);
    String apiResponse = dataApi.solrQuery(SolrCore.TRANSCRIPTOMICS_EXPERIMENT, query);

    Map resp = jsonReader.readValue(apiResponse);

    return resp;
}

From source file:edu.vt.vbi.patric.portlets.PathwayFinder.java

License:Apache License

@SuppressWarnings("unchecked")
private JSONObject processPathwayTab(DataApiHandler dataApi, String pathwayId, String ecNumber,
        String annotation, String taxonId, String genomeId, String keyword)
        throws PortletException, IOException {

    JSONObject jsonResult = new JSONObject();
    SolrQuery query = new SolrQuery("*:*");

    if (pathwayId != null && !pathwayId.equals("")) {
        query.addFilterQuery("pathway_id:" + pathwayId);
    }/*from   w  ww.  j  a  v  a2  s .co m*/

    if (ecNumber != null && !ecNumber.equals("")) {
        query.addFilterQuery("ec_number:" + ecNumber);
    }

    if (annotation != null && !annotation.equals("") && !annotation.equalsIgnoreCase("ALL")) {
        query.addFilterQuery("annotation:" + annotation);
    }

    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 (keyword != null && !keyword.equals("")) {
        query.setQuery(keyword);
    }

    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("processPathwayTab: [{}] {}", 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(value.get("val").toString(), value);
            listPathwayIds.add(value.get("val").toString());
        }

        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("processPathwayTab: [{}] {}", 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.PathwayFinder.java

License:Apache License

@SuppressWarnings("unchecked")
private JSONObject processEcNumberTab(DataApiHandler dataApi, String pathwayId, String ecNumber,
        String annotation, String taxonId, String genomeId, String keyword)
        throws PortletException, IOException {

    JSONObject jsonResult = new JSONObject();
    SolrQuery query = new SolrQuery("*:*");

    if (pathwayId != null && !pathwayId.equals("")) {
        query.addFilterQuery("pathway_id:" + pathwayId);
    }/*from  w ww .  j a  v  a 2s  .co  m*/

    if (ecNumber != null && !ecNumber.equals("")) {
        query.addFilterQuery("ec_number:" + ecNumber);
    }

    if (annotation != null && !annotation.equals("")) {
        query.addFilterQuery("annotation:" + annotation);
    }

    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 (keyword != null && !keyword.equals("")) {
        query.setQuery(keyword);
    }

    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("processEcNumberTab: [{}] {}", 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(1000000, listPathwayIds.size()));

            LOGGER.trace("processEcNumberTab: [{}] {}", 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
    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.PathwayFinder.java

License:Apache License

@SuppressWarnings("unchecked")
private JSONObject processGeneTab(DataApiHandler dataApi, String pathwayId, String ecNumber, String annotation,
        String taxonId, String genomeId, String keyword) throws PortletException, IOException {

    LOGGER.debug("pathwayId:{}, ecNumber:{}, annotation:{}, taxonId:{}, genomeId:{}, keyword:{}", pathwayId,
            ecNumber, annotation, taxonId, genomeId, keyword);

    JSONObject jsonResult = new JSONObject();
    SolrQuery query = new SolrQuery("*:*");

    if (pathwayId != null && !pathwayId.equals("")) {
        query.addFilterQuery("pathway_id:" + pathwayId);
    }/*from   w w  w  .j  a  v  a  2s .c o  m*/

    if (ecNumber != null && !ecNumber.equals("")) {
        query.addFilterQuery("ec_number:(" + ecNumber.replaceAll(",", " OR ").replaceAll("'", "") + ")");
    }

    if (annotation != null && !annotation.equals("")) {
        query.addFilterQuery("annotation:" + annotation);
    }

    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 (keyword != null && !keyword.equals("")) {
        query.setQuery(keyword);
    }

    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("processGeneTab: [{}] {}", 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());
        }

        // 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("processGeneTab: [{}] {}", SolrCore.FEATURE.getSolrCoreName(), featureQuery);

            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);
            }
            count_total = items.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.PathwayTable.java

License:Apache License

@SuppressWarnings("unchecked")
public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {

    response.setContentType("application/json");

    HashMap<String, String> key = new HashMap<>();

    if (request.getParameter("id") != null) {
        key.put("feature_id", request.getParameter("id"));
    }//  w  w w . ja  v  a 2  s. c om

    DataApiHandler dataApi = new DataApiHandler(request);

    int count_total = 0;
    JSONArray results = new JSONArray();
    List<String> pathwayKeys = new ArrayList<>();
    Map<String, Integer> mapOccurrence = new HashMap<>();
    try {
        SolrQuery query = new SolrQuery("feature_id:" + request.getParameter("id"));
        query.setRows(dataApi.MAX_ROWS);

        String apiResponse = dataApi.solrQuery(SolrCore.PATHWAY, query);

        Map resp = jsonReader.readValue(apiResponse);
        Map respBody = (Map) resp.get("response");

        count_total = (Integer) respBody.get("numFound");
        List<Map> sdl = (List<Map>) respBody.get("docs");

        for (Map doc : sdl) {
            String pathwayKey = "(pathway_id:" + doc.get("pathway_id").toString() + " AND ec_number:"
                    + doc.get("ec_number").toString() + ")";

            pathwayKeys.add(pathwayKey);
        }

        SolrQuery queryRef = new SolrQuery(StringUtils.join(pathwayKeys, " OR "));
        queryRef.setFields("pathway_id,ec_number,occurrence");
        queryRef.setRows(pathwayKeys.size());

        apiResponse = dataApi.solrQuery(SolrCore.PATHWAY_REF, queryRef);

        resp = jsonReader.readValue(apiResponse);
        respBody = (Map) resp.get("response");

        List<Map> refList = (List<Map>) respBody.get("docs");

        for (Map doc : refList) {
            mapOccurrence.put(doc.get("pathway_id") + "_" + doc.get("ec_number"),
                    (Integer) doc.get("occurrence"));
        }

        for (Map doc : sdl) {
            JSONObject item = new JSONObject();
            item.put("pathway_id", doc.get("pathway_id"));
            item.put("feature_id", doc.get("feature_id"));
            item.put("pathway_name", doc.get("pathway_name"));
            item.put("pathway_class", doc.get("pathway_class"));
            item.put("algorithm", doc.get("annotation"));
            item.put("ec_number", doc.get("ec_number"));
            item.put("ec_name", doc.get("ec_description"));
            item.put("taxon_id", doc.get("taxon_id"));
            item.put("genome_id", doc.get("genome_id"));

            item.put("occurrence", mapOccurrence.get(doc.get("pathway_id") + "_" + doc.get("ec_number")));

            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();
}