Java tutorial
/* * Copyright 2015 OpenCB * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.opencb.cellbase.lib.impl; import com.mongodb.client.model.Filters; import org.bson.Document; import org.bson.conversions.Bson; import org.opencb.biodata.models.core.Region; import org.opencb.biodata.models.core.RegulatoryFeature; import org.opencb.cellbase.core.api.RegulationDBAdaptor; import org.opencb.cellbase.lib.MongoDBCollectionConfiguration; import org.opencb.commons.datastore.core.Query; import org.opencb.commons.datastore.core.QueryOptions; import org.opencb.commons.datastore.core.QueryResult; import org.opencb.commons.datastore.mongodb.MongoDataStore; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.function.Consumer; /** * Created by imedina on 07/12/15. */ public class RegulationMongoDBAdaptor extends MongoDBAdaptor implements RegulationDBAdaptor<RegulatoryFeature> { public RegulationMongoDBAdaptor(String species, String assembly, MongoDataStore mongoDataStore) { super(species, assembly, mongoDataStore); mongoDBCollection = mongoDataStore.getCollection("regulatory_region"); logger.debug("RegulationMongoDBAdaptor: in 'constructor'"); } @Override public QueryResult<RegulatoryFeature> next(Query query, QueryOptions options) { return null; } @Override public QueryResult nativeNext(Query query, QueryOptions options) { return null; } @Override public QueryResult rank(Query query, String field, int numResults, boolean asc) { return null; } @Override public QueryResult groupBy(Query query, String field, QueryOptions options) { Bson bsonQuery = parseQuery(query); return groupBy(bsonQuery, field, "name", options); } @Override public QueryResult groupBy(Query query, List<String> fields, QueryOptions options) { Bson bsonQuery = parseQuery(query); return groupBy(bsonQuery, fields, "name", options); } @Override public QueryResult getIntervalFrequencies(Query query, int intervalSize, QueryOptions options) { if (query.getString(QueryParams.REGION.key()) != null) { Region region = Region.parseRegion(query.getString(QueryParams.REGION.key())); Bson bsonDocument = parseQuery(query); return getIntervalFrequencies(bsonDocument, region, intervalSize, options); } return null; } @Override public QueryResult<Long> update(List objectList, String field, String[] innerFields) { return null; } @Override public QueryResult<Long> count(Query query) { Bson bsonDocument = parseQuery(query); return mongoDBCollection.count(bsonDocument); } @Override public QueryResult distinct(Query query, String field) { Bson bsonDocument = parseQuery(query); return mongoDBCollection.distinct(field, bsonDocument); } @Override public QueryResult stats(Query query) { return null; } @Override public QueryResult<RegulatoryFeature> get(Query query, QueryOptions options) { Bson bson = parseQuery(query); options = addPrivateExcludeOptions(options); return mongoDBCollection.find(bson, null, RegulatoryFeature.class, options); } @Override public QueryResult nativeGet(Query query, QueryOptions options) { Bson bson = parseQuery(query); return mongoDBCollection.find(bson, options); } @Override public Iterator<RegulatoryFeature> iterator(Query query, QueryOptions options) { return null; } @Override public Iterator nativeIterator(Query query, QueryOptions options) { Bson bson = parseQuery(query); return mongoDBCollection.nativeQuery().find(bson, options).iterator(); } @Override public void forEach(Query query, Consumer<? super Object> action, QueryOptions options) { } private Bson parseQuery(Query query) { List<Bson> andBsonList = new ArrayList<>(); createRegionQuery(query, QueryParams.REGION.key(), MongoDBCollectionConfiguration.REGULATORY_REGION_CHUNK_SIZE, andBsonList); createOrQuery(query, QueryParams.NAME.key(), "name", andBsonList); createOrQuery(query, QueryParams.FEATURE_TYPE.key(), "featureType", andBsonList); createOrQuery(query, QueryParams.FEATURE_CLASS.key(), "featureClass", andBsonList); createOrQuery(query, QueryParams.CELL_TYPES.key(), "cellTypes", andBsonList); createOrQuery(query, QueryParams.SCORE.key(), "score", andBsonList); if (andBsonList.size() > 0) { return Filters.and(andBsonList); } else { return new Document(); } } }