00001
00002
00003
00004 import types
00005
00006 import Base, Tags
00007
00008 import Reaction, Regulation, Transunit, Regulon, Enzrxn, Compound, Protein, Gene, Pathway
00009 import Promoter
00010 BasicModules = [
00011 Reaction,
00012 Enzrxn,
00013 Compound,
00014 Protein,
00015 Gene,
00016 Pathway
00017 ]
00018
00019 ExtendedModules = [
00020
00021
00022 Regulation,
00023 Promoter,
00024 Transunit,
00025 Regulon
00026 ]
00027
00028
00029
00030 import Transporter
00031
00032 try:
00033 import ScrumPy
00034 ScrumPy.Init(Batch=1)
00035 except:
00036 pass
00037
00038 key, assoc = "key", "assoc"
00039 exact, sub, super = "exact", "sub", "super"
00040
00041 def _IsSuper(s1, s2):
00042 return s1.find(s2) != -1
00043
00044 def _IsSub(s1, s2):
00045 return s2.find(s1) != -1
00046
00047
00048 def Pass(*foo,**bar):
00049 pass
00050
00051 def Rep(a,b):
00052 print a, b
00053
00054 class Organism:
00055 def __init__(self, path=Base.DefaultPath,data="MetaCyc",ModDBRep=Rep,DBRecRep=Pass, UseExtended=False):
00056 self.data=data
00057 self.path=path
00058 m = self.Missing = {}
00059 self.DBs = [m]
00060 self.DBdic = {"Missing":m}
00061 self.Assocs = {}
00062 self.AssocXref = {}
00063
00064 Modules=BasicModules
00065 if UseExtended:
00066 Modules += ExtendedModules
00067
00068 for Mod in Modules:
00069 dbname = Mod.__name__.split(".")[-1:][0]
00070
00071 db = Mod.DB(path=path+data,Org=self,RecRep=DBRecRep)
00072 setattr(self, dbname, db)
00073 self.DBs.append(db)
00074 self.DBdic[dbname] =db
00075 ModDBRep(1, dbname)
00076
00077 if '# MetaCyc\n' in self.Reaction.Comments:
00078 ExtraReacs = Reaction.DB(path=path, file = "ExtraReacs.dat")
00079 self.DBs.append(ExtraReacs)
00080 self.DBdic["ExtraReacs"] = ExtraReacs
00081
00082
00083
00084
00085
00086 self.AssocXref = {}
00087
00088
00089 def __getitem__(self,k):
00090 for db in self.DBs:
00091 if db.has_key(k):
00092 return db[k]
00093 return []
00094
00095
00096 def has_key(self,k):
00097 for db in self.DBs:
00098 if db.has_key(k):
00099 return True
00100 return False
00101
00102 def keys(self):
00103 rv = []
00104 for db in self.DBs:
00105 rv += db.keys()
00106 return rv
00107
00108
00109
00110 def AddAssoc(self, keys, record):
00111 uid = record.UID
00112 for key in keys:
00113 ku = key+uid
00114 if not self.AssocXref.has_key(ku):
00115 self.AssocXref[ku] = 1
00116 if self.Assocs.has_key(key):
00117 self.Assocs[key].append(record)
00118 else:
00119 self.Assocs[key] = [record]
00120
00121
00122 def StrSearch(self, targ="", stype=key, opt=exact):
00123
00124 rv = []
00125 if stype==assoc:
00126 targ = targ.upper()
00127
00128 dic = {
00129 key:self,
00130 assoc:self.Assocs
00131 }[stype]
00132
00133 if opt == exact:
00134 if stype ==key:
00135 rv = [dic[targ]]
00136 else:
00137 rv += dic[targ]
00138 else:
00139 fun = {
00140 super:_IsSuper,
00141 sub:_IsSub
00142 }[opt]
00143
00144 for k in dic.keys():
00145 if fun(targ,k):
00146 result = dic[k]
00147 if type(result)==types.ListType:
00148 rv+= result
00149 else:
00150 rv.append(dic[k])
00151
00152 return rv
00153
00154
00155 def GetAssocs(self,key):
00156
00157 k = key.upper()
00158 rv = {}
00159 if self.Assocs.has_key(k):
00160 rv = self.Assocs[k]
00161 else:
00162 self.Assocs[k] = rv
00163
00164 return rv
00165
00166 def SearchAssocs(self,targ):
00167
00168 rv = []
00169 targ = targ.upper()
00170
00171 for item in self.Assocs.items():
00172 if item[0].find(targ) != -1:
00173 rv += item[1][:]
00174 return rv
00175
00176 def FieldSearch(self, targ, Fields=["UNIQUE-ID"],dbs="All",SubStr=False):
00177 if dbs == "All":
00178 dbs=self.DBdic.keys()
00179
00180 rv = []
00181 targ = targ.upper()
00182 for db in dbs:
00183 for rec in self.DBdic[db].values():
00184 for f in Fields:
00185 if rec.has_key(f):
00186 fvals = rec[f]
00187 if f == "UNIQUE-ID":
00188 fvals = [fvals]
00189 fvals = map(lambda x:x.upper(),fvals)
00190 if SubStr:
00191 fvals = " ".join(fvals)
00192
00193 if targ in fvals:
00194 rv.append(rec)
00195 break
00196
00197 return rv
00198
00199
00200
00201
00202
00203 def Print(self,list):
00204
00205 for item in list:
00206 print self[item]
00207
00208
00209 def WhereIs(self,key):
00210 try:
00211 return self[key].RecordClass
00212 except:
00213 return "Nowhere"
00214
00215 def Gene2Path(self,gene):
00216 if type(gene) == types.StringType:
00217 gene = self[gene]
00218
00219 cs = gene.TravChildren()
00220 ps = []
00221 for c in cs:
00222 if c.RecordClass == "Pathway":
00223 ps.append(c)
00224 return ps
00225
00226
00227 def Gene2Reac(self, gene):
00228 if type(gene) == types.StringType:
00229 gene = self[gene]
00230
00231 cs = gene.TravChildren()
00232 rs = []
00233 for c in cs:
00234 if c.RecordClass == "Reaction":
00235 rs.append(c)
00236 return rs
00237
00238 def GetGenes(self, targ):
00239 if type(targ) == types.StringType:
00240 targ = self[targ]
00241
00242 ps = targ.TravParents()
00243 gs = []
00244 for p in ps:
00245 if p.RecordClass == "Gene":
00246 gs.append(p)
00247 return gs
00248
00249
00250 def ToScrumPy(self, FileName):
00251
00252 file = open(FileName, "w")
00253
00254 file.write("Structural()\nElType(int)\n")
00255
00256 for r in self.Reaction.values():
00257 if len(r[Tags.Left])>0 and len(r[Tags.Right])>0:
00258 file.write(r.AsScrumPy())
00259
00260 file.close()
00261 return ScrumPy.Model(FileName)
00262
00263
00264