-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProtParser.py.Apr10
503 lines (379 loc) · 18 KB
/
ProtParser.py.Apr10
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
#!/usr/bin/python
import os
import sys
import glob
import time
import xml.etree.ElementTree as ET # imports element tree function
#tree = ET.parse(xml_file) # uses ET function on the xml file
#root = tree.getroot() # creates root function for calling roots from the xml file. root[0] calls the first child in the element tree
def find_key_word(xml_file, key_word, overall_series_count, current_series_count):
tree = ET.parse(xml_file) # uses ET function on the xml file
root = tree.getroot() # creates root function for calling roots from the xml file. root[0] calls the first child in the element tree
j = overall_series_count - current_series_count
key_value_counter = 0
total_scan_time = '0:0'
#print(overall_series_count)
#print(current_series_count)
#print(j)
#print('\n')
key_word_value = ['-' for i in range(current_series_count+1)]
#print('These are initial key word values')
#print(key_word_value)
while j < overall_series_count + 1:
k = 0
if key_word.split('->')[0] == 'Scan time' and (j < overall_series_count + 1):
try:
total_scan_time = str(int(total_scan_time.split(':')[0]) + int(root[j][0][1][0][1].text[3:9].split(':')[0])
+ (int(total_scan_time.split(':')[1])
+ int(root[j][0][1][0][1].text[3:9].split(':')[1]))
// int(60)) + ':' + str((int(total_scan_time.split(':')[1])
+ int(root[j][0][1][0][1].text[3:9].split(':')[1])) % 60)
key_word_value[key_value_counter] = root[j][0][1][0][1].text[3:9]
except Exception:
k = 100
pass
while k < 100:
l = 0
while l < 100:
try:
if key_word.split('->')[0] == 'PAT':
key_word_value[key_value_counter] = root[j][0][1][0][1].text.split(' ')[8]
if key_word.split('->')[0] == root[j][0][1][k][l][0].text: # check to see if search item has been found
#print('KEY WORD FOUND!')
key_word_value[key_value_counter] = root[j][0][1][k][l][1].text # places the parameter value in the key_wokrd_value array
k = 100 # sets k and l to 100 to break out of the two while loops so that we can move to the next
l = 100 # protocol, which prevents redundancies
except Exception: # prevents error from stopping script when element does not exist
pass
l += 1
k += 1
j += 1
key_value_counter += 1
return (key_word_value, total_scan_time)
def prot_mapper(xml_for_mapping):
#print('XML entering prot_mapper')
#print(xml_for_mapping)
#print('\n')
tree = ET.parse(xml_for_mapping) # uses ET function on the xml file
root = tree.getroot() # creates root function for calling roots from the xml file. root[0] calls the first child in the element tree
i = 0
protocol_num = 0
while i < 100:
try:
check = root[0][0][2][0][0][i].get('name')
i += 1
protocol_num = i
except Exception:
i = 100
pass
i = 0
j = 0
current_series_num = 0
overall_series_count = 0
while i <= protocol_num: # this while loop finds the number of protocols
try:
check = root[0][0][2][0][0][i][j].get('name')
j += 1
except Exception:
current_series_num = j # - tocExists
overall_series_count = overall_series_count + j
if current_series_num != 0:
create_parameter_matrices(xml_for_mapping, i, overall_series_count, current_series_num) # middle argument may need to be i+1
i += 1
j = 0
current_series_num = 0
pass
return
def create_parameter_matrices(xml_file, prot_num, overall_series_num, current_series_num):
print('This is file name for parsing:')
print(xml_file)
print('\n')
tree = ET.parse(xml_file) # uses ET function on the xml file
root = tree.getroot() # creates root function for calling roots from the xml file. root[0] calls the first child in the element tree
if os.path.isfile('customParameters.txt') == True and os.path.isfile('complexParameters.txt') == True:
custom_parameter_list = [line.rstrip('\n') for line in open('customParameters.txt')] # creates a list by reading parameters from text file
complex_parameter_list = [line.rstrip('\n') for line in open('complexParameters.txt')]
elif os.path.isfile(os.environ['WIKIPROTPATH'] + 'customParameters.txt') == True \
and os.path.isfile(os.environ['WIKIPROTPATH'] + 'complexParameters.txt') == True:
custom_parameter_list = [line.rstrip('\n') for line in open(os.environ['WIKIPROTPATH']
+ '/customParameters.txt')] # creates a list by reading parameters from text file
complex_parameter_list = [line.rstrip('\n') for line in open(os.environ['WIKIPROTPATH']
+ '/complexParameters.txt')]
else:
print('customParameters.txt and/or complexParameters.txt not found!')
sys.exit()
custom_parameter_matrix = [['-' for x in range(len(custom_parameter_list)+1)] for y in range(current_series_num + 1)] # creates a 2D array with size parameter list by protocol number
complex_parameter_matrix = [['-' for x in range(current_series_num + 1)] for y in range(len(complex_parameter_list) + 1)]
custom_parameter_matrix[0][0] = 'Series'
complex_parameter_matrix[0][0] = 'Sequence Parameters'
i = 0
total_scan_time = 'Scan time not available'
while i < len(custom_parameter_list): #populates custom_parameter_matrix with all the parameters in the first row
custom_parameter_matrix[0][i+1] = custom_parameter_list[i].split('->')[1]
i += 1
j = 0
while j < len(complex_parameter_list): #populates complex_parameter_matrix with all the parameters in the first column
complex_parameter_matrix[j+1][0] = complex_parameter_list[j].split('->')[1]
j += 1
m = 0
while m < current_series_num:
custom_parameter_matrix[m+1][0] = root[0][0][2][0][0][prot_num][m].get('name')
complex_parameter_matrix[0][m + 1] = root[0][0][2][0][0][prot_num][m].get('name')
m += 1
#print('Before population')
#print(custom_parameter_matrix)
#print('\n')
#print(complex_parameter_matrix)
#print('\n')
custom_parameter_counter = 0
for parameter in custom_parameter_list: #loop through all parameters in custom_parameter_list to find all the values for each series
total_scan_time = find_key_word(xml_file, parameter, overall_series_num, current_series_num)[1]
#elif parameter.split('->')[0] == 'Matrix':
# matrix1Looper = 1
# while matrix1Looper < protocolCount + 1: # multiplies the values of the phase and base resolution and inputs to the Matrix1
# try:
# custom_parameter_matrix[matrix1Looper][8] = str(custom_parameter_matrix[matrix1Looper][6]) + 'X ' + str(
# int(custom_parameter_matrix[matrix1Looper][6]) * int(custom_parameter_matrix[matrix1Looper][7].split("%")[0]) / 100)
# except Exception:
# pass
# matrix1Looper += 1
if parameter.split('->')[0] == 'Notes':
pass
else:
#print(find_key_word(xml_file, parameter, overall_series_num, current_series_num)[0])
key_word_values = ['-' for i in range(current_series_num)]
key_word_values = find_key_word(xml_file, parameter, overall_series_num, current_series_num)[0]
k = 1
while k < current_series_num + 1: #loops through the 1D array of the parameter values for each series
custom_parameter_matrix[k][custom_parameter_counter+1] = key_word_values[k]
k += 1
custom_parameter_counter += 1
complex_parameter_counter = 0
for parameter in complex_parameter_list: #loop through all parameters in complex_parameter_list to find all the values for each series
total_scan_time = find_key_word(xml_file, parameter, overall_series_num, current_series_num)[1]
if parameter.split('->')[0] == 'Notes':
pass
else:
key_word_values = ['-' for i in range(current_series_num)]
key_word_values = find_key_word(xml_file, parameter, overall_series_num, current_series_num)[0]
k = 1
while k < current_series_num + 1: #loops through the 1D array of the parameter values for each series
complex_parameter_matrix[complex_parameter_counter+1][k] = key_word_values[k]
k += 1
complex_parameter_counter += 1
#print('After searching keywords')
#print(custom_parameter_matrix)
#print('\n')
#print(complex_parameter_matrix)
#print('\n')
write_matrices_to_html_file(xml_file, custom_parameter_matrix, complex_parameter_matrix, total_scan_time, len(custom_parameter_list), len(complex_parameter_list), prot_num, current_series_num)
return
def write_matrices_to_html_file(xml_file, custom_parameter_matrix, complex_parameter_matrix, total_scan_time, custom_parameter_num, complex_parameter_num, prot_num, series_num):
tree = ET.parse(xml_file) # uses ET function on the xml file
root = tree.getroot() # creates root function for calling roots from the xml file. root[0] calls the first child in the element tree
seriesName = xml_file.split('.xml')[0]+'-'+root[0][0][2][0][0][prot_num].get('name')
seriesNameFile = seriesName.replace(" ", "_")
seriesNameFile = seriesName.replace("/", "_") + '.htm'
currentPath = os.getcwd()
seriesNameFile = currentPath + "/" + seriesNameFile
toc_exists = 0
if root[0].tag == 'PrintTOC':
toc_exists = 1
else:
toc_exists = 0
if os.path.exists(seriesNameFile):
f = open(seriesNameFile, 'r')
#print('WILL ADD TO CURRENT HTML')
f.seek(0)
s = f.read()
R = s.split('</body>')
f.close()
f = open(seriesNameFile, 'w+')
f.write(R[0])
else:
f = open(seriesNameFile, 'w+')
#print('CREATING NEW HTML')
f.write('<!DOCTYPE html> \n')
f.write('<html>\n')
f.write('<head>\n')
f.write('<style>\n')
f.write('table, th, td {\n')
f.write('\t border: 1px solid black;\n')
f.write('}\n')
f.write('th, td { \n \t padding: 15px;\n}')
f.write('</style>\n')
f.write('</head>\n')
f.write('<body>\n')
if toc_exists == 1:
f.write('<h1>')
f.write(root[0][0][2][0][0][0].get('name')+'-'+root[0][0][2][0][0][prot_num].get('name'))
f.write(' (Total scan time: ' + total_scan_time.split(':')[0] + ':' + total_scan_time.split(':')[1] + ')')
f.write('</h1>\n')
f.write('<h2>')
f.write('Overview')
f.write('</h2>\n')
else:
f.write('<h1>')
f.write(root[0][0][1][0][0].text[35:49]+'-'+root[0][0][2][0][0][prot_num].get('name'))
f.write(' (Total scan time: ' + total_scan_time.split(':')[0] + ':' + total_scan_time.split(':')[1] + ' min)')
f.write('</h1>\n')
f.write('<table style="width:100%">\n')
writeCounterColumn1 = 0
while writeCounterColumn1 < 1:
writeCounterRow1 = 0
f.write('<table style="background-color:#FFFFE0;"> \n')
f.write('<tr style="background-color:#BDB76B;color:ffffff;"> \n')
while writeCounterRow1 < custom_parameter_num:
f.write('<th>')
f.write(str(custom_parameter_matrix[writeCounterColumn1][writeCounterRow1]))
f.write('</th>')
writeCounterRow1 += 1
f.write('</tr>')
f.write('\n')
writeCounterColumn1 += 1
writeCounterColumn1 = 1
while writeCounterColumn1 < series_num + 1:
writeCounterRow1 = 0
f.write('<tr>')
while writeCounterRow1 < custom_parameter_num:
f.write('<td>')
f.write(custom_parameter_matrix[writeCounterColumn1][writeCounterRow1])
f.write('</td>')
writeCounterRow1 += 1
f.write('</tr>')
f.write('\n')
writeCounterColumn1 += 1
f.write('</table>\n')
f.write('<br>')
f.write('</br>')
f.write('\n')
f.write('<table style="width:100%">\n')
writeCounterColumn2 = 0
f.write('<h2>\n')
f.write('Detailed')
f.write('\n')
f.write('</h2>\n')
while writeCounterColumn2 < 1:
writeCounterRow2 = 0
f.write('<table style="background-color:#CAE8FC;"> \n')
f.write('<tr style="background-color:#0613D7;color:#FBFBFC;"> \n')
while writeCounterRow2 < series_num + 1:
f.write('<th>')
f.write(complex_parameter_matrix[writeCounterColumn2][writeCounterRow2])
f.write('</th>')
writeCounterRow2 += 1
f.write('</tr>')
f.write('\n')
writeCounterColumn2 += 1
writeCounterColumn2 = 1
while writeCounterColumn2 < complex_parameter_num: # maybe help?- 1:
writeCounterRow2 = 0
f.write('<tr>')
#while writeCounterRow2 < series_num + 1:#add one?
while writeCounterRow2 < len(complex_parameter_matrix[writeCounterColumn2]):
f.write('<td>')
f.write(complex_parameter_matrix[writeCounterColumn2][writeCounterRow2])
f.write('</td>')
writeCounterRow2 += 1
f.write('</tr>')
f.write('\n')
writeCounterColumn2 += 1
f.write('</table>\n')
f.write('\n')
f.write('<br>')
f.write('</br>')
f.write('<textarea rows="6" cols="140">')
f.write('Insert notes here')
f.write('</textarea>')
f.write('<br>')
f.write('</br>')
f.write('<textarea rows="6" cols="140">')
f.write('Indications')
f.write('</textarea>')
f.write('<br>')
f.write('</br>')
f.write('<img src=')
f.write('"')
f.write(xml_file.split('xml')[0])
f.write('jpg')
f.write('"')
f.write(' alt="Placer Image" style="width:304px;height:228px;">')
f.write('\n')
f.write('\n')
f.write('<br>')
f.write('</br>')
f.write('</body>\n')
f.write('</html>\n')
f.close()
return
def parse_all_prots():
currentPath = os.getcwd()
path = currentPath + "/*.xml"
for gname in glob.glob(path):
tree = ET.parse(gname.split('/')[len(gname.split('/'))-1])
root = tree.getroot()
g = os.path.getmtime(gname)
gname = gname.split('/')[len(gname.split('/'))-1]
seriesName = gname.split('.xml')[0] + '-' + root[0][0][2][0][0][0].get('name')
htmlFile = seriesName.replace(" ", "_")
htmlFile = seriesName.replace("/", "_") + '.htm'
if os.path.exists(htmlFile):
f = os.path.getmtime(htmlFile)
if f < g:
#print(gname)
prot_mapper(gname)
else:
print('ProtParser has already run on ' + gname + '\n')
else:
#print(gname)
prot_mapper(gname)
return
def move_through_directories_recursively():
print('Will now run ProtParser on all xml files in directories in form like this: '
'"$WIKIPROTPATH/Scanners/SCANNER_NAME/NEURO"')
owd = os.environ['WIKIPROTPATH'] + 'Scanners/'
os.chdir(owd)
directoryList1 = next(os.walk('.'))[1]
i = 0
while (i < len(directoryList1)):
if (directoryList1[i] != 'apps'):
if (directoryList1[i] != 'files'):
if (directoryList1[i] != 'uploads'):
if (directoryList1[i] != '.idea'):
os.chdir(directoryList1[i])
# print(directoryList1[i])
directoryList2 = next(os.walk('.'))[1]
sowd = os.getcwd()
j = 0
while (j < len(directoryList2)):
if (directoryList2[j] != 'apps'):
if (directoryList2[j] != 'files'):
if (directoryList2[j] != 'uploads'):
if (directoryList2[j] != '.idea'):
branch_directory = directoryList2[j]
os.chdir(branch_directory)
parse_all_prots()
os.chdir(sowd)
j = j + 1
os.chdir(owd)
i = i + 1
return
def parsing_options():
if len(sys.argv) != 2:
print('"python $WIKIPROTPATH/ProtParser.py <xml_file_name> or <all_in_cwd> or <all_directories>"')
sys.exit()
else:
xml_file = sys.argv[1]
if xml_file.endswith('all_in_cwd') or xml_file.endswith('ALL_IN_CWD'):
print('All xml files in current directory will be run through protParser now!')
parse_all_prots()
elif xml_file.endswith('all_directories') or xml_file.endswith('ALL_DIRECTORIES'):
move_through_directories_recursively()
else:
if xml_file.endswith('.xml'):
prot_mapper(xml_file)
else:
print('File name must end in .xml. ')
print('"python $WIKIPROTPATH/ProtParser.py <xml_file_name> or <all_in_cwd> or <all_directories>"')
return
parsing_options()