Example usage for com.mongodb DBObject get

List of usage examples for com.mongodb DBObject get

Introduction

In this page you can find the example usage for com.mongodb DBObject get.

Prototype

Object get(String key);

Source Link

Document

Gets a field from this object by a given name.

Usage

From source file:act.server.MongoDB.java

License:Open Source License

public List<P<Reaction.RefDataSource, String>> getReferences(Long reactionID) {
    if (reactionID < 0) {
        reactionID = Reaction.reverseID(reactionID);
    }//from   w  w w . j ava2s.c  o m
    DBObject query = new BasicDBObject();
    query.put("_id", reactionID);
    List<P<Reaction.RefDataSource, String>> refs = new ArrayList<>();
    DBObject reaction = this.dbReactions.findOne(query);
    if (reaction != null) {
        BasicDBList dbrefs = (BasicDBList) reaction.get("references");
        if (dbrefs != null)
            for (Object oo : dbrefs) {
                DBObject ref = (DBObject) oo;
                Reaction.RefDataSource src = Reaction.RefDataSource.valueOf((String) ref.get("src"));
                String val = (String) ref.get("val");
                refs.add(new P<Reaction.RefDataSource, String>(src, val));
            }
    }
    return refs;
}

From source file:act.server.MongoDB.java

License:Open Source License

public Set<String> getKMValues(Long reactionID) {
    DBObject query = new BasicDBObject();
    query.put("_id", reactionID);
    Set<String> kmSet = new HashSet<String>();
    DBObject reaction = this.dbReactions.findOne(query);
    if (reaction != null) {
        BasicDBList kms = (BasicDBList) reaction.get("km_values");
        if (kms != null) {
            for (Object km : kms) {
                kmSet.add((String) km);
            }//from   w w w.j  a va  2s  . c  o m
        }
    }
    return kmSet;
}

From source file:act.server.MongoDB.java

License:Open Source License

public Set<String> getTurnoverNumbers(Long reactionID) {
    DBObject query = new BasicDBObject();
    query.put("_id", reactionID);
    Set<String> turnoverSet = new HashSet<String>();
    DBObject reaction = this.dbReactions.findOne(query);
    if (reaction != null) {
        BasicDBList turnovers = (BasicDBList) reaction.get("turnover_numbers");
        if (turnovers != null) {
            for (Object turnover : turnovers) {
                turnoverSet.add((String) turnover);
            }//from  ww w.  j a v a 2s. c o m
        }
    }
    return turnoverSet;
}

From source file:act.server.MongoDB.java

License:Open Source License

public int submitToActSeqDB(Seq.AccDB src, String ec, String org, Long org_id, String seq,
        List<JSONObject> references, Set<Long> rxns, DBObject meta) {
    BasicDBObject doc = new BasicDBObject();
    int id = new Long(this.dbSeq.count()).intValue();
    doc.put("_id", id);
    doc.put("src", src.name()); // genbank, uniprot, swissprot, trembl, embl
    doc.put("ecnum", ec);
    doc.put("org", org);
    doc.put("org_id", org_id); // this is the NCBI Taxonomy id, should correlate with db.organismnames{org_id} and db.organisms.{id}
    doc.put("seq", seq);

    BasicDBList refs = new BasicDBList();
    for (JSONObject ref : references) {
        refs.add(MongoDBToJSON.conv(ref));
    }/*from   w ww  .jav  a2  s .  c  o  m*/
    doc.put("references", refs);

    doc.put("metadata", meta); // the metadata contains the uniprot acc#, name, uniprot catalytic activity,
    Object accession = meta.get("accession");

    doc.put("rxn_refs", to_dblist(rxns));

    this.dbSeq.insert(doc);

    if (org != null && seq != null)
        System.out.format("Inserted %s = [%s, %s] = %s %s\n", accession, ec,
                org.substring(0, Math.min(10, org.length())), seq.substring(0, Math.min(20, seq.length())),
                refs);

    return id;
}

From source file:act.server.MongoDB.java

License:Open Source License

/**
 * graphByOrganism() returns a list of all reactionIDs containing the given organismID.
 *
 * @param organismID/*from   ww w  . j a v  a  2  s. c  o  m*/
 * @return List<Long> List of reaction IDs for given organismID
 */
public List<Long> graphByOrganism(Long organismID) {

    DBObject query = new BasicDBObject();
    if (organismID == null || organismID > -1)
        query.put("organisms.id", organismID);
    List<Long> graphList = new ArrayList<Long>();

    DBCursor reactionCursor = this.dbReactions.find(query);
    for (DBObject i : reactionCursor) {
        graphList.add(((Integer) i.get("_id")).longValue()); // checked: db type IS int
    }
    return graphList;
}

From source file:act.server.MongoDB.java

License:Open Source License

/**
 * getOrganisms() returns a list of all unique species IDs in database
 * mapped to itself, its parents, and descendants
 *
 * @return//w  w  w .  j a  va 2s .  c om
 */
@SuppressWarnings("unchecked")
public Map<Long, Set<Long>> getOrganisms() {
    List<Long> ids = (List<Long>) this.dbReactions.distinct("organisms.id");
    //map species id to all ids associated with it
    Map<Long, Set<Long>> speciesIDs = new HashMap<Long, Set<Long>>();
    for (Long organismID : ids) {
        //check if organism id on species level
        List<Long> idsToAdd = new ArrayList<Long>();
        Long speciesID;
        DBObject orgQuery = new BasicDBObject();
        orgQuery.put("_id", organismID);
        DBObject org = dbOrganisms.findOne(orgQuery);
        String rank = (String) org.get("rank");
        Long parent = (Long) org.get("parent_id"); // checked: db type IS long
        speciesID = null;
        while (organismID != 1) {
            idsToAdd.add(organismID);
            if (rank.equals("species")) {
                speciesID = organismID;
                //break;
            }
            orgQuery.put("_id", parent);
            org = dbOrganisms.findOne(orgQuery);
            organismID = parent;
            rank = (String) org.get("rank");
            parent = (Long) org.get("parent_id"); // checked: db type IS long
        }
        if (speciesID == null)
            continue;
        if (!speciesIDs.containsKey(speciesID)) {
            speciesIDs.put(speciesID, new HashSet<Long>());
        }
        speciesIDs.get(speciesID).addAll(idsToAdd);
    }
    return speciesIDs;
}

From source file:act.server.MongoDB.java

License:Open Source License

public Map<String, Long> getKeggID_ActID(boolean useCached) {
    if (keggID_ActID == null || !useCached)
        keggID_ActID = new HashMap<String, Long>();
    else//from w w w  . j a  v  a2 s  .c o m
        return keggID_ActID;
    DBIterator it = getIteratorOverChemicals();
    while (it.hasNext()) {
        Chemical c = getNextChemical(it);
        DBObject o = (DBObject) c.getRef(Chemical.REFS.KEGG);
        if (o == null)
            continue;
        BasicDBList list = (BasicDBList) o.get("id");
        for (Object s : list) {
            keggID_ActID.put((String) s, c.getUuid());
        }
    }
    return keggID_ActID;
}

From source file:act.shared.helpers.MongoDBToJSON.java

License:Open Source License

public static JSONObject conv(DBObject o) {
    JSONObject result = new JSONObject();
    try {/* w  w w  .  j a  v  a  2s  .c  o  m*/
        for (String k : o.keySet()) {
            Object v = o.get(k);
            if (v instanceof BasicDBList) {
                result.put(k, conv((BasicDBList) v));
            } else if (v instanceof DBObject) {
                result.put(k, conv((DBObject) v));
            } else {
                result.put(k, v);
            }
        }
        return result;
    } catch (JSONException je) {
        return null;
    }
}

From source file:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private void process(XMLStreamReader xml) throws XMLStreamException {
    String tag;/*from   w  ww.j a  v  a2  s.c  o  m*/
    String root = null;
    Stack<DBObject> json = new Stack<DBObject>();
    DBObject js;
    while (xml.hasNext()) {
        int eventType = xml.next();
        while (xml.isWhiteSpace() || eventType == XMLEvent.SPACE)
            eventType = xml.next();

        switch (eventType) {
        case XMLEvent.START_ELEMENT:
            tag = xml.getLocalName();
            if (root == null) {
                root = tag;
            } else {
                json.push(new BasicDBObject());
            }
            break;
        case XMLEvent.END_ELEMENT:
            tag = xml.getLocalName();
            if (tag.equals(root)) {
                // will terminate in next iteration
            } else {
                js = json.pop();
                if (json.size() == 0) {
                    if (tag.equals(rowTag))
                        printEntry(js);
                    else
                        printUnwantedEntry(js);
                } else {
                    putListStrOrJSON(json.peek(), tag, js);
                }
            }
            break;

        case XMLEvent.CHARACTERS:
            String txt = xml.getText();
            js = json.peek();
            if (js.containsField(strTag)) {
                txt = js.get(strTag) + txt;
                js.removeField(strTag);
            }
            js.put(strTag, txt);
            break;

        case XMLEvent.START_DOCUMENT:
            break;
        case XMLEvent.END_DOCUMENT:
            break;
        case XMLEvent.COMMENT:
        case XMLEvent.ENTITY_REFERENCE:
        case XMLEvent.ATTRIBUTE:
        case XMLEvent.PROCESSING_INSTRUCTION:
        case XMLEvent.DTD:
        case XMLEvent.CDATA:
        case XMLEvent.SPACE:
            System.out.format("%s --\n", eventType);
            break;
        }
    }
}

From source file:act.shared.helpers.XMLToImportantChemicals.java

License:Open Source License

private void putListStrOrJSON(DBObject json, String tag, DBObject toAdd) {
    // if it is a string add it unencapsulated
    if (toAdd.keySet().size() == 1 && toAdd.containsField(strTag))
        putElemOrList(json, tag, toAdd.get(strTag));
    else/*from   w w w.  j  av  a  2s.  c  om*/
        putElemOrList(json, tag, toAdd);
}