14
14
from pymongo import MongoClient
15
15
16
16
17
- class Config :
18
- def __init__ (self , genome = None ):
17
+ class Config :
18
+ def __init__ (self , genome = None ):
19
19
# get the credentials
20
20
self .config = getCredentials ()
21
21
# the root directory is 2 levels up, get its dirname
22
22
self .ROOT_PATH = (os .path .dirname (os .path .dirname (os .path .dirname (os .path .abspath (__file__ )))))
23
23
# set all the paths defined in ROOT/config/paths.conf
24
- self .setPaths ()
24
+ self .setPaths ()
25
25
self .PRIMER3_SETTINGS = os .path .join (self .ROOT_PATH , 'config/primer3settings.conf' )
26
26
# where the primer3 in and output files are written
27
27
self .PRIMER3_DIR = os .path .join (self .ROOT_PATH , 'src/primer-design/files/primer3Files' )
28
-
28
+
29
29
if genome :
30
30
# connect to the mongoDB
31
- self .genome = genome
31
+ self .genome = genome
32
32
mongoDB , release , curr_geneCollection , guideCollection , primerCollection , metadataCollection = self .getAttributes ()
33
33
self .mongoDB = mongoDB
34
34
self .release = release
35
35
self .curr_geneCollection = curr_geneCollection
36
36
self .guideCollection = guideCollection
37
37
self .primerCollection = primerCollection
38
- self .metadataCollection = metadataCollection
38
+ self .metadataCollection = metadataCollection
39
39
self .organismName = self .getOrgName ()
40
40
41
41
@@ -48,24 +48,13 @@ def setPaths(self):
48
48
self .PRIMER3 = line .split ('=' )[1 ].strip ()
49
49
elif re .match (r"^PRIMER3_CONFIG=" , line ):
50
50
self .PRIMER3_CONFIG = line .split ('=' )[1 ].strip ()
51
- elif re .match (r"DICEY_EXEC=" , line ):
52
- self .DICEY = line .split ('=' )[1 ].strip ()
53
- elif re .match (r"DICEY_PATH=" , line ):
54
- self .DICEY_PATH = line .split ('=' )[1 ].strip ()
55
51
56
52
if not self .BLAST :
57
53
print ("Error: path to BLAST executable not defined in paths.conf" )
58
54
elif not self .PRIMER3 :
59
55
print ("Error: path to primer3 executable not defined in paths.conf" )
60
56
elif not self .PRIMER3_CONFIG :
61
57
print ("Error: path to primer3 config directory not defined in paths.conf" )
62
- elif not self .DICEY :
63
- print ("Error: path to dicey executable not defined in paths.conf" )
64
- elif not self .DICEY_PATH :
65
- print ("Error: path to dicey program folder not defined in paths.conf" )
66
-
67
- return
68
-
69
58
70
59
def getAttributes (self ):
71
60
if self .config :
@@ -74,10 +63,10 @@ def getAttributes(self):
74
63
else :
75
64
# otherwise, attempt connecting without credentials
76
65
client = MongoClient ()
77
-
66
+
78
67
db = client [self .genome ]
79
68
collections = db .collection_names ()
80
-
69
+
81
70
# get the release we're using from the most recent geneInfo collection
82
71
curr_release = fetchCurrentRelease (self .genome , collections )
83
72
@@ -86,23 +75,23 @@ def getAttributes(self):
86
75
nameGuideCol = "gRNAResultCollection"
87
76
namePrimerCol = "primerCollection"
88
77
metadataCol = "metadata"
89
-
78
+
90
79
return db , curr_release , db [nameGeneCol ], db [nameGuideCol ], db [namePrimerCol ], db [metadataCol ]
91
80
92
81
93
82
def getOrgName (self ):
94
- orgName = ''
83
+ orgName = ''
95
84
metadataRecords = self .metadataCollection .find ({})
96
85
if metadataRecords .count () == 1 :
97
- for record in metadataRecords :
98
- orgName = str (record ['org_name' ])
86
+ for record in metadataRecords :
87
+ orgName = str (record ['org_name' ])
99
88
else :
100
89
print ("Error: metadata collection for " + str (self .genome ) + " is misconfigured" )
101
-
90
+
102
91
return orgName
103
-
104
92
105
- @staticmethod
93
+
94
+ @staticmethod
106
95
def fetchStrandofGene (ensid , genome ):
107
96
curr_geneCollection = getCurrentGeneCollection (genome )
108
97
record = curr_geneCollection .find_one ({'ENSID' : ensid })
@@ -119,40 +108,40 @@ def getCredentials():
119
108
except Exception , e :
120
109
# this is ok it just means there are no credentials
121
110
return
122
- for line in config_file :
111
+ for line in config_file :
123
112
var , value = line .rstrip ().split ("=" )
124
113
if var == "password" :
125
114
# passwords need to have special characters escaped
126
115
config [var ] = urllib .quote (value )
127
116
else :
128
117
config [var ] = value
129
-
130
- return config
118
+
119
+ return config
131
120
132
121
133
122
def getCurrentGeneCollection (genome ):
134
123
# get credentials and connect to mongodb
135
- config = getCredentials ()
124
+ config = getCredentials ()
136
125
if config :
137
126
client = MongoClient ('mongodb://%s:%s@localhost' % (config ['username' ], config ['password' ]))
138
127
else :
139
128
# if no credentials defined
140
129
client = MongoClient ()
141
-
130
+
142
131
db = client [genome ]
143
132
collections = db .collection_names ()
144
-
133
+
145
134
# get the release we're using from the most recent geneInfo collection
146
135
curr_release = fetchCurrentRelease (genome , collections )
147
136
148
137
# determine the collection name
149
138
nameGeneCol = "geneInfo_" + str (curr_release )
150
-
139
+
151
140
return db [nameGeneCol ]
152
141
153
142
154
143
def fetchCurrentRelease (genome , collections = None ):
155
-
144
+
156
145
# optionally, if function calling already has list of connections, they can be passed
157
146
# otherwise, connect to mongodb and get them
158
147
if not collections :
@@ -171,7 +160,7 @@ def fetchCurrentRelease(genome, collections=None):
171
160
cltn_release = c .split ("_" ,1 )[1 ]
172
161
if int (cltn_release ) > int (curr_release ):
173
162
curr_release = cltn_release
174
-
163
+
175
164
return curr_release
176
165
177
166
@@ -183,11 +172,11 @@ def fetchInstalledGenomes():
183
172
else :
184
173
client = MongoClient ()
185
174
186
- genomes = []
175
+ genomes = []
187
176
dbs = sorted (client .database_names (), key = lambda v : v .upper ())
188
177
default_dbs = ['admin' , 'local' ,'config' ]
189
178
for db in dbs :
190
- if db not in default_dbs :
179
+ if db not in default_dbs :
191
180
metadataCollection = client [db ]['metadata' ]
192
181
metadataRecords = metadataCollection .find ({})
193
182
if metadataRecords .count () == 1 :
@@ -196,7 +185,7 @@ def fetchInstalledGenomes():
196
185
genomes .append ((db , orgName )) # store org code and name in list as tuple
197
186
else :
198
187
print ("Error: metadata collection for " + str (db ) + " is misconfigured" )
199
- return genomes
188
+ return genomes
200
189
201
190
202
191
def main ():
@@ -208,11 +197,11 @@ def main():
208
197
print "Databases currently available:"
209
198
genomes = fetchInstalledGenomes ()
210
199
for g in genomes :
211
- orgName = g [1 ]
200
+ orgName = g [1 ]
212
201
genomeString = g [0 ] + " (" + orgName + ")"
213
202
print genomeString
214
-
215
-
203
+
204
+
216
205
if __name__ == "__main__" :
217
206
main ()
218
-
207
+
0 commit comments