-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvisualizeMiniprothint.py
executable file
·150 lines (115 loc) · 4.09 KB
/
visualizeMiniprothint.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
#!/usr/bin/env python3
# ==============================================================
# Tomas Bruna
#
# Visualize miniprothint intron scores
# ==============================================================
import argparse
import csv
import re
import sys
import random
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
def getSignature(row):
return row[0] + "_" + row[3] + "_" + row[4] + "_" + row[6]
def extractFeature(text, feature):
regex = feature + '=([^;]+)'
search = re.search(regex, text)
if search:
return search.groups()[0]
else:
return None
def loadAnnotation(annotFile):
annot = set()
for row in csv.reader(open(annotFile), delimiter='\t'):
if (row[2].lower() == "intron"):
annot.add(getSignature(row))
return annot
def plotScores(annot, inputFile, outputFile, args):
TP = 0
FP = 0
allX = []
allY = []
colors = []
maxX = 0
maxY = 0
for row in csv.reader(open(inputFile), delimiter='\t'):
if (row[2].lower() != "intron"):
continue
signature = getSignature(row)
color = 'purple'
if signature in annot:
TP += 1
color = 'green'
else:
FP += 1
x = float(extractFeature(row[8], "al_score")) + \
random.uniform(-0.01, 0.01)
y = float(row[5]) + random.uniform(-0.5, 0.5)
if args.ylim != -1:
if y > args.ylim:
continue
if x > maxX:
maxX = x
if y > maxY:
maxY = y
allX.append(x)
allY.append(y)
colors.append(color)
plt.scatter(x=allX, y=allY, marker='o', s=0.1, color=colors,
alpha=args.opacity, clip_on=False)
lstyle = '--'
lsize = 2
plt.plot([0.25, maxX], [3.5, 3.5], color='tab:red', ls=lstyle,
linewidth=lsize)
plt.plot([0.25, 0.25], [3.5, maxY], color='tab:red', ls=lstyle,
linewidth=lsize)
plt.plot([0.1, 0.1], [0, maxY], color='b', ls=':', linewidth=lsize)
# Legend
yMargin = 0
yShift = -0.048
plt.text(0.05, 1 + yMargin, " TP (" + str(TP) + ")" +
"\n FP (" + str(FP) + ")",
va="top", transform=plt.gca().transAxes,
bbox=dict(facecolor='white', edgecolor='black',
boxstyle='square,pad=0.75'))
plt.plot(0.05, .989, '.', ms=10, color='green', clip_on=False,
transform=plt.gca().transAxes, zorder=4)
plt.plot(0.05, .989 + yShift, '.', ms=10, color='purple', clip_on=False,
transform=plt.gca().transAxes, zorder=4)
plt.xlabel("Intron borders alignment (IBA)")
plt.ylabel("Intron mapping coverage (IMC)")
if args.logYScale:
plt.yscale('log')
plt.box(on=None)
if args.ylim != -1:
plt.ylim(0, args.ylim + 0.5)
plt.xlim(-0.01, maxX)
plt.savefig(outputFile)
def main():
args = parseCmd()
with open("scatter.sh", "w") as file:
file.write("#!/usr/bin/env bash\n")
file.write(" ".join(sys.argv) + "\n")
annot = loadAnnotation(args.annotation)
plotScores(annot, args.input, args.output, args)
def parseCmd():
parser = argparse.ArgumentParser(description='Visualize miniprothint\
intron scores')
parser.add_argument('input', metavar='miniprothint.gff', type=str,
help='Introns scored by miniprot boundary \
scorer/miniprohtint')
parser.add_argument('annotation', metavar='introns_annot.gff', type=str,
help='Annotated introns.')
parser.add_argument('output', type=str,
help='Output figure.')
parser.add_argument('--opacity', type=float, default=1,
help='Opacity of individual points. Default = 1')
parser.add_argument('--ylim', type=float, default=-1,
help='Limit y axis by this number. Default = No limit')
parser.add_argument('--logYScale', default=False, action='store_true')
return parser.parse_args()
if __name__ == '__main__':
main()