-
Notifications
You must be signed in to change notification settings - Fork 0
/
tsv2rdf_hint.py
359 lines (292 loc) · 11.1 KB
/
tsv2rdf_hint.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#coding: UTF-8
'''
Created on 2018/01/06
'''
import sys,os
from jinja2 import Template, Environment, FileSystemLoader
from optparse import OptionParser
import json
import logging.handlers
import logging
IN_DATA_DIR = 'data_h'
RDF_TEMPLATE='templ_hint.ttl'
EXEC_PATH = '/tmp'
MOUNT_PATH = '/mnt'
TEMPLATE_BODY = 'templ_hint.ttl'
TEMPLATE_EVI = 'templ_hint.ttl.evi'
TEMPLATE_PREFIX = 'templ_hint.ttl.prefix'
class Message(object):
def __init__(self, subject_no, data ):
'''
@param subject_no: Subject serial number
@param data: HiNT data (1 row)
@summary: Store HiNT data (1 row) in the Message object
'''
self.SNo = subject_no
self.UniprotA = data["Uniprot_A"]
self.UniprotB = data["Uniprot_B"]
self.GeneA = data["Gene_A"]
self.GeneB = data["Gene_B"]
self.ORFA = data["ORF_A"]
self.ORFB = data["ORF_B"]
self.AliasA = data["Alias_A"]
self.AliasB = data["Alias_B"]
#Publications
self.Publications = []
comma = 0
for publication in data["pmid:method:quality"].split('|'):
pmids, method, quality = publication.split(':')
if ';' in pmids or ',' in pmids:
#if publication has multiple pmid
pmids = pmids.split(';') if ';' in pmids else pmids.split(',')
for pmid in pmids:
new_publication = '%s:%s:%s' % (pmid.strip(), method.strip(), quality.strip())
self.Publications.append(Publications(comma, new_publication))
comma += 1
else:
self.Publications.append(Publications(comma, publication))
pass
comma += 1
'''
self.Publications = [ Publications(comma, publication)
for comma, publication in enumerate(data["pmid:method:quality"].split('|')) ]
'''
self.hilo = data["hi/lo"]
self.proteinForm = data["proteinForm"]
return
class Publications(object):
def __init__(self, comma, publication ):
'''
@param comma: Variables for controlling output of commas when outputting multiple publications
@param publication: ID of document
@summary: Store multiple Publications in the Publications object
'''
wk_publication = publication.split(':')
self.Comma = comma
self.DB = "pdb" if (wk_publication[0][0:4] == "PDB_") else "pubmed"
self.ID = wk_publication[0][4:].lower() if (wk_publication[0][0:4] == "PDB_") else wk_publication[0]
self.Method = wk_publication[1]
self.Quality = "high-throughput" if (wk_publication[2] == "HT") else "literature-curated" if (wk_publication[2] == "LC") else "unknown" #I列[2]
return
def read_tsv(bi_all, bi_hq, cc_al, cc_hq):
import pandas as pd
import uuid
uu = str(uuid.uuid1())
try:
hint_bi_allDF=pd.read_csv(bi_all, delimiter="\t")
hint_cc_allDF=pd.read_csv(cc_al, delimiter="\t")
if cc_hq is not None:
hint_bi_hqDF=pd.read_csv(bi_hq, delimiter="\t")
else:
hint_bi_hqDF = pd.DataFrame(columns=hint_bi_allDF.columns)
if cc_hq is not None:
hint_cc_hqDF=pd.read_csv(cc_hq, delimiter="\t")
else:
hint_cc_hqDF = pd.DataFrame(columns=hint_cc_allDF.columns)
except Exception as e:
logger.error("%s" % e.message)
sys.exit()
#
# binary
#
qdiffDF=pd.merge(hint_bi_allDF, hint_bi_hqDF, on=["Uniprot_A", "Uniprot_B"], how="outer")
#qdiffDF.to_excel("debug/debug_%s_qdiffDF.xlsx" % uu)
hint_bi_onlylqDF=qdiffDF[(qdiffDF["pmid:method:quality_y"].fillna("nan") == "nan")][[
"Uniprot_A",
"Uniprot_B",
"Gene_A_x",
"Gene_B_x",
"ORF_A_x",
"ORF_B_x",
"Alias_A_x",
"Alias_B_x",
"pmid:method:quality_x"]]
hint_bi_onlylqDF.columns=[
"Uniprot_A",
"Uniprot_B",
"Gene_A",
"Gene_B",
"ORF_A",
"ORF_B",
"Alias_A",
"Alias_B",
"pmid:method:quality"]
hint_bi_onlylqDF["hi/lo"]="low"
hint_bi_onlylqDF["proteinForm"]="binary"
#hint_bi_onlylqDF.to_excel("debug/debug_%s_hint_bi_onlylqDF.xlsx" % uu)
hint_bi_onlyhqDF=qdiffDF[(
qdiffDF["pmid:method:quality_y"].fillna("nan") != "nan")
& (qdiffDF["pmid:method:quality_x"].fillna("nan") != "nan")
& (qdiffDF["pmid:method:quality_x"].fillna("nan") == qdiffDF["pmid:method:quality_y"].fillna("nan"))][
["Uniprot_A",
"Uniprot_B",
"Gene_A_x",
"Gene_B_x",
"ORF_A_x",
"ORF_B_x",
"Alias_A_x",
"Alias_B_x",
"pmid:method:quality_x"]]
hint_bi_onlyhqDF.columns=[
"Uniprot_A",
"Uniprot_B",
"Gene_A",
"Gene_B",
"ORF_A",
"ORF_B",
"Alias_A",
"Alias_B",
"pmid:method:quality"]
hint_bi_onlyhqDF["hi/lo"]="high"
hint_bi_onlyhqDF["proteinForm"]="binary"
#hint_bi_onlyhqDF.to_excel("debug/debug_%s_hint_bi_onlyhqDF.xlsx" % uu)
hint_biDF=pd.concat([hint_bi_onlyhqDF, hint_bi_onlylqDF]).sort_index()
#
# co-complex
#
qdiff2DF=pd.merge(hint_cc_allDF, hint_cc_hqDF, on=["Uniprot_A", "Uniprot_B"], how="outer")
#qdiff2DF.to_excel("debug/debug_%s_qdiff2DF.xlsx" % uu)
hint_cc_onlylqDF=qdiff2DF[(qdiff2DF["pmid:method:quality_y"].fillna("nan") == "nan")][[
"Uniprot_A",
"Uniprot_B",
"Gene_A_x",
"Gene_B_x",
"ORF_A_x",
"ORF_B_x",
"Alias_A_x",
"Alias_B_x",
"pmid:method:quality_x"]]
hint_cc_onlylqDF.columns=[
"Uniprot_A",
"Uniprot_B",
"Gene_A",
"Gene_B",
"ORF_A",
"ORF_B",
"Alias_A",
"Alias_B",
"pmid:method:quality"]
hint_cc_onlylqDF["hi/lo"]="low"
hint_cc_onlylqDF["proteinForm"]="co-complex"
#hint_cc_onlylqDF.to_excel("debug/debug_%s_hint_cc_onlylqDF.xlsx" % uu)
hint_cc_onlyhqDF=qdiff2DF[(qdiff2DF["pmid:method:quality_y"].fillna("nan") != "nan") & (qdiff2DF["pmid:method:quality_x"].fillna("nan") != "nan")
& (qdiff2DF["pmid:method:quality_x"].fillna("nan") == qdiff2DF["pmid:method:quality_y"].fillna("nan"))][
["Uniprot_A",
"Uniprot_B",
"Gene_A_y",
"Gene_B_y",
"ORF_A_y",
"ORF_B_y",
"Alias_A_y",
"Alias_B_y",
"pmid:method:quality_y"]]
hint_cc_onlyhqDF.columns=[
"Uniprot_A",
"Uniprot_B",
"Gene_A",
"Gene_B",
"ORF_A",
"ORF_B",
"Alias_A",
"Alias_B",
"pmid:method:quality"]
hint_cc_onlyhqDF["hi/lo"]="high"
hint_cc_onlyhqDF["proteinForm"]="co-complex"
#hint_cc_onlyhqDF.to_excel("debug/debug_%s_hint_cc_onlyhqDF.xlsx" % uu)
hint_ccDF=pd.concat([hint_cc_onlyhqDF, hint_cc_onlylqDF]).sort_index()
hint_DF=pd.concat([hint_biDF, hint_ccDF]).reset_index()
del hint_DF["index"]
return hint_DF
def main(fw, template, organism=None, bi_all = None, bi_hq = None, cc_al = None, cc_hq = None ):
env = Environment(loader = FileSystemLoader(".", encoding='utf8'), autoescape = False)
imageTemplate = env.get_template(template['body'])
evidenceList=[]
hint_DF = read_tsv(bi_all, bi_hq, cc_al, cc_hq)
for row in hint_DF.iterrows():
#Message Body
msgObject = Message(row[0], row[1])
evidenceList.extend([ '\t'.join([pub.DB,pub.ID,pub.Method,pub.Quality]) for pub in msgObject.Publications])
namespace = dict(message=msgObject)
FeedContent = imageTemplate.render(namespace, organism=organism).encode('utf-8')
fw.write(FeedContent)
# Read template (evidence)
env = Environment(loader = FileSystemLoader(".", encoding='utf8'), autoescape = False)
imageTemplate = env.get_template(template['evidence'])
evidences_uq_wk = list(set(evidenceList))
evidences_uq_wk2 = map(lambda x: x.split('\t'), evidences_uq_wk)
evidences = [dict(DB=x[0],ID=x[1],Method=x[2],Quality=x[3]) for x in evidences_uq_wk2]
namespace = dict(Publications=evidences)
FeedContent = imageTemplate.render(namespace).encode('utf-8')
fw.write(FeedContent)
return
if __name__ == '__main__':
parser = OptionParser()
parser.add_option( "-c", type="string", help="config_file", dest="config", default= 'tsv2rdf_hint.json')
(options, args) = parser.parse_args()
#Log setting
argvs = sys.argv
LOG_FILENAME = '%s.log' % argvs[0].split('.')[0]
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s',level=logging.DEBUG)
logger = logging.getLogger()
#File log
file_log = logging.handlers.RotatingFileHandler(filename=LOG_FILENAME)
file_log.setLevel(logging.DEBUG)
file_log.setFormatter(logging.Formatter('%(asctime)s;%(levelname)s:%(message)s'))
#Log handler setting
#logging.getLogger().addHandler(stream_log)
logging.getLogger().addHandler(file_log)
#Reading the configuration file
try:
f = open(options.config ,"r")
except Exception as e:
logger.error("%s: %s" % (e.message,options.config))
sys.exit()
config = json.load(f)
logger.info("Organism: %s" % ",".join([ k for k,v in config['organism'].items() ]))
output_file = config['output_file']
template = config['template']
data_path = config['data_path']
#Output file
if os.getcwd() == '/tmp':
output_file = os.path.join(MOUNT_PATH, output_file)
fw = open(output_file, 'w')
else:
fw = open(output_file, 'w')
#Check template file
if os.path.exists(template['body']) != True:
template['body'] = os.path.join(EXEC_PATH, template['body'])
logger.info("Set template file path: %s" % template['body'])
if os.path.exists(template['evidence']) != True:
template['evidence'] = os.path.join(EXEC_PATH, template['evidence'])
logger.info("Set template file path: %s" % template['evidence'])
if os.path.exists(template['prefix']) != True:
template['prefix'] = os.path.join(EXEC_PATH, template['prefix'])
logger.info("Set template file path: %s" % template['prefix'])
if os.path.exists(data_path) != True:
data_path = os.path.join(MOUNT_PATH, data_path)
logger.info("Set data_path: %s" % data_path)
#Output of prefix
fp = open(template['prefix'], 'r')
prefixes = fp.readlines()
for prefix in prefixes:
fw.write(prefix)
for k,v in config['organism'].items():
if 'binary_hq' in v:
bi_hq = os.path.join(data_path, v['binary_hq'])
else:
bi_hq = None
if 'cocomp_hq' in v:
cc_hq = os.path.join(data_path, v['cocomp_hq'])
else:
cc_hq = None
logger.info("Processing organism: %s" % k)
main(
fw,
template,
organism = k,
bi_all = os.path.join(data_path, v['binary_all']),
bi_hq = bi_hq,
cc_al = os.path.join(data_path, v['cocomp_all']),
cc_hq = cc_hq
)
pass