List of usage examples for com.mongodb DBObject get
Object get(String key);
From source file:act.server.MongoDB.java
License:Open Source License
public Long getExistingDBIdForInChI(String inchi) { // TODO: should this return some UUID type instead of Long? if (this.dbChemicals == null) return null; // TODO: should this throw an exception instead? BasicDBObject query = new BasicDBObject("InChI", inchi); BasicDBObject fields = new BasicDBObject("_id", true); DBObject o = this.dbChemicals.findOne(query, fields); if (o == null) { return null; }/* ww w . jav a 2s . c om*/ // TODO: does this need to be checked? return (Long) o.get("_id"); }
From source file:act.server.MongoDB.java
License:Open Source License
private long alreadyEntered(Cofactor cof) { BasicDBObject query;/*www .j a v a2s. c o m*/ String inchi = cof.getInChI(); long retId = -1; if (inchi != null) { query = new BasicDBObject(); query.put("InChI", inchi); DBObject o = this.dbCofactors.findOne(query); if (o != null) retId = (Long) o.get("_id"); // checked: db type IS long } return retId; }
From source file:act.server.MongoDB.java
License:Open Source License
public List<Long> getRxnsWith(Long reactant, Long product) { BasicDBObject query = new BasicDBObject(); query.put("enz_summary.products.pubchem", product); query.put("enz_summary.substrates.pubchem", reactant); DBCursor cur = this.dbReactions.find(query); List<Long> reactions = new ArrayList<Long>(); while (cur.hasNext()) { DBObject o = cur.next(); long id = (Integer) o.get("_id"); // checked: db type IS int reactions.add(id);/*from w w w .j a va2s. co m*/ } cur.close(); return reactions; }
From source file:act.server.MongoDB.java
License:Open Source License
public List<Long> getRxnsWithEnzyme(String enzyme, Long org, List<Long> substrates) { BasicDBObject query = new BasicDBObject(); query.put("ecnum", enzyme); query.put("organisms.id", org); for (Long substrate : substrates) { BasicDBObject mainQuery = new BasicDBObject(); mainQuery.put("$ne", substrate); BasicDBList queryList = new BasicDBList(); BasicDBObject productQuery = new BasicDBObject(); productQuery.put("enz_summary.products.pubchem", mainQuery); BasicDBObject substrateQuery = new BasicDBObject(); substrateQuery.put("enz_summary.substrates.pubchem", mainQuery); queryList.add(substrateQuery);//from ww w .j a v a2 s .c o m queryList.add(productQuery); query.put("$or", queryList); } DBCursor cur = this.dbReactions.find(query); List<Long> reactions = new ArrayList<Long>(); while (cur.hasNext()) { DBObject o = cur.next(); long id = (Integer) o.get("_id"); // checked: db type IS int reactions.add(id); } cur.close(); return reactions; }
From source file:act.server.MongoDB.java
License:Open Source License
public List<Long> getRxnsWithSubstrate(String enzyme, Long org, List<Long> substrates) { BasicDBObject query = new BasicDBObject(); query.put("organisms.id", org); BasicDBObject enzymeQuery = new BasicDBObject(); enzymeQuery.put("ecnum", enzyme); query.put("$ne", enzymeQuery); for (Long substrate : substrates) { BasicDBList queryList = new BasicDBList(); DBObject querySubstrate = new BasicDBObject(); querySubstrate.put("enz_summary.substrates.pubchem", substrate); DBObject queryProduct = new BasicDBObject(); queryProduct.put("enz_summary.products.pubchem", substrate); queryList.add(querySubstrate);//w ww. j a va 2 s .c o m queryList.add(queryProduct); query.put("$or", queryList); } DBCursor cur = this.dbReactions.find(query); List<Long> reactions = new ArrayList<Long>(); while (cur.hasNext()) { DBObject o = cur.next(); long id = (Integer) o.get("_id"); // checked: db type IS int reactions.add(id); } cur.close(); return reactions; }
From source file:act.server.MongoDB.java
License:Open Source License
public long getChemicalIDFromName(String chemName, boolean caseInsensitive) { BasicDBObject query = new BasicDBObject(); DBObject brenda = new BasicDBObject(); DBObject pubchem = new BasicDBObject(); DBObject synonyms = new BasicDBObject(); if (caseInsensitive) { String escapedName = Pattern.quote(chemName); Pattern regex = Pattern.compile("^" + escapedName + "$", Pattern.CASE_INSENSITIVE); brenda.put("names.brenda", regex); pubchem.put("names.pubchem.values", regex); synonyms.put("names.synonyms", regex); } else {// w w w . j a va2 s.c o m brenda.put("names.brenda", chemName); pubchem.put("names.pubchem.values", chemName); synonyms.put("names.synonyms", chemName); } BasicDBList ors = new BasicDBList(); ors.add(brenda); ors.add(pubchem); ors.add(synonyms); query.put("$or", ors); Long id; DBObject o = this.dbChemicals.findOne(query); if (o != null) id = (Long) o.get("_id"); // checked: db type IS Long else id = -1L; return id; }
From source file:act.server.MongoDB.java
License:Open Source License
public long getChemicalIDFromExactBrendaName(String chemName) { BasicDBObject query = new BasicDBObject(); query.put("names.brenda", chemName.toLowerCase()); Long id;/*from w w w .j a va 2 s.c o m*/ DBObject o = this.dbChemicals.findOne(query); if (o != null) id = (Long) o.get("_id"); // checked: db type IS Long else id = -1L; return id; }
From source file:act.server.MongoDB.java
License:Open Source License
public String getChemicalDBJSON(Long uuid) { BasicDBObject query = new BasicDBObject(); query.put("_id", uuid); DBObject o = this.dbChemicals.findOne(query); if (o == null) return null; Set<String> keys = o.keySet(); String json = "{\n"; for (String key : keys) { json += "\t" + key + " : " + o.get(key) + ",\n"; }//from w w w.j a v a2s . c om json += "}"; return json; }
From source file:act.server.MongoDB.java
License:Open Source License
public Map<String, Long> constructAllInChIs() { Map<String, Long> chems = new HashMap<String, Long>(); BasicDBObject keys = new BasicDBObject(); keys.append("_id", true); keys.append("InChI", true); DBCursor cur = constructCursorForMatchingChemicals(null, null, keys); while (cur.hasNext()) { DBObject o = cur.next(); long uuid = (Long) o.get("_id"); // checked: db type IS long String inchi = (String) o.get("InChI"); chems.put(inchi, uuid);//w ww . j a v a 2s .c o m } cur.close(); return chems; }
From source file:act.server.MongoDB.java
License:Open Source License
public Chemical convertDBObjectToChemical(DBObject o) { long uuid;/*from w w w. jav a2 s . c o m*/ // WTF!? Are some chemicals ids int and some long? // this code below should not be needed, unless our db is mucked up try { uuid = (Long) o.get("_id"); // checked: db type IS long } catch (ClassCastException e) { System.err.println("WARNING: MongoDB.convertDBObjectToChemical ClassCast db.chemicals.id is not Long?"); uuid = ((Integer) o.get("_id")).longValue(); // this should be dead code } String chemName = (String) o.get("canonical"); DBObject xrefs = (DBObject) o.get("xref"); Long pcid = null; try { pcid = (Long) (xrefs.get("pubchem")); } catch (Exception e) { } if (pcid == null) { pcid = (long) -1; } String inchi = (String) o.get("InChI"); String inchiKey = (String) o.get("InChIKey"); String smiles = (String) o.get("SMILES"); Chemical c = new Chemical(uuid, pcid, chemName, smiles); c.setInchi(inchi); c.setCanon((String) o.get("canonical")); try { for (String typ : xrefs.keySet()) { if (typ.equals("pubchem")) continue; c.putRef(Chemical.REFS.valueOf(typ), MongoDBToJSON.conv((DBObject) xrefs.get(typ))); } } catch (Exception e) { } if (o.get("derived_data") != null) { BasicDBList matchedRos = (BasicDBList) ((DBObject) o.get("derived_data")).get("matched_ros"); if (matchedRos != null) { for (Object roId : matchedRos) { c.addSubstructureRoId((Integer) roId); } } } BasicDBList names = (BasicDBList) ((DBObject) o.get("names")).get("brenda"); if (names != null) { for (Object n : names) { c.addBrendaNames((String) n); } } if (names != null) { names = (BasicDBList) ((DBObject) o.get("names")).get("synonyms"); for (Object n : names) { c.addSynonym((String) n); } } if (names != null) { names = (BasicDBList) ((DBObject) o.get("names")).get("pubchem"); for (Object n : names) { String typ = (String) ((DBObject) n).get("type"); BasicDBList pnames = (BasicDBList) ((DBObject) n).get("values"); List<String> s = new ArrayList<String>(); for (Object os : pnames) s.add((String) os); c.addNames(typ, s.toArray(new String[0])); } } if ((Boolean) o.get("isCofactor")) c.setAsCofactor(); if ((Boolean) o.get("isNative")) c.setAsNative(); if ((Double) o.get("estimateEnergy") != null) c.setEstimatedEnergy((Double) o.get("estimateEnergy")); BasicDBList keywords = (BasicDBList) o.get("keywords"); if (keywords != null) for (Object k : keywords) c.addKeyword((String) k); BasicDBList cikeywords = (BasicDBList) o.get("keywords_case_insensitive"); if (cikeywords != null) for (Object k : cikeywords) c.addCaseInsensitiveKeyword((String) k); BasicDBList vendors = (BasicDBList) o.get("vendors"); Integer num_vendors = (Integer) o.get("num_vendors"); Integer chemspiderid = (Integer) o.get("csid"); c.setChemSpiderVendorXrefs(vendors == null ? null : MongoDBToJSON.conv(vendors)); c.setChemSpiderNumUniqueVendors(num_vendors); c.setChemSpiderID(chemspiderid); /** * Shortest name is most useful so just use that. */ //TODO: what are we doing with shortest name here? String shortestName = c.getCanon(); for (String name : c.getBrendaNames()) { if (shortestName == null || name.length() < shortestName.length()) shortestName = name; } for (String name : c.getSynonyms()) { if (shortestName == null || name.length() < shortestName.length()) shortestName = name; } return c; }