Move MongoDB connection handling to a separate class

This commit is contained in:
Joel Thibault 2012-04-26 15:48:48 -04:00
parent db3cd1abd5
commit fd57d27f45
4 changed files with 88 additions and 72 deletions

View File

@ -8,7 +8,8 @@ package org.broadinstitute.sting.gatk.walkers;
* To change this template use File | Settings | File Templates.
*/
import com.mongodb.*;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import org.broad.tribble.Feature;
import org.broadinstitute.sting.commandline.Input;
import org.broadinstitute.sting.commandline.Output;
@ -17,7 +18,7 @@ import org.broadinstitute.sting.gatk.contexts.AlignmentContext;
import org.broadinstitute.sting.gatk.contexts.ReferenceContext;
import org.broadinstitute.sting.gatk.refdata.RefMetaDataTracker;
import org.broadinstitute.sting.utils.collections.Pair;
import org.broadinstitute.sting.utils.exceptions.StingException;
import org.broadinstitute.sting.utils.db.MongoDB;
import org.broadinstitute.sting.utils.variantcontext.VariantContext;
import java.io.File;
@ -35,55 +36,36 @@ public class InsertRODsWalker extends RodWalker<Integer, Integer> {
@Output
PrintStream out;
private final static String MONGO_HOST = "gsa4.broadinstitute.org";
private final static Integer MONGO_PORT = 43054;
private final static String MONGO_DB_NAME = "bjorn";
private final static String MONGO_ATTRIBUTES_COLLECTION = "attributes";
private final static String MONGO_SAMPLES_COLLECTION = "samples";
protected Mongo mongo;
protected DBCollection mongoAttributes;
protected DBCollection mongoSamples;
private String RODFileName;
@Override
public void initialize() {
try {
mongo = new Mongo(MONGO_HOST, MONGO_PORT);
DB mongoDb = mongo.getDB(MONGO_DB_NAME);
mongoAttributes = mongoDb.getCollection(MONGO_ATTRIBUTES_COLLECTION);
mongoSamples = mongoDb.getCollection(MONGO_SAMPLES_COLLECTION);
DBCollection mongoAttributes = MongoDB.getAttributesCollection();
DBCollection mongoSamples = MongoDB.getSamplesCollection();
RODFileName = input.getSource();
int lastSep = RODFileName.lastIndexOf(File.separator);
RODFileName = RODFileName.substring(lastSep + 1);
RODFileName = input.getSource();
int lastSep = RODFileName.lastIndexOf(File.separator);
RODFileName = RODFileName.substring(lastSep + 1);
// set up indices
// set up indices
mongoAttributes.ensureIndex("location");
mongoAttributes.ensureIndex("sourceROD");
mongoAttributes.ensureIndex("contig");
mongoAttributes.ensureIndex("start");
mongoAttributes.ensureIndex("stop");
mongoAttributes.ensureIndex("location");
mongoAttributes.ensureIndex("sourceROD");
mongoAttributes.ensureIndex("contig");
mongoAttributes.ensureIndex("start");
mongoAttributes.ensureIndex("stop");
mongoSamples.ensureIndex("location");
mongoSamples.ensureIndex("sample");
mongoSamples.ensureIndex("sourceROD");
mongoSamples.ensureIndex("contig");
mongoSamples.ensureIndex("start");
mongoSamples.ensureIndex("stop");
mongoSamples.ensureIndex("location");
mongoSamples.ensureIndex("sample");
mongoSamples.ensureIndex("sourceROD");
mongoSamples.ensureIndex("contig");
mongoSamples.ensureIndex("start");
mongoSamples.ensureIndex("stop");
// set up primary keys
mongoAttributes.ensureIndex(new BasicDBObject("location", 1).append("sourceROD", 1).append("alleles", 1), new BasicDBObject("unique", 1));
mongoSamples.ensureIndex(new BasicDBObject("location", 1).append("sourceROD", 1).append("alleles", 1).append("sample", 1), new BasicDBObject("unique", 1));
}
catch (MongoException e) {
throw e;
}
catch (java.net.UnknownHostException e) {
throw new StingException(e.getMessage(), e);
}
// set up primary keys
mongoAttributes.ensureIndex(new BasicDBObject("location", 1).append("sourceROD", 1).append("alleles", 1), new BasicDBObject("unique", 1));
mongoSamples.ensureIndex(new BasicDBObject("location", 1).append("sourceROD", 1).append("alleles", 1).append("sample", 1), new BasicDBObject("unique", 1));
}
/**
@ -104,6 +86,9 @@ public class InsertRODsWalker extends RodWalker<Integer, Integer> {
if ( tracker == null )
return 0;
DBCollection mongoAttributes = MongoDB.getAttributesCollection();
DBCollection mongoSamples = MongoDB.getSamplesCollection();
for ( Feature feature : tracker.getValues(Feature.class, context.getLocation()) ) {
if ( feature instanceof VariantContext ) {
VariantContext vc = (VariantContext) feature;
@ -131,6 +116,6 @@ public class InsertRODsWalker extends RodWalker<Integer, Integer> {
}
public void onTraversalDone(Integer result) {
mongo.close();
MongoDB.close();
}
}

View File

@ -24,7 +24,10 @@
package org.broadinstitute.sting.gatk.walkers.variantutils;
import com.mongodb.*;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import org.broadinstitute.sting.commandline.*;
import org.broadinstitute.sting.gatk.GenomeAnalysisEngine;
import org.broadinstitute.sting.gatk.arguments.StandardVariantContextInputArgumentCollection;
@ -39,7 +42,7 @@ import org.broadinstitute.sting.utils.MendelianViolation;
import org.broadinstitute.sting.utils.SampleUtils;
import org.broadinstitute.sting.utils.codecs.vcf.*;
import org.broadinstitute.sting.utils.collections.Pair;
import org.broadinstitute.sting.utils.exceptions.StingException;
import org.broadinstitute.sting.utils.db.MongoDB;
import org.broadinstitute.sting.utils.exceptions.UserException;
import org.broadinstitute.sting.utils.text.XReadLines;
import org.broadinstitute.sting.utils.variantcontext.*;
@ -349,16 +352,6 @@ public class SelectVariants extends RodWalker<Integer, Integer> implements TreeR
private Set<String> IDsToKeep = null;
private final static String MONGO_HOST = "gsa4.broadinstitute.org";
private final static Integer MONGO_PORT = 43054;
private final static String MONGO_DB_NAME = "bjorn";
private final static String MONGO_ATTRIBUTES_COLLECTION = "attributes";
private final static String MONGO_SAMPLES_COLLECTION = "samples";
protected Mongo mongo;
protected DBCollection mongoAttributes;
protected DBCollection mongoSamples;
/**
* Set up the VCF writer, the sample expressions and regexs, and the JEXL matcher
*/
@ -458,19 +451,6 @@ public class SelectVariants extends RodWalker<Integer, Integer> implements TreeR
throw new UserException.CouldNotReadInputFile(rsIDFile, e);
}
}
try {
mongo = new Mongo(MONGO_HOST, MONGO_PORT);
DB mongoDb = mongo.getDB(MONGO_DB_NAME);
mongoAttributes = mongoDb.getCollection(MONGO_ATTRIBUTES_COLLECTION);
mongoSamples = mongoDb.getCollection(MONGO_SAMPLES_COLLECTION);
}
catch (MongoException e) {
throw e;
}
catch (java.net.UnknownHostException e) {
throw new StingException(e.getMessage(), e);
}
}
/**
@ -571,8 +551,8 @@ public class SelectVariants extends RodWalker<Integer, Integer> implements TreeR
query.put("start", start);
// can't know stop location for deletions from reference
DBCursor attributesCursor = mongoAttributes.find(query);
DBCursor samplesCursor = mongoSamples.find(query);
DBCursor attributesCursor = MongoDB.getAttributesCollection().find(query);
DBCursor samplesCursor = MongoDB.getSamplesCollection().find(query);
Map<Pair<String,List<Allele>>,VariantContextBuilder> attributesFromDB = new HashMap<Pair<String,List<Allele>>,VariantContextBuilder>();
@ -841,7 +821,8 @@ public class SelectVariants extends RodWalker<Integer, Integer> implements TreeR
}
public void onTraversalDone(Integer result) {
mongo.close();
MongoDB.close();
logger.info(result + " records processed.");
if (SELECT_RANDOM_NUMBER) {

View File

@ -0,0 +1,52 @@
package org.broadinstitute.sting.utils.db;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import org.broadinstitute.sting.utils.exceptions.StingException;
import java.net.UnknownHostException;
/**
* Created with IntelliJ IDEA.
* User: thibault
* Date: 4/26/12
* Time: 3:01 PM
* Handles Mongo DB connections
*/
final public class MongoDB {
private final static String MONGO_HOST = "gsa4.broadinstitute.org";
private final static Integer MONGO_PORT = 43054;
private final static String MONGO_DB_NAME = "bjorn";
private final static String MONGO_ATTRIBUTES_COLLECTION = "attributes";
private final static String MONGO_SAMPLES_COLLECTION = "samples";
protected Mongo mongo;
protected DBCollection mongoAttributes;
protected DBCollection mongoSamples;
final private static MongoDB INSTANCE = new MongoDB();
public static DBCollection getAttributesCollection() {
return INSTANCE.mongoAttributes;
}
public static DBCollection getSamplesCollection() {
return INSTANCE.mongoSamples;
}
public static void close() {
INSTANCE.mongo.close();
}
private MongoDB() {
try {
mongo = new Mongo(MONGO_HOST, MONGO_PORT);
DB mongoDb = mongo.getDB(MONGO_DB_NAME);
mongoAttributes = mongoDb.getCollection(MONGO_ATTRIBUTES_COLLECTION);
mongoSamples = mongoDb.getCollection(MONGO_SAMPLES_COLLECTION);
} catch (UnknownHostException e) {
throw new StingException(e.getMessage(), e);
}
}
}

View File

@ -1,11 +1,9 @@
package org.broadinstitute.sting.utils.variantcontext;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import org.broad.tribble.Feature;
import org.broad.tribble.TribbleException;
import org.broad.tribble.util.ParsingUtils;
import org.broadinstitute.sting.utils.GenomeLoc;
import org.broadinstitute.sting.utils.codecs.vcf.VCFConstants;
import org.broadinstitute.sting.utils.collections.Pair;
import org.broadinstitute.sting.utils.exceptions.ReviewedStingException;