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

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

Introduction

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

Prototype

public SolrQuery setRows(Integer rows) 

Source Link

Usage

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);
    }//ww w .  j  a va  2  s.  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);
    }/*  ww w  .jav a  2 s.com*/

    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"));
    }/*from   w w w.  j a  va 2s.c o m*/

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

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

License:Apache License

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

    response.setContentType("application/json");

    String callType = request.getParameter("callType");

    if (callType != null && callType.equals("savetopk")) {

        String cId = request.getParameter("cId");
        String cType = request.getParameter("cType");
        String map = request.getParameter("map");
        String algorithm = request.getParameter("algorithm");
        String ec_number = request.getParameter("ec_number");

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

        if (cId != null && !cId.equals("")) {
            key.put("genomeId", cId);
        }//from ww  w  . jav  a 2s .co m
        if (cType != null && !cType.equals("")) {
            key.put("cType", cType);
        }

        if (map != null && !map.equals("")) {
            key.put("map", map);
        }

        if (algorithm != null && !algorithm.equals("")) {
            key.put("algorithm", algorithm);
        }

        if (ec_number != null && !ec_number.equals("")) {
            key.put("ec_number", ec_number);
        }

        key.put("which", "download_from_heatmap_feature");

        long pk = (new Random()).nextLong();

        SessionHandler.getInstance().set(SessionHandler.PREFIX + pk, jsonWriter.writeValueAsString(key));

        PrintWriter writer = response.getWriter();
        writer.write("" + pk);
        writer.close();

    } else if (callType != null && callType.equals("show")) {

        JSONObject jsonResult = new JSONObject();
        JSONArray results = new JSONArray();
        String pk = request.getParameter("pk");
        Map<String, String> key = jsonReader
                .readValue(SessionHandler.getInstance().get(SessionHandler.PREFIX + pk));

        DataApiHandler dataApi = new DataApiHandler(request);
        try {
            SolrQuery query = new SolrQuery("*:*");
            List<String> joinConditions = new ArrayList<>();

            if (key.containsKey("map")) {
                joinConditions.add("pathway_id:(" + key.get("map") + ")");
            }
            if (key.containsKey("algorithm")) {
                joinConditions.add("annotation:(" + key.get("algorithm") + ")");
            }
            if (key.containsKey("ec_number")) {
                joinConditions.add("ec_number:(" + key.get("ec_number").replaceAll(",", " OR ") + ")");
            }
            if (key.containsKey("genomeId")) {
                joinConditions.add("genome_id:(" + key.get("genomeId").replace(",", " OR ") + ")");
            }

            if (!joinConditions.isEmpty()) {
                query.addFilterQuery(SolrCore.PATHWAY.getSolrCoreJoin("feature_id", "feature_id",
                        StringUtils.join(joinConditions, " AND ")));
            }
            query.setRows(10000);

            LOGGER.debug("[{}] {}", SolrCore.FEATURE.getSolrCoreName(), query.toString());

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

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

            int numFound = (Integer) respBody.get("numFound");
            List<GenomeFeature> features = dataApi.bindDocuments((List<Map>) respBody.get("docs"),
                    GenomeFeature.class);

            for (GenomeFeature feature : features) {

                results.add(feature.toJSONObject());
            }

            jsonResult.put("total", numFound);
            jsonResult.put("results", results);
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
        }

        PrintWriter writer = response.getWriter();
        jsonResult.writeJSONString(writer);
        writer.close();
    }
}

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

License:Apache License

protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
    response.setContentType("text/html");

    SiteHelper.setHtmlMetaElements(request, response, "Phylogeny");

    List<Integer> phylogenyOrderIds = Arrays.asList(2037, 1385, 80840, 213849, 51291, 186802, 91347, 186826,
            118969, 356, 766, 136, 72273, 135623);

    String contextType = request.getParameter("context_type");
    String contextId = request.getParameter("context_id");

    if (contextType != null && contextId != null) {

        DataApiHandler dataApi = new DataApiHandler(request);

        List<Map<String, Object>> orderList = new ArrayList<>();
        int taxonId = 0;

        try {/*  ww w. j av a  2s  .  c  o  m*/
            if (contextType.equals("genome")) {

                Genome genome = dataApi.getGenome(contextId);
                taxonId = genome.getTaxonId();
            } else {
                taxonId = Integer.parseInt(contextId);
            }

            // Step1. has Order in lineage?
            Taxonomy taxonomy = dataApi.getTaxonomy(taxonId);

            List<String> lineageRanks = taxonomy.getLineageRanks();

            if (lineageRanks.contains("order")) {
                List<Integer> lineageIds = taxonomy.getLineageIds();
                List<String> lineageNames = taxonomy.getLineageNames();

                int index = lineageRanks.indexOf("order");
                int orderTaxonId = lineageIds.get(index);

                if (phylogenyOrderIds.contains(orderTaxonId)) {
                    Map order = new HashMap();
                    order.put("name", lineageNames.get(index));
                    order.put("taxonId", lineageIds.get(index));

                    orderList.add(order);
                }
            }

            if (orderList.isEmpty()) {
                // no rank Order found in lineage, then,
                // Step2. has Order rank in descendants
                SolrQuery query = new SolrQuery(
                        "lineage_ids:" + taxonId + " AND taxon_rank:order AND taxon_id:("
                                + StringUtils.join(phylogenyOrderIds, " OR ") + ")");
                query.setFields("taxon_id,taxon_name,taxon_rank");
                query.setRows(100);

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

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

                List<Map> sdl = (List<Map>) respBody.get("docs");
                for (Map doc : sdl) {
                    if (doc.get("taxon_rank").equals("order")) {
                        Map node = new HashMap<>();
                        node.put("taxonId", doc.get("taxon_id"));
                        node.put("name", doc.get("taxon_name").toString());

                        orderList.add(node);
                    }
                }
            }
        } catch (MalformedURLException e) {
            LOGGER.error(e.getMessage(), e);
        }

        request.setAttribute("orderList", orderList);
        request.setAttribute("taxonId", taxonId);

        PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/index.jsp");
        prd.include(request, response);
    }
}

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

License:Apache License

public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {

    String contextType = request.getParameter("context_type");
    String contextId = request.getParameter("context_id");
    String genomeFilter = request.getParameter("genome_filter");

    DataApiHandler dataApi = new DataApiHandler(request);

    if (contextType != null && contextId != null) {

        if (contextType.equals("genome")) {

            Genome genome = dataApi.getGenome(contextId);

            if (genome != null) {

                // check genome_amr data is available.
                SolrQuery query = new SolrQuery("genome_id:" + genome.getId());
                query.addSort("antibiotic", SolrQuery.ORDER.asc).addSort("resistant_phenotype",
                        SolrQuery.ORDER.asc);
                query.setRows(dataApi.MAX_ROWS);
                dataApi = new DataApiHandler(request);
                String apiResponse = dataApi.solrQuery(SolrCore.GENOME_AMR, query);

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

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

                request.setAttribute("genome", genome);
                request.setAttribute("amr", amr);

                response.setContentType("text/html");
                PortletRequestDispatcher prd = getPortletContext()
                        .getRequestDispatcher("/WEB-INF/jsp/overview/sequence_summary.jsp");
                prd.include(request, response);
            }/*from  w w  w.j  a  v a 2  s  .  c  o  m*/
        } else {
            // taxon level

            List<String> annotations = Arrays.asList("PATRIC", "RefSeq");

            for (String annotation : annotations) {

                String queryParam;

                switch (annotation) {
                case "PATRIC":
                    queryParam = "patric_cds:[1 TO *]";
                    break;
                case "RefSeq":
                    queryParam = "refseq_cds:[1 TO *]";
                    break;
                default:
                    queryParam = "*:*";
                    break;
                }

                if (genomeFilter != null && !genomeFilter.equals("")) {
                    queryParam += " AND (" + genomeFilter + ")";
                }

                Map counts = dataApi.getFieldFacets(SolrCore.GENOME, queryParam,
                        "taxon_lineage_ids:" + contextId, "genome_status");

                request.setAttribute(annotation, counts);
            }

            response.setContentType("text/html");
            PortletRequestDispatcher prd = getPortletContext()
                    .getRequestDispatcher("/WEB-INF/jsp/overview/sequence_summary.jsp");
            prd.include(request, response);
        }
    }
}

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

License:Apache License

public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {
    int taxonId = Integer.parseInt(request.getParameter("taxonId"));
    String mode = request.getParameter("mode");

    DataApiHandler dataApi = new DataApiHandler(request);
    JSONArray tree;/*w  w  w.  j  a  v a2  s  .c  o  m*/
    response.setContentType("application/json");

    switch (mode) {
    case "txtree":
        tree = OrganismTreeBuilder.buildGenomeTree(dataApi, taxonId);
        tree.writeJSONString(response.getWriter());
        break;
    case "azlist":
        tree = OrganismTreeBuilder.buildGenomeList(dataApi, taxonId);
        tree.writeJSONString(response.getWriter());
        break;
    case "tgm":
        tree = OrganismTreeBuilder.buildTaxonGenomeMapping(dataApi, taxonId);
        tree.writeJSONString(response.getWriter());
        break;
    case "search":
        String searchOn = request.getParameter("searchon");
        String keyword = request.getParameter("query");

        tree = new JSONArray();
        SolrQuery query = new SolrQuery();
        if (searchOn.equals("txtree")) {
            query.setQuery("taxon_name:" + keyword + " AND genomes:[1 TO *] AND lineage_ids:" + taxonId);
            query.setRows(10000).addField("taxon_name,taxon_id");

            String apiResponse = dataApi.solrQuery(SolrCore.TAXONOMY, query);
            Map resp = jsonReader.readValue(apiResponse);
            Map respBody = (Map) resp.get("response");

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

            for (Taxonomy taxon : taxonomyList) {
                JSONObject item = new JSONObject();
                item.put("display_name", taxon.getTaxonName());
                item.put("taxon_id", taxon.getId());

                tree.add(item);
            }
        } else {
            // searchOn is "azlist"
            query.setQuery("genome_name:" + keyword + " AND taxon_lineage_ids:" + taxonId);
            query.setRows(10000).addField("genome_name,genome_id");

            String apiResponse = dataApi.solrQuery(SolrCore.TAXONOMY, query);
            Map resp = jsonReader.readValue(apiResponse);
            Map respBody = (Map) resp.get("response");

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

            for (Genome genome : genomeList) {
                JSONObject item = new JSONObject();
                item.put("display_name", genome.getGenomeName());
                item.put("genome_id", genome.getId());

                tree.add(item);
            }
        }

        JSONObject result = new JSONObject();
        result.put("genomeList", tree);
        result.put("keyword", keyword);
        result.put("totalCount", tree.size());

        result.writeJSONString(response.getWriter());
        break;
    default:
        tree = new JSONArray();
        tree.writeJSONString(response.getWriter());
        break;
    }
}

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

License:Apache License

protected void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
    response.setContentType("text/html");
    response.setTitle("TB");

    int mtbTaxon = 77643;
    DataApiHandler dataApi = new DataApiHandler(request);

    Map<Integer, Integer> genomes = new HashMap<>();
    int cntExperiments = -1;

    // getting genome count
    {/*from  w w  w .java  2  s  .c  o  m*/
        SolrQuery query = new SolrQuery("taxon_id:(1773 OR 77643)");
        query.setRows(2).addField("taxon_id,genomes");

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

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

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

        for (Taxonomy taxonomy : taxonomyList) {
            genomes.put(taxonomy.getId(), taxonomy.getGenomeCount());
        }
    }

    // getting expression data count
    {
        SolrQuery query = new SolrQuery("*:*");
        query.setRows(0).addFilterQuery(
                SolrCore.GENOME.getSolrCoreJoin("genome_id", "genome_ids", "taxon_lineage_ids:1763"));

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

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

        cntExperiments = (Integer) respBody.get("numFound");
    }
    request.setAttribute("genomes", genomes);
    request.setAttribute("cntExperiments", cntExperiments);
    request.setAttribute("mtbTaxon", mtbTaxon);

    PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher("/WEB-INF/jsp/community/tb.jsp");
    prd.include(request, response);
}

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

License:Apache License

private JSONObject getEnrichmentPathway(ResourceRequest request, List<String> featureIDs) throws IOException {

    DataApiHandler dataApi = new DataApiHandler(request);

    // 1. get Pathway ID, Pathway Name & genomeID
    //solr/pathway/select?q=feature_id:(PATRIC.83332.12.NC_000962.CDS.34.1524.fwd)&fl=pathway_name,pathway_id,gid

    Map<String, JSONObject> pathwayMap = new LinkedHashMap<>();
    Set<String> listFeatureID = new HashSet<>();
    Set<String> listGenomeID = new HashSet<>();
    Set<String> listPathwayID = new HashSet<>();

    SolrQuery query = new SolrQuery("feature_id:(" + StringUtils.join(featureIDs, " OR ") + ")");
    int queryRows = Math.max(dataApi.MAX_ROWS, (featureIDs.size() * 2));
    query.addField("pathway_name,pathway_id,genome_id,feature_id").setRows(queryRows);

    LOGGER.trace("Enrichment 1/3: [{}] {}", SolrCore.PATHWAY.getSolrCoreName(), query);

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

    Map resp = jsonReader.readValue(apiResponse);
    Map respBody = (Map) resp.get("response");
    List<Map> pathwayList = (List) respBody.get("docs");

    for (Map doc : pathwayList) {
        JSONObject pw = new JSONObject();
        pw.put("pathway_id", doc.get("pathway_id"));
        pw.put("pathway_name", doc.get("pathway_name"));
        pathwayMap.put(doc.get("pathway_id").toString(), pw);

        // LOGGER.debug("{}", pw.toJSONString());
        listFeatureID.add(doc.get("feature_id").toString());
        listGenomeID.add(doc.get("genome_id").toString());
        listPathwayID.add(doc.get("pathway_id").toString());
    }//from www .j  ava  2 s.co m

    // 2. get pathway ID & Ocnt
    //solr/pathway/select?q=feature_id:(PATRIC.83332.12.NC_000962.CDS.34.1524.fwd)&rows=0&facet=true
    // &json.facet={stat:{field:{field:pathway_id,limit:-1,facet:{gene_count:"unique(feature_id)"}}}}
    query = new SolrQuery("feature_id:(" + StringUtils.join(featureIDs, " OR ") + ")");
    query.setRows(0).setFacet(true);
    query.add("json.facet",
            "{stat:{field:{field:pathway_id,limit:-1,facet:{gene_count:\"unique(feature_id)\"}}}}");

    LOGGER.trace("Enrichment 2/3: [{}] {}", SolrCore.PATHWAY.getSolrCoreName(),
            URLDecoder.decode(query.toString(), "UTF-8"));

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

    resp = jsonReader.readValue(apiResponse);
    Map facets = (Map) resp.get("facets");
    if ((Integer) facets.get("count") > 0) {
        Map stat = (Map) facets.get("stat");
        List<Map> buckets = (List) stat.get("buckets");

        for (Map value : buckets) {
            String aPathwayId = value.get("val").toString();

            if (pathwayMap.containsKey(aPathwayId)) {
                pathwayMap.get(aPathwayId).put("ocnt", value.get("gene_count"));
            }
        }
    }

    // 3. with genomeID, get pathway ID & Ecnt
    //solr/pathway/select?q=genome_id:83332.12 AND pathway_id:(00230 OR 00240)&fq=annotation:PATRIC&rows=0&facet=true //&facet.mincount=1&facet.limit=-1
    // &json.facet={stat:{field:{field:pathway_id,limit:-1,facet:{gene_count:"unique(feature_id)"}}}}
    if (!listGenomeID.isEmpty() && !listPathwayID.isEmpty()) {
        query = new SolrQuery("genome_id:(" + StringUtils.join(listGenomeID, " OR ") + ") AND pathway_id:("
                + StringUtils.join(listPathwayID, " OR ") + ")");
        query.setRows(0).setFacet(true).addFilterQuery("annotation:PATRIC");
        query.add("json.facet",
                "{stat:{field:{field:pathway_id,limit:-1,facet:{gene_count:\"unique(feature_id)\"}}}}");

        LOGGER.trace("Enrichment 3/3: {}", query.toString());

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

        resp = jsonReader.readValue(apiResponse);
        facets = (Map) resp.get("facets");
        Map stat = (Map) facets.get("stat");
        List<Map> buckets = (List) stat.get("buckets");

        for (Map value : buckets) {
            pathwayMap.get(value.get("val").toString()).put("ecnt", value.get("gene_count"));
        }
    }

    // 4. Merge hash and calculate percentage on the fly
    JSONObject jsonResult = new JSONObject();
    JSONArray results = new JSONArray();
    for (JSONObject item : pathwayMap.values()) {
        if (item.get("ecnt") != null && item.get("ocnt") != null) {
            float ecnt = Float.parseFloat(item.get("ecnt").toString());
            float ocnt = Float.parseFloat(item.get("ocnt").toString());
            float percentage = ocnt / ecnt * 100;
            item.put("percentage", (int) percentage);
            results.add(item);
        }
    }
    jsonResult.put("results", results);
    jsonResult.put("total", results.size());
    jsonResult.put("featureRequested", featureIDs.size());
    jsonResult.put("featureFound", listFeatureID.size());

    return jsonResult;
}

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

License:Apache License

public void serveResource(ResourceRequest request, ResourceResponse response)
        throws PortletException, IOException {
    String callType = request.getParameter("callType");

    if (callType != null) {
        switch (callType) {
        case "saveParams": {

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

            Map<String, String> key = new HashMap<>();
            key.put("keyword", "locus_tag:(" + keyword + ") OR refseq_locus_tag:(" + keyword + ") ");
            key.put("fields", "pid");

            SolrQuery query = dataApi.buildSolrQuery(key, null, null, 0, -1, false);

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

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

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

            Set<String> sampleIds = new HashSet<>();

            for (Map doc : sdl) {
                sampleIds.add(doc.get("pid").toString());
            }/*from  w ww  . ja  v  a  2s .  c  o  m*/

            String sId = StringUtils.join(sampleIds, ",");

            key = new HashMap();
            if (!keyword.equals("")) {
                key.put("keyword", keyword);
            }

            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();

            if (!sId.equals("")) {
                key.put("sampleId", sId);
                long pk = (new Random()).nextLong();

                SessionHandler.getInstance().set(SessionHandler.PREFIX + pk,
                        jsonWriter.writeValueAsString(key));

                writer.write("" + pk);
            } else {
                writer.write("");
            }
            writer.close();

            break;
        }
        case "getTables": {

            String expId = request.getParameter("expId");
            String sampleId = request.getParameter("sampleId");
            String wsExperimentId = request.getParameter("wsExperimentId");
            String wsSampleId = request.getParameter("wsSampleId");
            String keyword = request.getParameter("keyword");
            DataApiHandler dataApi = new DataApiHandler(request);

            JSONArray sample = new JSONArray();

            if ((sampleId != null && !sampleId.equals("")) || (expId != null && !expId.equals(""))) {

                String query_keyword = "";

                if (expId != null && !expId.equals("")) {
                    query_keyword += "eid:(" + expId.replaceAll(",", " OR ") + ")";
                }

                if (sampleId != null && !sampleId.equals("")) {
                    if (query_keyword.length() > 0) {
                        query_keyword += " AND ";
                    }
                    query_keyword += "pid:(" + sampleId.replaceAll(",", " OR ") + ")";
                }

                Map<String, String> key = new HashMap<>();
                key.put("keyword", query_keyword);
                key.put("fields", "pid,expname,expmean,timepoint,mutant,strain,condition");

                SolrQuery query = dataApi.buildSolrQuery(key, null, null, 0, -1, false);

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

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

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

                for (final Map doc : sdl) {
                    final JSONObject item = new JSONObject(doc);
                    sample.add(item);
                }
            }

            // Read from JSON if collection parameter is there
            ExpressionDataCollection parser = null;
            if (wsExperimentId != null && !wsExperimentId.equals("")) {
                String token = getAuthorizationToken(request);

                parser = new ExpressionDataCollection(wsExperimentId, token);
                parser.read(ExpressionDataCollection.CONTENT_SAMPLE);
                if (wsSampleId != null && !wsSampleId.equals("")) {
                    parser.filter(wsSampleId, ExpressionDataCollection.CONTENT_SAMPLE);
                }
                // Append samples from collection to samples from DB
                sample = parser.append(sample, ExpressionDataCollection.CONTENT_SAMPLE);
            }

            String sampleList = "";
            sampleList += ((JSONObject) sample.get(0)).get("pid");

            for (int i = 1; i < sample.size(); i++) {
                sampleList += "," + ((JSONObject) sample.get(i)).get("pid");
            }

            JSONObject jsonResult = new JSONObject();
            jsonResult.put(ExpressionDataCollection.CONTENT_SAMPLE + "Total", sample.size());
            jsonResult.put(ExpressionDataCollection.CONTENT_SAMPLE, sample);
            JSONArray expression = new JSONArray();

            if ((sampleId != null && !sampleId.equals("")) || (expId != null && !expId.equals(""))) {

                String query_keyword = "";

                if (keyword != null && !keyword.equals("")) {
                    query_keyword += "(alt_locus_tag:(" + keyword + ") OR refseq_locus_tag:(" + keyword + ")) ";
                }

                if (expId != null && !expId.equals("")) {
                    if (query_keyword.length() > 0) {
                        query_keyword += " AND ";
                    }
                    query_keyword += "eid:(" + expId.replaceAll(",", " OR ") + ")";
                }

                if (sampleId != null && !sampleId.equals("")) {
                    if (query_keyword.length() > 0) {
                        query_keyword += " AND ";
                    }
                    query_keyword += "pid:(" + sampleId.replaceAll(",", " OR ") + ")";
                }

                Map<String, String> key = new HashMap<>();
                key.put("keyword", query_keyword);
                key.put("fields", "pid,refseq_locus_tag,feature_id,log_ratio,z_score");

                SolrQuery query = dataApi.buildSolrQuery(key, null, null, 0, -1, false);

                LOGGER.trace("getTables: [{}] {}", SolrCore.TRANSCRIPTOMICS_GENE.getSolrCoreName(), query);

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

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

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

                for (final Map doc : sdl) {
                    final JSONObject item = new JSONObject(doc);
                    expression.add(item);
                }

                // TODO: re-implement when data API removes limit 25k records
                int start = 0;
                int fetchedSize = sdl.size();
                while (fetchedSize == 25000) {
                    start += 25000;
                    query.setStart(start);

                    LOGGER.trace("getTables: [{}] {}", SolrCore.TRANSCRIPTOMICS_GENE.getSolrCoreName(), query);

                    final String apiResponseSub = dataApi.solrQuery(SolrCore.TRANSCRIPTOMICS_GENE, query);
                    final Map respSub = jsonReader.readValue(apiResponseSub);
                    final Map respBodySub = (Map) respSub.get("response");

                    sdl = (List<Map>) respBodySub.get("docs");
                    fetchedSize = sdl.size();

                    for (final Map doc : sdl) {
                        final JSONObject item = new JSONObject(doc);
                        expression.add(item);
                    }
                }
            }

            if (wsExperimentId != null && !wsExperimentId.equals("")) {

                parser.read(ExpressionDataCollection.CONTENT_EXPRESSION);
                if (wsSampleId != null && !wsSampleId.equals(""))
                    parser.filter(wsSampleId, ExpressionDataCollection.CONTENT_EXPRESSION);

                // Append expression from collection to expression from DB
                expression = parser.append(expression, ExpressionDataCollection.CONTENT_EXPRESSION);
            }

            JSONArray stats = getExperimentStats(dataApi, expression, sampleList, sample);
            jsonResult.put(ExpressionDataCollection.CONTENT_EXPRESSION + "Total", stats.size());
            jsonResult.put(ExpressionDataCollection.CONTENT_EXPRESSION, stats);

            response.setContentType("application/json");
            PrintWriter writer = response.getWriter();
            jsonResult.writeJSONString(writer);
            writer.close();

            break;
        }
        case "doClustering": {

            String data = request.getParameter("data");
            String g = request.getParameter("g");
            String e = request.getParameter("e");
            String m = request.getParameter("m");
            String ge = request.getParameter("ge");
            String pk = request.getParameter("pk");
            String action = request.getParameter("action");

            String folder = "/tmp/";
            String filename = folder + "tmp_" + pk + ".txt";
            String output_filename = folder + "cluster_tmp_" + pk;
            try {

                PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)));
                out.write(data);
                out.close();

            } catch (Exception es) {
                LOGGER.error(es.getMessage(), es);
            }

            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            if (action.equals("Run"))
                writer.write(doCLustering(filename, output_filename, g, e, m, ge).toString());

            writer.close();

            break;
        }
        case "saveState": {

            String keyType = request.getParameter("keyType");
            String pageAt = request.getParameter("pageAt");
            String sampleFilter = request.getParameter("sampleFilter");
            String regex = request.getParameter("regex");
            String regexGN = request.getParameter("regexGN");
            String upFold = request.getParameter("upFold");
            String downFold = request.getParameter("downFold");
            String upZscore = request.getParameter("upZscore");
            String downZscore = request.getParameter("downZscore");
            String significantGenes = request.getParameter("significantGenes");
            String ClusterColumnOrder = request.getParameter("ClusterColumnOrder");
            String ClusterRowOrder = request.getParameter("ClusterRowOrder");
            String heatmapState = request.getParameter("heatmapState");
            String heatmapAxis = request.getParameter("heatmapAxis");
            String colorScheme = request.getParameter("colorScheme");
            String filterOffset = request.getParameter("filterOffset");

            Map<String, String> key = new HashMap<>();
            key.put("sampleFilter", (sampleFilter == null) ? "" : sampleFilter);
            key.put("pageAt", (pageAt == null) ? "" : pageAt);
            key.put("regex", (regex == null) ? "" : regex);
            key.put("regexGN", (regexGN == null) ? "" : regexGN);
            key.put("upFold", (upFold == null) ? "" : upFold);
            key.put("downFold", (downFold == null) ? "" : downFold);
            key.put("upZscore", (upZscore == null) ? "" : upZscore);
            key.put("downZscore", (downZscore == null) ? "" : downZscore);
            key.put("significantGenes", (significantGenes == null) ? "" : significantGenes);
            key.put("ClusterRowOrder", (ClusterRowOrder == null) ? "" : ClusterRowOrder);
            key.put("ClusterColumnOrder", (ClusterColumnOrder == null) ? "" : ClusterColumnOrder);
            key.put("heatmapState", (heatmapState == null) ? "" : heatmapState);
            key.put("heatmapAxis", (heatmapAxis == null) ? "" : heatmapAxis);
            key.put("colorScheme", (colorScheme == null) ? "" : colorScheme);
            key.put("filterOffset", (filterOffset == null) ? "" : filterOffset);

            long pk = (new Random()).nextLong();
            SessionHandler.getInstance().set(SessionHandler.PREFIX + pk, jsonWriter.writeValueAsString(key));

            response.setContentType("text/html");
            PrintWriter writer = response.getWriter();
            writer.write("" + pk);
            writer.close();

            break;
        }
        case "getState": {

            String keyType = request.getParameter("keyType");
            String pk = request.getParameter("random");

            if ((pk != null) && (keyType != null)) {
                JSONArray results = new JSONArray();
                JSONObject a = new JSONObject();
                Map<String, String> key = jsonReader
                        .readValue(SessionHandler.getInstance().get(SessionHandler.PREFIX + pk));
                if (key != null) {
                    a.put("sampleFilter", key.get("sampleFilter"));
                    a.put("pageAt", key.get("pageAt"));
                    a.put("regex", key.get("regex"));
                    a.put("regexGN", key.get("regexGN"));
                    a.put("upFold", key.get("upFold"));
                    a.put("downFold", key.get("downFold"));
                    a.put("upZscore", key.get("upZscore"));
                    a.put("downZscore", key.get("downZscore"));
                    a.put("significantGenes", key.get("significantGenes"));
                    a.put("ClusterRowOrder", key.get("ClusterRowOrder"));
                    a.put("ClusterColumnOrder", key.get("ClusterColumnOrder"));
                    a.put("heatmapState", key.get("heatmapState"));
                    a.put("heatmapAxis", key.get("heatmapAxis"));
                    a.put("colorScheme", key.get("colorScheme"));
                    a.put("filterOffset", key.get("filterOffset"));
                }
                results.add(a);
                response.setContentType("application/json");
                PrintWriter writer = response.getWriter();
                results.writeJSONString(writer);
                writer.close();
            }
            break;
        }
        case "downloadFeatures": {
            String featureIds = request.getParameter("featureIds");
            String fileFormat = request.getParameter("fileFormat");
            String fileName = "Table_Gene";

            List<String> tableHeader = DownloadHelper.getHeaderForFeatures();
            List<String> tableField = DownloadHelper.getFieldsForFeatures();
            JSONArray tableSource = new JSONArray();

            DataApiHandler dataApi = new DataApiHandler(request);

            SolrQuery query = new SolrQuery("feature_id:(" + featureIds.replaceAll(",", " OR ") + ")");
            query.setFields(StringUtils.join(DownloadHelper.getFieldsForFeatures(), ","));
            query.setRows(dataApi.MAX_ROWS);

            LOGGER.trace("downloadFeatures: [{}] {}", SolrCore.FEATURE.getSolrCoreName(), query);

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

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

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

            for (final GenomeFeature feature : features) {
                tableSource.add(feature.toJSONObject());
            }

            final ExcelHelper excel = new ExcelHelper("xssf", tableHeader, tableField, tableSource);
            excel.buildSpreadsheet();

            if (fileFormat.equalsIgnoreCase("xlsx")) {
                response.setContentType("application/octetstream");
                response.addProperty("Content-Disposition",
                        "attachment; filename=\"" + fileName + "." + fileFormat + "\"");

                excel.writeSpreadsheettoBrowser(response.getPortletOutputStream());
            } else {
                response.setContentType("application/octetstream");
                response.addProperty("Content-Disposition",
                        "attachment; filename=\"" + fileName + "." + fileFormat + "\"");

                response.getWriter().write(excel.writeToTextFile());
            }
        }
        }
    }
}