-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat_matrix.py
More file actions
124 lines (104 loc) · 3.84 KB
/
format_matrix.py
File metadata and controls
124 lines (104 loc) · 3.84 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
import openpyxl
from openpyxl.styles import Font, Alignment, Border, Side, PatternFill
from openpyxl.formatting.rule import ColorScaleRule
import sys
import getopt
#test for updates
def format_matrix(input_file, remove_artifacts=False):
wb = openpyxl.load_workbook(input_file)
ws = wb.active
# Define number format and ranges
decimal_format = '0.0000'
cell_ranges_for_decimals = [ws['B24:U42'], ws['B48:V48']] # List of ranges to format with decimals
bold_center_range = ws['A23:V51'] # Range to apply bold and centered alignment
regular_ranges = [ws['A45:V47'], ws['A50:B51'], ws['B23:U23'], ws['A24:A48'], ws['V45:V48']]
outline_ranges = [ws['B23:U23'], ws['A24:U42'], ws['B45:V45'], ws['A46:V48']]
white_fill_ranges = [ws['A22:A23'], ws['B22:W22'], ws['V23:W44'], ws['W45:W49'], ws['A43:A45'], ws['B43:U44'], ws['A49:V49']]
# Define font and alignment
bold_font = Font(name='Calibri', bold=True) # Set font to Calibri and bold
regular_font = Font(name='Calibri', bold=False)
center_alignment = Alignment(horizontal='center', vertical='center')
# Bold and center
for row in bold_center_range:
for cell in row:
cell.font = bold_font
cell.alignment = center_alignment
#Decimal formatting and Calibri font to the two matrices
for cell_range in cell_ranges_for_decimals:
for row in cell_range:
for cell in row:
cell.number_format = decimal_format
cell.font = bold_font
#3-color scale for conditional formatting
color_scale_rule = ColorScaleRule(
start_type='min', start_color='4BACC6', # Blue for minimum
mid_type='percentile', mid_value=50, mid_color='FFEB84', # Yellow for midpoint
end_type='max', end_color='C0504D' # Red for maximum
)
# Apply the color scale rule to each range
ws.conditional_formatting.add('B24:U42', color_scale_rule)
ws.conditional_formatting.add('B48:U48', color_scale_rule)
# Remove artifacts
grey_fill = PatternFill(fill_type='solid', start_color='D9D9D9', end_color='D9D9D9')
if remove_artifacts:
# Definitions can be changed
ptm_definitions = {'NtoD', 'QtoE', 'EtoS', 'StoD', 'TtoE', 'StoA', 'YtoF'}
for ptm in ptm_definitions:
source = ptm[0]
dest = ptm[-1]
for row in ws['B23:U23']:
for cell in row:
if cell.value == source:
source_loc = cell.column
for row2 in ws['A24:A42']:
for cell2 in row2:
if cell2.value == dest:
dest_loc = cell2.row
target_cell = ws.cell(row=dest_loc, column=source_loc)
target_cell.value = ''
target_cell.fill = grey_fill
for range in regular_ranges:
regular_range = range
for row in regular_range:
for cell in row:
cell.font = regular_font
thin_border = Border(
left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin')
)
for range in outline_ranges:
outline_range = range
for row in outline_range:
for cell in row:
cell.border = thin_border
white_fill = PatternFill(fill_type='solid', start_color='FFFFFF', end_color='FFFFFF')
for range in white_fill_ranges:
white_fill_range = range
for row in white_fill_range:
for cell in row:
cell.fill = white_fill
matrix_out = f"{input_file}"
wb.save(matrix_out)
return matrix_out
if __name__ == '__main__':
input_file = ''
remove_artifacts = False
try:
options, remainder = getopt.getopt(sys.argv[1:], '', ['input_file=', 'remove_artifacts'])
except getopt.GetoptError:
print("Usage: python format_matrix.py --input_file matrix.xlsx [--remove_artifacts]")
sys.exit(2)
for opt, arg in options:
if opt == '--input_file':
input_file = arg
elif opt == '--remove_artifacts':
remove_artifacts = True
else:
print(f"Warning! Command-line argument: {opt} not recognized. Exiting...")
sys.exit(2)
if not input_file:
print('Error: --input_file argument is required.')
sys.exit(2)
format_matrix(input_file, remove_artifacts)