forked from albertwcheng/albert-bioinformatics-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRefGeneTable2sGFF3.py
executable file
·245 lines (185 loc) · 6.69 KB
/
RefGeneTable2sGFF3.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python
'''
Copyright 2010 Wu Albert Cheng <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
'''
from sys import *
from getopt import getopt
#REF GENE TABLE
#-1 Xkr4.cSep07 chr1 - 3195863 3205824 3204882 3205110 2 3195863,3203519, 3197398,3205824, 0 Xkr4 cmpl cmpl -1,0,
programName=argv[0]
opt,args=getopt(argv[1:],'',['source=','element-separator=','replace=','with=','expand-parents','input-is-gene-pred','output-rename-list='])
source="."
elementSeparator="@"
replaceWith=["",""]
expandParents=False
GenePredFormatInput=False
outputRenameList=None
for o,v in opt:
if o=="--source":
source=v
elif o=='--element-separator':
elementSeparator=v
elif o=='--replace':
replaceWith[0]=v
elif o=='--with':
replaceWith[1]=v
elif o=='--expand-parents':
expandParents=True
elif o=='--input-is-gene-pred':
GenePredFormatInput=True
elif o=='--output-rename-list':
outputRenameList=v
try:
filename,=args
except:
print >> stderr,"Usage:",programName,"filename"
print >> stderr,"[Options]"
print >> stderr,"--source x [.] .set the source name to x"
print >> stderr,"--element-separator s [@]. set the element separator to s. for example, x=@ => Enah@EXON1"
print >> stderr,"--replace x --with y. Defaut: No replacement. for example, x=_ , y=@, Transposase_14 => Transposase@14"
print >> stderr,"--expand-parents. Allow only one parent per exon"
print >> stderr,"--input-is-gene-pred. No bin info, only nine columns (http://genome.ucsc.edu/FAQ/FAQformat#format9). Default is RefGene Table Schema"
print >> stderr,"--output-rename-list filename. Output the mapping from original name to renamed due to --replace x --with y option"
exit()
print >> stdout,"##gff-version 3"
fil=open(filename)
lino=0
genes=dict()
def within(x,left,right):
return x>=left and x<=right
def formAttributeList(D):
keyvaluepairs=[]
for k,v in D.items():
keyvaluepairs.append(k+"="+",".join(v))
return ";".join(keyvaluepairs)
orderedGenes=[]
if outputRenameList:
outputRenameList=open(outputRenameList,"w")
for lin in fil:
lino+=1
if lino%1000==1:
print >> stderr,"processing line",lino
fields=lin.rstrip("\r\n").split("\t")
try:
if GenePredFormatInput:
transcriptname,chrom,strand,start,end,cdsstart,cdsend,numexons,exonstarts,exonends=fields[:10]
genename=".".join(transcriptname.split(".")[:-1])
else:
dummy,transcriptname,chrom,strand,start,end,cdsstart,cdsend,numexons,exonstarts,exonends,score,genename,dum2,dum3,frames=fields[:16]
if replaceWith[0]!="":
origGeneName=genename
origTranscriptName=transcriptname
for r,t in zip(replaceWith[0],replaceWith[1]):
genename=genename.replace(r,t)
transcriptname=transcriptname.replace(r,t)
print >> outputRenameList,origGeneName+"\t"+genename
print >> outputRenameList,origTranscriptName+"\t"+transcriptname
except:
print >> stderr,"error parsing line",lino,":",fields
exit()
exonstarts=exonstarts.split(",")
exonends=exonends.split(",")
try:
geneinfo=genes[genename]
except KeyError:
geneinfo=[chrom,source,"gene",1000000000,0,".",strand,".",{"ID":[genename],"Name":[genename]},[],dict(),[]]
genes[genename]=geneinfo
orderedGenes.append(genename)
#do sth for this transcript
start=int(start)+1
end=int(end)
cdsstart=int(cdsstart)+1
cdsend=int(cdsend)
geneinfo[3]=str(min(start,int(geneinfo[3])))
geneinfo[4]=str(max(end,int(geneinfo[4])))
mRNAs=geneinfo[9]
exons=geneinfo[10]
cds=geneinfo[11]
mRNAs.append([chrom,source,"mRNA",str(start),str(end),".",strand,".",{"ID":[transcriptname],"Parent":[genename]}])
cdsstate=-1
numR=len(exonstarts)
if len(exonstarts)!=len(exonends):
print >> stderr,"Error in RefGeneTable. Abort. fields=",fields
exit()
for i in range(0,numR):
#frame=frames[i] #useless
estart=exonstarts[i]
eend=exonends[i]
if len(estart)==0 or len(eend)==0:
break
estart=int(estart)+1
eend=int(eend)
if expandParents:
ekey=transcriptname+"_"+str(estart)+"_"+str(eend)
else:
ekey=str(estart)+"_"+str(eend)
length=eend-estart+1
if strand in ["+","-"]:
if cdsstate==-1:
if within(cdsstart,estart,eend):
cdsstate=0
if strand=="+":
phase=(cdsstart-estart)%3
phase_nextadd=(length-phase)%3
else:
phase=(eend-cdsstart+1)%3
elif cdsstate==0:
if strand=="+":
phase+=phase_nextadd
phase_nextadd=(length-phase)%3
else:
phase+=length%3
#if within(cdsend,estart,eend):
# cdsstate=1
if cdsstate == 0: #in [0,1]:
phase%=3
thisCDSStart=max(cdsstart,estart)
thisCDSEnd=min(cdsend,eend)
cds.append([chrom,source,"CDS",str(thisCDSStart),str(thisCDSEnd),".",strand,str(phase),{"ID":[genename+elementSeparator+"CDS"+str(len(cds)+1)],"Parent":[transcriptname]}])
#new to correct for CDS starting and ending in the same exon
if within(cdsend,estart,eend):
cdsstate=2
#if cdsstate==1:
# cdsstate=2
try:
exonRecord=exons[ekey]
except KeyError:
#exon not existed
exonRecord=[chrom,source,"exon",str(estart),str(eend),".",strand,".",{"ID":[genename+elementSeparator+"EXON"+str(len(exons.values())+1)],"Parent":[]}]
exons[ekey]=exonRecord
exonAttributes=exonRecord[8]
exonAttributes["Parent"].append(transcriptname)
if outputRenameList:
outputRenameList.close()
fil.close()
def printGffRecord(to,record):
recordDup=record[0:9]
recordDup[8]=formAttributeList(recordDup[8])
print >> stdout,"\t".join(recordDup)
#for geneName,geneInfo in genes.items():
for geneName in orderedGenes:
geneInfo=genes[geneName]
#now print gene info
printGffRecord(stdout,geneInfo)
mRNAs,exons,cdses=geneInfo[9:12]
for mRNA in mRNAs:
printGffRecord(stdout,mRNA)
for ekey,exon in exons.items():
printGffRecord(stdout,exon)
for cds in cdses:
printGffRecord(stdout,cds)