-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateReport.py
More file actions
725 lines (560 loc) · 29.7 KB
/
CreateReport.py
File metadata and controls
725 lines (560 loc) · 29.7 KB
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
import json
import sqlite3
from datetime import datetime
import time
from urllib.parse import unquote
from fontTools.ttLib import TTFont
import re
import pandas as pd
DEFAULT_FONT = "LMSans"
MISSING_CHAR = '▯'
MISSING_CHAR_FONT = "Symbola"
workflow_types = {"gen": "General Lipidomics", "di": "Direct Infusion", "sep": "Separation", "img": "Imaging"}
accepted_languages = {"en", "jp"}
current_language_dictionary = None
current_language = "en"
font_sets = {
"LMSans": [
["LMSansR", "lmsans10-regular.otf"],
["LMSansB", "lmsans10-bold.otf"],
["LMSansI", "lmsans10-oblique.otf"],
["LMSansBI", "lmsans10-boldoblique.otf"],
],
"HaranoAjiMincho": [
["HaranoAjiMinchoR", "HaranoAjiMincho-regular.otf"],
["HaranoAjiMinchoB", "HaranoAjiMincho-bold.otf"],
["HaranoAjiMinchoI", "HaranoAjiMincho-regular.otf"],
["HaranoAjiMinchoBI", "HaranoAjiMincho-bold.otf"],
],
"NotoSansDevanagari": [
["NotoSansDevanagariR", "NotoSansDevanagari-Regular.ttf"],
["NotoSansDevanagariB", "NotoSansDevanagari-Bold.ttf"],
["NotoSansDevanagariI", "NotoSansDevanagari-Regular.ttf"],
["NotoSansDevanagariBI", "NotoSansDevanagari-Bold.ttf"],
],
"DejaVuSans": [
["DejaVuSansR", "DejaVuSans.ttf"],
["DejaVuSansB", "DejaVuSans-Bold.ttf"],
["DejaVuSansI", "DejaVuSans-Oblique.ttf"],
["DejaVuSansBI", "DejaVuSans-BoldOblique.ttf"],
],
"Symbola": [
["SymbolaR", "Symbola.ttf"],
["SymbolaB", "Symbola.ttf"],
["SymbolaI", "Symbola.ttf"],
["SymbolaBI", "Symbola.ttf"],
],
}
fallback_fonts = ["LMSans", "HaranoAjiMincho", "NotoSansDevanagari", "DejaVuSans", "Symbola"]
font_types = {"R", "B", "I", "BI"}
font_preamble = []
FONT_COVERAGE = {}
for font_set_name, font_set in font_sets.items():
font_preamble.append(f"""
\\newfontfamily\\{font_set_name}[
Path = ./fonts/,
UprightFont = {font_set[0][1]},
BoldFont = {font_set[1][1]},
ItalicFont = {font_set[2][1]},
BoldItalicFont = {font_set[3][1]}
]{{{font_set_name}}}\n\n
""")
for name, path in font_set:
ttf = TTFont("./fonts/" + path)
cmap = ttf["cmap"].getBestCmap()
FONT_COVERAGE[name] = set(cmap.keys())
ttf.close()
#font_preamble += [f"\\setmainfont{{{DEFAULT_FONT}}}"]
font_preamble = "".join(font_preamble)
def split_verison_number(text_version):
version_array = [1, 0, 0];
text_version = text_version.split(".")
if len(text_version) != 3 or len(text_version[0]) < 2:
return version_array
text_version[0] = text_version[0][1:]
if not text_version[0].isdigit() or not text_version[1].isdigit() or not text_version[2].isdigit():
return version_array
version_array[0] = int(text_version[0])
version_array[1] = int(text_version[1])
version_array[2] = int(text_version[2])
return version_array
def is_first_version_higher(first_version, version_to_test):
if first_version == None or version_to_test == None: return False
first_version_array = split_verison_number(first_version)
version_to_test_array = split_verison_number(version_to_test)
return (
(version_to_test_array[0] < first_version_array[0]) or
(version_to_test_array[0] == first_version_array[0]) and (
(version_to_test_array[1] < first_version_array[1]) or
(version_to_test_array[1] == first_version_array[1]) and (
(version_to_test_array[2] < first_version_array[2])
)
)
)
def get_workflow_type(mycursor, table_prefix, uid, entry_id):
sql = "SELECT form, fields FROM %sentries WHERE user_id = ? AND id = ?;" % table_prefix
mycursor.execute(sql, (uid, entry_id))
result = json.loads(mycursor.fetchone()["fields"])
for field in result["pages"][0]["content"]:
if field["name"] == "workflowtype":
workflow_type = field["value"] if field["value"] in workflow_types else "gen"
return workflow_type
def fill_report_fields(mycursor, table_prefix, uid, entry_id, titles, report_fields, version):
global current_language_dictionary, current_language
sql = "SELECT form, fields FROM %sentries WHERE user_id = ? AND id = ?;" % table_prefix
mycursor.execute(sql, (uid, entry_id))
result = json.loads(mycursor.fetchone()["fields"])
workflow_type = "gen"
visible = {}
conditions = {}
choice_to_field = {}
field_map = {}
form_version = result["version"] if "version" in result else "v0.9.9"
version.append(form_version)
for field in result["pages"][0]["content"]:
if field["name"] == "workflowtype":
workflow_type = field["value"] if field["value"] in workflow_types else "gen"
if "language" in result and result["language"] in accepted_languages:
current_language = result["language"]
if current_language_dictionary == None and current_language != "en":
df_checklist = pd.read_excel(f"languages/{current_language}/checklist.xlsx")
df_lipid_class = pd.read_excel(f"languages/{current_language}/lipid-class.xlsx")
df_sample = pd.read_excel(f"languages/{current_language}/sample.xlsx")
df_meta = pd.read_excel(f"languages/{current_language}/meta.xlsx")
current_language_dictionary = {row["name"]: [row["label"], row["description"]] for i, row in df_checklist.iterrows()}
current_language_dictionary = {**{row["name"]: [row["label"], row["description"]] for i, row in df_lipid_class.iterrows()}, **current_language_dictionary}
current_language_dictionary = {**{row["name"]: [row["label"], row["description"]] for i, row in df_sample.iterrows()}, **current_language_dictionary}
current_language_dictionary = {**{row["name"]: [row["label"], "NaN"] for i, row in df_meta.iterrows()}, **current_language_dictionary}
current_language_dictionary = {k: [v1 if pd.notna(v1) else None, v2 if pd.notna(v2) else None] for k, (v1, v2) in current_language_dictionary.items()}
for page in result["pages"]:
for field in page["content"]:
field_name = field["name"]
visible[field_name] = True
if current_language_dictionary != None and current_language != "en":
field["label"], field["description"] = translate(field)
field_map[field_name] = field
if field["type"] in {"select", "multiple"} and "choice" in field:
if field["type"] == "select":
if sum(e["value"] for e in field["choice"]) != 1:
for e in field["choice"]: e["value"] = 0
field["choice"][0]["value"] = 1
for choice in field["choice"]:
if "name" in choice:
choice["label"], choice["description"] = translate(choice)
field_map[choice["name"]] = choice
choice_to_field[choice["name"]] = field_name
else:
choice_to_field[field_name] = field_name
# check for any logical conditions
if "condition" not in field or len(field["condition"]) == 0: continue
condition = []
for condition_and in field["condition"].split("|"):
conjunction = []
for con in condition_and.split("&"):
single_condition = None
operator = "="
if con.find("~") != -1:
single_condition = con.split("~")
operator = "~"
else:
single_condition = con.split("=")
if len(single_condition) != 2: continue;
key, value = single_condition
l = len(value)
if value[0] == "'" and value[-1] == "'":
value = value[1 : -1]
else:
try:
value = float(value)
except:
continue
conjunction.append([key, operator, value])
condition.append(conjunction)
conditions[field_name] = condition
for page in result["pages"]:
for field in page["content"]:
field_name = field["name"]
if "type" in field and field["type"] in {"number", "select"} \
and (("required" not in field) or (field["required"] == 0)) \
and (("activated" not in field) or (field["activated"] == 0)):
#and (is_first_version_higher(form_version, "v.2.4.0") and (("activated" not in field) or (field["activated"] == 0))):
visible[field_name] = False
continue
if "condition" not in field or len(field["condition"]) == 0: continue
visible[field_name] = False
for condition_and in conditions[field_name]:
condition_met = True
for single_condition in condition_and:
key, operator, value = single_condition
conditional_field_key = choice_to_field[key]
conditional_field = field_map[conditional_field_key]
if conditional_field["type"] in {"number", "select"}:
#if is_first_version_higher(form_version, "v.2.4.0") and conditional_field["type"] in {"number", "select"}:
condition_met &= ("required" not in conditional_field) or (conditional_field["required"] == 1) or (("activated" in conditional_field) and (conditional_field["activated"] == 1))
field_value = field_map[key]["value"] if ("type" not in field_map[key] or field_map[key]["type"] != "number") else float(field_map[key]["value"])
condition_met &= (conditional_field_key in visible and visible[conditional_field_key]) and ((operator == "=" and field_value == value) or (operator == "~" and field_value != value))
visible[field_name] |= condition_met
for page in result["pages"]:
page_title = translate_meta(page, page["title"])
title_name = page["name"] if "name" in page else ""
if type(page_title) == dict and page_title != None and "workflowtype" in field_map and "value" in field_map["workflowtype"]:
wt = field_map["workflowtype"]["value"]
if wt in page_title:
page_title = page_title[wt]
title_name += "#" + wt
elif "default" in page_title:
page_title = page_title["default"]
title_name += "#default"
else:
page_title = "X"
page_title = font_encoding(translate_meta(title_name, page_title))
titles.append(page_title)
report_fields.append([])
values = {}
for field in page["content"]:
if "type" not in field or "name" not in field or "label" not in field: continue
if field["name"] in visible and not visible[field["name"]]: continue
if field["type"] == "text":
if field["label"][:5].lower() == "other":
if field["label"][6:].lower() in values:
values[field["label"][6:].lower()][-1] = field["value"]
else:
if len(field["value"]) > 0:
values[field["label"].lower()] = [field["value"]]
report_fields[-1].append([field["label"], ""])
elif field["type"] == "number":
values[field["label"].lower()] = [str(field["value"])]
report_fields[-1].append([field["label"], ""])
elif field["type"] in {"select", "multiple"}:
choice_values = []
for choice in field["choice"]:
if choice["value"] == 1:
choice_values.append(choice["label"])
values[field["label"].lower()] = choice_values
if len(choice_values) > 0:
report_fields[-1].append([field["label"], ""])
elif field["type"] == "table":
values[field["label"].lower()] = ["!!!TABLE!!!%s!!!CONTENT!!!%s" % (field["columns"], field["value"])]
#values[field["label"].lower()] = [field["value"]]
report_fields[-1].append([field["label"], ""])
for i in range(len(report_fields[-1])):
key = report_fields[-1][i][0].lower()
if key in values:
report_fields[-1][i][1] = ", ".join(values[key])
def has_matching_xml_tags(s):
# Regex to find all tags (both opening and closing)
tag_pattern = re.finditer(r"</?([a-zA-Z0-9]+)[^>]*>", s)
stack = []
for tag in tag_pattern:
full_tag = tag.group(0) # Full matched tag (e.g., "<sup>" or "</sup>")
tag_name = tag.group(1) # Extracted tag name (e.g., "sup")
if full_tag.startswith("</"): # Closing tag
if not stack or stack[-1] != tag_name:
return False # Mismatched closing tag
stack.pop() # Match found → Pop from stack
elif not full_tag.endswith("/>"): # Opening tag (excluding self-closing)
stack.append(tag_name) # Push opening tag to stack
return len(stack) == 0 # True if all tags are matched
def contains_xml_tags(s):
return bool(re.search(r"<[^<>]+>", s))
def unicoding(t):
def encode(text):
return "".join(["{\\ }" if ord(c) == 32 else "\\char\"%s" % hex(ord(c))[2:].upper() for c in text])
encoded = ""
if contains_xml_tags(t):
if not has_matching_xml_tags(t):
encoded = "".join(t)
else:
tokens = [t]
for pattern in ["<sup>", "</sup>", "<sub>", "</sub>"]:
test_tokens = tokens
tokens = []
for token in test_tokens:
split_tokens = token.split(pattern)
for i, token in enumerate(split_tokens):
if i > 0: tokens.append(pattern)
if len(token) > 0: tokens.append(token)
for i in range(len(tokens)):
if tokens[i] == "<sup>": tokens[i] = "\\textsuperscript{"
elif tokens[i] == "<sub>": tokens[i] = "\\textsubscript{"
elif tokens[i] in {"</sub>", "</sup>"}: tokens[i] = "}"
else: tokens[i] = encode(tokens[i])
encoded = "".join(tokens)
else:
encoded = encode(t)
return encoded
def font_encoding(text, font_type = "R", size = None, to_string = True):
if font_type not in font_types:
raise Exception(f"Font type '{font_type}' not in {font_types}.")
latex = []
last_font = None
last_position = 0
def add_encoding(txt, st, en, font, lst, s, ft):
if en - st > 0:
if ft in {"B", "BI"}: lst.append(r"{\textbf{")
if ft in {"I", "BI"}: lst.append(r"{\textit{")
font_code = f"\\fontsize{{{s}}}{{{int(s + 2)}}}" if s != None else ""
if font != None:
lst.append(f"{{{font_code}\\selectfont {{\\{font} {unicoding(txt[st : en])}}}}}")
else:
lst.append(f"{{{font_code}\\selectfont {{\\{MISSING_CHAR_FONT} {MISSING_CHAR * (en - st)}}} }}")
if ft in {"I", "BI"}: lst.append(r"}}")
if ft in {"B", "BI"}: lst.append(r"}}")
for i, c in enumerate(text):
# pick font
current_font = None
for font in fallback_fonts:
if ord(c) in FONT_COVERAGE[font + font_type]:
current_font = font
break
if last_font != current_font:
add_encoding(text, last_position, i, last_font, latex, size, font_type)
last_position = i
last_font = current_font
add_encoding(text, last_position, len(text), last_font, latex, size, font_type)
return "".join(latex) if to_string else latex
def translate(field):
label = field["label"] if "label" in field else ""
description = field["description"] if "description" in field else ""
if (current_language_dictionary == None) or ("name" not in field) or (field["name"] not in current_language_dictionary):
return [label, description]
field_name = field["name"]
label = label if current_language_dictionary[field_name][0] == None else current_language_dictionary[field_name][0]
description = description if current_language_dictionary[field_name][1] == None else current_language_dictionary[field_name][1]
return [label, description]
def translate_meta(field, default_text):
global current_language_dictionary
if current_language_dictionary == None: return default_text
field_name = ""
if type(field) == str:
field_name = field
else:
if "name" not in field: return default_text
field_name = field["name"]
if field_name not in current_language_dictionary: return default_text
return default_text if current_language_dictionary[field_name][0] == None else current_language_dictionary[field_name][0]
def make_table(title, text):
column_labels, content = text[11:].split("!!!CONTENT!!!")
column_labels = column_labels.split("|")
content = content.split("|")
num_cols = len(column_labels)
result_text = ["%s \\\\ \\multicolumn{7}{P{0.99\\textwidth}}{" % title]
result_text.append("\\centering \\begin{tabular}{%s}\\rowcolor{ILSgreen!60}" % (("P{%0.3f\\hsize}" % (0.93 / num_cols)) * num_cols))
result_text.append(" & ".join("\\textbf{\\color{white}%s}" % column_label for column_label in column_labels) + "\\\\ \\hline \\end{tabular} \n")
result_text.append("}\\\\ \n")
row_cnt, col_cnt = 0, 0
for cell in content:
if col_cnt == 0:
if row_cnt > 0: result_text.append(" \\end{tabular} }\\\\ \n")
result_text.append("\\multicolumn{7}{P{0.99\\textwidth}}{")
result_text.append("\\centering \\begin{tabular}{%s}\\rowcolor{%s}" % ((("P{%0.3f\\hsize}" % (0.93 / num_cols)) * num_cols), ("ILSgreen!20" if row_cnt % 2 == 1 else "white")))
else:
result_text.append(" & ")
result_text.append(font_encoding(unquote(cell)))
col_cnt += 1
if col_cnt == num_cols:
result_text.append("\\\\")
col_cnt = 0
row_cnt += 1
result_text.append(" \\hline \\end{tabular} \\\\[1pt] }\\\\ \n")
return "".join(result_text)
result_text = ["\\multicolumn{3}{P{0.49\\textwidth}}{%s\\newline \\vskip-7px \n" % title]
column_labels, content = text[11:].split("!!!CONTENT!!!")
column_labels = column_labels.split("|")
content = content.split("|")
num_cols = len(column_labels)
result_text.append("\\centering \\begin{tabular}{%s}\\rowcolor{ILSgreen!60}" % (("P{%0.3f\\hsize}" % (0.93 / num_cols)) * num_cols))
result_text.append(" & ".join("\\textbf{\\color{white}%s}" % column_label for column_label in column_labels) + "\\\\ \\hline \n")
row_cnt = 0
for i, cell in enumerate(content):
if i % num_cols == 0:
result_text.append("\\rowcolor{%s}" % ("ILSgreen!20" if row_cnt % 2 == 1 else "white"))
else:
result_text.append(" & ")
result_text.append(font_encoding(unquote(cell)))
if i % num_cols == num_cols - 1:
result_text.append("\\\\")
row_cnt += 1
if i % num_cols < num_cols - 1: result_text.append("\\\\")
result_text.append("\\hline \\end{tabular} \\vskip-7px}")
return "".join(result_text)
def create_report(mycursor, table_prefix, uid, entry_id, report_file):
## fill general data
version, titles, report_fields = [], [], []
workflow_type = get_workflow_type(mycursor, table_prefix, uid, entry_id)
fill_report_fields(mycursor, table_prefix, uid, entry_id, titles, report_fields, version)
version = version[0]
for i in range(len(titles)):
if type(titles[i]) == dict:
if workflow_type in titles[i]: titles[i] = titles[i][workflow_type]
elif "default" in titles[i]: titles[i] = titles[i]["default"]
else: titles[i] = "NA"
## fill sample specific data
sql = "SELECT sample_form_entry_id FROM %sconnect_sample WHERE main_form_entry_id = %i;" % (table_prefix, entry_id)
mycursor.execute(sql)
sample_entry_ids = [row["sample_form_entry_id"] for row in mycursor.fetchall()]
sample_report_fields = []
sample_prefix = font_encoding(translate_meta("single_sample", "Sample"))
sample_titles = ["%s %i" % (sample_prefix, i + 1) for i in range(len(sample_entry_ids))]
for i, sample_entry_id in enumerate(sample_entry_ids):
tmp_titles = []
tmp_report_fields = []
fill_report_fields(mycursor, table_prefix, uid, sample_entry_id, tmp_titles, tmp_report_fields, [])
sample_ids = {k: v for k, v in tmp_report_fields[0][:3]}
if "Sample set name" in sample_ids and "Sample origin" in sample_ids and "Sample type" in sample_ids:
sample_titles[i] = "%s / %s / %s" % (sample_ids["Sample set name"], sample_ids["Sample origin"], sample_ids["Sample type"])
tmp_report_fields[0] = tmp_report_fields[0][3:]
sample_titles[i] = font_encoding(sample_titles[i])
sample_report_fields.append(tmp_report_fields[0])
## fill lipid class specific data
sql = "SELECT class_form_entry_id FROM %sconnect_lipid_class WHERE main_form_entry_id = %i;" % (table_prefix, entry_id)
mycursor.execute(sql)
class_entry_ids = [row["class_form_entry_id"] for row in mycursor.fetchall()]
lipid_classes_report_fields = []
lipid_classes_titles = []
for i, class_entry_id in enumerate(class_entry_ids):
tmp_titles = []
tmp_report_fields = []
fill_report_fields(mycursor, table_prefix, uid, class_entry_id, tmp_titles, tmp_report_fields, [])
lipid_classes_report_fields += tmp_report_fields
lipid_class_prefix = "%s %i" % (font_encoding(translate_meta("lipid_class_header", "Lipid class")), i + 1)
for tmp_report_field, tmp_title in zip(tmp_report_fields, tmp_titles):
lipid_class, adduct_ms1, adduct_ms2, other_adduct_ms1, other_adduct_ms2 = "", "", "", "", ""
for tmp_rep_field in tmp_report_field:
if tmp_rep_field[0] == "Lipid class": lipid_class = tmp_rep_field[1]
elif tmp_rep_field[0] == "MS<sup>1</sup> adduct": adduct_ms1 = tmp_rep_field[1]
elif tmp_rep_field[0] == "MS<sup>2</sup> adduct": adduct_ms2 = tmp_rep_field[1]
elif tmp_rep_field[0] == "Other MS<sup>1</sup> adduct": other_adduct_ms1 = tmp_rep_field[1]
elif tmp_rep_field[0] == "Other MS<sup>2</sup> adduct": other_adduct_ms2 = tmp_rep_field[1]
if len(lipid_class) > 0:
lipid_class_prefix = lipid_class
if len(adduct_ms1) > 0: lipid_class_prefix += adduct_ms1
elif len(other_adduct_ms1) > 0: lipid_class_prefix += other_adduct_ms1
elif len(adduct_ms2) > 0: lipid_class_prefix += adduct_ms2
elif len(other_adduct_ms2) > 0: lipid_class_prefix += other_adduct_ms2
lipid_class_title = "%i) %s / %s" % (i + 1, lipid_class_prefix, tmp_title)
lipid_classes_titles.append(lipid_class_title)
tex_preamble = """\\documentclass{article}
% add packages
\\usepackage[utf8x]{inputenc}
\\usepackage{graphicx}
\\usepackage{xcolor}
\\usepackage{adjustbox}
\\usepackage{amsmath}
\\usepackage[a4paper, portrait, margin=2cm]{geometry}
\\usepackage{tocloft}
\\usepackage{array}
\\usepackage{colortbl}
\\usepackage{longtable}
\\setlength{\\LTpre}{1pt}
\\setlength{\\LTpost}{2pt}
\\usepackage{textcomp}
\\usepackage[hidelinks]{hyperref}
\\usepackage{titlesec}
\\usepackage{needspace}
\\usepackage{fontspec}
"""
tex_preamble += font_preamble + "\n"
tex_preamble += """
\\makeatletter
\\renewcommand{\\section}{\\@startsection{section}{1}{0pt}
{0ex} %% negative space before
{0pt} %% space after
{\\normalfont\\Large\\bfseries}}
\\renewcommand{\\subsection}{\\@startsection{subsection}{2}{0pt}
{0ex} %% negative space before
{1ex} %% space after
{\\normalfont\\large\\bfseries}}
\\makeatother
\\setlength{\\parskip}{0pt} %% remove paragraph skips if any
\\setlength{\\parindent}{0pt} %% optional, keep indentation
\\newcommand{\\Sec}[1]{\\Needspace{5\\baselineskip}\\section{#1}}
%% define variables
\\renewcommand{\\arraystretch}{1.3}
\\renewcommand{\\contentsname}{%s}
\\renewcommand*{\\cftsecindent}{2.5em}
\\renewcommand*{\\cftsubsecindent}{4.5em}
\\definecolor{ILSgreen}{HTML}{7EBA28}
\\definecolor{TitleGray}{HTML}{999999}
\\newcolumntype{P}[1]{>{\\raggedright\\arraybackslash}p{#1}}
\\newcolumntype{C}[1]{>{\\raggedleft\\arraybackslash}p{#1}}
\\newcommand{\\mainbox}[1]{\\Sec{#1}}
\\newcommand*{\\tabindent}{0px}
\\setcounter{secnumdepth}{0}
\\newcommand{\\grayline}{\\arrayrulecolor{TitleGray}\\hline\\arrayrulecolor{black}}
%% \\newenvironment{pageblock}{\\par\\nobreak\\vfil\\penalty0\\vfilneg\\vtop\\bgroup}{\\par\\xdef\\tpd{\\the\\prevdepth}\\egroup\\prevdepth=\\tpd}
%% \\newenvironment{pageblock}{\\par\\nobreak\\vfil\\penalty0\\vfilneg\\bgroup}{\\par\\xdef\\tpd{\\the\\prevdepth}\\egroup\\prevdepth=\\tpd}
\\begin{document}
\\begingroup\\fontsize{8pt}{10pt}\\selectfont
\\hfill\\includegraphics[width=50pt]{images/ILS_standalone_logo.pdf}\\\\
\\begin{flushright}\\vskip-15pt{\\tiny %s \\href{https://lipidomicstandards.org}{https://lipidomicstandards.org}, %s """ % (
font_encoding(translate_meta("created_by", "Created by")),
font_encoding(translate_meta("contents_report", "Contents of Report")),
font_encoding(translate_meta("version", "version")),
)
tex_preamble += version
tex_preamble += """}\\end{flushright}
\\vskip-45pt
\\tableofcontents
~\\\\~\\\\
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% General Workflow
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
"""
tex_suffix = """\\endgroup
\\end{document}
"""
with open(report_file, "wt", encoding = "utf-8") as tex:
tex.write("%s\n\n" % tex_preamble)
def write_data(mainbox, titles, report_fields, main_section = False, lipid_classes = False):
#for i in range(len(titles)): titles[i] = font_encoding(titles[i])
for section in report_fields:
for row in section:
row[0] = font_encoding(row[0])
if row[1][:11] != "!!!TABLE!!!": row[1] = font_encoding(row[1]) if len(row[1]) > 0 else font_encoding("-")
ii = 0
for i, mc in enumerate(titles):
if len(report_fields[i]) == 0: continue
#tex.write("\\begin{pageblock}\n")
if i == 0: tex.write("\\mainbox{%s}~\\\\\n" % font_encoding(mainbox))
tex.write("\\subsection{\\textcolor{TitleGray}{%s}}\n" % mc)
#tex.write("\\textbf{\\large \\textcolor{TitleGray}{%s}}\n" % mc)
tex.write("\\vskip-1.2em\\noindent\\textcolor{ILSgreen}{\\rule{\\textwidth}{1.5pt}}\n")
tex.write("\\newline{\\vskip-10px\\noindent\\textcolor{gray!20}{\\rule{\\textwidth}{10pt}}}\n")
tex.write("\\setlength\\tabcolsep{0pt}\\begin{longtable}{@{}P{0.26\\textwidth}P{0.005\\textwidth}P{0.23\\textwidth}P{0.01\\textwidth}@{}P{0.26\\textwidth}P{0.005\\textwidth}P{0.23\\textwidth}}\n")
if i == 0 and main_section:
tex.write("%s & \\multicolumn{6}{@{}P{0.70\\textwidth}}{%s} \\\\\n" % (report_fields[0][0][0], report_fields[0][0][1]))
tex.write("\\hline\n")
# create time stamp of now
date_time = datetime.fromtimestamp(time.time())
str_date_time = date_time.strftime("%m/%d/%Y")
report_fields[i][0] = [font_encoding(translate_meta("document_creation_date", "Document creation date")), str_date_time]
left = True
n = len(report_fields[i])
for ci in range(n):
# prevent that a table will be put on the right column
if left and ci + 1 < n and report_fields[i][ci + 1][1][:11] == "!!!TABLE!!!":
report_fields[i][ci], report_fields[i][ci + 1] = report_fields[i][ci + 1], report_fields[i][ci]
if report_fields[i][ci][1][:11] == "!!!TABLE!!!":
tex.write(make_table(report_fields[i][ci][0], report_fields[i][ci][1]))
else:
if left:
tex.write("%s & & %s" % (report_fields[i][ci][0], report_fields[i][ci][1]))
left = False
if ci + 1 == n: tex.write(" & & & & \\\\\n")
else:
tex.write(" & & %s & & %s\\\\\n" % (report_fields[i][ci][0], report_fields[i][ci][1]))
if ci + 1 < n: tex.write("\\cline{1-3}\\cline{5-7}\n")
left = True
tex.write("\\end{longtable}\n")
tex.write("\\vskip-1.3em\\noindent\\textcolor{ILSgreen}{\\rule{\\textwidth}{1.5pt}}\n")
#tex.write("\\end{pageblock}\\par~\\\\[20pt]\n\n\n")
tex.write("\\par~\\\\\n\n\n")
if lipid_classes and ii % 2 == 1: tex.write("~\\\\[20pt] \n\n\n")
ii += 1
write_data(translate_meta(workflow_type, "%s Workflow" % workflow_types[workflow_type]), titles, report_fields, main_section = True)
tex.write("~\\\\~\\\\\n")
write_data(translate_meta("sample_descriptions", "Sample Descriptions"), sample_titles, sample_report_fields)
tex.write("~\\\\~\\\\\n")
write_data(translate_meta("lipid_class_descriptions", "Lipid Class Descriptions"), lipid_classes_titles, lipid_classes_report_fields, lipid_classes = True)
tex.write("%s\n\n" % tex_suffix)