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

/home/mark/model/software/ScrumPy/ScrumPy/Bioinf/PyoCyc/Gene.py

00001 
00002 import Base, Tags
00003 import OrderedList
00004 
00005 
00006 
00007 DefaultFile="genes.dat"
00008 
00009 class Record(Base.Record):
00010     ChildFields=[Tags.Prod]
00011     ParentFields=[Tags.CompOf]
00012     RecordClass = "Gene"
00013 
00014     def Finished(self):
00015         Base.Record.Finished(self)
00016         try:
00017             self.MidPos = (int(self[Tags.LeftEnd][0])+int(self[Tags.RightEnd][0]))/2
00018         except:
00019             self.MidPos = 0
00020 
00021 class DB(Base.DB):
00022     def __init__(self,path=Base.DefaultPath,file=DefaultFile,**kwargs):
00023         Base.DB.__init__(self,path=path,file=file,RecClass=Record,**kwargs)
00024 
00025         ## create the gene position infomation
00026 
00027         self.PosList = []      # ordered list of mid positions
00028         self.PosDic = {}      # map positions -> UIDs
00029         self.GenList = []     # list of UIDs orderd by position on chromosome
00030         self.NoPosList = [] # list of UIDs of genes with no positional information
00031          
00032         for gene  in self.values():
00033             mp = gene.MidPos
00034             uid = gene.UID
00035             if mp ==0:
00036                 self.NoPosList.append(uid)
00037             else:
00038                 self.PosDic[mp] = uid   # we know that mps are unique, so don't check for multiples
00039                 OrderedList.Insert(self.PosList, mp)
00040 
00041         for mp in self.PosList:
00042             self.GenList.append(self.PosDic[mp])
00043 
00044 
00045 
00046     def BPSearch(self, targ=0, lo=0, hi=0, targ_t="Mid"):   # search by base pair pos of midpoint
00047         geneidx = OrderedList.FindNearest(self.PosList,targ)
00048         gene = self.PosDic[self.PosList[geneidx]]
00049         uids =  self.Neighbours_b(gene, lo,hi)
00050         rv = []
00051         for uid in uids:
00052             rv.append(self[uid])
00053         return rv
00054 
00055 
00056     def GPSearch(self, targ=0, lo=0, hi=0,**ignore):
00057         
00058         lo = int(targ + lo)
00059         if lo < 0:
00060             lo = 0
00061             
00062         hi = int(targ+hi)
00063         if hi >= len(self):
00064             hi = len(self -1)
00065 
00066         rv = []
00067         for gene in self.GenList[lo:hi+1]:
00068             rv.append(self[gene])
00069         return rv
00070             
00071 
00072 
00073     def Neighbours_g(self, uid, lo, hi):
00074         "uids of neighbouring lo-hi genes, determined by gene order"
00075 
00076         def minmax(x, lolim,hilim):
00077             if x > hilim: return hilim
00078             if x < lolim: return lolim
00079             return x
00080 
00081         if lo >hi: lo,hi=hi,lo
00082         maxidx = len(self.PosList)-1
00083         idx = self.GenList.index(uid)
00084         top = minmax(idx+hi,0,maxidx)
00085         bot = minmax(idx+lo,0,maxidx)
00086       
00087         return self.GenList[bot:top+1]
00088 
00089     def Neighbours_b(self, uid, lo, hi):
00090         "uids of neighbouring lo-hi genes, determined by base pair"
00091         if lo >hi: lo,hi = hi,lo
00092         mp = self[uid].MidPos
00093         loidx = OrderedList.FindNearest(self.PosList,mp+lo)
00094         hiidx = OrderedList.FindNearest(self.PosList,mp+hi)
00095         rv = []
00096         
00097         for gene in self.PosList[loidx:hiidx+1]:
00098             rv.append(self.PosDic[gene])
00099         return rv
00100 
00101 
00102         
00103             
00104 
00105 
00106 
00107 
00108 
00109 
00110 

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