Example usage for com.mongodb.client.model Filters eq

List of usage examples for com.mongodb.client.model Filters eq

Introduction

In this page you can find the example usage for com.mongodb.client.model Filters eq.

Prototype

public static <TItem> Bson eq(final String fieldName, @Nullable final TItem value) 

Source Link

Document

Creates a filter that matches all documents where the value of the field name equals the specified value.

Usage

From source file:module.ImportArrayExpressInit.java

License:Open Source License

public ImportArrayExpressInit() {

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

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

    // ===== Pattern =====
    String patternText = "\\[[\\p{Print}\\p{Space}]+\\]";
    ;//from  w ww  .j  av  a 2s.  co m
    Pattern pattern = Pattern.compile(patternText);

    // ===== Series =====

    for (String accession : listAccessions) {

        String urlString = "https://www.ebi.ac.uk/arrayexpress/files/" + accession + "/" + accession
                + ".idf.txt";
        System.out.println(urlString);
        String text = webService.loadUrl(urlString);

        String[] parts = text.split(lineSeparator);
        List<String> dataSeries = new ArrayList<String>(Arrays.asList(parts));

        AESeries series = new AESeries(dataSeries);
        System.out.println(series);

        // ===== Check if already imported as a GSE ===== 
        boolean isGseFound = false;
        String gseNumber = null;
        for (String secondaryAccession : series.getListAccessions()) {
            if (secondaryAccession.startsWith("GSE")) {
                gseNumber = secondaryAccession;
                Document gse = db.getCollection("series").find(Filters.eq("_id", secondaryAccession)).first();
                isGseFound = gse != null;
                // System.out.println("GEO accession " +  gseNumber + " found: " + isGseFound);
            }
        }

        if (!isGseFound) {

            // ===== Create Mongo series =====

            List<String> listSeriesAcc = new ArrayList<String>();
            listSeriesAcc.add(accession);

            Document docSeries = mongoService.createSeries(accession, series.getTitle(), null,
                    series.getSubmissionDate(), series.getSubmissionDate());

            if (series.getListAccessions() != null && !series.getListAccessions().isEmpty()) {
                listSeriesAcc.addAll(series.getListAccessions());
            }

            docSeries.put("accessions", listSeriesAcc);

            UpdateResult updateResult = collectionSeries.updateOne(Filters.eq("_id", accession),
                    new Document("$set", docSeries));
            if (updateResult.getMatchedCount() == 0) {
                collectionSeries.insertOne(docSeries);
            }

            System.out.println(docSeries);

            // ===== Import clinical data =====

            String url = "https://www.ebi.ac.uk/arrayexpress/files/" + accession + "/" + series.getSdrf();
            System.out.println(url);
            String clindata = webService.loadUrl(url);

            String[] clinparts = clindata.split(lineSeparator);
            List<String> data = new ArrayList<String>(Arrays.asList(clinparts));

            // ===== Samples =====

            List<String> header = this.createHeader(data.get(0), pattern);
            System.out.println(header);

            for (int i = 1; i < data.size(); i++) {

                Integer nbSamples = data.size() - 1;

                Map<String, Object> mapParameters = this.createParameters(data.get(i), header);
                String idSample = this.createIdSample(mapParameters);

                if (idSample == null) {
                    System.err.println("idSample is not recongnized for " + mapParameters);
                    mongoClient.close();
                    System.exit(0);
                }

                String organism = (String) mapParameters.get("organism");
                if (organism == null || organism.isEmpty()) {
                    organism = "Homo sapiens";
                }
                String platform = (String) mapParameters.get("LIBRARY_STRATEGY");
                if (platform != null && !platform.isEmpty()) {
                    platform = platform.toLowerCase().trim();
                } else {
                    platform = "rna-seq";
                }
                String layout = (String) mapParameters.get("LIBRARY_LAYOUT");
                if (layout != null && !layout.isEmpty()) {
                    layout = layout.toLowerCase().trim();
                }

                Document docSampleExist = collectionSamples.find(Filters.eq("_id", idSample)).first();
                boolean docAlreadyExist = docSampleExist != null;

                boolean analysed = false;

                if (docAlreadyExist) {
                    analysed = (Boolean) docSampleExist.get("analyzed");
                    System.out.println(i + "/" + nbSamples + "\t " + docSeries.get("_id") + "\t " + idSample
                            + ":  already exists in the database, analyzed=" + analysed);
                } else {
                    System.out.println(i + "/" + nbSamples + "\t " + docSeries.get("_id") + "\t " + idSample);
                }

                // ===== Sample Document =====

                Document docSample = mongoService.createSample(idSample, (String) docSeries.get("_id"),
                        listSeriesAcc, organism, (Date) docSeries.get("submission_date"),
                        (Date) docSeries.get("last_update"), analysed);

                // ===== Mandatory parameters =====

                // Preserve "exp_group" if the document exists already

                Document expGroup = null;
                if (docAlreadyExist) {
                    expGroup = (Document) docSampleExist.get("exp_group");
                } else {
                    expGroup = mongoService.createExpGroup(docSample, platform,
                            (String) mapParameters.get("organism part"),
                            (String) mapParameters.get("Source Name"), organism);
                    if (layout != null) {
                        expGroup.append("layout", layout);

                        // run_name
                        int j = 0;
                        boolean isFound = false;
                        String runName = null;
                        while (!isFound && j < listRunNameParameters.length) {
                            runName = (String) mapParameters.get(listRunNameParameters[j]);
                            isFound = runName != null;
                            j++;
                        }
                        if (runName != null) {
                            expGroup.append("run_name", runName);
                        }

                    }
                }

                docSample.append("exp_group", expGroup);

                // ===== Supplementary parameters =====

                Document parameters = mongoService.createParameters(docSample, mapParameters);
                docSample.append("parameters", parameters);

                // === Delete if already exist ===
                collectionSamples.deleteOne(eq("_id", idSample));

                // ===== Insert data =====
                collectionSamples.insertOne(docSample);

                // ===== Update series for platforms =====
                List<String> listPlatforms = collectionSamples
                        .distinct("exp_group.id_platform", Filters.in("series", accession), String.class)
                        .into(new ArrayList<String>());
                docSeries.append("platforms", listPlatforms);
                collectionSeries.updateOne(Filters.eq("_id", accession), new Document("$set", docSeries));

            }

        } else {
            System.out.println("GEO accession " + gseNumber + " corresponding to  " + accession
                    + " exists already. Skip import.");
        }
    }

    mongoClient.close();

}

From source file:module.ImportGeo.java

License:Open Source License

public ImportGeo() {

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

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

    // ===== Insert data =====

    for (int k = 0; k < listGseNumber.length; k++) {

        String gseNumber = listGseNumber[k];

        System.out.println("------------------------------------------");
        System.out.println(k + " Import " + gseNumber);

        // ===== Load GSE =====

        NcbiGeoGse gse = new NcbiGeoGse(webService.loadGeo(gseNumber));
        System.out.println(gse);//from   w w w  . jav a  2  s .co  m

        // ===== Series =====

        MongoCollection<Document> collectionSeries = db.getCollection("series");

        Document docSeries = mongoService.createSeries(gse.getGseNumber(), gse.getTitle(), gse.getListGpl(),
                gse.getSubmissionDate(), gse.getLastUpdate());

        UpdateResult updateResult = collectionSeries.updateOne(Filters.eq("_id", gse.getGseNumber()),
                new Document("$set", docSeries));
        if (updateResult.getMatchedCount() == 0) {
            collectionSeries.insertOne(docSeries);
        }

        // ===== Platforms =====

        MongoCollection<Document> collectionPlatforms = db.getCollection("platform");

        for (int i = 0; i < gse.getListGpl().size(); i++) {

            NcbiGeoGpl gpl = new NcbiGeoGpl(webService.loadGeo(gse.getListGpl().get(i)));

            System.out.println("\t Import platform " + gpl.getGplNumber());

            Document docPlatforms = mongoService.createPlatform(gpl.getGplNumber(), gpl.getTitle(),
                    gpl.getTaxid(), gpl.getOrganism(), gpl.getManufacturer(), gpl.getSubmissionDate(),
                    gpl.getLastUpdate(), gpl.getTechnology());

            UpdateResult res = collectionPlatforms.updateOne(Filters.eq("_id", gpl.getGplNumber()),
                    new Document("$set", docPlatforms));
            if (res.getMatchedCount() == 0) {
                collectionPlatforms.insertOne(docPlatforms);
            }
        }

        // ===== Samples ======

        MongoCollection<Document> collectionSamples = db.getCollection("sample");

        // for (int i=0; i<1; i++) {
        for (int i = 0; i < gse.getListGsm().size(); i++) {

            NcbiGeoGsm gsm = new NcbiGeoGsm(webService.loadGeo(gse.getListGsm().get(i)));

            Document docSampleExist = collectionSamples.find(Filters.eq("_id", gsm.getGsmNumber())).first();
            boolean docAlreadyExist = docSampleExist != null;

            boolean analysed = false;

            if (docAlreadyExist) {
                analysed = (Boolean) docSampleExist.get("analyzed");
                System.out.println(i + "/" + gse.getListGsm().size() + "\t " + gse.getGseNumber() + "\t "
                        + gsm.getGsmNumber() + ":  already exists in the database, analyzed=" + analysed);
            } else {
                System.out.println(i + "/" + gse.getListGsm().size() + "\t " + gse.getGseNumber() + "\t "
                        + gsm.getGsmNumber());
            }

            // ===== Sample Document =====

            Document docSample = mongoService.createSample(gsm.getGsmNumber(), gse.getGseNumber(),
                    gsm.getListGse(), gsm.getOrganism(), gsm.getSubmissionDate(), gsm.getLastUpdate(),
                    analysed);

            // ===== Mandatory parameters =====

            // Preserve "exp_group" if the document exists already

            Document expGroup = null;
            if (docAlreadyExist) {
                expGroup = (Document) docSampleExist.get("exp_group");
            } else {
                expGroup = mongoService.createExpGroup(docSample, gsm.getGplNumber(), gsm.getTitle(),
                        gsm.getSourceName(), gsm.getOrganism());

            }
            docSample.append("exp_group", expGroup);

            // ===== Supplementary parameters =====

            Document parameters = generateParameters(gsm);
            docSample.append("parameters", parameters);

            // === Delete if already exist ===
            collectionSamples.deleteOne(eq("_id", gsm.getGsmNumber()));

            // ===== Insert data =====
            collectionSamples.insertOne(docSample);

        }

    }

    mongoClient.close();
}

From source file:module.ImportPlatformFromFile.java

License:Open Source License

public ImportPlatformFromFile() {

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

    // === INPUT ===
    String idPlatform = "GPL97";
    String inputfile = this.getInputDirectory() + this.getDirSeparator() + "GPL97-17394.txt";
    String gpl = idPlatform.toLowerCase().trim();

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

    // ===== DAO =====
    OmGeneDao geneDao = new OmGeneDao(session);

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

    try {// www.  j  av a 2  s.  com
        // === Begin transaction ===
        session.beginTransaction();

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

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

        List<String> header = fileService.readHeader(listRows, "\t");

        if (header == null || header.isEmpty()) {
            throw new ImportDataException("The header is empty");
        } else {
            System.out.println("Header " + header);
        }

        Integer indId = fileService.findIndex(header, "ID");
        Integer indGbacc = fileService.findIndex(header, "GB_ACC");
        Integer indEntrez = fileService.findIndex(header, "ENTREZ");

        if (indId == null || indGbacc == null || indEntrez == null) {
            throw new ImportDataException("Header not recognized: " + "ID index=" + indId + ", GB_ACC index="
                    + indGbacc + ", ENTREZ index=" + indEntrez);
        } else {
            System.out.println("The following header items are recognized:");
            System.out.println("\t ID index=" + indId + ": " + header.get(indId));
            System.out.println("\t GB_ACC index=" + indGbacc + ": " + header.get(indGbacc));
            System.out.println("\t ENTREZ index=" + indEntrez + ": " + header.get(indEntrez));
        }

        // ===== Recognize data =====

        List<List<String>> data = fileService.readData(listRows, "\t");

        if (data == null || data.isEmpty()) {
            throw new ImportDataException("The data are empty");
        } else {
            System.out.println(
                    "The data are sucessfully loaded: rows " + data.size() + ", columns " + data.get(0).size());
        }

        // ===== Create specific tables =====

        String sqlCheckTableProbe = "select * from information_schema.tables WHERE table_schema = 'hs' and table_name='om_probe_"
                + gpl + "'";

        List<Object> result = session.createNativeQuery(sqlCheckTableProbe).getResultList();

        String tableProbe = "hs.om_probe_" + gpl;
        String tableGP = "hs.om_gp_" + gpl;

        if (result == null || result.isEmpty()) {
            // Table probe
            String sqlCreateTableProbe = "create table " + tableProbe
                    + "(id_probe             VARCHAR(50)          not null,"
                    + " genbank_acc          VARCHAR(50)          null," + " constraint pk_om_probe_" + gpl
                    + " primary key (id_probe))";
            session.createNativeQuery(sqlCreateTableProbe).executeUpdate();

            // Table gp
            String sqlCreateTableGP = "create table " + tableGP
                    + "(id_probe             VARCHAR(50)          not null,"
                    + " id_gene              INT4                 not null," + " constraint pk_om_gp_" + gpl
                    + " primary key (id_probe, id_gene))";
            session.createNativeQuery(sqlCreateTableGP).executeUpdate();

            // Foregn keys

            String sqlAlterTableProbe = "alter table " + tableGP + " add constraint fk_gp_probe_" + gpl
                    + " foreign key (id_probe)" + "  references " + tableProbe
                    + " (id_probe) on delete restrict on update restrict";
            session.createNativeQuery(sqlAlterTableProbe).executeUpdate();

            String sqlAlterTableGene = "alter table " + tableGP + " add constraint fk_gp_gene_" + gpl
                    + " foreign key (id_gene)"
                    + "  references hs.om_gene (id_gene) on delete restrict on update restrict";
            session.createNativeQuery(sqlAlterTableGene).executeUpdate();
        }

        // ===== Import data =====

        for (int i = 0; i < data.size(); i++) {
            // for (int i=0; i<10; i++) {

            List<String> dataline = data.get(i);

            String idProbe = dataline.get(indId).trim();
            String genbankAcc = dataline.get(indGbacc).trim();

            String sqlInsertProbe = "insert into " + tableProbe + " values('" + idProbe + "',  null)";
            if (genbankAcc != null && !genbankAcc.isEmpty()) {
                sqlInsertProbe = "insert into " + tableProbe + " values('" + idProbe + "', '" + genbankAcc
                        + "')";
            }
            session.createNativeQuery(sqlInsertProbe).executeUpdate();

            OmGenbankUnigene gu = session.get(OmGenbankUnigene.class, genbankAcc);
            if (gu == null && genbankAcc != null && !genbankAcc.isEmpty()) {
                gu = new OmGenbankUnigene();
                gu.setGenbankAcc(genbankAcc);
                session.save(gu);
            }

            String listEntrez = null;
            String[] parts = null;
            if (indEntrez < dataline.size()) {
                listEntrez = dataline.get(indEntrez).trim();
                parts = listEntrez.split("[///\\p{Space}]");

                for (String entrezString : parts) {

                    Integer entrez = null;

                    try {
                        entrez = Integer.parseInt(entrezString);
                    } catch (NumberFormatException e) {
                        // nothing to do
                    }

                    if (entrez != null) {

                        OmGene gene = geneDao.find(entrez);
                        if (gene == null) {
                            gene = geneDao.createGene(entrez, null);
                        }

                        String sqlInsertGP = "insert into " + tableGP + " values('" + idProbe + "', " + entrez
                                + ")";
                        session.createNativeQuery(sqlInsertGP).executeUpdate();

                    }
                }
            }

            if (i % 1000 == 0) {
                System.out.println(i + "\t" + idProbe + "\t" + genbankAcc + "\t" + listEntrez + "\t"
                        + Arrays.toString(parts));
            }

            if (i % 20 == 0) {
                session.flush();
            }
        }

        // ===== Subscribe platform =====

        OmPlatform platform = session.get(OmPlatform.class, idPlatform);
        if (platform != null) {
            platform.setEnabled(true);
            session.update(platform);
        } else {
            MongoCollection<Document> collection = db.getCollection("platforms");
            Document docPlatform = collection.find(Filters.eq("_id", idPlatform)).first();
            String title = docPlatform.getString("title");
            String manufacturer = docPlatform.getString("manufacturer");
            platform = new OmPlatform();
            platform.setIdPlatform(idPlatform);
            platform.setTitle(title);
            platform.setManufacturer(manufacturer);
            platform.setEnabled(true);
            session.save(platform);
        }

        // ===== Rights =====
        String sqlRights;
        String[] users = { "epimed_prod", "epimed_web", "epimed_script" };
        for (String user : users) {
            sqlRights = "GRANT SELECT ON ALL TABLES IN SCHEMA hs TO " + user;
            session.createNativeQuery(sqlRights).executeUpdate();
        }
        sqlRights = "GRANT ALL ON ALL TABLES IN SCHEMA hs TO epimed_admin";
        session.createNativeQuery(sqlRights).executeUpdate();

        // === Commit transaction ===
        session.getTransaction().commit();
        // session.getTransaction().rollback();

    } catch (Exception e) {
        session.getTransaction().rollback();
        System.out.println("ROLLBACK in module " + this.getClass().getName());
        e.printStackTrace();
    } finally {
        if (session.isOpen()) {
            session.close();
        }
        sessionFactory.close();
        mongoClient.close();
    }

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

}

From source file:module.script.AddSeriesToSamples.java

License:Open Source License

public AddSeriesToSamples() {

    // ===== Service =====
    FormatService formatService = new FormatService();

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

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

    Set<String> setProjects = new HashSet<String>();

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

    Bson filters = Filters.and(Filters.in("series", "PRJNA270632"));

    List<Document> listDocuments = collection.find(filters).into(new ArrayList<Document>());

    for (int i = 0; i < listDocuments.size(); i++) {

        Document doc = listDocuments.get(i);
        Document expgroup = doc.get("exp_group", Document.class);

        if (expgroup.get("exp_Mcount") != null) {

            List<String> projects = doc.get("series", ArrayList.class);
            setProjects.clear();/*from   w ww .  ja v a 2s.  c  om*/
            setProjects.addAll(projects);
            setProjects.add("TISSUE_SPECIFIC_GENES_HS");
            doc.put("series", setProjects);
            System.out.println(doc.getString("_id") + " " + projects + " -> " + setProjects);

            collection.updateOne(Filters.eq("_id", doc.getString("_id")), new Document("$set", doc));
        }

    }

    mongoClient.close();
}

From source file:module.script.CorrectImportedData.java

License:Open Source License

public CorrectImportedData() {

    // ===== Service =====
    FormatService formatService = new FormatService();

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

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

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

    Bson filters = Filters.and(Filters.eq("main_gse_number", gseNumber));

    List<Document> listDocuments = collection.find(filters).into(new ArrayList<Document>());

    for (int i = 0; i < listDocuments.size(); i++) {

        Document doc = listDocuments.get(i);
        Document expgroup = (Document) doc.get("exp_group");
        Document parameters = (Document) doc.get("parameters");

        expgroup.append("id_tissue_stage", 2);
        expgroup.append("tissue_stage", "fetal");

        // Update Mongo document
        doc.put("exp_group", expgroup);
        // doc.put("parameters", parameters);
        doc.put("analyzed", true);

        System.out.println(expgroup);

        collection.updateOne(Filters.eq("_id", doc.getString("_id")), new Document("$set", doc));
    }//  w ww  .  j  a  v a  2s. c o  m

    mongoClient.close();
}

From source file:module.script.emtab365.ImportSamplesEMTAB365.java

License:Open Source License

public ImportSamplesEMTAB365() {

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

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

    // ===== Collections ======
    MongoCollection<Document> collectionPlatforms = db.getCollection("platforms");
    MongoCollection<Document> collectionSeries = db.getCollection("series");
    MongoCollection<Document> collectionSamples = db.getCollection("samples");

    // ===== Excel data loader =====

    String inputfile = this.getInputDirectory() + this.getDirSeparator() + "E-MTAB-365.sdrf.xlsx";
    System.out.println("LOADING \t " + inputfile);
    excelService.load(inputfile);/*from  w w w .  j a  v a 2 s .  co m*/

    // ===== Init values ======

    String idSeries = "E-MTAB-365";
    List<String> listSeries = new ArrayList<String>();
    listSeries.add(idSeries);
    Document docSeries = collectionSeries.find(Filters.eq("_id", idSeries)).first();
    String organism = "Homo sapiens";

    // ==== Header processing ====

    Map<Integer, String> mapHeader = new HashMap<Integer, String>();
    for (int i = 0; i < excelService.getHeader().size(); i++) {
        String headerItem = (String) excelService.getHeader().get(i);
        if (headerItem != null && headerItem.contains("[")) {
            String[] parts = headerItem.split("[\\[\\]]");
            headerItem = parts[1];
            headerItem = headerItem.replaceAll("[:_\\.]", " ");
        }
        mapHeader.put(i, headerItem.trim());
    }
    System.out.println(mapHeader);

    for (int i = 0; i < excelService.getData().size(); i++) {
        // for (int i=0; i<1; i++) {

        List<Object> dataline = excelService.getData().get(i);
        String idSample = (String) dataline.get(0);

        if (!idSample.equals("pool XX")) {

            String idPlatform = ((String) dataline.get(54)).trim();
            if (idPlatform.contains("A-AFFY-44")) {
                idPlatform = "GPL570";
            } else {
                Document docPlatform = mongoService.createPlatform(idPlatform, null, "9606", "Homo sapiens",
                        null, null, null, null);
                UpdateResult res = collectionPlatforms.updateOne(
                        Filters.eq("_id", docPlatform.getString("_id")), new Document("$set", docPlatform));
                if (res.getMatchedCount() == 0) {
                    collectionPlatforms.insertOne(docPlatform);
                }
            }

            Document docSample = mongoService.createSample(idSample, idSeries, listSeries, organism,
                    (Date) docSeries.get("submission_date"), (Date) docSeries.get("last_update"), false);

            // === exp_group ===

            Document expgroup = mongoService.createExpGroup(docSample, idPlatform, null, null, organism);
            docSample.append("exp_group", expgroup);

            // === parameters ===

            Map<String, Object> mapParameters = new HashMap<String, Object>();

            for (int j = 0; j < dataline.size(); j++) {

                String key = mapHeader.get(j);
                Object value = dataline.get(j);

                if (value instanceof String) {
                    String valueString = ((String) value).trim();
                    if (valueString != null && !valueString.isEmpty() && !valueString.equals("NA")
                            && !valueString.equals("ND")) {
                        value = valueString;
                    } else {
                        value = null;
                    }
                }

                if (key != null && value != null) {
                    mapParameters.put(key, value);
                    // System.out.println(key + "='" + value+"'");
                }
            }

            Document parameters = mongoService.createParameters(docSample, mapParameters);
            docSample.append("parameters", parameters);

            // === Delete if already exist ===
            collectionSamples.deleteOne(Filters.eq("_id", docSample.getString("_id")));

            // ===== Insert data =====
            collectionSamples.insertOne(docSample);

            System.out.println(docSample);

        }
    }

    mongoClient.close();
}

From source file:module.script.emtab365.UpdateSamplesEMTAB365.java

License:Open Source License

public UpdateSamplesEMTAB365() {

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

    // ===== INIT =====

    ClMorphology ductal = session.get(ClMorphology.class, "8500/3"); // 8500/3   Infiltrating duct carcinoma, NOS (C50._)
    ClMorphology lobular = session.get(ClMorphology.class, "8520/3"); // 8520/3   Lobular carcinoma, NOS (C50._)
    ClMorphology morphology = session.get(ClMorphology.class, "8010/3"); // Carcinoma

    ClTopology breast = session.get(ClTopology.class, "C50.9"); // Breast
    ClTopology blood = session.get(ClTopology.class, "C42.0"); // Blood
    ClTopology lymphNode = session.get(ClTopology.class, "C77.9"); // Lymph node

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

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

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

    List<Document> listDocuments = collection.find(Filters.in("series", gseNumber))
            .into(new ArrayList<Document>());

    for (int i = 0; i < listDocuments.size(); i++) {
        Document doc = listDocuments.get(i);
        Document expgroup = (Document) doc.get("exp_group");
        Document parameters = (Document) doc.get("parameters");

        String histoType = parameters.getString("Histology");
        String histoSubtype = parameters.getString("CIT classification");

        expgroup.put("histology_subtype", histoSubtype);

        if (histoType != null && histoType.toLowerCase().equals("lobular")) {
            morphology = lobular;//w ww .  ja v a2 s  . c  o m
        }
        if (histoType != null && histoType.toLowerCase().equals("ductal")) {
            morphology = ductal;
        }

        expgroup.put("id_morphology", morphology.getIdMorphology());
        expgroup.put("morphology", morphology.getName());

        expgroup.put("sample_source", parameters.getString("Source Name"));

        String organismPart = parameters.getString("OrgansimPart");

        ClTopology topology = null;
        if (organismPart != null) {

            if (organismPart.toLowerCase().contains("breast")) {
                topology = breast;
            }

            if (organismPart.toLowerCase().contains("blood")) {
                topology = blood;
            }
            if (organismPart.toLowerCase().contains("lymph")) {
                topology = lymphNode;
            }

        } else {
            topology = breast;
        }

        expgroup.put("id_topology", topology.getIdTopology());
        expgroup.put("topology", topology.getName());
        expgroup.put("id_topology_group", topology.getClTopologyGroup().getIdGroup());
        expgroup.put("topology_group", topology.getClTopologyGroup().getName());

        // ==== Survival =====

        Object dfs_months = parameters.get("Delay Metastasis Free Survival months");
        if (dfs_months != null) {
            expgroup.put("dfs_months", dfs_months);
        }

        Object os_months = parameters.get("Delay Overall Survival months");
        if (os_months != null) {
            expgroup.put("os_months", os_months);
        }

        Double os = (Double) expgroup.get("os_months");
        Double dfs = (Double) expgroup.get("dfs_months");
        if (os != null && dfs != null && dfs.equals(os)) {
            expgroup.put("relapsed", false);
        }

        if (os != null && dfs != null && dfs < os) {
            expgroup.put("relapsed", true);
        }

        if (os != null && dfs != null && dfs > os) {
            expgroup.put("relapsed", null);
        }

        Object relapseDate = parameters.get("Relapse  Metastasis Date");
        if (relapseDate != null) {
            expgroup.put("relapsed", true);
        }

        // ==== Grade ====
        expgroup.put("tnm_grade", parameters.get("Grade  Scarff Bloom Richardson"));

        // ==== Files =====

        expgroup.put("ftp", parameters.getString("ArrayExpress FTP file"));
        expgroup.put("file_name", parameters.getString("Array Data File"));

        expgroup.remove("individual");
        if (parameters.getString("Individual") != null) {
            expgroup.put("individual", parameters.getString("Individual"));
        }

        // ==== Biomarkers ====
        /*
        String p53 = parameters.getString("Phenotype - TP53  Gene mutation  Status");
        expgroup.put("p53", value)
                
        String pr = parameters.getString("PGR  Protein expression");
        String er = parameters.getString("ESR1  Protein expression");
        String her2 = parameters.getString("ERBB2  Protein expression");
         */

        doc.put("exp_group", expgroup);

        System.out.println(i + " " + doc.get("_id") + " " + doc.get("analyzed") + " " + expgroup);

        if (commit) {
            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.epilung.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");
    MongoCollection<Document> collectionPlatforms = db.getCollection("platforms");

    Bson filters = Filters.and(//from  w ww.  ja va 2s. c om
            Filters.in("exp_group.id_platform", new String[] { "GPL13534", "GPL8490", "GPL21145" }),
            Filters.eq("exp_group.id_tissue_status", 1), Filters.ne("exp_group.id_topology", null));

    /*
    List<Document> list = collectionSamples
    .find(filters)
    .into(new ArrayList<Document>());
    */

    List<Document> list = collectionSamples.aggregate(Arrays.asList(Aggregates.match(filters),
            Aggregates.group("$exp_group.topology", 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));
    }

    collectionPlatforms.find(Filters.regex("title", ".*ethyl.*")).forEach(printBlock);

    mongoClient.close();

}

From source file:module.script.epilung.SearchSamplesLungAdenocarcinoma.java

License:Open Source License

public SearchSamplesLungAdenocarcinoma() {

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

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

    System.out.println("\n================== SUMMARY ==================");

    Bson[] pFilters = { Filters.eq("_id", "GPL570"), Filters.eq("type", "rna-seq") };

    for (Bson pFilter : pFilters) {

        // === Platforms ===
        List<String> platforms = new ArrayList<String>();
        List<String> platformstext = new ArrayList<String>();
        List<Document> list = collectionPlatforms.find(pFilter).into(new ArrayList<Document>());
        for (Document doc : list) {
            platforms.add(doc.getString("_id"));
            platformstext.add(doc.getString("_id") + " " + doc.getString("type"));
        }/*from  w w  w  .j  a v  a2 s.com*/

        String lungAdenoFilterName = "Lung adenocarcinoma samples with survival";
        Bson lungAdenoFilter = Filters.and(Filters.in("exp_group.id_platform", platforms),
                Filters.eq("exp_group.id_tissue_status", 3), Filters.eq("exp_group.id_topology_group", "C34"),
                Filters.regex("exp_group.morphology", ".*denocarcinoma.*"),
                Filters.or(Filters.ne("exp_group.os_months", null), Filters.ne("exp_group.dfs_months", null)));

        String ntlFilterName = "Normal lung samples";
        Bson ntlFilter = Filters.and(Filters.in("exp_group.id_platform", platforms),
                Filters.eq("exp_group.id_tissue_status", 1), Filters.eq("exp_group.id_topology_group", "C34"));

        String[] filterNames = { lungAdenoFilterName, ntlFilterName };
        Bson[] sFilters = { lungAdenoFilter, ntlFilter };

        for (int i = 0; i < sFilters.length; i++) {

            Bson filter = sFilters[i];
            String filterName = filterNames[i];

            List<Document> docs = collectionSamples.find(filter).into(new ArrayList<Document>());

            Set<String> setGse = new HashSet<String>();
            for (Document doc : docs) {
                setGse.add(doc.getString("main_gse_number"));
                // System.out.println(doc);
            }

            System.out.println("-------------------------------------------");
            System.out.println("Query: " + filterName);
            System.out.println("Platforms: " + platformstext);
            System.out.println("Samples: " + docs.size());
            System.out.println("Series: " + setGse);

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

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

    mongoClient.close();

}

From source file:module.script.epimed_ontology.AddEpimedGroupToSamples.java

License:Open Source License

public AddEpimedGroupToSamples() {

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

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

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

    List<Document> samples = collectionSample.find().into(new ArrayList<Document>());

    for (int i = 0; i < samples.size(); i++) {
        Document sample = samples.get(i);
        Document expgroup = sample.get("exp_group", Document.class);

        String idTopology = expgroup.getString("id_topology");

        if (idTopology != null && !idTopology.isEmpty()) {

            ClTopology topology = topologyDao.find(idTopology);
            ClEpimedGroup grp1 = topology.getClEpimedGroup();
            ClEpimedGroup grp2 = grp1.getParent();
            ClEpimedGroup grp3 = grp2.getParent();
            expgroup.append("tissue_group_level1", grp1.getName());
            expgroup.append("tissue_group_level2", grp2.getName());
            expgroup.append("tissue_group_level3", grp3.getName());

            System.out.println((i + 1) + "/" + samples.size() + " " + expgroup);

            sample.append("exp_group", expgroup);
            collectionSample.updateOne(Filters.eq("_id", sample.getString("_id")),
                    new Document("$set", sample));
        }//from  w w  w  .j a va2s .  co m

    }

    // === Commit transaction ===
    // session.getTransaction().commit();
    session.getTransaction().rollback();

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

    mongoClient.close();
}