diff --git a/chipathlon/db.py b/chipathlon/db.py index 76c2c8a29443c6ca5c73d795fd2fb0ae884b3f18..6f28b616d2ab6719a73fb906e0856d1b1b7a5cb2 100644 --- a/chipathlon/db.py +++ b/chipathlon/db.py @@ -1,3 +1,24 @@ from pymongo import MongoClient +import gridfs +import sys +import traceback -def setup +class MongoDB(object): + + def __init__(self, host, username, password): + self.client = MongoClient(args.host) + self.db = client.chipseq + try: + self.db.authenticate(args.username, args.password, mechanism="SCRAM-SHA-1") + except: + print("Could not authenticate to db %s!" % (host,)) + print traceback.format_exc() + sys.exit(1) + self.gfs = gridfs.GridFS(self.db) + return + + def load_bed(self, collection, result_id, bed_file, attributes = {}): + + return + + diff --git a/examples/gridfs_example.py b/examples/gridfs_example.py new file mode 100644 index 0000000000000000000000000000000000000000..5451ed060306940e4bd68a77f592396b024d9214 --- /dev/null +++ b/examples/gridfs_example.py @@ -0,0 +1,32 @@ +from pymongo import MongoClient +import argparse +from pprint import pprint +import gridfs + +parser = argparse.ArgumentParser(description = "Perform a join between the experiment and sample collections.") +parser.add_argument("--password", dest="password", required=True, help="Database user password.") +parser.add_argument("--username", dest="username", default="aknecht", required=True, help="Database user.") +parser.add_argument("--host", dest="host", default="hcc-anvil-241-41.unl.edu", required=True, help="Database host.") +args = parser.parse_args() + +# Everything in a db named chipseq +# Set up connection and authenticate + +client = MongoClient(args.host) +db = client.chipseq +db.authenticate(args.username, args.password, mechanism="SCRAM-SHA-1") + +gfs = gridfs.GridFS(db) + +# Put a file +with open("join_example.py", "r") as rh: + gfs.put(rh, filename = "foo", attribute1 = "asdf", attribute2 = "asdf") + +# Find files +cursor = gfs.find({ + "filename": "foo", + "attribute1": "asdf" +}) + +for document in cursor: + print document.read() diff --git a/examples/join_example.py b/examples/join_example.py new file mode 100644 index 0000000000000000000000000000000000000000..64c62d42b118fddb8240256c927f04b4f6e3cce0 --- /dev/null +++ b/examples/join_example.py @@ -0,0 +1,33 @@ +from pymongo import MongoClient +import argparse +from pprint import pprint + +parser = argparse.ArgumentParser(description = "Perform a join between the experiment and sample collections.") +parser.add_argument("--password", dest="password", required=True, help="Database user password.") +parser.add_argument("--username", dest="username", default="aknecht", required=True, help="Database user.") +parser.add_argument("--host", dest="host", default="hcc-anvil-241-41.unl.edu", required=True, help="Database host.") +args = parser.parse_args() + +# Everything in a db named chipseq +# Set up connection and authenticate + +client = MongoClient(args.host) +db = client.chipseq +db.authenticate(args.username, args.password, mechanism="SCRAM-SHA-1") + +cursor = db.experiments.aggregate([ + { + "$lookup": { + "from": "samples", + "localField": "_id", + "foreignField": "experiment_id", + "as": "samples" + } + } + ## Potentially more aggregation steps here. +]) + +for document in cursor: + if len(document["samples"]) > 0: + pprint(document) + break