db.py 15.3 KB
Newer Older
aknecht2's avatar
aknecht2 committed
1
from pymongo import MongoClient
2
import pymongo.errors
3
4
5
import gridfs
import sys
import traceback
6
import os
7
import itertools
8
import time
9
import chipathlon.conf
10
from pprint import pprint
11
import hashlib
aknecht2's avatar
aknecht2 committed
12

13

14
15
class MongoDB(object):

16
    def __init__(self, host, username, password, debug=False):
17
18
19
20
21
22
23
24
25
26
        """
        :param host: The host address of the MongoDB database.
        :type host: str
        :param username: The username of the account for the MongoDB database.
        :type username: str
        :param password: The password for the user.
        :type password: str
        :param debug: If true print out debug messages
        :type debug: bool
        """
27
        self.debug = debug
28
29
30
        self.host = host
        self.username = username
        self.password = password
31
32
        self.client = MongoClient(host)
        self.db = self.client.chipseq
33
        try:
34
            self.db.authenticate(username, password, mechanism="SCRAM-SHA-1")
35
36
37
38
39
40
41
        except:
            print("Could not authenticate to db %s!" % (host,))
            print traceback.format_exc()
            sys.exit(1)
        self.gfs = gridfs.GridFS(self.db)
        return

42
    def delete_result(self, result_id):
43
44
45
46
47
48
        """
        :param result_id: ID of result to delete
        :type result_id: ObjectId

        Deletes a result and it's corresponding gridfs entry.
        """
49
50
51
52
53
54
55
56
57
58
        cursor = self.db.results.find({
            "_id": result_id
        })
        if cursor.count() == 1:
            result = cursor.next()
            self.gfs.delete(result["gridfs_id"])
            self.db[result["result_type"]].delete_many({"result_id": result["_id"]})
            self.db.results.delete_one({"_id": result["_id"]})
        else:
            print "result_id %s doesn't exist." % (result_id,)
59
60
        return

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
    def _get_result_query(self, result, genome):
        query = {
            "result_type": result.file_type,
            "assembly": genome.assembly,
            "timestamp": {"$exists": True},
            "file_name": result.full_name
        }
        # In the case that there are 0 samples we just want to check for existence.
        control_sample_accessions = result.get_accessions("control")
        signal_sample_accessions = result.get_accessions("signal")
        query["control_sample_accessions"] = {"$all": control_sample_accessions} if (len(control_sample_accessions) > 0) else {"$exists": True}
        query["signal_sample_accessions"] = {"$all": signal_sample_accessions} if (len(signal_sample_accessions) > 0) else {"$exists": True}
        for job in result.all_jobs:
            job_args = job.get_db_arguments()
            arg_keys = job_args.keys()
            if len(arg_keys) == 0:
                query[job.job_name] = {"$exists": True}
            else:
                for arg_name in arg_keys:
                    query[job.job_name + "." + arg_name] = job_args[arg_name]
81
82
        if self.debug:
            print "Result query: %s" % (query,)
83
84
85
        return query

    def result_exists(self, result, genome):
86
87
88
89
90
91
92
93
        """
        :param result: The result to check.
        :type result: :py:meth:~chipathlon.result.Result
        :param genome: The genome to find information from.
        :type genome: :py:meth:~chipathlon.genome.Genome

        Check if a result exists.
        """
94
95
96
97
98
99
100
101
102
        try:
            cursor = self.db.results.find(self._get_result_query(result, genome))
            return cursor.count() > 0
        except pymongo.errors.OperationFailure as e:
            print "Error checking result [%s]: %s" % (file_name, e)
        return False


    def get_result(self, result, genome):
103
104
105
106
107
108
109
110
111
        """
        :param result: The result to check.
        :type result: :py:meth:~chipathlon.result.Result
        :param genome: The genome to find information from.
        :type genome: :py:meth:~chipathlon.genome.Genome

        Get the metadata for the result from the database.  If multiple results
        exist, the most recently saved result is returned.
        """
112
        try:
113
            cursor = self.db.results.find(self._get_result_query(result, genome))
114
115
            if cursor.count() > 0:
                return cursor.sort("timestamp", pymongo.DESCENDING).next()
116
        except pymongo.errors.OperationFailure as e:
117
            print "Error checking result [%s]: %s" % (file_name, e)
118
        return None
119

120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    def save_result(self, output_file, control_sample_accessions, signal_sample_accessions, result_type, additional_data = {}, gfs_attributes = {}):
        """
        :param output_file: The path to the result to save.
        :type output_file: str
        :param control_sample_accessions: A list of control accessions.
        :type control_sample_accessions: list
        :param signal_sample_accessions: A list of signal accessions.
        :type signal_sample_accessions: list
        :param result_type: Useful file type info
        :type result_type: str
        :param additional_data: Additional metadata to store in mongo.
        :type additional_data: dict
        :param gfs_attributes: Additional metadata to store in gridfs.
        :type gfs_attributes: dict

        Saves a result file into mongodb and also creates the corresponding
        gridfs file.
        """
138
139
        # Make sure output_file exists
        if os.path.isfile(output_file):
140
            # Make sure that all control_sample_accessions & signal_sample_accessions are valid
141
            # REMEMBER, these are ids for control & experiment SAMPLES
142
143
            valid_controls = [self.is_valid_sample(cid) for cid in control_sample_accessions]
            valid_experiments = [self.is_valid_sample(eid) for eid in signal_sample_accessions]
144
            if all(valid_controls) and all(valid_experiments):
145
                gfs_attributes["file_type"] = result_type
146
147
148
149
150
151
152
                # First, we load the output file into gfs
                with open(output_file, "r") as rh:
                    # Calling put returns the gfs id
                    gridfs_id = self.gfs.put(rh, filename=os.path.basename(output_file), **gfs_attributes)
                # Now, we create the actual result entry by combining all necessary info
                result_entry = {
                    "gridfs_id": gridfs_id,
153
154
                    "control_sample_accessions": control_sample_accessions,
                    "signal_sample_accessions": signal_sample_accessions,
155
156
157
                    "result_type": result_type,
                    "file_name": output_file,
                    "timestamp": time.time()
158
159
160
161
162
163
164
                }
                # Add additional attributes into the result_entry
                result_entry.update(additional_data)
                # Insert the entry into the database, and return the id
                result = self.db.results.insert_one(result_entry)
                return (True, "Result created successfully.", result.inserted_id)
            else:
165
                msg = "Not all input ids are valid.  The following are invalid: "
166
                for id_list, valid_list in zip([control_sample_accessions, signal_sample_accessions], [valid_controls, valid_experiments]):
167
                    msg += ", ".join([id_list[i] for i, valid in enumerate(valid_list) if not valid])
168
169
170
171
        else:
            msg = "Specified output_file %s does not exist." % (output_file,)
        return (False, msg, None)

172
    def is_valid_sample(self, sample_accession):
173
174
175
176
177
178
        """
        :param sample_accession: The accession number to check.
        :type sample_accession: str

        Ensures that a sample with the accession specified actually exists.
        """
179
180
181
182
183
184
185
186
187
188
        try:
            cursor = self.db.samples.find({
                "accession": sample_accession
            })
            if cursor.count() == 1:
                return True
        except pymongo.errors.OperationFailure as e:
            print "Error with sample_accession %s: %s" % (sample_accession, e)
        return False

189
190
191
192
193
194
195
    def is_valid_experiment(self, experiment_accession):
        """
        :param experiment_accession: The accession number to check.
        :type experiment_accession: str

        Ensures that an experiment with the accession specified actually exists.
        """
196
197
198
        try:
            cursor = self.db.experiments.find({
                "target": {"$exists": True},
199
                "@id": "/experiments/%s/" % (experiment_accession,)
200
201
202
203
            })
            if cursor.count() == 1:
                return True
        except pymongo.errors.OperationFailure as e:
204
            print "Error with experiment_accession %s: %s" % (experiment_accession, e)
205
206
        return False

Adam Caprez's avatar
Adam Caprez committed
207
    def fetch_from_gridfs(self, gridfs_id, filename, checkmd5=True):
208
209
210
211
212
        """
        :param gridfs_id: GridFS _id of file to get.
        :type gridfs_id: bson.objectid.ObjectId
        :param filename: Filename to save file to.
        :type filename: str
213
214
        :param checkmd5: Whether or not to validate the md5 of the result
        :type checkmd5: bool
215

216
217
218
        Fetch the file with the corresponding id and save it under the
        specified 'filename'.  If checkmd5 is specified, validate that the saved
        file has a correct md5 value.
219
        """
220
221
222
223
224
225
226
        try:
            gridfs_file = self.gfs.get(gridfs_id)
            gridfs_md5 = gridfs_file.md5
        except gridfs.errors.NoFile as e:
            print "Error fetching file from GridFS!\nNo file with ID '%s'" % (gridfs_id)
            print e
            sys.exit(1)
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242

        try:
            output_fh = open(filename,'wb')
        except IOError as e:
            print "Error creating GridFS output file '%s':" % (filename)
            print (e.errno,e.strerror)
            sys.exit(1)

        hash_md5 = hashlib.md5()
        for chunk in gridfs_file:
            output_fh.write(chunk)
            hash_md5.update(chunk)

        output_fh.close()
        gridfs_file.close()

Adam Caprez's avatar
Adam Caprez committed
243
244
245
246
247
248
        if checkmd5:
            if gridfs_md5 == hash_md5.hexdigest():
                return True
            else:
                print "MD5 mismatch saving file from GridFS to '%s'" % (filename)
                return False
249
        else:
Adam Caprez's avatar
Adam Caprez committed
250
            return True
251

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
    def get_sample(self, accession, file_type):
        """
        :param accession: The accession number of the target sample
        :type accession: string
        :param file_type: The file type of the target sample should be [fastq|bam]
        :type file_type: string

        Gets the associated sample based on accession number and file_type
        """
        valid = True
        msg = ""
        data = {}
        cursor = self.db.samples.find({
            "accession": accession,
            "file_type": file_type
        })
        if cursor.count() == 1:
            data = cursor.next()
        else:
            valid = False
            msg = "Found %s files with accession: %s, file_type: %s. Should only be 1." % (
                cursor.count(),
                accession,
                file_type
            )
        return (valid, msg, data)

279
280
281
282
283
284
285
286
287
288
289
    def get_samples(self, experiment_accession, file_type):
        """
        :param experiment_accession: Accession number of the experiment to grab samples from.
        :type experiment_accession: str
        :param file_type: File type of samples to grab usually fastq or bam
        :type file_type: str

        Validates and gets samples for the given experiment.  Experiments must
        have control and signal samples of the provided file_type to be
        considered valid.
        """
290
291
292
        valid = True
        msg = ""
        data = {}
293
        # First, check to make sure the target experiment is valid
294
        if self.is_valid_experiment(experiment_accession):
295
296
            # Next, we check that there is a least 1 possible control
            check3 = self.db.experiments.find({
297
                "target": {"$exists": True},
298
299
300
                "assembly.0": {"$exists": True},
                "assembly.1": {"$exists": False},
                "possible_controls.0": {"$exists": True},
301
                "@id": "/experiments/%s/" % (experiment_accession,)
302
            })
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
            if check3.count() == 1:
                # Complicated aggregtaion pipeline does the following steps:
                # 1. Find the experiment that matches the given id
                # 2. Join samples into the collection by exp_id
                # 3. Iterate through possible_controls
                # 4. Join possible_control data into control_exps
                # 5. Iterate through control_exps
                # 6. Join samples into the control_exps by exp_id
                # 7. Re-aggregate all data into arrays
                pipeline = [
                    {
                        "$match": {
                            "target": {"$exists": True},
                            "assembly.0": {"$exists": True},
                            "assembly.1": {"$exists": False},
                            "possible_controls.0": {"$exists": True},
319
                            "@id": "/experiments/%s/" % (experiment_accession,)
320
                        }
321
322
323
324
325
                    },
                    {
                        "$lookup": {
                            "from": "samples",
                            "localField": "uuid",
326
                            "foreignField": "experiment_accession",
327
                            "as": "samples"
328
                        }
329
330
331
332
333
334
335
336
                    },
                    {
                        "$unwind": "$possible_controls"
                    },
                    {
                        "$lookup": {
                            "from": "samples",
                            "localField": "possible_controls.uuid",
337
                            "foreignField": "experiment_accession",
338
339
340
341
342
343
344
345
346
347
348
349
350
351
                            "as": "possible_controls.samples"
                        }
                    },
                    {
                        "$group": {
                            "_id": "$_id",
                            "possible_controls": {"$push": "$possible_controls"},
                            "samples": {"$push": "$samples"}
                        }
                    }
                ]
                cursor = self.db.experiments.aggregate(pipeline)
                # We should have only 1 document
                document = cursor.next()
352
                control_inputs = [sample for control in document["possible_controls"] for sample in control["samples"] if ("file_type" in sample and sample["file_type"] == file_type)]
353
354
                signal_inputs = [sample for sample in document["samples"][0] if ("file_type" in sample and sample["file_type"] == file_type)]
                if (len(control_inputs) > 0 and len(signal_inputs) > 0):
355
                    msg = "Succesfully retrieved input files for experiment with id '%s'.\n" % (experiment_accession,)
356
357
                    data = {
                        "control": control_inputs,
358
                        "signal": signal_inputs
359
                    }
360
361
                else:
                    valid = False
362
                    msg = "Experiment with id '%s' has %s possible control inputs, and %s possible experiment inputs.\n" % (experiment_accession, len(control_inputs), len(experiment_inputs))
363
364
            else:
                valid = False
365
                msg = "Experiment with id '%s' does not have possible_controls.\n" % (experiment_accession,)
366
367
        else:
            valid = False
368
            msg = "Experiment with id '%s' is not valid!  It may not exist, or it may be missing required metadata.\n" % (experiment_accession,)
369
        return (valid, msg, data)