/
validation_triple.py
198 lines (151 loc) · 6.2 KB
/
validation_triple.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
#!/usr/bin/env python
################################################################################
##
## validation_triple.py
## Author: Satoshi Takahama (satoshi.takahama@epfl.ch)
## Jul. 2017
##
## -----------------------------------------------------------------------------
##
## This file is part of APRL-SSP
##
## APRL-SSP is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## APRL-SSP is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with APRL-SSP. If not, see <http://www.gnu.org/licenses/>.
##
################################################################################
import os
import re
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from argparse import ArgumentParser, RawTextHelpFormatter
## -----------------------------------------------------------------------------
## Define command-line arguments
parser = ArgumentParser(description='''
============================================================
Perform validation described by aprl-carbontypes. Supercedes validation_atoms.R. Example usage:
$ python validation_triple.py -f apinene_MCMgroups_atomfulltable.csv -a apinene_commonatoms.csv -o apinene
''',formatter_class=RawTextHelpFormatter)
## Arguments
parser.add_argument('-f','--atomfulltable',type=str,
help='file generated by substructure_generate_fulltable.py; csv format')
parser.add_argument('-a','--atomcommon',type=str,
help='file generated by substructure_seach.py with common_atoms.csv patterns; csv format')
parser.add_argument('-o','--outputprefix',type=str,default='output',
help='output prefix')
if __name__=='__main__':
## -------------------------------------------------------------------------
## parse arguments
# args = parser.parse_args('-f ssp/apinene_MCMgroups_atomfulltable.csv -a ssp/apinene_commonatoms.csv -o apinene'.split())
args = parser.parse_args()
filename = {
'fulltable':args.atomfulltable,
'atoms':args.atomcommon
}
prefix = args.outputprefix
## -------------------------------------------------------------------------
## read
# atype = lambda x: x[0].upper()
def atype(x):
try:
return x[0].upper()
except:
return np.nan
atoms = pd.read_csv(filename['atoms']).set_index('compound')
atoms.columns = atoms.columns.map(atype)
fulltable = pd.read_csv(filename['fulltable'])
fulltable['atype'] = (
fulltable['type'].map(atype)
.astype('category', categories=atoms.columns)
)
## -------------------------------------------------------------------------
## atom completeness (atoms in matched groups per compound)
ismatched = ~fulltable['match'].isnull() # this is critical
atomsv = (
fulltable
.loc[ismatched, ['compound', 'atom', 'atype']]
.drop_duplicates()
.groupby(['compound', 'atype'])['atom'].count()
.unstack(level='atype', fill_value=0)
)
## -------------------------------------------------------------------------
## FG specificity (groups per atom)
groupv = (
fulltable.groupby(['compound', 'atom', 'atype'])['match'].count()
.reset_index('atype')
)
## relevel
groupv['atype'] = pd.Categorical(groupv['atype'], categories=groupv['atype'].unique().categories)
## -------------------------------------------------------------------------
## carbon specificity
carbonv = (
fulltable.loc[fulltable['atype']=='C']
.groupby(['compound', 'group', 'match'])['atom'].count()
.reset_index('group')
)
carbonv['group'] = carbonv['group'].astype('category')
## -------------------------------------------------------------------------
## export
# plt.ion()
def plotframe(ax, mx):
dx = 1 if mx < 5 else 2 # just a heuristic for tick interval
ticks = np.arange(0, mx+1, dx)
lim = np.array([0, mx]) + np.array([-1, 1]) * 0.04 * mx
##
ax.set_autoscale_on(False)
ax.set_xlim(lim)
ax.set_ylim(lim)
#
ax.set_xticks(ticks)
ax.set_yticks(ticks)
##
ax.plot(lim, lim)
def jitterplot(ax, x, y, seed = 1):
np.random.seed(seed)
jitter = np.random.normal(0, .05, [len(x), 2])
if len(y.unique())==1:
jitter[:,1] = 0
ax.scatter(x.cat.codes + jitter[:,0], y + jitter[:,1])
ax.set_xticks(np.arange(len(x.cat.categories)))
ax.set_xticklabels(x.cat.categories)
ax.set_yticks(np.arange(y.min(), y.max()+1))
# atoms
plt.close()
fig = plt.figure()
ncol = len(atomsv.columns)
for i, x in enumerate(atomsv.columns):
ax = fig.add_subplot(1, ncol, i+1)
plotframe(ax, atoms[x].max())
ax.scatter(atoms[x], atomsv.loc[atoms.index,x])
ax.set_title(x)
fig.text(0.5, 0.04, 'True atom count by compound', ha='center')
fig.text(0.04, 0.5, 'Matched atom count by compound', va='center', rotation='vertical')
fig.set_size_inches((12,5))
fig.savefig('{}_validation_completeness.pdf'.format(prefix))
# groups
plt.close()
fig, ax = plt.subplots()
jitterplot(ax, groupv['atype'], groupv['match'], 1)
ax.set_xlabel('Element')
ax.set_ylabel('Matched atom count per group')
fig.savefig('{}_validation_specificity_FG.pdf'.format(prefix))
# carbon
plt.close()
fig, ax = plt.subplots()
jitterplot(ax, carbonv['group'], carbonv['atom'], 2)
ax.set_xlabel('Group')
ax.set_ylabel('Matched C per group')
plt.xticks(rotation = 90)
fig.subplots_adjust(bottom=0.35)
fig.savefig('{}_validation_specificity_carbon.pdf'.format(prefix))
## -------------------------------------------------------------------------