• Main Page
  • Namespaces
  • Classes
  • Files
  • File List

/home/mark/model/software/ScrumPy/ScrumPy/Structural/Condense.py

00001 import sys
00002 import ScrumPy
00003 from ScrumPy.Util import Format
00004 
00005 
00006 TOL=1e-6
00007 
00008 
00009 
00010 def MakeSM(smx):
00011     
00012     sm = smx.Copy()
00013     for met in smx.Externs:
00014         sm.DelRow(met)
00015     sm.Externs = smx.Externs[:]
00016     
00017     return sm
00018 
00019 
00020 def AreOrthogonal(a,b,tol=TOL ):
00021 
00022     theta = abs(ScrumPy.Util.Seq.CosTheta(a,b))
00023 
00024     return abs(theta) <tol
00025 
00026 
00027 def FindDeads(m, txs=[], tol=TOL):
00028     """ identify dead reactions as those that are orthogonal to all transporters in txs.
00029         See Poolman et al 2008 JTB """
00030 
00031     
00032     k = m.sm.OrthNullSpace()
00033     lentxs = len(txs)
00034     rv = []
00035 
00036     for reac in k.rnames:
00037         if not reac in txs:
00038             dead = max(map(abs, k[reac])) < tol
00039             nchecked = 0
00040             
00041             while not dead and nchecked < lentxs:
00042                 tx = txs[nchecked]
00043                 nchecked +=1
00044                 dead = AreOrthogonal(k[tx],k[reac],tol)
00045                 
00046             if dead:
00047                 rv.append(reac)
00048 
00049     return rv
00050         
00051 
00052 def DelDeads(m,txs=[],tol = TOL):
00053 
00054     deads = FindDeads(m, txs, tol)
00055     m.DelReactions(deads)
00056 
00057     return deads
00058 
00059 
00060 
00061 def HasSubsets(ess):
00062 
00063     for ss in ess:
00064         if len(ess[ss]) >1:
00065             return True
00066     return False
00067 
00068 def RenameEss(ess,sfx):
00069 
00070     for k in ess.keys():
00071         if len(ess[k])>1:
00072             ess[k+"_"+str(sfx)] = ess[k]
00073             del ess[k]
00074         
00075 
00076 
00077 def _Condense(m, IsosEss):
00078     
00079     isos = m.DelIsoforms()
00080     e = m.EnzSubsets()
00081     del e["DeadReacs"]
00082     nits = len(IsosEss)
00083     RenameEss(e, nits)
00084 
00085     IsosEss.append([isos,e])
00086     print nits
00087 
00088     
00089     emtx = e.ToMtx()
00090     #emtx.DelCol("DeadReacs")
00091     smx = m.smexterns.Mul(emtx)
00092     smx.Externs = m.sm.Externs[:]
00093     smx.ZapZeroes()
00094     smx.Irrevs = e.Irrev_ss()
00095 
00096     m.smexterns=smx
00097     m.sm = MakeSM(smx)
00098 
00099     CanCondense = len(isos) >0 or HasSubsets(e)
00100     CanCondense = CanCondense and len(m.sm) >0  # check len(m.sm) just in case we've eliminated ALL internal mets
00101     
00102 
00103     if CanCondense:  
00104         _Condense(m,IsosEss)
00105 
00106  
00107 
00108 def Condense(m,txs=[],tol=TOL):
00109 
00110     deads =  DelDeads(m, txs,tol)
00111     print len(deads), "dead"
00112 
00113     m.sm.ReQuote()
00114     m.smexterns.ReQuote()
00115     IsosEss = []
00116     _Condense(m, IsosEss)
00117     IsosEss[0][1]["DeadReacs"] = deads
00118 
00119     
00120     m.smexterns.ToScrumPy(m.ModelName+"_Condensed.spy")
00121 
00122     open(m.ModelName+"_Condensed.py","w").write(Format.FormatNested(IsosEss,Quoted=False))
00123 
00124     m.sm.DeQuote()
00125     m.smexterns.DeQuote()                                
00126                                                 
00127 
00128     
00129 
00130     return IsosEss
00131 
00132     
00133                               
00134 
00135     
00136         
00137         
00138             
00139         
00140     

Generated on Tue Sep 4 2012 15:38:02 for ScrumPy by  doxygen 1.7.1