-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_matrix3.py
154 lines (126 loc) · 5.68 KB
/
convert_matrix3.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
#!/usr/bin/python3
import argparse
import os
import re
import sys
import csv
import pandas as pd
def apply_threshold(i, t):
"""
Return i if i>t else 0
Parameters:
i - value to test [float]
t - threshold [float]
"""
if abs(i)>t:
return i
else:
return 0.0
def read_matrix_from_file(matrix_file, threshold):
"""
Read the matrix from the input file
Values <=threshold are set to 0
"""
matrix = []
with open(matrix_file) as infile:
for line in infile:
if(line != "\n"):
row = line.strip() #.split(sep)
row_values = re.compile(",").split(row)
row_values = list(map(float, row_values))
for i in range(len(row_values)):
row_values[i] = apply_threshold(row_values[i], threshold)
matrix.append(row_values)
return matrix
def read_positions_from_file(positions_file):
"""
Read the matrix from the input file
Values <=threshold are set to 0
"""
positions = []
with open(positions_file) as infile:
for line in infile:
if(line.rstrip()):
row = line.strip() #.split(sep)
row_values = re.compile("\s+").split(row)
row_values = list(map(float, row_values))
positions.append(row_values)
return positions
def determine_matrix_max(matrix):
"""
Determine the max value in the matrix
(e.g. used for normalization)
"""
max_val = -1.0
for row in matrix:
for value in row:
if value > max_val:
max_val = value
return max_val
def read_id_file(id_file):
"""
Read the file containing the row/column IDs
"""
ids = []
with open(id_file) as infile:
for line in infile:
if line.rstrip() != "":
ids.append(line.rstrip())
return ids
def write_output_file(matrix, positions, ids, output_file, threshold, max_val, file_id,atlas):
"""
Write tab-sep. output file - output file must be the name of the file without extension
"""
# NOTE: appends to the file if it exists instead of overwriting!
write_header = True
if (os.path.isfile(output_file)):
write_header = False
with open(output_file, "a") as outfile:
if (write_header):
outfile.write("Origin_Area\tTarget_Area\tMeasured_Connection_Strength\tNormalized_Connection_Strength\tOriginx\tOriginy\tOriginz\tTargetx\tTargety\tTargetz\tPatient_ID\tAtlas\n")
for i in range(len(ids)):
for j in range(len(ids)):
if(matrix[i][j]>threshold and matrix[i][j]!=0):
normalized_value = matrix[i][j] / max_val
outfile.write("%s\t%s\t%.15f\t%.15f\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\t%.6f\t%s\t%s\n" % (ids[i], ids[j], matrix[i][j], normalized_value, positions[i][0], positions[i][1], positions[i][2], positions[j][0], positions[j][1], positions[j][2],file_id,atlas))
########################################################################################################################
########################################################################################################################
parser = argparse.ArgumentParser(description='''Convert matrix file to value-per-line file''',
epilog="Biomax Informatics AG")
parser.add_argument('-i', metavar="<matrix file>", action='store', dest='matrix_file',
help='Input file containing the matrix with values', required=True)
parser.add_argument('-n', metavar="<name file>", action='store', dest='id_file',
help='File containing the row/column names of the matrix', required=True)
parser.add_argument('-p', metavar="<positions file>", action='store', dest='positions_file',
help='File containing the row/column names of the matrix', required=True)
parser.add_argument('-o', metavar="<output file>", action='store', dest='out_file',
help='Output file', required=True)
parser.add_argument('-id', metavar="<file id>", action='store', dest='file_id',
help='File ID (will be appended to the output lines!)', required=True)
parser.add_argument('-atlas', metavar="<atlas>", action='store', dest='atlas',
help='atlas (will be appended to the output lines!)', required=True)
parser.add_argument('-t', metavar="<float>", action='store', dest='threshold',
default="0.0", help='Threshold value. Values equal or lower than this will be set to 0 [default=0.0]')
parser._optionals.title = "Arguments"
params = parser.parse_args()
matrix_file = params.matrix_file
id_file = params.id_file
positions_file = params.positions_file
output_file = params.out_file
threshold = float(params.threshold)
file_id = params.file_id
atlas = params.atlas
# check parameters
for f in [matrix_file, positions_file, id_file]:
if not os.path.isfile(f):
raise Exception("Input file %s does not exist!" % f)
sys.exit(1)
##############################
ids = read_id_file(id_file)
print("Got %d ids" % len(ids))
matrix_values = read_matrix_from_file(matrix_file, threshold)
positions = read_positions_from_file(positions_file)
max_value = determine_matrix_max(matrix_values)
print("position values %d " % len(positions))
print("matrix values %d " % len(matrix_values))
write_output_file(matrix_values, positions, ids, output_file, threshold, max_value, file_id,atlas)