List of usage examples for com.mongodb DBObject containsField
boolean containsField(String s);
From source file:org.oncoblocks.centromere.mongodb.ImportUtils.java
License:Apache License
/** * Serializes an object into a string format that can be inserted into a MongoDB collection. * /*from w w w . j av a2s. c om*/ * @param entity * @return */ public String convertEntityToJson(Object entity) { MongoConverter converter = mongoTemplate.getConverter(); DBObject dbObject = new BasicDBObject(); converter.write(entity, dbObject); if (dbObject.containsField("_id") && dbObject.get("_id") == null) { dbObject.removeField("_id"); } if (dbObject.containsField("_class")) { dbObject.removeField("_class"); } return dbObject.toString(); }
From source file:org.opencb.cellbase.mongodb.loader.MongoDBCellBaseLoader.java
License:Apache License
private void addChunkId(DBObject dbObject) { if (chunkSizes != null && chunkSizes.length > 0) { List<String> chunkIds = new ArrayList<>(); for (int chunkSize : chunkSizes) { int chunkStart = (Integer) dbObject.get("start") / chunkSize; int chunkEnd = (Integer) dbObject.get("end") / chunkSize; String chunkIdSuffix = chunkSize / 1000 + "k"; for (int i = chunkStart; i <= chunkEnd; i++) { if (dbObject.containsField("chromosome")) { chunkIds.add(dbObject.get("chromosome") + "_" + i + "_" + chunkIdSuffix); } else { chunkIds.add(dbObject.get("sequenceName") + "_" + i + "_" + chunkIdSuffix); }/* w ww .j a v a 2s . c o m*/ } } dbObject.put("_chunkIds", chunkIds); } }
From source file:org.opencb.opencga.storage.mongodb.alignment.DBObjectToMeanCoverageConverter.java
License:Apache License
@Override public MeanCoverage convertToDataModelType(DBObject dbObject) { String[] split = dbObject.get(CoverageMongoDBWriter.ID_FIELD).toString().split("_"); float averageFloat; Object average;//from w w w .jav a 2 s. co m if (dbObject.containsField(CoverageMongoDBWriter.FILES_FIELD)) { average = ((BasicDBObject) ((BasicDBList) dbObject.get(CoverageMongoDBWriter.FILES_FIELD)).get(0)) .get(CoverageMongoDBWriter.AVERAGE_FIELD); //coverage = ((Double) ((BasicDBObject) ((BasicDBList) dbObject.get(CoverageMongoWriter.FILES_FIELD)).get(0)).get(CoverageMongoWriter.AVERAGE_FIELD)).floatValue(); } else if (dbObject.containsField(CoverageMongoDBWriter.AVERAGE_FIELD)) { average = dbObject.get(CoverageMongoDBWriter.AVERAGE_FIELD); //coverage = ((Double) dbObject.get(CoverageMongoWriter.AVERAGE_FIELD)).floatValue(); } else { //TODO: Show a error message return null; } if (average instanceof Float) { averageFloat = (Float) average; } else if (average instanceof Double) { averageFloat = ((Double) average).floatValue(); } else if (average == null) { return null; } else { averageFloat = Float.parseFloat(average.toString()); } return new MeanCoverage(split[2], split[0], Integer.parseInt(split[1]), averageFloat); }
From source file:org.opencb.opencga.storage.mongodb.alignment.DBObjectToRegionCoverageConverter.java
License:Apache License
@Override public RegionCoverage convertToDataModelType(DBObject dbObject) { String[] split = dbObject.get(CoverageMongoDBWriter.ID_FIELD).toString().split("_"); RegionCoverage regionCoverage = new RegionCoverage(); BasicDBList coverageList;//from w ww .jav a 2s .c om if (dbObject.containsField(CoverageMongoDBWriter.FILES_FIELD)) { coverageList = (BasicDBList) ((BasicDBObject) ((BasicDBList) dbObject .get(CoverageMongoDBWriter.FILES_FIELD)).get(0)).get(CoverageMongoDBWriter.COVERAGE_FIELD); } else if (dbObject.containsField(CoverageMongoDBWriter.COVERAGE_FIELD)) { coverageList = (BasicDBList) dbObject.get(CoverageMongoDBWriter.FILES_FIELD); } else { //TODO: Show a error message return null; } short[] all = new short[coverageList.size()]; int i = 0; for (Object o : coverageList) { all[i++] = ((Integer) o).shortValue(); } // short[] all = (short[]) ((BasicDBObject) ((BasicDBList) dbObject.get(CoverageMongoWriter.FILES_FIELD)).get(0)).get(CoverageMongoWriter.COVERAGE_FIELD); //short[] all = (short[]) dbObject.get(CoverageMongoWriter.COVERAGE_FIELD); regionCoverage.setAll(all); regionCoverage.setChromosome(split[0]); regionCoverage.setStart(Integer.parseInt(split[1]) * all.length + 1); return regionCoverage; }
From source file:org.opencb.opencga.storage.mongodb.variant.DBObjectToVariantAnnotationConverter.java
License:Apache License
@Override public VariantAnnotation convertToDataModelType(DBObject object) { VariantAnnotation va = new VariantAnnotation(); //ConsequenceType List<ConsequenceType> consequenceTypes = new LinkedList<>(); Object cts = object.get(CONSEQUENCE_TYPE_FIELD); if (cts != null && cts instanceof BasicDBList) { for (Object o : ((BasicDBList) cts)) { if (o instanceof DBObject) { DBObject ct = (DBObject) o; //SO accession name List<String> soAccessionNames = new LinkedList<>(); if (ct.containsField(SO_ACCESSION_FIELD)) { if (ct.get(SO_ACCESSION_FIELD) instanceof List) { List<Integer> list = (List) ct.get(SO_ACCESSION_FIELD); for (Integer so : list) { soAccessionNames.add(ConsequenceTypeMappings.accessionToTerm.get(so)); }/*from w w w .ja v a 2 s . co m*/ } else { soAccessionNames .add(ConsequenceTypeMappings.accessionToTerm.get(ct.get(SO_ACCESSION_FIELD))); } } //ProteinSubstitutionScores List<Score> proteinSubstitutionScores = new LinkedList<>(); if (ct.containsField(PROTEIN_SUBSTITUTION_SCORE_FIELD)) { List<DBObject> list = (List) ct.get(PROTEIN_SUBSTITUTION_SCORE_FIELD); for (DBObject dbObject : list) { proteinSubstitutionScores.add(new Score(getDefault(dbObject, SCORE_SCORE_FIELD, 0.0), getDefault(dbObject, SCORE_SOURCE_FIELD, ""), getDefault(dbObject, SCORE_DESCRIPTION_FIELD, ""))); } } if (ct.containsField(POLYPHEN_FIELD)) { DBObject dbObject = (DBObject) ct.get(POLYPHEN_FIELD); proteinSubstitutionScores.add(new Score(getDefault(dbObject, SCORE_SCORE_FIELD, 0.0), "polyphen", getDefault(dbObject, SCORE_DESCRIPTION_FIELD, ""))); } if (ct.containsField(SIFT_FIELD)) { DBObject dbObject = (DBObject) ct.get(SIFT_FIELD); proteinSubstitutionScores.add(new Score(getDefault(dbObject, SCORE_SCORE_FIELD, 0.0), "sift", getDefault(dbObject, SCORE_DESCRIPTION_FIELD, ""))); } consequenceTypes.add(new ConsequenceType(getDefault(ct, GENE_NAME_FIELD, "") /*.toString()*/, getDefault(ct, ENSEMBL_GENE_ID_FIELD, "") /*.toString()*/, getDefault(ct, ENSEMBL_TRANSCRIPT_ID_FIELD, "") /*.toString()*/, getDefault(ct, STRAND_FIELD, "") /*.toString()*/, getDefault(ct, BIOTYPE_FIELD, "") /*.toString()*/, getDefault(ct, C_DNA_POSITION_FIELD, 0), getDefault(ct, CDS_POSITION_FIELD, 0), getDefault(ct, AA_POSITION_FIELD, 0), getDefault(ct, AA_CHANGE_FIELD, "") /*.toString() */, getDefault(ct, CODON_FIELD, "") /*.toString() */, proteinSubstitutionScores, soAccessionNames, Collections.<ExpressionValue>emptyList())); } } } va.setConsequenceTypes(consequenceTypes); //Conserved Region Scores List<Score> conservedRegionScores = new LinkedList<>(); if (object.containsField(CONSERVED_REGION_SCORE_FIELD)) { List<DBObject> list = (List) object.get(CONSERVED_REGION_SCORE_FIELD); for (DBObject dbObject : list) { conservedRegionScores.add(new Score(getDefault(dbObject, SCORE_SCORE_FIELD, 0.0), getDefault(dbObject, SCORE_SOURCE_FIELD, ""), getDefault(dbObject, SCORE_DESCRIPTION_FIELD, ""))); } } va.setConservationScores(conservedRegionScores); //Population frequencies List<PopulationFrequency> populationFrequencies = new LinkedList<>(); if (object.containsField(POPULATION_FREQUENCIES_FIELD)) { List<DBObject> list = (List) object.get(POPULATION_FREQUENCIES_FIELD); for (DBObject dbObject : list) { populationFrequencies.add(new PopulationFrequency( getDefault(dbObject, POPULATION_FREQUENCY_STUDY_FIELD, ""), getDefault(dbObject, POPULATION_FREQUENCY_POP_FIELD, ""), getDefault(dbObject, POPULATION_FREQUENCY_SUPERPOP_FIELD, ""), getDefault(dbObject, POPULATION_FREQUENCY_REFERENCE_ALLELE_FIELD, ""), getDefault(dbObject, POPULATION_FREQUENCY_ALTERNATE_ALLELE_FIELD, ""), (float) getDefault(dbObject, POPULATION_FREQUENCY_REFERENCE_FREQUENCY_FIELD, -1.0), (float) getDefault(dbObject, POPULATION_FREQUENCY_ALTERNATE_FREQUENCY_FIELD, -1.0))); } } va.setPopulationFrequencies(populationFrequencies); // Drug-Gene Interactions Map<String, List<Object>> drugGeneInteractionMap = new HashMap<>(); List<Object> drugs = new LinkedList<>(); if (object.containsField(DRUG_FIELD)) { List<DBObject> list = (List) object.get(DRUG_FIELD); for (DBObject dbObject : list) { drugs.add(dbObject.toMap()); } drugGeneInteractionMap.put("dgidb", drugs); } va.setGeneDrugInteraction(drugGeneInteractionMap); //XREfs List<Xref> xrefs = new LinkedList<>(); Object xrs = object.get(XREFS_FIELD); if (xrs != null && xrs instanceof BasicDBList) { for (Object o : (BasicDBList) xrs) { if (o instanceof DBObject) { DBObject xref = (DBObject) o; xrefs.add(new Xref((String) xref.get(XREF_ID_FIELD), (String) xref.get(XREF_SOURCE_FIELD))); } } } va.setXrefs(xrefs); //Clinical Data if (object.containsField(CLINICAL_DATA_FIELD)) { DBObject clinicalData = ((DBObject) object.get(CLINICAL_DATA_FIELD)); va.setClinical(clinicalData.toMap()); } return va; }
From source file:org.opencb.opencga.storage.mongodb.variant.DBObjectToVariantConverter.java
License:Apache License
@Override public Variant convertToDataModelType(DBObject object) { String chromosome = (String) object.get(CHROMOSOME_FIELD); int start = (int) object.get(START_FIELD); int end = (int) object.get(END_FIELD); String reference = (String) object.get(REFERENCE_FIELD); String alternate = (String) object.get(ALTERNATE_FIELD); Variant variant = new Variant(chromosome, start, end, reference, alternate); if (object.containsField(IDS_FIELD)) { Object ids = object.get(IDS_FIELD); variant.setIds(new HashSet<>(((Collection<String>) ids))); }//w ww . j av a2s. c om // Transform HGVS: List of map entries -> Map of lists BasicDBList mongoHgvs = (BasicDBList) object.get(HGVS_FIELD); if (mongoHgvs != null) { for (Object o : mongoHgvs) { DBObject dbo = (DBObject) o; variant.addHgvs((String) dbo.get(HGVS_TYPE_FIELD), (String) dbo.get(HGVS_NAME_FIELD)); } } // Files if (variantSourceEntryConverter != null) { BasicDBList mongoFiles = (BasicDBList) object.get(STUDIES_FIELD); if (mongoFiles != null) { for (Object o : mongoFiles) { DBObject dbo = (DBObject) o; variant.addSourceEntry(variantSourceEntryConverter.convertToDataModelType(dbo)); } } } // Annotations DBObject mongoAnnotation; Object o = object.get(ANNOTATION_FIELD); if (o instanceof List) { if (!((List) o).isEmpty()) { mongoAnnotation = (DBObject) ((List) o).get(0); } else { mongoAnnotation = null; } } else { mongoAnnotation = (DBObject) object.get(ANNOTATION_FIELD); } if (mongoAnnotation != null) { VariantAnnotation annotation = variantAnnotationConverter.convertToDataModelType(mongoAnnotation); annotation.setChromosome(variant.getChromosome()); annotation.setAlternateAllele(variant.getAlternate()); annotation.setReferenceAllele(variant.getReference()); annotation.setStart(variant.getStart()); variant.setAnnotation(annotation); } // Statistics if (statsConverter != null && object.containsField(STATS_FIELD)) { DBObject stats = (DBObject) object.get(STATS_FIELD); statsConverter.convertCohortsToDataModelType(stats, variant); } return variant; }
From source file:org.opencb.opencga.storage.mongodb.variant.DBObjectToVariantSourceConverter.java
License:Apache License
@Override public VariantSource convertToDataModelType(DBObject object) { VariantStudy.StudyType studyType = VariantStudy.StudyType .fromString(object.get(STUDYTYPE_FIELD).toString()); VariantSource source = new VariantSource((String) object.get(FILENAME_FIELD), (String) object.get(FILEID_FIELD), (String) object.get(STUDYID_FIELD), (String) object.get(STUDYNAME_FIELD), studyType, VariantSource.Aggregation.NONE); // Samples// w w w . j a va2s .co m if (object.containsField(SAMPLES_FIELD)) { Map<String, Integer> samplesPosition = new HashMap<>(); for (Map.Entry<String, Integer> entry : ((Map<String, Integer>) object.get(SAMPLES_FIELD)).entrySet()) { samplesPosition.put(entry.getKey().replace(CHARACTER_TO_REPLACE_DOTS, '.'), entry.getValue()); } source.setSamplesPosition(samplesPosition); } // Statistics DBObject statsObject = (DBObject) object.get(STATS_FIELD); if (statsObject != null) { VariantGlobalStats stats = new VariantGlobalStats((int) statsObject.get(NUMVARIANTS_FIELD), (int) statsObject.get(NUMSAMPLES_FIELD), (int) statsObject.get(NUMSNPS_FIELD), (int) statsObject.get(NUMINDELS_FIELD), 0, // TODO Add structural variants to schema! (int) statsObject.get(NUMPASSFILTERS_FIELD), (int) statsObject.get(NUMTRANSITIONS_FIELD), (int) statsObject.get(NUMTRANSVERSIONS_FIELD), -1, ((Double) statsObject.get(MEANQUALITY_FIELD)).floatValue(), null); // stats.setSamplesCount((int) statsObject.get(NUMSAMPLES_FIELD)); // stats.setVariantsCount((int) statsObject.get(NUMVARIANTS_FIELD)); // stats.setSnpsCount((int) statsObject.get(NUMSNPS_FIELD)); // stats.setIndelsCount((int) statsObject.get(NUMINDELS_FIELD)); // stats.setPassCount((int) statsObject.get(NUMPASSFILTERS_FIELD)); // stats.setTransitionsCount((int) statsObject.get(NUMTRANSITIONS_FIELD)); // stats.setTransversionsCount((int) statsObject.get(NUMTRANSVERSIONS_FIELD)); // stats.setMeanQuality(((Double) statsObject.get(MEANQUALITY_FIELD)).floatValue()); source.setStats(stats); } // Metadata BasicDBObject metadata = (BasicDBObject) object.get(METADATA_FIELD); for (Map.Entry<String, Object> o : metadata.entrySet()) { source.addMetadata(o.getKey().replace(CHARACTER_TO_REPLACE_DOTS, '.'), o.getValue()); } return source; }
From source file:org.opencb.opencga.storage.mongodb.variant.DBObjectToVariantSourceEntryConverter.java
License:Apache License
@Override public VariantSourceEntry convertToDataModelType(DBObject object) { int studyId = ((Number) object.get(STUDYID_FIELD)).intValue(); // String fileId = this.fileId == null? null : String.valueOf(this.fileId); String fileId = returnedFiles != null && returnedFiles.size() == 1 ? returnedFiles.iterator().next().toString() : null;/* www . j a v a2 s . com*/ VariantSourceEntry file = new VariantSourceEntry(fileId, getStudyName(studyId)); // String fileId = (String) object.get(FILEID_FIELD); DBObject fileObject = null; if (object.containsField(FILES_FIELD)) { for (DBObject dbObject : (List<DBObject>) object.get(FILES_FIELD)) { Integer fid = ((Integer) dbObject.get(FILEID_FIELD)); String fileId_ = fid.toString() + "_"; if (returnedFiles != null && !returnedFiles.contains(fid)) { continue; } fileObject = dbObject; // Attributes if (fileObject.containsField(ATTRIBUTES_FIELD)) { Map<String, Object> attrs = ((DBObject) fileObject.get(ATTRIBUTES_FIELD)).toMap(); for (Map.Entry<String, Object> entry : attrs.entrySet()) { // Unzip the "src" field, if available if (entry.getKey().equals("src")) { if (includeSrc) { byte[] o = (byte[]) entry.getValue(); try { file.addAttribute(fileId_ + entry.getKey(), org.opencb.commons.utils.StringUtils.gunzip(o)); } catch (IOException ex) { Logger.getLogger(DBObjectToVariantSourceEntryConverter.class.getName()) .log(Level.SEVERE, null, ex); } } } else { file.addAttribute( fileId_ + entry.getKey() .replace(DBObjectToStudyConfigurationConverter.TO_REPLACE_DOTS, "."), entry.getValue().toString()); } } } if (fileObject.containsField(ORI_FIELD)) { DBObject _ori = (DBObject) fileObject.get(ORI_FIELD); String ori = _ori.get("s") + ":" + _ori.get("i"); file.addAttribute(fileId_ + "ori", ori); } } } // Alternate alleles if (fileObject != null && fileObject.containsField(ALTERNATES_FIELD)) { List list = (List) fileObject.get(ALTERNATES_FIELD); String[] alternatives = new String[list.size()]; int i = 0; for (Object o : list) { alternatives[i] = o.toString(); i++; } file.setSecondaryAlternates(alternatives); } // if (fileObject != null && fileObject.containsField(FORMAT_FIELD)) { // file.setFormat((String) fileObject.get(FORMAT_FIELD)); // } else { file.setFormat("GT"); // } // Samples if (samplesConverter != null && object.containsField(GENOTYPES_FIELD)) { Map<String, Map<String, String>> samplesData = samplesConverter.convertToDataModelType(object, studyId); // Add the samples to the Java object, combining the data structures // with the samples' names and the genotypes for (Map.Entry<String, Map<String, String>> sampleData : samplesData.entrySet()) { file.addSampleData(sampleData.getKey(), sampleData.getValue()); } } return file; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
private DBObject createProjection(Query query, QueryOptions options) { DBObject projection = new BasicDBObject(); if (options == null) { options = new QueryOptions(); }/*from ww w.ja v a 2 s. c om*/ 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"); } } List<String> includeList = options.getAsStringList("include"); if (!includeList.isEmpty()) { //Include some for (String s : includeList) { String key = DBObjectToVariantConverter.toShortFieldName(s); if (key != null) { projection.put(key, 1); } else { logger.warn("Unknown include field: {}", s); } } } else { //Include all for (String values : DBObjectToVariantConverter.fieldsMap.values()) { projection.put(values, 1); } if (options.containsKey("exclude")) { // Exclude some List<String> excludeList = options.getAsStringList("exclude"); for (String s : excludeList) { String key = DBObjectToVariantConverter.toShortFieldName(s); if (key != null) { projection.removeField(key); } else { logger.warn("Unknown exclude field: {}", s); } } } } if (query.containsKey(VariantQueryParams.RETURNED_FILES.key()) && projection.containsField(DBObjectToVariantConverter.STUDIES_FIELD)) { List<Integer> files = query.getAsIntegerList(VariantQueryParams.RETURNED_FILES.key()); projection.put(DBObjectToVariantConverter.STUDIES_FIELD, new BasicDBObject("$elemMatch", new BasicDBObject( DBObjectToVariantSourceEntryConverter.FILES_FIELD + "." + DBObjectToVariantSourceEntryConverter.FILEID_FIELD, new BasicDBObject("$in", files)))); } if (query.containsKey(VariantQueryParams.RETURNED_STUDIES.key()) && projection.containsField(DBObjectToVariantConverter.STUDIES_FIELD)) { List<Integer> studiesIds = getStudyIds(query.getAsList(VariantQueryParams.RETURNED_STUDIES.key()), options); // List<Integer> studies = query.getAsIntegerList(VariantQueryParams.RETURNED_STUDIES.key()); if (!studiesIds.isEmpty()) { projection.put(DBObjectToVariantConverter.STUDIES_FIELD, new BasicDBObject("$elemMatch", new BasicDBObject(DBObjectToVariantSourceEntryConverter.STUDYID_FIELD, new BasicDBObject("$in", studiesIds)))); } } logger.debug("Projection: {}", projection); return projection; }
From source file:org.opencb.opencga.storage.mongodb.variant.VariantMongoDBAdaptor.java
License:Apache License
@Deprecated private DBObject parseProjectionQueryOptions(QueryOptions options) { DBObject projection = new BasicDBObject(); if (options == null) { return projection; }/* w w w.ja va 2s.c om*/ List<String> includeList = options.getAsStringList("include"); if (!includeList.isEmpty()) { //Include some for (String s : includeList) { String key = DBObjectToVariantConverter.toShortFieldName(s); if (key != null) { projection.put(key, 1); } else { logger.warn("Unknown include field: {}", s); } } } else { //Include all for (String values : DBObjectToVariantConverter.fieldsMap.values()) { projection.put(values, 1); } if (options.containsKey("exclude")) { // Exclude some List<String> excludeList = options.getAsStringList("exclude"); for (String s : excludeList) { String key = DBObjectToVariantConverter.toShortFieldName(s); if (key != null) { projection.removeField(key); } else { logger.warn("Unknown exclude field: {}", s); } } } } if (options.containsKey(VariantQueryParams.RETURNED_FILES.key()) && projection.containsField(DBObjectToVariantConverter.STUDIES_FIELD)) { // List<String> files = options.getListAs(FILES, String.class); int file = options.getInt(VariantQueryParams.RETURNED_FILES.key()); projection.put(DBObjectToVariantConverter.STUDIES_FIELD, new BasicDBObject("$elemMatch", new BasicDBObject(DBObjectToVariantSourceEntryConverter.FILES_FIELD + "." + DBObjectToVariantSourceEntryConverter.FILEID_FIELD, file // new BasicDBObject( // "$in", // files // ) ))); } logger.debug("Projection: {}", projection); return projection; }