-
Notifications
You must be signed in to change notification settings - Fork 1
/
downsample.py
153 lines (124 loc) · 4.83 KB
/
downsample.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
import os
import re
import pandas as pd
RAW_PATH = './reference_data'
lipid_match = re.compile("""
(?P<hydroxy>[\d,]*OH-)?
(?P<keto>\d*Oxo-)?
(?P<cyclic>[\d,]*Cyc-)?
(?P<methyl>[\d,]*Me-|Br-|A-|I-|Neo-)?
(?P<base>C\d\d?:\d)
(?P<db>ω?[\d,]+[ct]?)?
(?P<other>-ALDE|-DMA|-DCA)?
""", re.VERBOSE)
def _colname_into_groups(colname):
match = lipid_match.match(colname)
if colname.startswith(('Other', 'Unknown')):
return {'base': 'Other'}
elif match is None:
return None
groups = {k: v for k, v in match.groupdict().items() if v is not None}
# a little hacky, but force all ALDE/DMA/etc compounds into other
if 'other' in groups:
return {'base': 'Other'}
return groups
def low_qual_name(colname):
groups = _colname_into_groups(colname)
if groups is None:
return None
name = ''
if 'other' in groups:
return 'Other'
if 'hydroxy' in groups:
name += 'OH-'
if 'keto' in groups:
name += 'Oxo-'
if 'methyl' in groups:
name += 'Br-'
if 'cyclic' in groups:
name += 'Cyc-'
name += groups['base']
name += groups.get('other', '')
return name
def med_qual_name(colname):
def get_pos(desc):
return re.findall('\d+', desc)
groups = _colname_into_groups(colname)
if groups is None:
return None
name = ''
for moiety in ['hydroxy', 'keto', 'cyclic']:
if moiety in groups:
if not get_pos(groups[moiety]):
return 'Other'
else:
name += groups[moiety]
if 'methyl' in groups:
if groups['methyl'] == 'Br-':
return 'Other'
if groups['methyl'].endswith('Me-') and not get_pos(groups['methyl']):
return 'Other'
name += groups['methyl']
name += groups['base']
if not groups['base'].endswith(':0'):
if 'db' not in groups:
return 'Other'
# only one double bond position is specified for medium quality
# with more references, we could maybe bump this up to require
# all the double bond positions (so medium quality is just
# missing stereo information)
name += 'ω' + str(get_pos(groups['db'])[0])
return name
# TODO: quality is high if all of the stereochem is specified
# e.g. no db, or t/c specified (think this covers most cases?)
# need to check db/other group interactions though
# (also cyclic group sterochem is never annotated; assumed cis?)
if __name__ == '__main__':
lo_table = pd.DataFrame()
md_table = pd.DataFrame()
for filename in os.listdir(RAW_PATH):
file_obj = open(os.path.join(RAW_PATH, filename), 'rb')
raw_table = pd.read_csv(file_obj, sep='\t', quoting=3)
# do a little cleanup; first, setting anything not filled into zero
raw_table = raw_table.fillna(0)
# now, set anything below the LOQ to be 1/3 the LOQ
raw_table = raw_table.applymap(lambda x: float(x[1:]) / 3 if
str(x).startswith('<') else x)
# copy the data over into scratch tables to
# 1. change column names by desired quality level
# 2. merge now identical columns (e.g. Other's)
for qual in ['lo', 'md']:
table = pd.DataFrame()
name_f = {
'lo': low_qual_name,
'md': med_qual_name,
}[qual]
for col in raw_table.columns:
colname = name_f(col)
if colname is None:
# append * to the beginning of metadata columns
table['*' + col] = raw_table[col]
elif colname in table:
table[colname] += raw_table[col].apply(float)
else:
table[colname] = raw_table[col].apply(float)
if qual == 'lo':
lo_table = pd.concat([lo_table, table])
elif qual == 'md':
md_table = pd.concat([md_table, table])
# replace all the NaN's with 0 or ""'s again (b/c of the
# concatenation
fillv = {c: ('' if c.startswith('*') else 0) for c in lo_table}
lo_table.fillna(fillv, inplace=True)
fillv = {c: ('' if c.startswith('*') else 0) for c in md_table}
md_table.fillna(fillv, inplace=True)
# now, quality filter out anything with more than 10% "Other"
lo_table = lo_table[lo_table['Other'] < 10]
md_table = md_table[md_table['Other'] < 10]
# and filter out anything without a taxonomy id
lo_table = lo_table[lo_table['*Tax ID'] != 0]
lo_table['*Tax ID'] = lo_table['*Tax ID'].astype(int)
md_table = md_table[md_table['*Tax ID'] != 0]
md_table['*Tax ID'] = md_table['*Tax ID'].astype(int)
lo_table.to_csv('./lo_qual.tsv', sep='\t', index=False, quoting=3)
md_table.to_csv('./md_qual.tsv', sep='\t', index=False, quoting=3)