This repository has been archived by the owner on Apr 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
dbtransformgeneration.py
86 lines (67 loc) · 2.07 KB
/
dbtransformgeneration.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
'''
@author: Andrea Cadeddu, [email protected]
'''
import pymongo
class CaseListGen(object):
'''
general db crawler class
'''
def __init__(self):
'''
Constructor
'''
#connect to localhost
self.dbr = pymongo.MongoClient().datatest
#self.dbr = pymongo.MongoClient().data
def loadReactionsIDs(self, filename):
f=open(filename, "r")
self.ids=[]
for lines in f:
line =lines.split()
self.ids.append(int(line[0]))
def genRetroEntries(self):
'''
this generate a list which contains ALL the db entry;
'''
cursor = self.dbr.retro.find()
self.retroentries = [a for a in cursor]
cursor.close()
def getRetroEntries(self):
if not self.retroentries:
self.genRetroEntries()
return self.retroentries
def genReactionList(self):
cursor = self.dbr.retro.find()
self.reactionlist=[a['rxn'] for a in cursor if a["rxn"] is not None]
cursor.close()
def getReactionList(self):
if not self.reactionlist:
self.genReactionlist()
return self.reactionlist
def rmRetroEntriesMatchingID(self):
self.retroentries=[entry for entry in self.retroentries if entry["_id"] not in self.ids]
def rmRetroEntriesNotMatchingID(self):
self.retroentries=[entry for entry in self.retroentries if entry["_id"] in self.ids]
def output(self, fname):
'''
'''
f=open(fname,"w")
for each in self.retroentries:
eid=each["_id"]
ename= each["name"]
try:
etrans=each["reaction_smarts"]
except:
etrans=""
printline=str(eid)+"\t"+ename + "\t" + etrans + "\n"
try:
f.write(printline)
except:
print(""+str(eid)+"failed, probably weird chars present")
if __name__ == "__main__":
# Testing
cgl=CaseListGen()
cgl.loadReactionsIDs("cycliz.txt")
cgl.genRetroEntries()
cgl.rmRetroEntriesNotMatchingID()
cgl.output("reactions.txt")