Example usage for com.mongodb DBObject keySet

List of usage examples for com.mongodb DBObject keySet

Introduction

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

Prototype

Set<String> keySet();

Source Link

Document

Returns this object's fields' names

Usage

From source file:org.opencb.opencga.storage.mongodb.alignment.CoverageMongoDBWriter.java

License:Apache License

private void secureInsert(DBObject query, DBObject object, String chromosome, int start, int size) {
    boolean documentExists = true;
    boolean fileExists = true;

    //Check if the document exists
    {/*from   w  w w .jav  a  2s. c  om*/
        QueryResult countId = collection.count(query);
        if (countId.getNumResults() == 1 && countId.getResultType().equals(Long.class.getCanonicalName())) {
            if ((Long) countId.getResult().get(0) < 1) {
                DBObject document = BasicDBObjectBuilder.start().append(FILES_FIELD, new BasicDBList())
                        .append(CHR_FIELD, chromosome).append(START_FIELD, start).append(SIZE_FIELD, size)
                        .get();
                document.putAll(query); //{_id:<chunkId>, files:[]}
                collection.insert(document, null); //Insert a document with empty files array.
                fileExists = false;
            }
        } else {
            logger.error(countId.getErrorMsg(), countId);
        }
    }

    if (documentExists) {
        //Check if the file exists
        BasicDBObject fileQuery = new BasicDBObject(FILES_FIELD + "." + FILE_ID_FIELD, fileId);
        fileQuery.putAll(query);
        QueryResult countFile = collection.count(fileQuery);
        if (countFile.getNumResults() == 1 && countFile.getResultType().equals(Long.class.getCanonicalName())) {
            if ((Long) countFile.getResult().get(0) < 1) {
                fileExists = false;
            }
        } else {
            logger.error(countFile.getErrorMsg(), countFile);
        }
    }

    if (fileExists) {
        BasicDBObject fileQuery = new BasicDBObject(FILES_FIELD + "." + FILE_ID_FIELD, fileId);
        fileQuery.putAll(query);

        BasicDBObject fileObject = new BasicDBObject();
        for (String key : object.keySet()) {
            fileObject.put(FILES_FIELD + ".$." + key, object.get(key));
        }
        //            DBObject update = new BasicDBObject("$set", new BasicDBObject(FILES_FIELD, fileObject));
        DBObject update = new BasicDBObject("$set", fileObject);

        //db.<collectionName>.update({_id:<chunkId>  , "files.id":<fileId>}, {$set:{"files.$.<objKey>":<objValue>}})
        collection.update(fileQuery, update, updateOptions);
    } else {
        BasicDBObject fileObject = new BasicDBObject(FILE_ID_FIELD, fileId);
        fileObject.putAll(object);
        DBObject update = new BasicDBObject("$addToSet", new BasicDBObject(FILES_FIELD, fileObject));

        //db.<collectionName>.update({_id:<chunkId>} , {$addToSet:{files:{id:<fileId>, <object>}}})
        collection.update(query, update, updateOptions);
    }
}

From source file:org.opencb.opencga.storage.mongodb.variant.adaptors.VariantMongoDBAdaptor.java

License:Apache License

private StudyConfiguration parseStudyQueryParams(Query query, QueryBuilder builder) {

    if (query != null) {
        Map<String, Integer> studies = getStudyConfigurationManager().getStudies(null);

        boolean singleStudy = studies.size() == 1;
        boolean validStudiesFilter = isValidParam(query, VariantQueryParams.STUDIES);
        // SAMPLES filter will add a FILES filter if absent
        boolean validFilesFilter = isValidParam(query, VariantQueryParams.FILES)
                || isValidParam(query, VariantQueryParams.SAMPLES);
        boolean otherFilters = isValidParam(query, VariantQueryParams.FILES)
                || isValidParam(query, VariantQueryParams.GENOTYPE)
                || isValidParam(query, VariantQueryParams.SAMPLES)
                || isValidParam(query, VariantQueryParams.FILTER);

        // Use an elemMatch with all the study filters if there is more than one study registered,
        // or FILES and STUDIES filters are being used.
        // If filters STUDIES+FILES is used, elemMatch is required to use the index correctly. See #493
        boolean studyElemMatch = (!singleStudy || (validFilesFilter && validStudiesFilter));

        // If only studyId filter is being used, elemMatch is not needed
        if (validStudiesFilter && !otherFilters) {
            studyElemMatch = false;//from   w ww . j av a 2s .co m
        }

        // If using an elemMatch for the study, keys don't need to start with "studies"
        String studyQueryPrefix = studyElemMatch ? "" : DocumentToVariantConverter.STUDIES_FIELD + '.';
        QueryBuilder studyBuilder = QueryBuilder.start();
        final StudyConfiguration defaultStudyConfiguration = utils.getDefaultStudyConfiguration(query, null);

        if (isValidParam(query, VariantQueryParams.STUDIES)) {
            String sidKey = DocumentToVariantConverter.STUDIES_FIELD + '.'
                    + DocumentToStudyVariantEntryConverter.STUDYID_FIELD;
            String value = query.getString(VariantQueryParams.STUDIES.key());

            // Check that the study exists
            QueryOperation studiesOperation = checkOperator(value);
            List<String> studiesNames = splitValue(value, studiesOperation);
            List<Integer> studyIds = utils.getStudyIds(studiesNames, studies); // Non negated studyIds

            // If the Studies query has an AND operator or includes negated fields, it can not be represented only
            // in the "elemMatch". It needs to be in the root
            boolean anyNegated = studiesNames.stream().anyMatch(VariantDBAdaptorUtils::isNegated);
            boolean studyFilterAtRoot = studiesOperation == QueryOperation.AND || anyNegated;
            if (studyFilterAtRoot) {
                addQueryFilter(sidKey, value, builder, QueryOperation.AND,
                        study -> utils.getStudyId(study, false, studies));
            }

            // Add all non negated studies to the elemMatch builder if it is being used,
            // or it is not and it has not been added to the root
            if (studyElemMatch || !studyFilterAtRoot) {
                if (!studyIds.isEmpty()) {
                    if (!singleStudy || anyNegated || validFilesFilter) {
                        String studyIdsCsv = studyIds.stream().map(Object::toString)
                                .collect(Collectors.joining(","));
                        addQueryIntegerFilter(
                                studyQueryPrefix + DocumentToStudyVariantEntryConverter.STUDYID_FIELD,
                                studyIdsCsv, studyBuilder, QueryOperation.AND);
                    } // There is only one study! We can skip this filter
                }
            }
        }

        if (isValidParam(query, VariantQueryParams.FILES)) {
            addQueryFilter(
                    studyQueryPrefix + DocumentToStudyVariantEntryConverter.FILES_FIELD + '.'
                            + DocumentToStudyVariantEntryConverter.FILEID_FIELD,
                    query.getString(VariantQueryParams.FILES.key()), studyBuilder, QueryOperation.AND,
                    f -> utils.getFileId(f, false, defaultStudyConfiguration));
        }

        if (isValidParam(query, VariantQueryParams.FILTER)) {
            String filesValue = query.getString(VariantQueryParams.FILES.key());
            QueryOperation filesOperation = checkOperator(filesValue);
            List<String> fileNames = splitValue(filesValue, filesOperation);
            List<Integer> fileIds = utils.getFileIds(fileNames, true, defaultStudyConfiguration);

            String fileQueryPrefix;
            if (fileIds.isEmpty()) {
                fileQueryPrefix = studyQueryPrefix + DocumentToStudyVariantEntryConverter.FILES_FIELD + '.';
                addQueryStringFilter(
                        fileQueryPrefix + DocumentToStudyVariantEntryConverter.ATTRIBUTES_FIELD + '.'
                                + StudyEntry.FILTER,
                        query.getString(VariantQueryParams.FILTER.key()), studyBuilder, QueryOperation.AND);
            } else {
                QueryBuilder fileBuilder = QueryBuilder.start();
                addQueryStringFilter(
                        DocumentToStudyVariantEntryConverter.ATTRIBUTES_FIELD + '.' + StudyEntry.FILTER,
                        query.getString(VariantQueryParams.FILTER.key()), fileBuilder, QueryOperation.AND);
                fileBuilder.and(DocumentToStudyVariantEntryConverter.FILEID_FIELD).in(fileIds);
                studyBuilder.and(studyQueryPrefix + DocumentToStudyVariantEntryConverter.FILES_FIELD)
                        .elemMatch(fileBuilder.get());
            }
        }

        Map<Object, List<String>> genotypesFilter = new HashMap<>();
        if (isValidParam(query, VariantQueryParams.GENOTYPE)) {
            String sampleGenotypes = query.getString(VariantQueryParams.GENOTYPE.key());
            parseGenotypeFilter(sampleGenotypes, genotypesFilter);
        }

        if (isValidParam(query, VariantQueryParams.SAMPLES)) {
            Set<Integer> files = new HashSet<>();
            String samples = query.getString(VariantQueryParams.SAMPLES.key());

            for (String sample : samples.split(",")) {
                int sampleId = utils.getSampleId(sample, defaultStudyConfiguration);
                genotypesFilter.put(sampleId,
                        Arrays.asList("1", "0/1", "0|1", "1|0", "1/1", "1|1", "1/2", "1|2", "2|1"));
                if (!isValidParam(query, VariantQueryParams.FILES) && defaultStudyConfiguration != null) {
                    for (Integer file : defaultStudyConfiguration.getIndexedFiles()) {
                        if (defaultStudyConfiguration.getSamplesInFiles().get(file).contains(sampleId)) {
                            files.add(file);
                        }
                    }
                }
            }

            // If there is no valid files filter, add files filter to speed up this query
            if (!isValidParam(query, VariantQueryParams.FILES) && !files.isEmpty()) {
                addQueryFilter(
                        studyQueryPrefix + DocumentToStudyVariantEntryConverter.FILES_FIELD + '.'
                                + DocumentToStudyVariantEntryConverter.FILEID_FIELD,
                        files, studyBuilder, QueryOperation.AND,
                        f -> utils.getFileId(f, false, defaultStudyConfiguration));
            }
        }

        if (!genotypesFilter.isEmpty()) {
            for (Map.Entry<Object, List<String>> entry : genotypesFilter.entrySet()) {
                Object sample = entry.getKey();
                List<String> genotypes = entry.getValue();

                int sampleId = utils.getSampleId(sample, defaultStudyConfiguration);

                QueryBuilder genotypesBuilder = QueryBuilder.start();

                List<String> defaultGenotypes;
                if (defaultStudyConfiguration != null) {
                    defaultGenotypes = defaultStudyConfiguration.getAttributes()
                            .getAsStringList(DEFAULT_GENOTYPE.key());
                } else {
                    defaultGenotypes = Arrays.asList("0/0", "0|0");
                }
                for (String genotype : genotypes) {
                    boolean negated = isNegated(genotype);
                    if (negated) {
                        genotype = genotype.substring(1);
                    }
                    if (defaultGenotypes.contains(genotype)) {
                        List<String> otherGenotypes = Arrays.asList("0/0", "0|0", "0/1", "1/0", "1/1", "-1/-1",
                                "0|1", "1|0", "1|1", "-1|-1", "0|2", "2|0", "2|1", "1|2", "2|2", "0/2", "2/0",
                                "2/1", "1/2", "2/2", DocumentToSamplesConverter.UNKNOWN_GENOTYPE);
                        if (negated) {
                            for (String otherGenotype : otherGenotypes) {
                                if (defaultGenotypes.contains(otherGenotype)) {
                                    continue;
                                }
                                String key = studyQueryPrefix
                                        + DocumentToStudyVariantEntryConverter.GENOTYPES_FIELD + '.'
                                        + otherGenotype;
                                genotypesBuilder.or(new BasicDBObject(key, sampleId));
                            }
                        } else {
                            QueryBuilder andBuilder = QueryBuilder.start();
                            for (String otherGenotype : otherGenotypes) {
                                if (defaultGenotypes.contains(otherGenotype)) {
                                    continue;
                                }
                                String key = studyQueryPrefix
                                        + DocumentToStudyVariantEntryConverter.GENOTYPES_FIELD + '.'
                                        + otherGenotype;
                                andBuilder.and(new BasicDBObject(key, new Document("$ne", sampleId)));
                            }
                            genotypesBuilder.or(andBuilder.get());
                        }
                    } else {
                        String s = studyQueryPrefix + DocumentToStudyVariantEntryConverter.GENOTYPES_FIELD + '.'
                                + DocumentToSamplesConverter.genotypeToStorageType(genotype);
                        if (negated) {
                            //and [ {"gt.0|1" : { $ne : <sampleId> } } ]
                            genotypesBuilder.and(new BasicDBObject(s, new BasicDBObject("$ne", sampleId)));

                        } else {
                            //or [ {"gt.0|1" : <sampleId> } ]
                            genotypesBuilder.or(new BasicDBObject(s, sampleId));
                        }
                    }
                }
                studyBuilder.and(genotypesBuilder.get());
            }
        }

        // If Study Query is used then we add a elemMatch query
        DBObject studyQuery = studyBuilder.get();
        if (!studyQuery.keySet().isEmpty()) {
            if (studyElemMatch) {
                builder.and(DocumentToVariantConverter.STUDIES_FIELD).elemMatch(studyQuery);
            } else {
                builder.and(studyQuery);
            }
        }
        return defaultStudyConfiguration;
    } else {
        return null;
    }
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

@Override
public QueryResult getFrequency(Query query, Region region, int regionIntervalSize) {
    // db.variants.aggregate( { $match: { $and: [ {chr: "1"}, {start: {$gt: 251391, $lt: 2701391}} ] }},
    //                        { $group: { _id: { $subtract: [ { $divide: ["$start", 20000] }, { $divide: [{$mod: ["$start", 20000]}, 20000] } ] },
    //                                  totalCount: {$sum: 1}}})

    QueryOptions options = new QueryOptions();

    // If interval is not provided is set to the value that returns 200 values
    if (regionIntervalSize <= 0) {
        //            regionIntervalSize = options.getInt("interval", (region.getEnd() - region.getStart()) / 200);
        regionIntervalSize = (region.getEnd() - region.getStart()) / 200;
    }//from   w  ww.  j  av  a2  s  .  c  om

    BasicDBObject start = new BasicDBObject("$gt", region.getStart());
    start.append("$lt", region.getEnd());

    BasicDBList andArr = new BasicDBList();
    andArr.add(new BasicDBObject(DBObjectToVariantConverter.CHROMOSOME_FIELD, region.getChromosome()));
    andArr.add(new BasicDBObject(DBObjectToVariantConverter.START_FIELD, start));

    // Parsing the rest of options
    QueryBuilder qb = new QueryBuilder();
    //        DBObject optionsMatch = parseQueryOptions(options, qb).get();
    DBObject optionsMatch = parseQuery(query, qb).get();
    if (!optionsMatch.keySet().isEmpty()) {
        andArr.add(optionsMatch);
    }
    DBObject match = new BasicDBObject("$match", new BasicDBObject("$and", andArr));

    //        qb.and("_at.chunkIds").in(chunkIds);
    //        qb.and(DBObjectToVariantConverter.END_FIELD).greaterThanEquals(region.getStart());
    //        qb.and(DBObjectToVariantConverter.START_FIELD).lessThanEquals(region.getEnd());
    //
    //        List<String> chunkIds = getChunkIds(region);
    //        DBObject regionObject = new BasicDBObject("_at.chunkIds", new BasicDBObject("$in", chunkIds))
    //                .append(DBObjectToVariantConverter.END_FIELD, new BasicDBObject("$gte", region.getStart()))
    //                .append(DBObjectToVariantConverter.START_FIELD, new BasicDBObject("$lte", region.getEnd()));

    BasicDBList divide1 = new BasicDBList();
    divide1.add("$start");
    divide1.add(regionIntervalSize);

    BasicDBList divide2 = new BasicDBList();
    divide2.add(new BasicDBObject("$mod", divide1));
    divide2.add(regionIntervalSize);

    BasicDBList subtractList = new BasicDBList();
    subtractList.add(new BasicDBObject("$divide", divide1));
    subtractList.add(new BasicDBObject("$divide", divide2));

    BasicDBObject subtract = new BasicDBObject("$subtract", subtractList);
    DBObject totalCount = new BasicDBObject("$sum", 1);
    BasicDBObject g = new BasicDBObject("_id", subtract);
    g.append("features_count", totalCount);
    DBObject group = new BasicDBObject("$group", g);
    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));

    //        logger.info("getAllIntervalFrequencies - (>_)>");
    //        System.out.println(options.toString());
    //        System.out.println(match.toString());
    //        System.out.println(group.toString());
    //        System.out.println(sort.toString());

    long dbTimeStart = System.currentTimeMillis();
    QueryResult output = variantsCollection.aggregate(/*"$histogram", */Arrays.asList(match, group, sort),
            options);
    long dbTimeEnd = System.currentTimeMillis();

    Map<Long, DBObject> ids = new HashMap<>();
    // Create DBObject for intervals with features inside them
    for (DBObject intervalObj : (List<DBObject>) output.getResult()) {
        Long _id = Math.round((Double) intervalObj.get("_id"));//is double

        DBObject intervalVisited = ids.get(_id);
        if (intervalVisited == null) {
            intervalObj.put("_id", _id);
            intervalObj.put("start", getChunkStart(_id.intValue(), regionIntervalSize));
            intervalObj.put("end", getChunkEnd(_id.intValue(), regionIntervalSize));
            intervalObj.put("chromosome", region.getChromosome());
            intervalObj.put("features_count", Math.log((int) intervalObj.get("features_count")));
            ids.put(_id, intervalObj);
        } else {
            Double sum = (Double) intervalVisited.get("features_count")
                    + Math.log((int) intervalObj.get("features_count"));
            intervalVisited.put("features_count", sum.intValue());
        }
    }

    // Create DBObject for intervals without features inside them
    BasicDBList resultList = new BasicDBList();
    int firstChunkId = getChunkId(region.getStart(), regionIntervalSize);
    int lastChunkId = getChunkId(region.getEnd(), regionIntervalSize);
    DBObject intervalObj;
    for (int chunkId = firstChunkId; chunkId <= lastChunkId; chunkId++) {
        intervalObj = ids.get((long) chunkId);
        if (intervalObj == null) {
            intervalObj = new BasicDBObject();
            intervalObj.put("_id", chunkId);
            intervalObj.put("start", getChunkStart(chunkId, regionIntervalSize));
            intervalObj.put("end", getChunkEnd(chunkId, regionIntervalSize));
            intervalObj.put("chromosome", region.getChromosome());
            intervalObj.put("features_count", 0);
        }
        resultList.add(intervalObj);
    }

    QueryResult queryResult = new QueryResult(region.toString(), ((Long) (dbTimeEnd - dbTimeStart)).intValue(),
            resultList.size(), resultList.size(), null, null, resultList);

    return queryResult;
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

private QueryBuilder parseQuery(Query query, QueryBuilder builder) {
    if (query != null) {

        /** VARIANT PARAMS **/

        if (query.containsKey(VariantQueryParams.REGION.key())
                && !query.getString(VariantQueryParams.REGION.key()).isEmpty()) {
            List<String> stringList = query.getAsStringList(VariantQueryParams.REGION.key());
            List<Region> regions = new ArrayList<>(stringList.size());
            for (String reg : stringList) {
                Region region = Region.parseRegion(reg);
                regions.add(region);/* ww w  . ja  v a  2 s  .  c o m*/
            }
            getRegionFilter(regions, builder);
        }

        if (query.getString(VariantQueryParams.ID.key()) != null
                && !query.getString(VariantQueryParams.ID.key()).isEmpty()) {
            List<String> ids = query.getAsStringList(VariantQueryParams.ID.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    ids, builder, QueryOperation.OR);
            addQueryListFilter(DBObjectToVariantConverter.IDS_FIELD, ids, builder, QueryOperation.OR);
        }

        if (query.containsKey(VariantQueryParams.GENE.key())) {
            List<String> xrefs = query.getAsStringList(VariantQueryParams.GENE.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    xrefs, builder, QueryOperation.OR);
        }

        if (query.containsKey(VariantQueryParams.REFERENCE.key())
                && query.getString(VariantQueryParams.REFERENCE.key()) != null) {
            addQueryStringFilter(DBObjectToVariantConverter.REFERENCE_FIELD,
                    query.getString(VariantQueryParams.REFERENCE.key()), builder);
        }

        if (query.containsKey(VariantQueryParams.ALTERNATE.key())
                && query.getString(VariantQueryParams.ALTERNATE.key()) != null) {
            addQueryStringFilter(DBObjectToVariantConverter.ALTERNATE_FIELD,
                    query.getString(VariantQueryParams.ALTERNATE.key()), builder);
        }

        if (query.containsKey(VariantQueryParams.TYPE.key())
                && !query.getString(VariantQueryParams.TYPE.key()).isEmpty()) {
            addQueryStringFilter(DBObjectToVariantConverter.TYPE_FIELD,
                    query.getString(VariantQueryParams.TYPE.key()), builder);
        }

        /** ANNOTATION PARAMS **/

        if (query.containsKey(VariantQueryParams.ANNOTATION_EXISTS.key())) {
            builder.and(DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                    + DBObjectToVariantAnnotationConverter.ANNOT_ID_FIELD);
            builder.exists(query.getBoolean(VariantQueryParams.ANNOTATION_EXISTS.key()));
        }

        if (query.containsKey(VariantQueryParams.ANNOT_XREF.key())) {
            List<String> xrefs = query.getAsStringList(VariantQueryParams.ANNOT_XREF.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    xrefs, builder, QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())) {
            List<String> cts = new ArrayList<>(
                    query.getAsStringList(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key()));
            List<Integer> ctsInteger = new ArrayList<>(cts.size());
            for (Iterator<String> iterator = cts.iterator(); iterator.hasNext();) {
                String ct = iterator.next();
                if (ct.startsWith("SO:")) {
                    ct = ct.substring(3);
                }
                try {
                    ctsInteger.add(Integer.parseInt(ct));
                } catch (NumberFormatException e) {
                    logger.error("Error parsing integer ", e);
                    iterator.remove(); //Remove the malformed query params.
                }
            }
            query.put(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key(), cts);
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD,
                    ctsInteger, builder, QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.ANNOT_BIOTYPE.key())) {
            List<String> biotypes = query.getAsStringList(VariantQueryParams.ANNOT_BIOTYPE.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.BIOTYPE_FIELD,
                    biotypes, builder, QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.POLYPHEN.key())) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POLYPHEN_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD,
                    query.getString(VariantQueryParams.POLYPHEN.key()), builder);
        }

        if (query.containsKey(VariantQueryParams.SIFT.key())) {
            System.out.println(query.getString(VariantQueryParams.SIFT.key()));
            addCompQueryFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SIFT_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD,
                    query.getString(VariantQueryParams.SIFT.key()), builder);
        }

        if (query.containsKey(VariantQueryParams.PROTEIN_SUBSTITUTION.key())) {
            List<String> list = new ArrayList<>(
                    query.getAsStringList(VariantQueryParams.PROTEIN_SUBSTITUTION.key()));
            addScoreFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.PROTEIN_SUBSTITUTION_SCORE_FIELD,
                    list, builder);
            query.put(VariantQueryParams.PROTEIN_SUBSTITUTION.key(), list); //Replace the QueryOption without the malformed query params
        }

        if (query.containsKey(VariantQueryParams.CONSERVATION.key())) {
            List<String> list = new ArrayList<>(query.getAsStringList(VariantQueryParams.CONSERVATION.key()));
            addScoreFilter(DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                    + DBObjectToVariantAnnotationConverter.CONSERVED_REGION_SCORE_FIELD, list, builder);
            query.put(VariantQueryParams.CONSERVATION.key(), list);
        }

        if (query.containsKey(VariantQueryParams.ALTERNATE_FREQUENCY.key())) {
            List<String> list = new ArrayList<>(
                    query.getAsStringList(VariantQueryParams.ALTERNATE_FREQUENCY.key()));
            addFrequencyFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD, list,
                    builder); // Same method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field (reference/alternate) where to check the frequency
        }

        if (query.containsKey(VariantQueryParams.REFERENCE_FREQUENCY.key())) {
            List<String> list = new ArrayList<>(
                    query.getAsStringList(VariantQueryParams.REFERENCE_FREQUENCY.key()));
            addFrequencyFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD, list,
                    builder); // Same method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field (reference/alternate) where to check the frequency
        }

        /** STATS PARAMS **/

        if (query.get(VariantQueryParams.STATS_MAF.key()) != null
                && !query.getString(VariantQueryParams.STATS_MAF.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MAF_FIELD,
                    query.getString(VariantQueryParams.STATS_MAF.key()), builder);
        }

        if (query.get(VariantQueryParams.STATS_MGF.key()) != null
                && !query.getString(VariantQueryParams.STATS_MGF.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MGF_FIELD,
                    query.getString(VariantQueryParams.STATS_MGF.key()), builder);
        }

        if (query.get(VariantQueryParams.MISSING_ALLELES.key()) != null
                && !query.getString(VariantQueryParams.MISSING_ALLELES.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "."
                            + DBObjectToVariantStatsConverter.MISSALLELE_FIELD,
                    query.getString(VariantQueryParams.MISSING_ALLELES.key()), builder);
        }

        if (query.get(VariantQueryParams.MISSING_GENOTYPES.key()) != null
                && !query.getString(VariantQueryParams.MISSING_GENOTYPES.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "."
                            + DBObjectToVariantStatsConverter.MISSGENOTYPE_FIELD,
                    query.getString(VariantQueryParams.MISSING_GENOTYPES.key()), builder);
        }

        if (query.get("numgt") != null && !query.getString("numgt").isEmpty()) {
            for (String numgt : query.getAsStringList("numgt")) {
                String[] split = numgt.split(":");
                addCompQueryFilter(
                        DBObjectToVariantConverter.STATS_FIELD + "."
                                + DBObjectToVariantStatsConverter.NUMGT_FIELD + "." + split[0],
                        split[1], builder);
            }
        }

        /** STUDIES **/
        QueryBuilder studyBuilder = QueryBuilder.start();

        if (query.containsKey(VariantQueryParams.STUDIES.key())) { // && !options.getList("studies").isEmpty() && !options.getListAs("studies", String.class).get(0).isEmpty()) {
            List<Integer> studyIds = getStudyIds(query.getAsList(VariantQueryParams.STUDIES.key()), null);
            addQueryListFilter(DBObjectToVariantSourceEntryConverter.STUDYID_FIELD, studyIds, studyBuilder,
                    QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.FILES.key())) { // && !options.getList("files").isEmpty() && !options.getListAs("files", String.class).get(0).isEmpty()) {
            addQueryListFilter(
                    DBObjectToVariantSourceEntryConverter.FILES_FIELD + "."
                            + DBObjectToVariantSourceEntryConverter.FILEID_FIELD,
                    query.getAsIntegerList(VariantQueryParams.FILES.key()), studyBuilder, QueryOperation.AND);
        }

        if (query.containsKey(VariantQueryParams.GENOTYPE.key())) {
            String sampleGenotypesCSV = query.getString(VariantQueryParams.GENOTYPE.key());

            //                String AND = ",";
            //                String OR = ";";
            //                String IS = ":";

            //                String AND = "AND";
            //                String OR = "OR";
            //                String IS = ":";

            String AND = ";";
            String OR = ",";
            String IS = ":";

            // we may need to know the study type
            //                studyConfigurationManager.getStudyConfiguration(1, null).getResult().get(0).

            String[] sampleGenotypesArray = sampleGenotypesCSV.split(AND);
            System.out.println("sampleGenotypesArray = " + Arrays.toString(sampleGenotypesArray));

            for (String sampleGenotypes : sampleGenotypesArray) {
                String[] sampleGenotype = sampleGenotypes.split(IS);
                if (sampleGenotype.length != 2) {
                    continue;
                }
                int sample = Integer.parseInt(sampleGenotype[0]);
                String[] genotypes = sampleGenotype[1].split(OR);
                QueryBuilder genotypesBuilder = QueryBuilder.start();
                for (String genotype : genotypes) {
                    if ("0/0".equals(genotype) || "0|0".equals(genotype)) {
                        QueryBuilder andBuilder = QueryBuilder.start();
                        List<String> otherGenotypes = Arrays.asList("0/1", "1/0", "1/1", "-1/-1", "0|1", "1|0",
                                "1|1", "-1|-1", "0|2", "2|0", "2|1", "1|2", "2|2", "0/2", "2/0", "2/1", "1/2",
                                "2/2", DBObjectToSamplesConverter.UNKNOWN_GENOTYPE);
                        for (String otherGenotype : otherGenotypes) {
                            andBuilder.and(new BasicDBObject(
                                    DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD + "." + otherGenotype,
                                    new BasicDBObject("$not", new BasicDBObject("$elemMatch",
                                            new BasicDBObject("$eq", sample)))));
                        }
                        genotypesBuilder.or(andBuilder.get());
                    } else {
                        String s = DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD + "."
                                + DBObjectToSamplesConverter.genotypeToStorageType(genotype);
                        //or [ {"samp.0|0" : { $elemMatch : { $eq : <sampleId> } } } ]
                        genotypesBuilder.or(new BasicDBObject(s,
                                new BasicDBObject("$elemMatch", new BasicDBObject("$eq", sample))));
                    }
                }
                studyBuilder.and(genotypesBuilder.get());
            }
        }

        // If Study Query is used then we add a elemMatch query
        DBObject studyQuery = studyBuilder.get();
        if (studyQuery.keySet().size() != 0) {
            builder.and(DBObjectToVariantConverter.STUDIES_FIELD).elemMatch(studyQuery);
        }
    }

    logger.debug("Find = " + builder.get());
    return builder;
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

/**
 * Two steps insertion:/*from  w ww.ja  v  a2 s. c o m*/
 *      First check that the variant and study exists making an update.
 *      For those who doesn't exist, pushes a study with the file and genotype information
 *
 *      The documents that throw a "dup key" exception are those variants that exist and have the study.
 *      Then, only for those variants, make a second update.
 *
 * *An interesting idea would be to invert this actions depending on the number of already inserted variants.
 *
 * @param loadedSampleIds Other loaded sampleIds EXCEPT those that are going to be loaded
 * @param data  Variants to insert
 */
QueryResult insert(List<Variant> data, int fileId, DBObjectToVariantConverter variantConverter,
        DBObjectToVariantSourceEntryConverter variantSourceEntryConverter,
        StudyConfiguration studyConfiguration, List<Integer> loadedSampleIds) {
    if (data.isEmpty()) {
        return new QueryResult("insertVariants");
    }
    List<DBObject> queries = new ArrayList<>(data.size());
    List<DBObject> updates = new ArrayList<>(data.size());
    Set<String> nonInsertedVariants;
    String fileIdStr = Integer.toString(fileId);

    {
        nonInsertedVariants = new HashSet<>();
        Map missingSamples = Collections.emptyMap();
        String defaultGenotype = studyConfiguration.getAttributes()
                .getString(MongoDBVariantStorageManager.DEFAULT_GENOTYPE, "");
        if (defaultGenotype.equals(DBObjectToSamplesConverter.UNKNOWN_GENOTYPE)) {
            logger.debug("Do not need fill gaps. DefaultGenotype is UNKNOWN_GENOTYPE({}).");
        } else if (!loadedSampleIds.isEmpty()) {
            missingSamples = new BasicDBObject(DBObjectToSamplesConverter.UNKNOWN_GENOTYPE, loadedSampleIds); // ?/?
        }
        for (Variant variant : data) {
            String id = variantConverter.buildStorageId(variant);
            for (VariantSourceEntry variantSourceEntry : variant.getSourceEntries().values()) {
                if (!variantSourceEntry.getFileId().equals(fileIdStr)) {
                    continue;
                }
                int studyId = studyConfiguration.getStudyId();
                DBObject study = variantSourceEntryConverter.convertToStorageType(variantSourceEntry);
                DBObject genotypes = (DBObject) study
                        .get(DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD);
                if (genotypes != null) { //If genotypes is null, genotypes are not suppose to be loaded
                    genotypes.putAll(missingSamples); //Add missing samples
                }
                DBObject push = new BasicDBObject(DBObjectToVariantConverter.STUDIES_FIELD, study);
                BasicDBObject update = new BasicDBObject().append("$push", push).append("$setOnInsert",
                        variantConverter.convertToStorageType(variant));
                if (variant.getIds() != null && !variant.getIds().isEmpty()
                        && !variant.getIds().iterator().next().isEmpty()) {
                    update.put("$addToSet", new BasicDBObject(DBObjectToVariantConverter.IDS_FIELD,
                            new BasicDBObject("$each", variant.getIds())));
                }
                // { _id: <variant_id>, "studies.sid": {$ne: <studyId> } }
                //If the variant exists and contains the study, this find will fail, will try to do the upsert, and throw a duplicated key exception.
                queries.add(new BasicDBObject("_id", id).append(
                        DBObjectToVariantConverter.STUDIES_FIELD + "."
                                + DBObjectToVariantSourceEntryConverter.STUDYID_FIELD,
                        new BasicDBObject("$ne", studyId)));
                updates.add(update);
            }
        }
        QueryOptions options = new QueryOptions("upsert", true);
        options.put("multi", false);
        try {
            variantsCollection.update(queries, updates, options);
        } catch (BulkWriteException e) {
            for (BulkWriteError writeError : e.getWriteErrors()) {
                if (writeError.getCode() == 11000) { //Dup Key error code
                    nonInsertedVariants.add(writeError.getMessage().split("dup key")[1].split("\"")[1]);
                } else {
                    throw e;
                }
            }
        }
        queries.clear();
        updates.clear();
    }

    for (Variant variant : data) {
        variant.setAnnotation(null);
        String id = variantConverter.buildStorageId(variant);

        if (nonInsertedVariants != null && !nonInsertedVariants.contains(id)) {
            continue; //Already inserted variant
        }

        for (VariantSourceEntry variantSourceEntry : variant.getSourceEntries().values()) {
            if (!variantSourceEntry.getFileId().equals(fileIdStr)) {
                continue;
            }

            DBObject studyObject = variantSourceEntryConverter.convertToStorageType(variantSourceEntry);
            DBObject genotypes = (DBObject) studyObject
                    .get(DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD);
            DBObject push = new BasicDBObject();
            if (genotypes != null) { //If genotypes is null, genotypes are not suppose to be loaded
                for (String genotype : genotypes.keySet()) {
                    push.put(
                            DBObjectToVariantConverter.STUDIES_FIELD + ".$."
                                    + DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD + "." + genotype,
                            new BasicDBObject("$each", genotypes.get(genotype)));
                }
            } else {
                push.put(
                        DBObjectToVariantConverter.STUDIES_FIELD + ".$."
                                + DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD,
                        Collections.emptyMap());
            }
            push.put(
                    DBObjectToVariantConverter.STUDIES_FIELD + ".$."
                            + DBObjectToVariantSourceEntryConverter.FILES_FIELD,
                    ((List) studyObject.get(DBObjectToVariantSourceEntryConverter.FILES_FIELD)).get(0));
            BasicDBObject update = new BasicDBObject(new BasicDBObject("$push", push));

            queries.add(new BasicDBObject("_id", id).append(
                    DBObjectToVariantConverter.STUDIES_FIELD + "."
                            + DBObjectToVariantSourceEntryConverter.STUDYID_FIELD,
                    studyConfiguration.getStudyId()));
            updates.add(update);

        }

    }
    if (queries.isEmpty()) {
        return new QueryResult();
    } else {
        QueryOptions options = new QueryOptions("upsert", false);
        options.put("multi", false);
        return variantsCollection.update(queries, updates, options);
    }
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

@Deprecated
private QueryBuilder parseQueryOptions(QueryOptions options, QueryBuilder builder) {
    if (options != null) {

        if (options.containsKey("sort")) {
            if (options.getBoolean("sort")) {
                options.put("sort", new BasicDBObject(DBObjectToVariantConverter.CHROMOSOME_FIELD, 1)
                        .append(DBObjectToVariantConverter.START_FIELD, 1));
            } else {
                options.remove("sort");
            }/*  www .  java2  s  .co m*/
        }

        /** GENOMIC REGION **/

        if (options.containsKey(VariantQueryParams.REGION.key())
                && !options.getString(VariantQueryParams.REGION.key()).isEmpty()) {
            List<String> stringList = options.getAsStringList(VariantQueryParams.REGION.key());
            List<Region> regions = new ArrayList<>(stringList.size());
            for (String reg : stringList) {
                Region region = Region.parseRegion(reg);
                regions.add(region);
            }
            getRegionFilter(regions, builder);
        }

        //            if (options.containsKey(VariantQueryParams.CHROMOSOME.key())) {
        //                List<String> chromosome = options.getAsStringList(VariantQueryParams.CHROMOSOME.key());
        //                addQueryListFilter(DBObjectToVariantConverter.CHROMOSOME_FIELD, chromosome, builder, QueryOperation.OR);
        //            }

        if (options.containsKey(VariantQueryParams.GENE.key())) {
            List<String> xrefs = options.getAsStringList(VariantQueryParams.GENE.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    xrefs, builder, QueryOperation.OR);
        }

        if (options.getString(VariantQueryParams.ID.key()) != null
                && !options.getString(VariantQueryParams.ID.key()).isEmpty()) { //) && !options.getString("id").isEmpty()) {
            List<String> ids = options.getAsStringList(VariantQueryParams.ID.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    ids, builder, QueryOperation.OR);
            addQueryListFilter(DBObjectToVariantConverter.IDS_FIELD, ids, builder, QueryOperation.OR);
        }

        /** VARIANT **/

        if (options.containsKey(VariantQueryParams.TYPE.key())) { // && !options.getString("type").isEmpty()) {
            addQueryStringFilter(DBObjectToVariantConverter.TYPE_FIELD,
                    options.getString(VariantQueryParams.TYPE.key()), builder);
        }

        if (options.containsKey(VariantQueryParams.REFERENCE.key())
                && options.getString(VariantQueryParams.REFERENCE.key()) != null) {
            addQueryStringFilter(DBObjectToVariantConverter.REFERENCE_FIELD,
                    options.getString(VariantQueryParams.REFERENCE.key()), builder);
        }

        if (options.containsKey(VariantQueryParams.ALTERNATE.key())
                && options.getString(VariantQueryParams.ALTERNATE.key()) != null) {
            addQueryStringFilter(DBObjectToVariantConverter.ALTERNATE_FIELD,
                    options.getString(VariantQueryParams.ALTERNATE.key()), builder);
        }

        /** ANNOTATION **/

        if (options.containsKey(VariantQueryParams.ANNOTATION_EXISTS.key())) {
            builder.and(DBObjectToVariantConverter.ANNOTATION_FIELD)
                    .exists(options.getBoolean(VariantQueryParams.ANNOTATION_EXISTS.key()));
        }

        if (options.containsKey(VariantQueryParams.ANNOT_XREF.key())) {
            List<String> xrefs = options.getAsStringList(VariantQueryParams.ANNOT_XREF.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREFS_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.XREF_ID_FIELD,
                    xrefs, builder, QueryOperation.AND);
        }

        if (options.containsKey(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key())) {
            List<String> cts = new ArrayList<>(
                    options.getAsStringList(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key()));
            List<Integer> ctsInteger = new ArrayList<>(cts.size());
            for (Iterator<String> iterator = cts.iterator(); iterator.hasNext();) {
                String ct = iterator.next();
                if (ct.startsWith("SO:")) {
                    ct = ct.substring(3);
                }
                try {
                    ctsInteger.add(Integer.parseInt(ct));
                } catch (NumberFormatException e) {
                    logger.error("Error parsing integer ", e);
                    iterator.remove(); //Remove the malformed query params.
                }
            }
            options.put(VariantQueryParams.ANNOT_CONSEQUENCE_TYPE.key(), cts); //Replace the QueryOption without the malformed query params
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SO_ACCESSION_FIELD,
                    ctsInteger, builder, QueryOperation.AND);
        }

        if (options.containsKey(VariantQueryParams.ANNOT_BIOTYPE.key())) {
            List<String> biotypes = options.getAsStringList(VariantQueryParams.ANNOT_BIOTYPE.key());
            addQueryListFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.BIOTYPE_FIELD,
                    biotypes, builder, QueryOperation.AND);
        }

        if (options.containsKey(VariantQueryParams.POLYPHEN.key())) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POLYPHEN_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD,
                    options.getString(VariantQueryParams.POLYPHEN.key()), builder);
        }

        if (options.containsKey(VariantQueryParams.SIFT.key())) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SIFT_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.SCORE_SCORE_FIELD,
                    options.getString(VariantQueryParams.SIFT.key()), builder);
        }

        if (options.containsKey(VariantQueryParams.PROTEIN_SUBSTITUTION.key())) {
            List<String> list = new ArrayList<>(
                    options.getAsStringList(VariantQueryParams.PROTEIN_SUBSTITUTION.key()));
            addScoreFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.CONSEQUENCE_TYPE_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.PROTEIN_SUBSTITUTION_SCORE_FIELD,
                    list, builder);
            options.put(VariantQueryParams.PROTEIN_SUBSTITUTION.key(), list); //Replace the QueryOption without the malformed query params
        }

        if (options.containsKey(VariantQueryParams.CONSERVATION.key())) {
            List<String> list = new ArrayList<>(options.getAsStringList(VariantQueryParams.CONSERVATION.key()));
            addScoreFilter(DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                    + DBObjectToVariantAnnotationConverter.CONSERVED_REGION_SCORE_FIELD, list, builder);
            options.put(VariantQueryParams.CONSERVATION.key(), list); //Replace the QueryOption without the malformed query params
        }

        if (options.containsKey(VariantQueryParams.ALTERNATE_FREQUENCY.key())) {
            List<String> list = new ArrayList<>(
                    options.getAsStringList(VariantQueryParams.ALTERNATE_FREQUENCY.key()));
            addFrequencyFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD, list,
                    builder); // Same method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field (reference/alternate) where to check the frequency
        }

        if (options.containsKey(VariantQueryParams.REFERENCE_FREQUENCY.key())) {
            List<String> list = new ArrayList<>(
                    options.getAsStringList(VariantQueryParams.REFERENCE_FREQUENCY.key()));
            addFrequencyFilter(
                    DBObjectToVariantConverter.ANNOTATION_FIELD + "."
                            + DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCIES_FIELD,
                    DBObjectToVariantAnnotationConverter.POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD, list,
                    builder); // Same method addFrequencyFilter is used for reference and allele frequencies. Need to provide the field (reference/alternate) where to check the frequency
        }

        /** STATS **/

        if (options.get(VariantQueryParams.STATS_MAF.key()) != null
                && !options.getString(VariantQueryParams.STATS_MAF.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MAF_FIELD,
                    options.getString(VariantQueryParams.STATS_MAF.key()), builder);
        }

        if (options.get(VariantQueryParams.STATS_MGF.key()) != null
                && !options.getString(VariantQueryParams.STATS_MGF.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.MGF_FIELD,
                    options.getString(VariantQueryParams.STATS_MGF.key()), builder);
        }

        if (options.get(VariantQueryParams.MISSING_ALLELES.key()) != null
                && !options.getString(VariantQueryParams.MISSING_ALLELES.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "."
                            + DBObjectToVariantStatsConverter.MISSALLELE_FIELD,
                    options.getString(VariantQueryParams.MISSING_ALLELES.key()), builder);
        }

        if (options.get(VariantQueryParams.MISSING_GENOTYPES.key()) != null
                && !options.getString(VariantQueryParams.MISSING_GENOTYPES.key()).isEmpty()) {
            addCompQueryFilter(
                    DBObjectToVariantConverter.STATS_FIELD + "."
                            + DBObjectToVariantStatsConverter.MISSGENOTYPE_FIELD,
                    options.getString(VariantQueryParams.MISSING_GENOTYPES.key()), builder);
        }

        if (options.get("numgt") != null && !options.getString("numgt").isEmpty()) {
            for (String numgt : options.getAsStringList("numgt")) {
                String[] split = numgt.split(":");
                addCompQueryFilter(
                        DBObjectToVariantConverter.STATS_FIELD + "."
                                + DBObjectToVariantStatsConverter.NUMGT_FIELD + "." + split[0],
                        split[1], builder);
            }
        }

        //            if (options.get("freqgt") != null && !options.getString("freqgt").isEmpty()) {
        //                for (String freqgt : getStringList(options.get("freqgt"))) {
        //                    String[] split = freqgt.split(":");
        //                    addCompQueryFilter(
        //                            DBObjectToVariantSourceEntryConverter.STATS_FIELD + "." + DBObjectToVariantStatsConverter.FREQGT_FIELD + "." + split[0],
        //                            split[1], builder);
        //                }
        //            }

        /** FILES **/
        QueryBuilder fileBuilder = QueryBuilder.start();

        if (options.containsKey(VariantQueryParams.STUDIES.key())) { // && !options.getList("studies").isEmpty() && !options.getListAs("studies", String.class).get(0).isEmpty()) {
            addQueryListFilter(DBObjectToVariantSourceEntryConverter.STUDYID_FIELD,
                    options.getAsIntegerList(VariantQueryParams.STUDIES.key()), fileBuilder,
                    QueryOperation.AND);
        }

        if (options.containsKey(VariantQueryParams.FILES.key())) { // && !options.getList("files").isEmpty() && !options.getListAs("files", String.class).get(0).isEmpty()) {
            addQueryListFilter(
                    DBObjectToVariantSourceEntryConverter.FILES_FIELD + "."
                            + DBObjectToVariantSourceEntryConverter.FILEID_FIELD,
                    options.getAsIntegerList(VariantQueryParams.FILES.key()), fileBuilder, QueryOperation.AND);
        }

        if (options.containsKey(VariantQueryParams.GENOTYPE.key())) {
            String sampleGenotypesCSV = options.getString(VariantQueryParams.GENOTYPE.key());

            //                String AND = ",";
            //                String OR = ";";
            //                String IS = ":";

            //                String AND = "AND";
            //                String OR = "OR";
            //                String IS = ":";

            String AND = ";";
            String OR = ",";
            String IS = ":";

            String[] sampleGenotypesArray = sampleGenotypesCSV.split(AND);
            for (String sampleGenotypes : sampleGenotypesArray) {
                String[] sampleGenotype = sampleGenotypes.split(IS);
                if (sampleGenotype.length != 2) {
                    continue;
                }
                int sample = Integer.parseInt(sampleGenotype[0]);
                String[] genotypes = sampleGenotype[1].split(OR);
                QueryBuilder genotypesBuilder = QueryBuilder.start();
                for (String genotype : genotypes) {
                    String s = DBObjectToVariantSourceEntryConverter.GENOTYPES_FIELD + "."
                            + DBObjectToSamplesConverter.genotypeToStorageType(genotype);
                    //or [ {"samp.0|0" : { $elemMatch : { $eq : <sampleId> } } } ]
                    genotypesBuilder.or(new BasicDBObject(s,
                            new BasicDBObject("$elemMatch", new BasicDBObject("$eq", sample))));
                }
                fileBuilder.and(genotypesBuilder.get());
            }
        }

        DBObject fileQuery = fileBuilder.get();
        if (fileQuery.keySet().size() != 0) {
            builder.and(DBObjectToVariantConverter.STUDIES_FIELD).elemMatch(fileQuery);
        }
    }

    logger.debug("Find = " + builder.get());
    return builder;
}

From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java

License:Apache License

@Override
@Deprecated//from   w  ww  .j a v a  2  s.co m
public QueryResult getVariantFrequencyByRegion(Region region, QueryOptions options) {
    // db.variants.aggregate( { $match: { $and: [ {chr: "1"}, {start: {$gt: 251391, $lt: 2701391}} ] }}, 
    //                        { $group: { _id: { $subtract: [ { $divide: ["$start", 20000] }, { $divide: [{$mod: ["$start", 20000]}, 20000] } ] }, 
    //                                  totalCount: {$sum: 1}}})

    if (options == null) {
        options = new QueryOptions();
    }

    int interval = options.getInt("interval", 20000);

    BasicDBObject start = new BasicDBObject("$gt", region.getStart());
    start.append("$lt", region.getEnd());

    BasicDBList andArr = new BasicDBList();
    andArr.add(new BasicDBObject(DBObjectToVariantConverter.CHROMOSOME_FIELD, region.getChromosome()));
    andArr.add(new BasicDBObject(DBObjectToVariantConverter.START_FIELD, start));

    // Parsing the rest of options
    QueryBuilder qb = new QueryBuilder();
    DBObject optionsMatch = parseQueryOptions(options, qb).get();
    if (!optionsMatch.keySet().isEmpty()) {
        andArr.add(optionsMatch);
    }
    DBObject match = new BasicDBObject("$match", new BasicDBObject("$and", andArr));

    //        qb.and("_at.chunkIds").in(chunkIds);
    //        qb.and(DBObjectToVariantConverter.END_FIELD).greaterThanEquals(region.getStart());
    //        qb.and(DBObjectToVariantConverter.START_FIELD).lessThanEquals(region.getEnd());
    //
    //        List<String> chunkIds = getChunkIds(region);
    //        DBObject regionObject = new BasicDBObject("_at.chunkIds", new BasicDBObject("$in", chunkIds))
    //                .append(DBObjectToVariantConverter.END_FIELD, new BasicDBObject("$gte", region.getStart()))
    //                .append(DBObjectToVariantConverter.START_FIELD, new BasicDBObject("$lte", region.getEnd()));

    BasicDBList divide1 = new BasicDBList();
    divide1.add("$start");
    divide1.add(interval);

    BasicDBList divide2 = new BasicDBList();
    divide2.add(new BasicDBObject("$mod", divide1));
    divide2.add(interval);

    BasicDBList subtractList = new BasicDBList();
    subtractList.add(new BasicDBObject("$divide", divide1));
    subtractList.add(new BasicDBObject("$divide", divide2));

    BasicDBObject substract = new BasicDBObject("$subtract", subtractList);

    DBObject totalCount = new BasicDBObject("$sum", 1);

    BasicDBObject g = new BasicDBObject("_id", substract);
    g.append("features_count", totalCount);
    DBObject group = new BasicDBObject("$group", g);

    DBObject sort = new BasicDBObject("$sort", new BasicDBObject("_id", 1));

    //        logger.info("getAllIntervalFrequencies - (>_)>");
    //        System.out.println(options.toString());
    //
    //        System.out.println(match.toString());
    //        System.out.println(group.toString());
    //        System.out.println(sort.toString());

    long dbTimeStart = System.currentTimeMillis();
    QueryResult output = variantsCollection.aggregate(/*"$histogram", */Arrays.asList(match, group, sort),
            options);
    long dbTimeEnd = System.currentTimeMillis();

    Map<Long, DBObject> ids = new HashMap<>();
    // Create DBObject for intervals with features inside them
    for (DBObject intervalObj : (List<DBObject>) output.getResult()) {
        Long _id = Math.round((Double) intervalObj.get("_id"));//is double

        DBObject intervalVisited = ids.get(_id);
        if (intervalVisited == null) {
            intervalObj.put("_id", _id);
            intervalObj.put("start", getChunkStart(_id.intValue(), interval));
            intervalObj.put("end", getChunkEnd(_id.intValue(), interval));
            intervalObj.put("chromosome", region.getChromosome());
            intervalObj.put("features_count", Math.log((int) intervalObj.get("features_count")));
            ids.put(_id, intervalObj);
        } else {
            Double sum = (Double) intervalVisited.get("features_count")
                    + Math.log((int) intervalObj.get("features_count"));
            intervalVisited.put("features_count", sum.intValue());
        }
    }

    // Create DBObject for intervals without features inside them
    BasicDBList resultList = new BasicDBList();
    int firstChunkId = getChunkId(region.getStart(), interval);
    int lastChunkId = getChunkId(region.getEnd(), interval);
    DBObject intervalObj;
    for (int chunkId = firstChunkId; chunkId <= lastChunkId; chunkId++) {
        intervalObj = ids.get((long) chunkId);
        if (intervalObj == null) {
            intervalObj = new BasicDBObject();
            intervalObj.put("_id", chunkId);
            intervalObj.put("start", getChunkStart(chunkId, interval));
            intervalObj.put("end", getChunkEnd(chunkId, interval));
            intervalObj.put("chromosome", region.getChromosome());
            intervalObj.put("features_count", 0);
        }
        resultList.add(intervalObj);
    }

    QueryResult queryResult = new QueryResult(region.toString(), ((Long) (dbTimeEnd - dbTimeStart)).intValue(),
            resultList.size(), resultList.size(), null, null, resultList);

    return queryResult;
}

From source file:org.openspotlight.storage.mongodb.MongoStorageSessionImpl.java

License:Open Source License

private StorageNode convertToNode(final Partition partition, final DBObject dbObject) throws Exception {
    final DBObject keyAsDbObj = (DBObject) dbObject.get(INDEXED);
    final List<String> keyNames = (List<String>) dbObject.get(KEY_NAMES);

    NodeKeyBuilder keyBuilder = new NodeKeyBuilderImpl((String) dbObject.get(NODE_TYPE), partition);

    for (final String s : keyAsDbObj.keySet()) {
        if (keyNames.contains(s)) {
            String valueAsString = convert(keyAsDbObj.get(s), String.class);
            if (NULL_VALUE.equals(valueAsString)) {
                valueAsString = null;/*from  ww  w. j  a v a2s.c o  m*/
            }
            keyBuilder.withSimpleKey(s, valueAsString);
        }
    }
    final String parentId = (String) dbObject.get(PARENT_ID);
    if (parentId != null) {
        keyBuilder.withParent(parentId);
    }
    final NodeKey uniqueKey = keyBuilder.andCreate();
    final StorageNode node = new StorageNodeImpl(uniqueKey, false);
    return node;
}

From source file:org.openspotlight.storage.mongodb.MongoStorageSessionImpl.java

License:Open Source License

@Override
public Set<Property> getProperties(final PropertyContainer element) throws Exception, IllegalStateException {
    checkNotNull("element", element);

    if (element instanceof StorageNode) {
        final StorageNode node = (StorageNode) element;
        final ImmutableSet.Builder<Property> builder = ImmutableSet.builder();
        for (final SimpleKey entry : node.getKey().getCompositeKey().getKeys()) {
            final PropertyImpl p = PropertyImpl.createKey(entry.getKeyName(), element);
            (p).setStringValueOnLoad(entry.getValue());
            builder.add(p);//from  w ww. j a  v  a  2  s  .  com
        }
        final DBObject reference = createNodeReference(node);
        final DBObject indexed = (DBObject) reference.get(INDEXED);
        final List<String> keyNames = (List<String>) reference.get(KEY_NAMES);
        if (indexed != null) {
            for (final String s : indexed.keySet()) {
                if (!keyNames.contains(s)) {
                    final PropertyImpl p = PropertyImpl.createIndexed(s, element);
                    String value = (String) indexed.get(s);
                    if (NULL_VALUE.equals(value)) {
                        value = null;
                    }
                    (p).setStringValueOnLoad(value);
                    builder.add(p);
                }
            }
        }

        final DBObject properties = (DBObject) reference.get(PROPERTIES);
        if (properties != null) {
            for (final String s : properties.keySet()) {
                final PropertyImpl p = PropertyImpl.createSimple(s, element);
                builder.add(p);
            }
        }

        return builder.build();

    } else if (element instanceof StorageLink) {
        final StorageLink linkEntry = (StorageLink) element;
        final ImmutableSet.Builder<Property> builder = ImmutableSet.builder();
        final DBObject reference = createLinkReference(linkEntry);
        final DBObject indexed = (DBObject) reference.get(INDEXED);
        if (indexed != null) {
            for (final String s : indexed.keySet()) {
                final PropertyImpl p = PropertyImpl.createIndexed(s, element);
                String value = (String) indexed.get(s);
                if (NULL_VALUE.equals(value)) {
                    value = null;
                }
                (p).setStringValueOnLoad(value);
                builder.add(p);
            }
        }

        final DBObject properties = (DBObject) reference.get(PROPERTIES);
        if (properties != null) {
            for (final String s : properties.keySet()) {
                final PropertyImpl p = PropertyImpl.createSimple(s, element);
                builder.add(p);
            }
        }

        return builder.build();
    } else {
        throw new IllegalStateException();
    }
}

From source file:org.opentaps.notes.repository.impl.NoteRepositoryImpl.java

License:Open Source License

private Note dbObjectToNote(DBObject noteDoc) {
    if (noteDoc == null) {
        return null;
    }/* w w w .ja  v a 2 s .  c  om*/

    Note note = factory.newInstance();
    note.setNoteId(noteDoc.get(NoteMongo.MONGO_ID_FIELD).toString());
    note.setNoteText((String) noteDoc.get(Note.Fields.noteText.getName()));
    if (noteDoc.containsField("createdByUserId")) {
        String userId = (String) noteDoc.get("createdByUserId");
        if (!GenericValidator.isBlankOrNull(userId)) {
            note.setCreatedByUser(new NoteUser(userId, (String) noteDoc.get("userIdType")));
        }
    } else if (noteDoc.containsField(Note.Fields.createdByUser.getName())) {
        BasicDBObject userDoc = (BasicDBObject) noteDoc.get(Note.Fields.createdByUser.getName());
        if (userDoc != null) {
            User user = new NoteUser();
            Set<Entry<String, Object>> entries = userDoc.entrySet();
            for (Entry<String, Object> entry : entries) {
                user.getProperties().put(entry.getKey(), entry.getValue());
            }
            note.setCreatedByUser(user);
        }
    }
    note.setClientDomain((String) noteDoc.get(Note.Fields.clientDomain.getName()));
    note.setSequenceNum((Long) noteDoc.get(Note.Fields.sequenceNum.getName()));
    Date dateTimeCreated = (Date) noteDoc.get(Note.Fields.dateTimeCreated.getName());
    if (dateTimeCreated != null) {
        note.setDateTimeCreated(new Timestamp(dateTimeCreated.getTime()));
    }
    // look for custom fields
    for (String field : noteDoc.keySet()) {
        if (!note.isBaseField(field)) {
            note.setAttribute(field, (String) noteDoc.get(field));
        }
    }

    return note;
}