/
translationload.py
481 lines (400 loc) · 14.3 KB
/
translationload.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
'''
#
# Purpose:
#
# To load new/replace existing translation records into Translation structures:
#
# MGI_TranslationType
# MGI_Translation
#
# Assumes:
#
# Side Effects:
#
# None
#
# Input(s):
#
# A tab-delimited file in the format:
# field 1: Acc ID of Good Name
# field 2: Good Name
# field 3: Bad Name
# field 4: Created By
#
# Parameters:
#
# processing modes:
# fullest - delete the exisiting Translation Type and the existing Translations (if they exist)
# load new Translations
#
# full - delete the existing Translations (but not the Translation Type) (if they exist)
# load new Translations
#
# preview - perform all record verifications but do not load the data or
# make any changes to the database.
# used for testing or to preview the load.
# add - do not delete existing Translations, add translations to the Translation Type,
# starting with the next available seqNum
#
# Output:
#
# 2 BCP files:
#
# MGI_TranslationType.bcp Translation Type record
# MGI_Translation.bcp Translation records
#
# Diagnostics file of all input parameters and SQL commands
# Error file
#
# Processing:
#
# 1. Verify Mode.
# if mode = fullest: delete MGI_TranslationType records
# if mode = full: delete MGI_Translation records
# if mode = add: do not delete anything
# if mode = preview: set "DEBUG" to True
#
# 2. Verify the Translation Type.
#
# If the Translation Type record exists and mode = fullest then
# delete Translation Type record (and hence, all translations)
# create a new Translation Type record
#
# If the Translation Type record exists and mode = full then
# delete translation records for that translation type
#
# else if the Translation Type record exists and mode = preview then
# do nothing here
#
# For each line in the input file:
#
# 1. Verify the Acc ID of the object (good name ) is valid.
# The MGI Type of the Acc ID must be the same as the MGI Type of the Translation Type.
# Duplciate Acc IDs are allowed (a good name can have more than one bad name).
# If the verification fails, report the error and skip the record.
#
# 2. Create MGI_Translation record for the MGI object.
#
# History:
#
# sc 07/17/2023 - update to latest postgres and python - add 'add' mode
#
# lec 03/21/2006
# lec 03/21/2006
# - to support MGI 3.44/SNP data load; added "fullest" mode
#
# lec 02/13/2003
# - part of JSAM
#
'''
import sys
import os
import string
import subprocess
import db
import mgi_utils
import loadlib
#globals
user = os.environ['MGD_DBUSER']
passwordFileName = os.environ['MGD_DBPASSWORDFILE']
mode = os.environ['TRANSMODE']
inputFileName = os.environ['TRANSINPUTFILE']
outputFileDir = os.environ['TRANSOUTPUTDIR']
transTypeName = os.environ['TRANSTYPENAME']
transMGIType = os.environ['TRANSMGITYPE']
transCompression = os.environ['TRANSCOMPRESSION']
vocabName = os.environ['VOCABNAME']
DEBUG = 0 # set DEBUG to false unless preview mode is selected
BCP_COMMAND = os.getenv('PG_DBUTILS') + '/bin/bcpin.csh'
inputFile = '' # file descriptor
diagFile = '' # file descriptor
errorFile = '' # file descriptor
transTypeFile = '' # file descriptor
transFile = '' # file descriptor
diagFileName = '' # file name
errorFileName = '' # file name
transTypeFileName = '' # file name
transFileName = '' # file name
bcpdelim = '|'
transTypeKey = 0 # primary key of translation type record
mgiTypeKey = 0 # primary key of translation type mgi type
vocabKey = 0 # primary key of translation's vocabulary
newTransType = 0 # flags whether to create new Trans Type record
loaddate = loadlib.loaddate
# sequence number of bad name in translation list
# if 'add' mode, this is reset to max(sequenceNum) + 1
seqNum = 1
def exit(status, message = None):
'''
# requires: status, the numeric exit status (integer)
# message (string)
#
# effects:
# Print message to stderr and exits
#
# returns:
#
'''
if message is not None:
sys.stderr.write('\n' + str(message) + '\n')
try:
inputFile.close()
diagFile.write('\n\nEnd Date/Time: %s\n' % (mgi_utils.date()))
errorFile.write('\n\nEnd Date/Time: %s\n' % (mgi_utils.date()))
diagFile.close()
errorFile.close()
transTypeFile.close()
transFile.close()
except:
pass
db.useOneConnection()
sys.exit(status)
def init():
'''
# requires:
#
# effects:
# 1. Processes command line options
# 2. Initializes local DBMS parameters
# 3. Initializes global file descriptors/file names
# 4. Initializes global keys
#
# returns:
#
'''
global inputFile, diagFile, errorFile, errorFileName, diagFileName
global transTypeFile, transFile
global transTypeFileName, transFileName
db.useOneConnection(1)
db.set_sqlUser(user)
db.set_sqlPasswordFromFile(passwordFileName)
# the default output file names are bases on 'inputFileName'
head, fileName = os.path.split(inputFileName)
# rename 'head'
head = outputFileDir
fdate = mgi_utils.date('%m%d%Y') # current date
diagFileName = head + '/' + fileName + '.' + fdate + '.diagnostics'
print(diagFileName)
errorFileName = head + '/' + fileName + '.' + fdate + '.error'
print(errorFileName)
transTypeFileName = head + '/' + fileName + '.' + fdate + '.MGI_TranslationType.bcp'
print(transTypeFileName)
transFileName = head + '/' + fileName + '.' + fdate + '.MGI_Translation.bcp'
print(transFileName)
try:
inputFile = open(inputFileName, 'r')
except:
exit(1, 'Could not open file %s\n' % inputFileName)
try:
diagFile = open(diagFileName, 'w')
except:
exit(1, 'Could not open file %s\n' % diagFileName)
try:
errorFile = open(errorFileName, 'w')
except:
exit(1, 'Could not open file %s\n' % errorFileName)
try:
transTypeFile = open(transTypeFileName, 'w')
except:
exit(1, 'Could not open file %s\n' % transTypeFileName)
try:
transFile = open(transFileName, 'w')
except:
exit(1, 'Could not open file %s\n' % transFileName)
# Log all SQL
db.set_sqlLogFunction(db.sqlLogAll)
# Set Log File Descriptor
#db.set_sqlLogFD(diagFile)
db.set_commandLogFile(diagFileName)
diagFile.write('Start Date/Time: %s\n' % (mgi_utils.date()))
diagFile.write('Server: %s\n' % (db.get_sqlServer()))
diagFile.write('Database: %s\n' % (db.get_sqlDatabase()))
diagFile.write('Input File: %s\n' % (inputFileName))
errorFile.write('Start Date/Time: %s\n\n' % (mgi_utils.date()))
def verifyMode():
'''
# requires:
#
# effects:
# Verifies the processing mode is valid. If it is not valid,
# the program is aborted.
# Sets globals based on processing mode.
# Deletes data based on processing mode.
#
# returns:
# nothing
#
'''
global DEBUG
if mode == 'preview':
DEBUG = 1
elif mode not in ['fullest', 'full', 'add']:
exit(1, 'Invalid Processing Mode: %s\n' % (mode))
def processTranslationType():
'''
# requires:
#
# effects:
# checks if a TranslationType record exists
# if yes and mode == 'fullest', delete it and add a new record
# if yes and mode == 'full', delete existing translations of that translation type
# if yes and mode == 'add', do not delete anything, add what is in the file to that translationtype
# if no, creates a new one
# sets global transTypeKey, mgiTypeKey, vocabKey
#
# returns:
#
'''
global transTypeKey, mgiTypeKey, vocabKey, newTransType, seqNum
newTransType = 0
results = db.sql('''select _TranslationType_key, _MGIType_key
from MGI_TranslationType
where translationType = '%s' ''' % (transTypeName), 'auto')
# if Translation Type exists....
if len(results) > 0:
transTypeKey = results[0]['_TranslationType_key']
mgiTypeKey = results[0]['_MGIType_key']
# delete any existing translation type record
if mode == 'fullest':
db.sql('delete from MGI_TranslationType where _TranslationType_key = %s' % (transTypeKey), None, execute = not DEBUG)
newTransType = 1
# delete any existing translation records (but leave translation type alone)
elif mode == 'full':
db.sql('delete from MGI_Translation where _TranslationType_key = %s' % (transTypeKey), None, execute = not DEBUG)
# get the max sequence number
elif mode == 'add':
results = db.sql('''select max(sequenceNum)as maxSeq from MGI_Translation''' , 'auto')
seqNum = results[0]['maxSeq'] + 1
print('seqNum: %s' % seqNum)
# else, create a new Translation Type
else:
newTransType = 1
if newTransType:
results = db.sql('''select max(_TranslationType_key) + 1 as maxKey
from MGI_TranslationType''', 'auto')
transTypeKey = results[0]['maxKey']
if transTypeKey is None:
transTypeKey = 1000
results = db.sql('''select _MGIType_key from ACC_MGIType where name = '%s' ''' % (transMGIType), 'auto')
mgiTypeKey = results[0]['_MGIType_key']
newTransType = 1
results = db.sql('''select _Vocab_key
from VOC_Vocab where name = '%s' ''' % (vocabName), 'auto')
if len(results) > 0:
vocabKey = results[0]['_Vocab_key']
else:
vocabKey = 0
def processFile():
'''
# requires:
#
# effects:
# Reads input file
# Verifies and Processes each line in the input file
#
# returns:
# nothing
#
'''
global seqNum
results = db.sql('''select max(_Translation_key) + 1 as maxKey
from MGI_Translation''', 'auto')
transKey = results[0]['maxKey']
if transKey is None:
transKey = 1000
lineNum = 0
# For each line in the input file
for line in inputFile.readlines():
error = 0
lineNum = lineNum + 1
# Split the line into tokens
tokens = str.split(line[:-1], '\t')
try:
objectID = tokens[0]
objectDescription = tokens[1]
term = tokens[2]
userID = tokens[3]
except:
exit(1, 'Invalid Line (%d): %s\n' % (lineNum, line))
continue
if vocabKey > 0:
objectKey = loadlib.verifyTerm(objectID, vocabKey, objectDescription, lineNum, errorFile)
else:
objectKey = loadlib.verifyObject(objectID, mgiTypeKey, objectDescription, lineNum, errorFile)
userKey = loadlib.verifyUser(userID, lineNum, errorFile)
if objectKey == 0 or userKey == 0:
# set error flag to true
error = 1
# if errors, continue to next record
if error:
continue
# if no errors, process
# add term to translation file
bcpWrite(transFile, [transKey, transTypeKey, objectKey, term, seqNum, userKey, userKey, loaddate, loaddate])
transKey = transKey + 1
seqNum = seqNum + 1
# end of "for line in inputFile.readlines():"
if newTransType:
bcpWrite(transTypeFile, [transTypeKey, mgiTypeKey, vocabKey, transTypeName, transCompression, 0, userKey, userKey, loaddate, loaddate])
def bcpWrite(fp, values):
'''
#
# requires:
# fp; file pointer of bcp file
# values; list of values
#
# effects:
# converts each value item to a string and writes out the values
# to the bcpFile using the appropriate delimiter
#
# returns:
# nothing
#
'''
# convert all members of values to strings
strvalues = []
for v in values:
strvalues.append(str(v))
#fp.write('%s\n' % (str.join(strvalues, bcpdelim)))
fp.write('%s\n' % (bcpdelim.join(strvalues)))
def bcpFiles():
'''
# requires:
#
# effects:
# BCPs the data into the database
#
# returns:
# nothing
#
'''
transTypeFile.close()
transFile.close()
bcpI = '%s %s %s' % (BCP_COMMAND, db.get_sqlServer(), db.get_sqlDatabase())
bcpII = '"|" "\\n" mgd'
bcp1 = '%s %s "/" %s %s' % (bcpI, 'MGI_TranslationType', transTypeFileName, bcpII)
bcp2 = '%s %s "/" %s %s' % (bcpI, 'MGI_Translation', transFileName, bcpII)
diagFile.write('%s\n' % bcp1)
diagFile.write('%s\n' % bcp2)
if DEBUG:
return
for bcpCmd in [bcp1, bcp2]:
diagFile.write('%s\n' % bcpCmd)
result = subprocess.run(bcpCmd, shell=True, capture_output=True, text=True)
stdout = result.stdout
stderr = result.stderr
statusCode = result.returncode
if statusCode != 0:
msg = '%s statusCode: %s stderr: %s\n' % (bcpCmd, statusCode, stderr)
diagFile.write(msg)
return statusCode
return statusCode
#
# Main
#
init()
verifyMode()
processTranslationType()
processFile()
bcpFiles()
exit(0)