Example usage for com.mongodb.client MongoDatabase getCollection

List of usage examples for com.mongodb.client MongoDatabase getCollection

Introduction

In this page you can find the example usage for com.mongodb.client MongoDatabase getCollection.

Prototype

MongoCollection<Document> getCollection(String collectionName);

Source Link

Document

Gets a collection.

Usage

From source file:module.script.TransferBrbAnnotations.java

License:Open Source License

@SuppressWarnings({ "unused", "unchecked" })
public TransferBrbAnnotations() {

    // ===== Session PostgreSQL =====
    SessionFactory sessionFactory = HibernateUtil
            .buildSessionFactory("config/epimed_semantic.hibernate.cfg.xml");
    Session session = sessionFactory.openSession();

    // ===== Session Mongo =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");

    MongoCollection<Document> collection = db.getCollection("samples");

    String sql = "select id_sample, main_gse_number, string_agg(id_substance, ', ') as list_substances from epimed_prod.om_sample "
            + "join epimed_prod.cl_biopatho using (id_biopatho) join epimed_prod.cl_patient using (id_patient) join epimed_prod.cl_exposure using (id_patient) "
            + "where exposed=true group by id_sample";

    List<Object> list = session.createSQLQuery(sql).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();

    for (Object item : list) {

        Map<String, Object> map = (HashMap<String, Object>) item;

        String gsmNumber = (String) map.get("id_sample");
        String gseNumber = (String) map.get("main_gse_number");

        System.out.println("-----------------------------");
        System.out.println(gseNumber + " " + gsmNumber);

        Document doc = collection.find(Filters.eq("_id", gsmNumber)).first();

        if (doc != null) {
            Document expGroup = (Document) doc.get("exp_group");
            expGroup.put("exposure", map.get("list_substances"));
            System.out.println(expGroup);

            // Update Mongo document
            doc.put("exp_group", expGroup);
            doc.put("analyzed", true);
            UpdateResult updateResult = collection.updateOne(Filters.eq("_id", gsmNumber),
                    new Document("$set", doc));
        }//from   w w  w  . j a va  2 s . c om

    }

    if (session.isOpen()) {
        session.close();
    }
    sessionFactory.close();

    mongoClient.close();
}

From source file:module.script.TransferBrbHistologyCodes.java

License:Open Source License

@SuppressWarnings({ "unused", "unchecked" })
public TransferBrbHistologyCodes() {

    // ===== Session PostgreSQL =====
    SessionFactory sessionFactory = HibernateUtil
            .buildSessionFactory("config/epimed_semantic.hibernate.cfg.xml");
    Session session = sessionFactory.openSession();

    // ===== Session Mongo =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");

    MongoCollection<Document> collection = db.getCollection("samples");

    String sql = "select id_sample, main_gse_number, index_histology_code, histology_code, ordered_histology_code from db_brb_lung.view_exp_group";

    List<Object> list = session.createSQLQuery(sql).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();

    for (Object item : list) {

        Map<String, Object> map = (HashMap<String, Object>) item;

        String gsmNumber = (String) map.get("id_sample");
        String gseNumber = (String) map.get("main_gse_number");

        System.out.println("-----------------------------");
        System.out.println(gseNumber + " " + gsmNumber);

        Document doc = collection.find(Filters.eq("_id", gsmNumber)).first();

        if (doc != null) {
            Document expGroup = (Document) doc.get("exp_group");
            expGroup.put("index_histology_code", map.get("index_histology_code"));
            expGroup.put("histology_code", map.get("histology_code"));
            expGroup.put("ordered_histology_code", map.get("ordered_histology_code"));
            System.out.println(expGroup);

            // Update Mongo document
            doc.put("exp_group", expGroup);
            doc.put("analyzed", true);
            UpdateResult updateResult = collection.updateOne(Filters.eq("_id", gsmNumber),
                    new Document("$set", doc));
        }/*from w w w .  j a  v  a  2 s .c  o m*/

    }

    if (session.isOpen()) {
        session.close();
    }
    sessionFactory.close();

    mongoClient.close();
}

From source file:module.script.TransferExposure.java

License:Open Source License

@SuppressWarnings({ "unused", "unchecked" })
public TransferExposure() {

    // ===== Session PostgreSQL =====
    SessionFactory sessionFactory = HibernateUtil
            .buildSessionFactory("config/epimed_semantic.hibernate.cfg.xml");
    Session session = sessionFactory.openSession();

    // ===== Session Mongo =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");

    MongoCollection<Document> collection = db.getCollection("samples");

    String sql = "select id_sample, main_gse_number, string_agg(id_substance, ', ') as list_substances from epimed_prod.om_sample "
            + "join epimed_prod.cl_biopatho using (id_biopatho) join epimed_prod.cl_patient using (id_patient) join epimed_prod.cl_exposure using (id_patient) "
            + "where exposed=true group by id_sample";

    List<Object> list = session.createSQLQuery(sql).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();

    for (Object item : list) {

        Map<String, Object> map = (HashMap<String, Object>) item;

        String gsmNumber = (String) map.get("id_sample");
        String gseNumber = (String) map.get("main_gse_number");

        System.out.println("-----------------------------");
        System.out.println(gseNumber + " " + gsmNumber);

        Document doc = collection.find(Filters.eq("_id", gsmNumber)).first();

        if (doc != null) {
            Document expGroup = (Document) doc.get("exp_group");
            expGroup.put("exposure", map.get("list_substances"));
            System.out.println(expGroup);

            // Update Mongo document
            doc.put("exp_group", expGroup);
            doc.put("analyzed", true);
            UpdateResult updateResult = collection.updateOne(Filters.eq("_id", gsmNumber),
                    new Document("$set", doc));
        }/* w ww.  ja  v a2  s. c o  m*/

    }

    if (session.isOpen()) {
        session.close();
    }
    sessionFactory.close();

    mongoClient.close();
}

From source file:module.script.TransferFromEpimedProd.java

License:Open Source License

@SuppressWarnings({ "unused", "unchecked" })
public TransferFromEpimedProd() {

    // ===== Session PostgreSQL =====
    SessionFactory sessionFactory = HibernateUtil
            .buildSessionFactory("config/epimed_semantic.hibernate.cfg.xml");
    Session session = sessionFactory.openSession();

    // ===== Session Mongo =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");

    MongoCollection<Document> collection = db.getCollection("samples");

    String sql = "select * from epimed_prod.view_exp_group order by main_gse_number, id_sample";
    List<Object> list = session.createSQLQuery(sql).setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP).list();

    String[] commonAttributes = { "pathology", "tnm_stage", "id_topology_group", "histology_subtype",
            "tissue_stage", "dead", "dfs_months", "age_max", "morphology", "id_tissue_stage", "topology", "sex",
            "age_min", "m", "topology_group", "n", "t", "collection_method", "id_morphology", "relapsed",
            "histology_type", "os_months", "id_topology" };

    List<ClPathology> listPathology = session.createCriteria(ClPathology.class).list();
    Map<String, ClPathology> mapPathology = new HashMap<String, ClPathology>();
    for (ClPathology p : listPathology) {
        mapPathology.put(p.getName(), p);
    }/* www. jav  a 2s  .  c  o m*/

    List<ClTissueStatus> listTissueStatus = session.createCriteria(ClTissueStatus.class).list();
    Map<Integer, ClTissueStatus> mapTissueStatus = new HashMap<Integer, ClTissueStatus>();
    for (ClTissueStatus t : listTissueStatus) {
        mapTissueStatus.put(t.getIdTissueStatus(), t);
    }

    System.out.println(listTissueStatus);

    for (Object item : list) {

        Map<String, Object> map = (HashMap<String, Object>) item;

        String gsmNumber = (String) map.get("id_sample");
        String gseNumber = (String) map.get("main_gse_number");

        System.out.println("-----------------------------");
        System.out.println(gseNumber + " " + gsmNumber);

        Document doc = collection.find(Filters.eq("_id", gsmNumber)).first();

        if (doc != null) {
            Document expGroup = (Document) doc.get("exp_group");
            for (int j = 0; j < commonAttributes.length; j++) {
                String attr = commonAttributes[j];
                expGroup.put(attr, map.get(attr));
            }

            // Treatment
            expGroup.put("treatment", map.get("treatment_type"));

            // Pathology
            String pathoString = (String) map.get("pathology");
            ClPathology pathology = mapPathology.get(pathoString);
            if (pathology != null) {
                expGroup.put("id_pathology", pathology.getIdPathology());
                expGroup.put("pathology", pathology.getName());
            }

            // Tissue status
            Integer idTissueStatus = (Integer) map.get("id_tissue_status");
            if (idTissueStatus != null && idTissueStatus > 3) {
                idTissueStatus = 3;
            }
            ClTissueStatus tissueStatus = mapTissueStatus.get(idTissueStatus);
            if (tissueStatus != null) {
                expGroup.put("id_tissue_status", tissueStatus.getIdTissueStatus());
                expGroup.put("tissue_status", tissueStatus.getName());
            }

            System.out.println("idTissueStatus=" + tissueStatus + ", pathology=" + pathology);
            System.out.println(expGroup);

            // Update Mongo document
            doc.put("exp_group", expGroup);
            doc.put("analyzed", true);
            UpdateResult updateResult = collection.updateOne(Filters.eq("_id", gsmNumber),
                    new Document("$set", doc));
        }

    }

    if (session.isOpen()) {
        session.close();
    }
    sessionFactory.close();

    mongoClient.close();
}

From source file:module.test.CreateProbesets.java

License:Open Source License

public CreateProbesets() {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");
    MongoCollection<Document> collectionProbesets = db.getCollection("probesets");

    Document docProbeset = mongoService.createProbeset("GPL570", "1007_s_at");
    collectionProbesets.insertOne(docProbeset);
    mongoClient.close();//ww  w . ja  va 2s.  com

}

From source file:module.test.CustomExport.java

License:Open Source License

public CustomExport() {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");

    MongoCollection<Document> collection = db.getCollection("samples");

    // ===== Find exp_group in the database =====

    // === Query 1 ===
    /*//w w  w .  j  a  va  2 s  .c  o m
    String queryName = "breast_cancer_GPL570";
    List<Bson> filters = new ArrayList<Bson>();
    filters.add(Filters.eq("exp_group.id_platform", "GPL570"));
    filters.add(Filters.eq("exp_group.id_topology_group", "C50"));
    filters.add(Filters.eq("exp_group.id_tissue_status", 3)); // tumoral
     */

    // === Query 2 ===
    /*
    String queryName = "breast_normal_GPL570";
    List<Bson> filters = new ArrayList<Bson>();
    filters.add(Filters.eq("exp_group.id_platform", "GPL570"));
    filters.add(Filters.eq("exp_group.id_topology_group", "C50"));
    filters.add(Filters.eq("exp_group.id_tissue_status", 1)); // normal
    */

    // === Query 3 ===
    String queryName = "breast_cancer_with_survival_GPL570";
    List<Bson> filters = new ArrayList<Bson>();
    filters.add(Filters.eq("exp_group.id_platform", "GPL570"));
    filters.add(Filters.eq("exp_group.id_topology_group", "C50"));
    filters.add(Filters.eq("exp_group.id_tissue_status", 3)); // tumoral
    filters.add(Filters.or(Filters.ne("exp_group.os_months", null), Filters.ne("exp_group.dfss_months", null),
            Filters.ne("exp_group.relapsed", null), Filters.ne("exp_group.dead", null)));

    Bson filter = Filters.and(filters);
    Long nbSamples = collection.count(filter);
    List<String> listSeries = collection.distinct("exp_group.main_gse_number", filter, String.class)
            .into(new ArrayList<String>());
    queryName = queryName + "_" + nbSamples + "_samples_" + listSeries.size() + "_series";

    List<Document> docExpGroup = collection.find(filter)
            .projection(Projections.fields(Projections.include("exp_group"), Projections.excludeId()))
            .into(new ArrayList<Document>());

    List<Document> docParam = collection.find(filter)
            .projection(Projections.fields(Projections.include("parameters"), Projections.excludeId()))
            .into(new ArrayList<Document>());

    mongoClient.close();

    // ===== Load Exp Group into a matrix =====

    List<String> headerExpGroup = new ArrayList<String>();
    List<Object> dataExpGroup = new ArrayList<Object>();

    for (int i = 0; i < docExpGroup.size(); i++) {
        Map<String, String> expGroup = (Map<String, String>) docExpGroup.get(i).get("exp_group");
        if (i == 0) {
            headerExpGroup.addAll(expGroup.keySet());
        }

        Object[] dataLine = new Object[headerExpGroup.size()];
        for (int j = 0; j < headerExpGroup.size(); j++) {
            dataLine[j] = expGroup.get(headerExpGroup.get(j));
        }
        dataExpGroup.add(dataLine);
    }

    // ===== Load Params into a matrix =====

    Set<String> headerParamSet = new HashSet<String>();
    List<String> headerParam = new ArrayList<String>();
    List<Object> dataParam = new ArrayList<Object>();

    for (int i = 0; i < docParam.size(); i++) {
        Map<String, String> param = (Map<String, String>) docParam.get(i).get("parameters");
        headerParamSet.addAll(param.keySet());
    }
    headerParam.addAll(headerParamSet);
    Collections.sort(headerParam);

    for (int i = 0; i < docParam.size(); i++) {
        Map<String, String> param = (Map<String, String>) docParam.get(i).get("parameters");
        Object[] dataLine = new Object[headerParam.size()];
        for (int j = 0; j < headerParam.size(); j++) {
            dataLine[j] = param.get(headerParam.get(j));
        }
        // System.out.println(Arrays.toString(dataLine));
        dataParam.add(dataLine);

    }

    // === Output ===

    String fileName = this.getOutputDirectory() + this.getDirSeparator() + "EpiMed_database_" + queryName + "_"
            + dateFormat.format(new Date()) + ".xlsx";
    System.out.println(fileName);
    XSSFWorkbook workbook = fileService.createWorkbook();
    fileService.addSheet(workbook, "exp_group_" + dateFormat.format(new Date()), headerExpGroup, dataExpGroup);
    fileService.addSheet(workbook, "parameters_" + dateFormat.format(new Date()), headerParam, dataParam);
    fileService.writeWorkbook(workbook, fileName);

}

From source file:module.test.ImportProbesets.java

License:Open Source License

public ImportProbesets() {

    // === Display ===
    System.out.println("\n================ BEGIN Module " + this.getClass().getName() + "================");

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");
    MongoCollection<Document> collectionProbesets = db.getCollection("probesets");

    // ===== Session PostgreSQL =====
    SessionFactory sessionFactory = HibernateUtil
            .buildSessionFactory("config/epimed_semantic.hibernate.cfg.xml");
    Session session = sessionFactory.openSession();

    String idPlatform = "GPL570";
    String gpl = idPlatform.toLowerCase();

    // ===== Load file =====

    String inputfile = this.getInputDirectory() + this.getDirSeparator() + "HG-U133_Plus_2.na36.annot.csv";

    System.out.println("ID Platform " + gpl);
    System.out.println("LOADING \t " + inputfile);
    System.out.println("Please wait... ");
    List<String> listRows = fileService.loadTextFile(inputfile);
    System.out.println("File sucessfully LOADED");

    // ===== Recognize header =====

    List<String> header = fileService.readHeader(listRows, "\",\"");
    System.out.println("Header " + header);
    List<List<String>> data = fileService.readData(listRows, "\",\"");
    System.out.println(/*from  ww w  .  j a  va  2  s .  c  om*/
            "The data are sucessfully loaded: rows " + data.size() + ", columns " + data.get(0).size());

    Integer indProbeset = fileService.findIndex(header, "Probe Set ID");
    Integer indGenes = fileService.findIndex(header, "Entrez Gene");
    Integer indUnigenes = fileService.findIndex(header, "UniGene ID");
    Integer indTranscripts = fileService.findIndex(header, "RefSeq Transcript ID");
    Integer indGb = fileService.findIndex(header, "Representative Public ID");

    for (int i = 0; i < 5; i++) {
        List<String> dataline = data.get(i);

        String probeset = dataline.get(indProbeset);
        String genes = dataline.get(indGenes);
        String unigenes = dataline.get(indUnigenes);
        String transcripts = dataline.get(indTranscripts);
        String gb = dataline.get(indGb);

        System.out.println(probeset + "\t" + genes + "\t" + formatService.splitInArray(unigenes, "///") + "\t"
                + gb + "\t" + transcripts);

        Document docProbeset = mongoService.createProbeset(idPlatform, probeset);
        docProbeset.put("genes", formatService.splitInArray(genes, "///"));
        docProbeset.put("unigenes", formatService.splitInArray(unigenes, "///"));

        List<String> listTranscripts = formatService.splitInArray(transcripts, "///");
        listTranscripts.addAll(formatService.splitInArray(gb, "///"));

        docProbeset.put("transcripts", listTranscripts);

        collectionProbesets.insertOne(docProbeset);

        /*
        for (int j=0; j<dataline.size(); j++) {
           String key = header.get(j);
           String value = dataline.get(j);
           System.out.println(key + ": " + value);
        }
        */
    }

    /*
    String tableProbe = "hs.om_probe_" + gpl;
    String tableGP = "hs.om_gp_" + gpl;
    List<Object []> listProbesets = session.createNativeQuery("select * from " + tableProbe + " order by id_probe").getResultList();
    for (int i=0; i<10; i++) {
            
       Object[] line = listProbesets.get(i);
            
       System.out.println(Arrays.toString(line));
       // Document docProbeset = mongoService.createProbeset(idPlatform, "1007_s_at");
       // collectionProbesets.insertOne(docProbeset);
    }
     */

    // === Close connections ===

    if (session.isOpen()) {
        session.close();
    }
    sessionFactory.close();
    mongoClient.close();

    // === Display ===
    System.out.println("================ END Module " + this.getClass().getName() + "================");

}

From source file:module.test.SearchPlatforms.java

License:Open Source License

public SearchPlatforms() {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");
    MongoCollection<Document> collectionSamples = db.getCollection("sample");
    MongoCollection<Document> collectionPlatforms = db.getCollection("platform");

    List<Document> list = collectionSamples
            .aggregate(Arrays.asList(Aggregates.group("$exp_group.id_platform", Accumulators.sum("total", 1)),
                    Aggregates.sort(Sorts.orderBy(Sorts.descending("total")))))
            .into(new ArrayList<Document>());

    for (int i = 0; i < list.size(); i++) {
        System.out.println((i + 1) + " " + list.get(i));
    }//w w  w .j a  v a2  s  .c o m

    // collectionPlatforms.find(Filters.regex("title", ".*ethyl.*")).forEach(printBlock);
    collectionPlatforms.find(Filters.eq("id_organism", "9606")).forEach(printBlock);

    mongoClient.close();

}

From source file:module.test.SearchSamples.java

License:Open Source License

public SearchSamples() {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");
    MongoCollection<Document> collectionSamples = db.getCollection("samples");

    List<Document> list = collectionSamples.find(Filters.and(Filters.eq("exp_group.id_tissue_stage", 4),
            Filters.ne("exp_group.main_gse_number", "GSE30654"))).into(new ArrayList<Document>());

    for (Document document : list) {
        System.out.println(document);
    }/*from w w w  .  j a v  a  2s  . c o  m*/

    mongoClient.close();

}

From source file:module.test.SearchSeries.java

License:Open Source License

public SearchSeries() throws ParseException {

    // ===== Connection =====

    MongoClient mongoClient = MongoUtil.buildMongoClient();
    MongoDatabase db = mongoClient.getDatabase("epimed_experiments");
    MongoCollection<Document> collectionSamples = db.getCollection("series");

    String[] listSeries = { "E-MTAB-365", "GSE3156", "GSE3744", "GSE4922", "GSE5764", "GSE6367", "GSE6883",
            "GSE8977", "GSE9014", "GSE9195", "GSE10885", "GSE11078", "GSE11352", "GSE12080", "GSE12237",
            "GSE12276", "GSE14017", "GSE16391", "GSE17215", "GSE17907", "GSE19615", "GSE21653", "GSE28681",
            "GSE38867" };

    List<Document> list = collectionSamples.find(Filters.in("_id", listSeries)).into(new ArrayList<Document>());

    Set<String> setGse = new HashSet<String>();
    List<String> listGse = new ArrayList<String>();
    for (int i = 0; i < list.size(); i++) {
        Document doc = list.get(i);
        setGse.add(doc.getString("_id"));
    }/*from  w ww.  j a v  a2s . c  om*/
    listGse.addAll(setGse);
    Collections.sort(listGse);

    System.out.println("Found " + list.size() + " samples");
    System.out.println("List GSE " + listGse);

    mongoClient.close();

}