forked from MustoeLab/StructureAnalysisTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcorrelation_code.py
More file actions
executable file
·209 lines (142 loc) · 6.96 KB
/
correlation_code.py
File metadata and controls
executable file
·209 lines (142 loc) · 6.96 KB
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
#!/usr/bin/env python
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plot
import numpy as np
import scipy.stats
import argparse
import sys
from ReactivityProfile import ReactivityProfile
def filterProfilesByNt(profile1, profile2, nts=None, name=None, exclhighbg=None):
"""Return matched reactivities from ReactivityProfile objects prof1 and prof2,
filtering out any NaNs
If nts argument is None, return all nts. Otherwise, return only these nts
name argument is passed to ReactivityProfile to get desired profile
"""
# lets check to make sure the sequences are the same
if not np.array_equal(profile1.sequence, profile2.sequence):
raise ValueError('Sequences are not the same!')
# initiliaze my mask (all false)
mask = np.zeros(len(profile1.sequence), dtype=bool)
if nts is None:
mask[:] = True #reassign all values to True
else:
for n in nts:
mask = mask | (profile1.sequence == n)
if exclhighbg is not None:
with np.errstate(invalid='ignore'):
mask = mask & (profile1.backprofile < exclhighbg) & (profile2.backprofile < exclhighbg)
# get the desired profiles
r1 = profile1.profile(name)
r2 = profile2.profile(name)
mask = mask & np.isfinite(r1) & np.isfinite(r2)
return r1[mask], r2[mask]
def plotCorrelation(var1, var2, ax, title='', xlabel='', ylabel=''):
if len(var1) == 0 and len(var2) == 0:
return
elif sum(var1) == 0 and sum(var2) == 0:
print("Norm profile not present. Skipping.")
return
regress = scipy.stats.linregress(var1, var2)
# get min and max values for plotting
xmin, ymin = min(var1), min(var2)
gmin = min(xmin, ymin)
xmax, ymax = max(var1), max(var2)
gmax = max(xmax, ymax)
ax.scatter(var1,var2)
# plot the diagonal
ax.plot([gmin, gmax], [gmin, gmax], 'k--', label='diagonal')
# plot the fit
x = np.linspace(xmin, xmax, num=100)
y = x*regress.slope + regress.intercept
ax.plot(x, y, 'r', label='fit')
# add labels and stuff
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.set_title('{} : R={:.3f} ; m={:.1f}'.format(title, regress.rvalue, regress.slope))
#############################################################################
if __name__=='__main__':
parser=argparse.ArgumentParser()
parser.add_argument('profile1', help='Path to first profile file')
parser.add_argument('profile2', help='Path to second profile file')
parser.add_argument('output', help='Path to output file')
parser.add_argument('--exclhighbg', type=float, help='Exclude nts with bg values greater than this cutoff')
parser.add_argument('--comparison', default='all', help='Type of comparison to perform. Options are raw/sub/back/norm/all (default=all)')
parser.add_argument('--profile', action='store_true', help='plot profiles superimposed (skyline plot)')
parser.add_argument('--GA', action='store_true', help='Flag to allow comparison between two N7 profiles.')
parser.add_argument("--mindepth", type=int, default = 100, help="Any nucleotide less than this depth is not plotted in any graph. (default = 100)")
args=parser.parse_args()
if args.comparison not in ('raw','sub', 'back', 'norm','all'):
exit('Invalid comparison selected. Options are raw/sub/back/norm/all')
# read in the desired files
p1 = ReactivityProfile(args.profile1, depthcut = args.mindepth)
p2 = ReactivityProfile(args.profile2, depthcut = args.mindepth)
for p in (p1, p2):
p.normprofile[np.isnan(p.subprofile)] = np.nan
if args.comparison in ('all','norm') and not args.GA:
if not np.all(np.isnan(p1.subprofile)):
p1.normalize(DMS=True)
if not np.all(np.isnan(p2.subprofile)):
p2.normalize(DMS=True)
if args.GA:
print("GA flag passed")
# create the matplotlib figure object
if args.comparison == 'all':
if not args.GA:
fig, ax = plot.subplots(4, 5, figsize=(20,16))
else:
fig, ax = plot.subplots(1, 4, figsize=(18,5))
if not args.GA:
for i,name in enumerate(('raw','sub','back','norm')):
# this loop will compute correlations for all nts combined, and then individually
for j, n in enumerate( ('All', 'A', 'C', 'U', 'G')):
if n=='All':
x,y = filterProfilesByNt(p1, p2, name=name, exclhighbg=args.exclhighbg)
else:
x,y = filterProfilesByNt(p1, p2, nts=n, name=name, exclhighbg=args.exclhighbg)
plotCorrelation(x,y, ax[i,j], title=name+' '+n)
else:
for i,name in enumerate(('raw','sub','back', 'norm')):
# this loop will compute correlations for all nts combined, and then individually
for j, n in enumerate( ('G')):
if n=='All':
x,y = filterProfilesByNt(p1, p2, name=name, exclhighbg=args.exclhighbg)
else:
x,y = filterProfilesByNt(p1, p2, nts=n, name=name, exclhighbg=args.exclhighbg)
plotCorrelation(x,y, ax[i], title=name+' '+n)
# label the axes
if not args.GA:
ax[3,2].set_xlabel(args.profile1.split('/')[-1])
ax[1,0].set_ylabel(args.profile2.split('/')[-1])
else:
fig.text(0.5, 0.01, args.profile1.split('/')[-1],
ha='center', va='bottom', fontsize=10)
plot.subplots_adjust(bottom=0.1)
ax[0].set_ylabel(args.profile2.split('/')[-1])
else:
fig, ax = plot.subplots(1, 5, figsize=(20,4))
# this loop will compute correlations for all nts combined, and then individually
if not args.GA:
for i, n in enumerate( ('All', 'A', 'C', 'U', 'G')):
if n=='All':
x,y = filterProfilesByNt(p1, p2, name=args.comparison, exclhighbg=args.exclhighbg)
else:
x,y = filterProfilesByNt(p1, p2, nts=n, name=args.comparison, exclhighbg=args.exclhighbg)
plotCorrelation(x,y, ax[i], title=n)
else:
for i, n in enumerate( ('G')):
if n=='All':
x,y = filterProfilesByNt(p1, p2, name=args.comparison, exclhighbg=args.exclhighbg)
else:
x,y = filterProfilesByNt(p1, p2, nts=n, name=args.comparison, exclhighbg=args.exclhighbg)
plotCorrelation(x,y, ax[i], title=n)
# label the axes
ax[2].set_xlabel(args.profile1.split('/')[-1])
ax[0].set_ylabel(args.profile2.split('/')[-1])
#make a bit prettier
if args.GA:
fig.tight_layout(rect=[0, 0.05, 1, 0.95])
else:
fig.tight_layout()
# save the data
fig.savefig(args.output)