List of usage examples for com.mongodb BasicDBObject get
public Object get(final String key)
From source file:act.installer.bing.BingSearchRanker.java
License:Open Source License
/** * This function is used to write out the conditional reachability results with data on target chemical, root chemical, * depth of steps from root to target chemical, the bing search results, all the other names associated with the target * and inchi of the target in a tsv file. This function is not scalable since it has to have an in-memory representation * of the target and root molecule's bing results to input the data into the TSV file. * @param descendantInchiToRootInchi mapping of chemical to its root chemical in the conditional reachability tree * @param depthOfPathFromRootToMolecule Since a chemical can be associated with only one root, there is a unique mapping between * the chemical and it's depth from the root. This structure holds that information. * @param outputPath The output path of the tsv file. * @throws IOException//w w w . j av a 2 s . com */ public void writeBingSearchRanksAsTSVUsingConditionalReachabilityFormat(Set<String> inchisToProcess, Map<String, String> descendantInchiToRootInchi, Map<String, Integer> depthOfPathFromRootToMolecule, String outputPath) throws IOException { // Define headers List<String> bingRankerHeaderFields = new ArrayList<>(); addChemicalHeaders(bingRankerHeaderFields); bingRankerHeaderFields.add(ConditionalReachabilityHeaderFields.DEPTH.name()); bingRankerHeaderFields.add(ConditionalReachabilityHeaderFields.ROOT_MOLECULE_BEST_NAME.name()); bingRankerHeaderFields.add(ConditionalReachabilityHeaderFields.TOTAL_COUNT_SEARCH_RESULTS_ROOT.name()); bingRankerHeaderFields.add(ConditionalReachabilityHeaderFields.ROOT_INCHI.name()); LOGGER.info("The total number of inchis are: %d", inchisToProcess.size()); LOGGER.info("Creating mappings between inchi and it's DB object"); DBCursor cursor = mongoDB.fetchNamesAndUsageForInchis(inchisToProcess); // TODO: We have to do an in-memory calculation of all the inchis since we need to pair up the descendant and root // db objects. This can take up a lot of memory. Map<String, BasicDBObject> inchiToDBObject = new HashMap<>(); int cursorCounter = 0; while (cursor.hasNext()) { cursorCounter++; BasicDBObject o = (BasicDBObject) cursor.next(); String inchi = o.get("InChI").toString(); if (inchi == null) { LOGGER.error("Inchi could not be parsed."); continue; } inchiToDBObject.put(inchi, o); } LOGGER.info("The total number of inchis found in the db is: %d", cursorCounter); LOGGER.info("Going to write to TSV file."); try (TSVWriter<String, String> tsvWriter = new TSVWriter<>(bingRankerHeaderFields)) { tsvWriter.open(new File(outputPath)); int counter = 0; for (String descendantInchi : descendantInchiToRootInchi.keySet()) { // Add all the descendant field results BasicDBObject descendentDBObject = inchiToDBObject.get(descendantInchi); if (descendentDBObject == null) { LOGGER.info("Could not find info on inchi %s", descendantInchi); continue; } // Add all descendant molecule fields Map<String, String> row = new HashMap<>(); updateRowWithChemicalInformation(descendentDBObject, row); // Add all the root molecule fields String rootInchi = descendantInchiToRootInchi.get(descendantInchi); row.put(ConditionalReachabilityHeaderFields.ROOT_INCHI.name(), rootInchi); BasicDBObject rootDBObject = inchiToDBObject.get(rootInchi); if (rootDBObject != null) { BasicDBObject rootXref = (BasicDBObject) rootDBObject.get("xref"); BasicDBObject rootBing = (BasicDBObject) rootXref.get("BING"); BasicDBObject rootMetadata = (BasicDBObject) rootBing.get("metadata"); String bestNameForRootMolecule = rootMetadata.get("best_name").toString(); row.put(ConditionalReachabilityHeaderFields.ROOT_MOLECULE_BEST_NAME.name(), bestNameForRootMolecule.equals("") ? rootInchi : bestNameForRootMolecule); row.put(ConditionalReachabilityHeaderFields.TOTAL_COUNT_SEARCH_RESULTS_ROOT.name(), rootMetadata.get("total_count_search_results").toString()); } else { row.put(ConditionalReachabilityHeaderFields.ROOT_MOLECULE_BEST_NAME.name(), rootInchi); row.put(ConditionalReachabilityHeaderFields.TOTAL_COUNT_SEARCH_RESULTS_ROOT.name(), DEFAULT_COUNT.toString()); } row.put(ConditionalReachabilityHeaderFields.DEPTH.name(), depthOfPathFromRootToMolecule.get(descendantInchi).toString()); tsvWriter.append(row); tsvWriter.flush(); counter++; } LOGGER.info("Wrote %d rows to %s", counter, outputPath); } }
From source file:act.installer.bing.BingSearchResults.java
License:Open Source License
/** This key function caches in a MongoDB collection and returns a set of SearchResults. * If present, the results are returned from the cache. If not, the results are queried and returned after updating * the cache./*from w w w. j av a2s . c o m*/ * @param name (String) the name to return results for. Will be normalized to lower case. * @return a set of SearchResults * @throws IOException */ public Set<SearchResult> getAndCacheTopSearchResults(String name) throws IOException { String formattedName = name.toLowerCase(); BasicDBObject nameSearchResultDBObject = bingCacheMongoDB .getNameSearchResultDBObjectFromName(formattedName); Set<SearchResult> searchResults = new HashSet<>(); // There are 3 cases: // 1) There is a corresponding entry in the cache AND the topSearchResults are populated. // In this case, we read from the cache and return the results. // 2) There is a corresponding entry in the cache BUT the topSearchResults are not populated. // This case occurs when only totalCountSearchResults is populated. // In this case, perform the relevant query, update the cache and return the results // 3) There is no corresponding entry in the cache. // In this case, perform the relevant query, create a new entry in the cache and return the results. if (nameSearchResultDBObject == null) { // Case 3) LOGGER.debug("No corresponding entry in the cache. Fetching results and populating cache."); // Query the results searchResults = fetchTopSearchResults(formattedName, TOP_N); // Create new object and update it NameSearchResults nameSearchResults = new NameSearchResults(formattedName); nameSearchResults.setTopSearchResults(searchResults); // Save new document in the cache bingCacheMongoDB.cacheNameSearchResult(nameSearchResults); return searchResults; } // There is an existing entry in the DB BasicDBList topSearchResultsList = (BasicDBList) nameSearchResultDBObject.get("topSearchResults"); if (topSearchResultsList == null) { // Case 2) LOGGER.debug( "Existing entry in the cache, with empty topSearchResults. Fetching results and updating cache."); // Query the results searchResults = fetchTopSearchResults(formattedName, TOP_N); // Create new object and update its instance variable NameSearchResults nameSearchResults = new NameSearchResults(formattedName); nameSearchResults.setTopSearchResults(searchResults); // Update the cache bingCacheMongoDB.updateTopSearchResults(formattedName, nameSearchResults); return searchResults; } // Case 1) LOGGER.debug("Existing entry in the cache, with populated topSearchResults. Returning from the cache."); for (Object topSearchResult : topSearchResultsList) { SearchResult searchResult = new SearchResult(); BasicDBObject topSearchResultDBObject = (BasicDBObject) topSearchResult; searchResult.populateFromBasicDBObject(topSearchResultDBObject); searchResults.add(searchResult); } return searchResults; }
From source file:act.installer.bing.BingSearchResults.java
License:Open Source License
public Set<SearchResult> getTopSearchResultsFromCache(String name) { Set<SearchResult> searchResults = new HashSet<>(); String formattedName = name.toLowerCase(); BasicDBObject nameSearchResultDBObject = bingCacheMongoDB .getNameSearchResultDBObjectFromName(formattedName); if (nameSearchResultDBObject == null) { return searchResults; }// w ww. j a va 2 s . c o m BasicDBList topSearchResultsList = (BasicDBList) nameSearchResultDBObject.get("topSearchResults"); if (topSearchResultsList == null) { return searchResults; } for (Object topSearchResult : topSearchResultsList) { SearchResult searchResult = new SearchResult(); BasicDBObject topSearchResultDBObject = (BasicDBObject) topSearchResult; searchResult.populateFromBasicDBObject(topSearchResultDBObject); searchResults.add(searchResult); } return searchResults; }
From source file:act.installer.bing.BingSearchResults.java
License:Open Source License
public Long getTotalCountSearchResultsFromCache(String name) { String formattedName = name.toLowerCase(); BasicDBObject nameSearchResultDBObject = bingCacheMongoDB .getNameSearchResultDBObjectFromName(formattedName); Long totalCountSearchResults; if (nameSearchResultDBObject == null) { return -1L; }/*from www . j a v a 2 s .c om*/ totalCountSearchResults = (Long) nameSearchResultDBObject.get("totalCountSearchResults"); if (totalCountSearchResults == null) { return -1L; } return totalCountSearchResults; }
From source file:act.installer.bing.BingSearchResults.java
License:Open Source License
/** This key function caches in a MongoDB collection and returns the total count of Bing search results. * If present, the results are returned from the cache. If not, the results are queried and returned after updating * the cache./*from w w w . j av a 2s . c o m*/ * @param name (String) the name to return results for. Will be normalized to lower case. * @return the total search result count * @throws IOException */ public Long getAndCacheTotalCountSearchResults(String name) throws IOException { String formattedName = name.toLowerCase(); BasicDBObject nameSearchResultDBObject = bingCacheMongoDB .getNameSearchResultDBObjectFromName(formattedName); Long totalCountSearchResults; // There are 3 cases: // 1) There is a corresponding entry in the cache AND the totalCountSearchResults are populated. // In this case, we read from the cache and return the results. // 2) There is a corresponding entry in the cache BUT the totalCountSearchResults are not populated. // This case occurs when only topSearchResults is populated. // In this case, perform the relevant query, update the cache and return the total count // 3) There is no corresponding entry in the cache. // In this case, perform the relevant query, create a new entry in the cache and return the total count. if (nameSearchResultDBObject == null) { // Case 3) LOGGER.debug("No corresponding entry in the cache. Fetching results and populating cache."); // Query the results totalCountSearchResults = fetchTotalCountSearchResults(formattedName); // Create new object and update it NameSearchResults nameSearchResults = new NameSearchResults(formattedName); nameSearchResults.setTotalCountSearchResults(totalCountSearchResults); // Save new document in the cache bingCacheMongoDB.cacheNameSearchResult(nameSearchResults); return totalCountSearchResults; } // There is an existing entry in the cache totalCountSearchResults = (Long) nameSearchResultDBObject.get("totalCountSearchResults"); if (totalCountSearchResults == null || totalCountSearchResults < 0) { // Case 2) LOGGER.debug("Existing entry in the cache, with empty totalCountSearchResults. " + "Fetching results and updating cache."); // Query the results totalCountSearchResults = fetchTotalCountSearchResults(formattedName); // Create new object and update its instance variable NameSearchResults nameSearchResults = new NameSearchResults(formattedName); nameSearchResults.setTotalCountSearchResults(totalCountSearchResults); // Update the cache bingCacheMongoDB.updateTotalCountSearchResults(formattedName, nameSearchResults); return totalCountSearchResults; } // Case 1) LOGGER.debug( "Existing entry in the cache, with populated totalCountSearchResults. Returning from the cache."); return totalCountSearchResults; }
From source file:act.installer.bing.SearchResult.java
License:Open Source License
public void populateFromBasicDBObject(BasicDBObject result) { setDescription((String) result.get("description")); setTitle((String) result.get("title")); setUrl((String) result.get("url")); }
From source file:act.installer.reachablesexplorer.FreemarkerRenderer.java
License:Open Source License
private Object buildReachableModel(Reachable r, List<PathwayDoc> pathwayDocs) { /* Freemarker's template language is based on a notion of "hashes," which are effectively just an untyped hierarchy * of maps and arrays culminating in scalar values (think of it like a JSON doc done up in plain Java types). * There are new facilities to run some Java accessors from within freemarker templates, but the language is * sufficiently brittle on its own that complicating things seems like a recipe for much pain. */*from ww w.j a va 2 s . c o m*/ * This is just how Freemarker works. Oh well. */ Map<String, Object> model = new HashMap<>(); model.put("pageTitle", r.getPageName()); model.put("inchi", r.getInchi()); model.put("smiles", r.getSmiles()); model.put("structureRendering", r.getStructureFilename()); model.put("cascade", r.getPathwayVisualization()); if (hidePathways) { model.put("hidePathways", true); // TODO: is there a cleaner way to make this URL? model.put("orderLink", String.format("{{SERVER}}%s?%s=%s", ORDER_PATH, ORDER_INCHI_KEY_PARAM, r.getInchiKey())); } if (r.getWordCloudFilename() != null) { model.put("wordcloudRendering", r.getWordCloudFilename()); } List<Object> pathways = new ArrayList<>(); for (PathwayDoc doc : pathwayDocs) { pathways.add(new HashMap<String, String>() { { put("link", doc.getPageName()); put("name", doc.getPathText()); // With help from http://stackoverflow.com/questions/13183982/html-entity-for-check-mark put("hasDna", doc.getHasDNA() ? "✓" : ""); } }); } if (pathways.size() > 0) { model.put("pathways", pathways); } List<PatentSummary> patentSummaries = r.getPatentSummaries(); if (patentSummaries != null && !patentSummaries.isEmpty()) { List<Map<String, String>> patentModel = patentSummaries.stream().map(p -> { return new HashMap<String, String>() { { put("title", p.getTitle()); put("link", p.generateUSPTOURL()); put("id", p.getId().replaceFirst("-.*$", "")); // Strip date + XML suffix, just leave grant number. } }; }).collect(Collectors.toList()); model.put("patents", patentModel); } Map<Chemical.REFS, BasicDBObject> xrefs = r.getXref(); // We check that xrefs is not null to avoid NPEs when a Reachable does not have xrefs (cross-references) populated. // It is expected that projections such as L3 or L4 are in that situation. if (xrefs != null) { // Each XREF being populated in a Reachable as DBObjects, serialization and deserialization causes funky problems // to appear. In particular, casting to BasicDBObjects fails because Jackson deserialized it as a LinkedHashMap // See http://stackoverflow.com/questions/28821715/java-lang-classcastexception-java-util-linkedhashmap-cannot-be-cast-to-com-test if (xrefs.containsKey(Chemical.REFS.BING)) { BasicDBObject bingXref = xrefs.get(Chemical.REFS.BING); Map<String, Object> bingMetadata = (Map) bingXref.get("metadata"); List<Map> bingUsageTerms = (List) bingMetadata.get("usage_terms"); if (bingUsageTerms.size() > 0) { List<Map<String, Object>> bingUsageTermsModel = bingUsageTerms.stream() .map(usageTerm -> new HashMap<String, Object>() { { put("usageTerm", usageTerm.get("usage_term")); put("urls", usageTerm.get("urls")); } }).collect(Collectors.toList()); Collections.sort(bingUsageTermsModel, (o1, o2) -> ((ArrayList) o2.get("urls")).size() - ((ArrayList) o1.get("urls")).size()); model.put("bingUsageTerms", bingUsageTermsModel); } } if (xrefs.containsKey(Chemical.REFS.WIKIPEDIA)) { BasicDBObject wikipediaXref = xrefs.get(Chemical.REFS.WIKIPEDIA); String wikipediaUrl = wikipediaXref.getString("dbid"); model.put("wikipediaUrl", wikipediaUrl); } } if (r.getSynonyms() != null) { if (r.getSynonyms().getPubchemSynonyms() != null) { List<Map<String, Object>> pubchemSynonymModel = r.getSynonyms().getPubchemSynonyms().entrySet() .stream().map(entry -> new HashMap<String, Object>() { { put("synonymType", entry.getKey().toString()); put("synonyms", entry.getValue().stream().collect(Collectors.toList())); } }).collect(Collectors.toList()); model.put("pubchemSynonyms", pubchemSynonymModel); } if (r.getSynonyms().getMeshHeadings() != null) { List<Map<String, Object>> meshHeadingModel = r.getSynonyms().getMeshHeadings().entrySet().stream() .map(entry -> new HashMap<String, Object>() { { String key = entry.getKey().toString(); put("synonymType", "NONE".equals(key) ? "MeSH" : key); put("synonyms", entry.getValue().stream().collect(Collectors.toList())); } }).collect(Collectors.toList()); model.put("meshHeadings", meshHeadingModel); } } if (r.getPhysiochemicalProperties() != null) { PhysiochemicalProperties physiochemicalProperties = r.getPhysiochemicalProperties(); model.put("physiochemicalProperties", new HashMap<String, String>() { { put("pka", String.format("%.2f", physiochemicalProperties.getPkaAcid1())); put("logp", String.format("%.2f", physiochemicalProperties.getLogPTrue())); put("hlb", String.format("%.2f", physiochemicalProperties.getHlbVal())); } }); } return model; }
From source file:act.server.MongoDB.java
License:Open Source License
private String getReactantFromMongoDocument(BasicDBObject family, String which, int i) { BasicDBList o = (BasicDBList) ((DBObject) family.get("enz_summary")).get(which); if (i >= o.size()) return ""; return "" + (Long) ((DBObject) o.get(i)).get("pubchem"); }
From source file:act.server.MongoDB.java
License:Open Source License
public NamesOfMolecule getNamesFromBasicDBObject(BasicDBObject c) { String inchi = (String) c.get("InChI"); NamesOfMolecule moleculeNames = new NamesOfMolecule(inchi); BasicDBObject names = (BasicDBObject) c.get("names"); if (names != null) { BasicDBList brendaNamesList = (BasicDBList) names.get("brenda"); if (brendaNamesList != null) { Set<String> brendaNames = new HashSet<>(); for (Object brendaName : brendaNamesList) { brendaNames.add((String) brendaName); }/*from w ww. j ava2 s.c om*/ moleculeNames.setBrendaNames(brendaNames); } } // XREF BasicDBObject xref = (BasicDBObject) c.get("xref"); if (xref != null) { // CHEBI BasicDBObject chebi = (BasicDBObject) xref.get("CHEBI"); if (chebi != null) { Set<String> chebiNames = new HashSet<>(); BasicDBObject chebiMetadata = (BasicDBObject) chebi.get("metadata"); BasicDBList chebiSynonymsList = (BasicDBList) chebiMetadata.get("Synonym"); if (chebiSynonymsList != null) { for (Object chebiName : chebiSynonymsList) { chebiNames.add((String) chebiName); } moleculeNames.setChebiNames(chebiNames); } } // METACYC BasicDBObject metacyc = (BasicDBObject) xref.get("METACYC"); if (metacyc != null) { Set<String> metacycNames = new HashSet<>(); BasicDBList metacycMetadata = (BasicDBList) metacyc.get("meta"); if (metacycMetadata != null) { for (Object metaCycMeta : metacycMetadata) { BasicDBObject metaCycMetaDBObject = (BasicDBObject) metaCycMeta; String metaCycName = (String) metaCycMetaDBObject.get("sname"); if (metaCycName == null) { continue; } metacycNames.add(metaCycName); } moleculeNames.setMetacycNames(metacycNames); } } // DRUGBANK BasicDBObject drugbank = (BasicDBObject) xref.get("DRUGBANK"); if (drugbank != null) { Set<String> drugbankNames = new HashSet<>(); BasicDBObject drugbankMetadata = (BasicDBObject) drugbank.get("metadata"); drugbankNames.add((String) drugbankMetadata.get("name")); BasicDBObject drugbankSynonyms = (BasicDBObject) drugbankMetadata.get("synonyms"); if (drugbankSynonyms != null) { if (drugbankSynonyms.get("synonym") instanceof String) { drugbankNames.add((String) drugbankSynonyms.get("synonym")); moleculeNames.setDrugbankNames(drugbankNames); } else { BasicDBList drugbankSynonymsList = (BasicDBList) drugbankSynonyms.get("synonym"); if (drugbankSynonymsList != null) { for (Object drugbankSynonym : drugbankSynonymsList) { drugbankNames.add((String) drugbankSynonym); } moleculeNames.setDrugbankNames(drugbankNames); } } } Set<String> drugbankBrands = new HashSet<>(); BasicDBObject drugbankBrandsObject = (BasicDBObject) drugbankMetadata.get("brands"); if (drugbankBrandsObject != null) { if (drugbankBrandsObject.get("brand") instanceof String) { drugbankBrands.add((String) drugbankBrandsObject.get("brand")); moleculeNames.setDrugbankBrands(drugbankBrands); } else { BasicDBList drugbankBrandsList = (BasicDBList) drugbankBrandsObject.get("brand"); if (drugbankBrandsList != null) { for (Object drugbankBrand : drugbankBrandsList) { drugbankBrands.add((String) drugbankBrand); } moleculeNames.setDrugbankBrands(drugbankBrands); } } } } // WIKIPEDIA BasicDBObject wikipedia = (BasicDBObject) xref.get("WIKIPEDIA"); if (wikipedia != null) { BasicDBObject wikipediaMetadata = (BasicDBObject) wikipedia.get("metadata"); if (wikipediaMetadata != null) { String wikipediaName = (String) wikipediaMetadata.get("article"); moleculeNames.setWikipediaName(wikipediaName); } } } return moleculeNames; }
From source file:act.server.MongoDB.java
License:Open Source License
/** * This function retrieves the ChEBI ID corresponding to an InChI. In the (frequent) case where no ChEBI xref is * present, null is returned.//from ww w . j ava 2 s .c o m * @param inchi input InChI representation of the chemical * @return String: the ChEBI ID corresponding to the InChI representation provided */ public String getChebiIDFromInchi(String inchi) { BasicDBObject whereQuery = new BasicDBObject("InChI", inchi); BasicDBObject existsQuery = new BasicDBObject("$exists", true); whereQuery.put("xref.CHEBI", existsQuery); BasicDBObject c = (BasicDBObject) dbChemicals.findOne(whereQuery, new BasicDBObject()); if (c == null) { return null; } else { BasicDBObject xref = (BasicDBObject) c.get("xref"); BasicDBObject chebi = (BasicDBObject) xref.get("CHEBI"); return (String) chebi.get("dbid"); } }