Commit 4ea8345f authored by aknecht2's avatar aknecht2
Browse files

Added examples. Created base db class with authentication>

parent 13235331
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
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()
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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment