annotate commons/core/parsing/FindRep.py @ 58:5f5c9b74c2dd

Uploaded
author m-zytnicki
date Fri, 07 Feb 2014 11:53:36 -0500
parents 769e306b7933
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
6
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
1 import re
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
2 from xml.sax.handler import ContentHandler
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
3
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
4 class FindRep( ContentHandler ):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
5 def __init__(self,outfileName, filter=0,count=0):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
6 self.inWindowContent = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
7 self.inSeqNameContent = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
8 self.inStartContent = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
9 self.inEndContent = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
10 self.inPeriodContent = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
11 self.inUnitContent = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
12 self.inScoreContent = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
13 self.count = count
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
14 self._outfileName = outfileName
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
15 self.filter=filter
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
16
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
17 def startDocument(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
18 self._fileout = open(self._outfileName,"w")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
19
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
20 def startElement(self,name,attrs):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
21 if name=="window":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
22 self.inWindowContent=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
23 elif name=="sequence-name":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
24 self.inSeqNameContent=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
25 self.seqname=""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
26 elif name=="repeat":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
27 self.inRepContent=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
28 self.start=""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
29 self.end=""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
30 self.period=""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
31 self.type={}
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
32 elif name=="start":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
33 self.inStartContent=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
34 elif name=="end":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
35 self.inEndContent=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
36 elif name=="period":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
37 self.inPeriodContent=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
38 elif name=="unit":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
39 self.inUnitContent=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
40 self.unit=""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
41 elif name=="score":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
42 self.inScoreContent=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
43 self.score=""
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
44
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
45 def characters(self,ch):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
46 if self.inSeqNameContent:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
47 self.seqname+=ch
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
48 elif self.inStartContent:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
49 self.start+=ch
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
50 elif self.inEndContent:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
51 self.end+=ch
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
52 elif self.inPeriodContent:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
53 self.period+=ch
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
54 elif self.inUnitContent:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
55 self.unit+=ch
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
56 elif self.inScoreContent:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
57 self.score+=ch
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
58
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
59 def endElement(self,name):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
60 if name=="window":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
61 self.inWindowContent=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
62 elif name=="sequence-name":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
63 self.inSeqNameContent=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
64 elif name=="repeat":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
65 self.inRepContent=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
66 start=int(self.start)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
67 end=int(self.end)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
68 period=int(self.period)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
69 score=float(self.score)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
70 if score>self.filter:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
71 return
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
72 max = 0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
73 self.count+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
74 for k,n in self.type.items():
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
75 if n>max:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
76 max = n
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
77 k_max = k
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
78
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
79 m=re.match("^[0-9]+.+\{Cut\}",self.seqname)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
80 if m!=None:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
81 seqname=self.seqname[m.start(0):m.end(0)-5].rstrip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
82 seqname=re.sub("^[0-9]+ ","",seqname).lstrip()
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
83 tok=self.seqname[m.end(0):].split("..")
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
84 astart=start+int(tok[0])-1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
85 aend=end+int(tok[0])-1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
86 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
87 astart=start
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
88 aend=end
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
89 seqname=self.seqname
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
90 if len(k_max) > 100:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
91 k_max=k_max[:48]+"..."+k_max[-51:]
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
92 strout="%d\t(%s)%d\t%s\t%d\t%d"%\
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
93 (self.count,k_max,(abs(start-end)+1)/period,\
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
94 seqname,astart,aend)
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
95 self._fileout.write("%s\n"%(strout))
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
96
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
97 elif name=="start":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
98 self.inStartContent=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
99 elif name=="end":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
100 self.inEndContent=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
101 elif name=="period":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
102 self.inPeriodContent=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
103 elif name=="score":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
104 self.inScoreContent=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
105 elif name=="unit":
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
106 self.inUnitContent=0
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
107 if self.type.has_key(self.unit):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
108 self.type[self.unit]+=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
109 else:
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
110 self.type[self.unit]=1
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
111
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
112 def endDocument(self):
769e306b7933 Change the repository level.
yufei-luo
parents:
diff changeset
113 self._fileout.close()