List of usage examples for com.mongodb.client MongoCollection find
FindIterable<TDocument> find(ClientSession clientSession);
From source file:module.script.ImportSupplementaryGSE20711.java
License:Open Source License
public ImportSupplementaryGSE20711() { // ===== 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"); // ===== Excel data loader ===== String inputfile = this.getInputDirectory() + this.getDirSeparator() + "GSE20711_emmm0003-0726-SD2.xlsx"; System.out.println("LOADING \t " + inputfile); ExcelService excelService = new ExcelService(); excelService.load(inputfile);/*from w w w. j av a2 s . c o m*/ String gseNumber = "GSE20711"; for (int i = 0; i < excelService.getData().size(); i++) { List<Object> dataLine = excelService.getData().get(i); String bcString = (String) dataLine.get(0); bcString = bcString.replaceAll("BC", ""); Integer bcNumber = Integer.parseInt(bcString); Document docSample = collection .find(Filters .and(Filters.in("series", gseNumber), Filters.eq("exp_group.sample_title", "Breast tumor from patient P_" + bcNumber + " (expression data)"))) .first(); System.out.println("-------------------------------------------"); System.out.println(dataLine); System.out.println(docSample); } if (session.isOpen()) { session.close(); } sessionFactory.close(); mongoClient.close(); }
From source file:module.script.ImportSupplementaryGSE25219.java
License:Open Source License
@SuppressWarnings({ "unused", "unchecked" }) public ImportSupplementaryGSE25219() { // ===== 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"); // ===== Excel data loader ===== String inputfile = this.getInputDirectory() + this.getDirSeparator() + "NIHMS321722-supplement-7.xlsx"; System.out.println("LOADING \t " + inputfile); ExcelService excelService = new ExcelService(); excelService.load(inputfile);/*from w w w . j a v a2 s .c om*/ // ===== Format raw data into data structures ====== List<Map<String, String>> listMap = new ArrayList<Map<String, String>>(); List<String> headerMap = new ArrayList<String>(); Map<String, String> mapBrain = new HashMap<String, String>(); for (int i = 0; i < excelService.getData().size(); i++) { List<Object> dataLine = excelService.getData().get(i); String brainCode = (String) dataLine.get(0); if (brainCode != null) { mapBrain = new HashMap<String, String>(); } // Existing brain code if (dataLine != null && dataLine.size() > 2 && dataLine.get(1) != null && dataLine.get(2) != null) { // System.out.println(dataLine.get(1) + " = " + dataLine.get(2)); mapBrain.put(dataLine.get(1).toString().trim(), dataLine.get(2).toString().trim()); } if (brainCode != null) { // New Brain code // System.out.println("brain code " + brainCode); headerMap.add(brainCode); listMap.add(mapBrain); } } // ===== Recognize data ===== for (int i = 0; i < headerMap.size(); i++) { System.out.println("----------------------------"); String code = headerMap.get(i); System.out.println(i + " " + code); Map<String, String> map = listMap.get(i); Map<String, String> updatedMap = new HashMap<String, String>(); for (Map.Entry<String, String> entry : map.entrySet()) { String key = entry.getKey(); String value = entry.getValue(); if (!key.toLowerCase().equals("age") // && !key.toLowerCase().equals("ethnicity") // && !key.toLowerCase().equals("sex") && !value.toLowerCase().equals("no data")) { updatedMap.put(key, value); } // System.out.println(key + " -> " + value); } List<Document> listDocuments = collection .find(Filters.and(Filters.eq("exp_group.main_gse_number", "GSE25219"), Filters.eq("parameters.brain code", code))) .into(new ArrayList<Document>()); System.out.println("Number of corresponding Mongo documents " + listDocuments.size()); System.out.println(updatedMap); for (int j = 0; j < listDocuments.size(); j++) { Document doc = listDocuments.get(j); Document parameters = (Document) doc.get("parameters"); parameters.putAll(updatedMap); System.out.println("\t" + parameters); // Update Mongo document doc.put("parameters", parameters); doc.put("analyzed", true); UpdateResult updateResult = collection.updateOne(Filters.eq("_id", doc.get("_id")), new Document("$set", doc)); } } if (session.isOpen()) { session.close(); } sessionFactory.close(); mongoClient.close(); }
From source file:module.script.pro12.TransferPro12.java
License:Open Source License
@SuppressWarnings({ "unchecked" }) public TransferPro12() { // ===== 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 from epimed_prod.om_sample join epimed_prod.om_sample_series using (id_sample) " + "join epimed_prod.om_series using (id_series) where id_series='PRO12'"; List<String> list = session.createSQLQuery(sql).list(); Document pro12 = new Document(); pro12.append("series", "PRO12"); for (String gsmNumber : list) { Document doc = collection.find(Filters.eq("_id", gsmNumber)).first(); System.out.println("-----------------------------"); System.out.println(gsmNumber + " " + doc); if (doc != null) { // Update Mongo document collection.updateOne(Filters.eq("_id", gsmNumber), new Document("$push", pro12)); }//w w w .j a v a 2 s . co m } if (session.isOpen()) { session.close(); } sessionFactory.close(); mongoClient.close(); }
From source file:module.script.proallchen.ImportProallChenOriginal.java
License:Open Source License
public ImportProallChenOriginal() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); // ===== Samples ====== MongoCollection<Document> collectionSamples = db.getCollection("samples"); // ===== Excel data loader ===== String inputfile = this.getInputDirectory() + this.getDirSeparator() + "PROALL_CHEN_clinical.xlsx"; System.out.println("LOADING \t " + inputfile); ExcelService excelService = new ExcelService(); excelService.load(inputfile);/*from w w w . ja v a 2 s .co m*/ System.out.println(excelService.getHeader()); String idSeries = "PROALL_CHEN"; List<String> listSeries = new ArrayList<String>(); listSeries.add(idSeries); for (int i = 0; i < excelService.getData().size(); i++) { List<Object> line = excelService.getData().get(i); String idSample = "ESM" + line.get(0); System.out.println(idSample + " " + line); Document docSample = collectionSamples.find(Filters.eq("_id", idSample.trim())).first(); System.out.println(docSample); Document parameters = (Document) docSample.get("parameters"); for (int j = 0; j < excelService.getHeader().size(); j++) { String header = (String) excelService.getHeader().get(j); Object value = line.get(j); // System.out.println(header + " = " + value); parameters.append(header, value); } System.out.println(parameters); // Update Mongo document docSample.put("parameters", parameters); UpdateResult updateResult = collectionSamples.updateOne(Filters.eq("_id", docSample.get("_id")), new Document("$set", docSample)); } mongoClient.close(); }
From source file:module.script.probcp.UpdateSamplesProbcp.java
License:Open Source License
public UpdateSamplesProbcp() { // ===== Connection ===== MongoClient mongoClient = MongoUtil.buildMongoClient(); MongoDatabase db = mongoClient.getDatabase("epimed_experiments"); MongoCollection<Document> collectionSamples = db.getCollection("samples"); // ===== Session PostgreSQL ===== SessionFactory sessionFactory = HibernateUtil .buildSessionFactory("config/epimed_semantic.hibernate.cfg.xml"); Session session = sessionFactory.openSession(); String[] studies = { "tya16" }; List<String> series = new ArrayList<String>(); for (int l = 0; l < studies.length; l++) { String idStudy = studies[l]; String studyName = idStudy.toUpperCase(); series.clear();/*from w w w . j av a2 s. c om*/ series.add(studyName); series.add("PROBCP"); String sql = "select * from st_bcp." + idStudy + "_sample order by id_sample"; List<Object> listSamples = session.createSQLQuery(sql).list(); for (int i = 0; i < listSamples.size(); i++) { Object[] lineSample = (Object[]) listSamples.get(i); String idSample = (String) lineSample[0]; String clinicalClassification = (String) lineSample[1]; String tnmStage = (String) lineSample[2]; Integer grade = (Integer) lineSample[3]; String type = (String) lineSample[4]; System.out.println(Arrays.toString(lineSample)); String id = studyName + "_" + idSample; Document docSample = collectionSamples.find(Filters.eq("_id", id)).first(); Document expgroup = (Document) docSample.get("exp_group"); expgroup.append("tnm_grade", grade); expgroup.append("tnm_stage", null); docSample.append("exp_group", expgroup); UpdateResult updateResult = collectionSamples.updateOne(Filters.eq("_id", id), new Document("$set", docSample)); System.out.println(docSample); } } if (session.isOpen()) { session.close(); } sessionFactory.close(); mongoClient.close(); }
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)); }//w w w. jav a2 s .co m } 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 om } 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)); }/*from www.j av a 2 s.co 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); }//from w w w . j ava 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.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 ww . j ava2 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); }