Repository navigation
Expand file tree
/
Copy pathHiCtool_utilities.py
More file actions
269 lines (243 loc) · 9.37 KB
/
Copy pathHiCtool_utilities.py
File metadata and controls
269 lines (243 loc) · 9.37 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
# HiCtool utility functions to work with HiCtool generated data.
def save_matrix(a_matrix, output_file):
"""
Save an intra-chromosomal contact matrix in the HiCtool compressed format to txt file.
1) The upper-triangular part of the matrix is selected (including the
diagonal).
2) Data are reshaped to form a vector.
3) All the consecutive zeros are replaced with a "0" followed by the
number of times zeros are repeated consecutively.
4) Data are saved to a txt file.
Arguments:
a_matrix (numpy matrix): input contact matrix to be saved
output_file (str): output file name in txt format
Output:
txt file containing the formatted data
"""
import numpy as np
n = len(a_matrix)
iu = np.triu_indices(n)
vect = a_matrix[iu].tolist()
with open (output_file,'w') as fout:
k = len(vect)
i = 0
count = 0
flag = False # flag to set if the end of the vector has been reached
while i < k and flag == False:
if vect[i] == 0:
count+=1
if (i+count == k):
w_out = str(0) + str(count)
fout.write('%s\n' %w_out)
flag = True
break
while vect[i+count] == 0 and flag == False:
count+=1
if (i+count == k):
w_out = str(0) + str(count)
fout.write('%s\n' %w_out)
flag = True
break
if flag == False:
w_out = str(0) + str(count)
fout.write('%s\n' %w_out)
i+=count
count = 0
else:
fout.write('%s\n' %vect[i])
i+=1
def load_matrix(input_file):
"""
Load an HiCtool compressed square (and symmetric) contact matrix from a txt file and parse it.
Arguments:
input_file (str): input file name in txt format (generated by the function
"save_matrix").
Return:
numpy array containing the parsed values stored in the input txt file to build a contact matrix.
"""
import numpy as np
print "Loading " + input_file + "..."
with open (input_file,'r') as infile:
matrix_vect = []
for i in infile:
if i[0] == "0" and i[1] != ".":
for k in xrange(int(i[1:-1])):
matrix_vect.append(0)
else:
j = i[:-1]
matrix_vect.append(float(j))
k = len(matrix_vect)
matrix_size = int((-1+np.sqrt(1+8*k))/2)
iu = np.triu_indices(matrix_size)
output_matrix_1 = np.zeros((matrix_size,matrix_size)) # upper triangular plus the diagonal
output_matrix_1[iu] = matrix_vect
diag_matrix = np.diag(np.diag(output_matrix_1)) # diagonal
output_matrix_2 = np.transpose(output_matrix_1) # lower triangular plus the diagonal
output_matrix = output_matrix_1 + output_matrix_2 - diag_matrix
print "Done!"
return output_matrix
def save_matrix_rectangular(a_matrix, output_file):
"""
Save an inter-chromosomal contact matrix in the HiCtool compressed format to txt file.
1) Data are reshaped to form a vector.
2) All the consecutive zeros are replaced with a "0" followed by the
number of times zeros are repeated consecutively.
3) Data are saved to a txt file.
Arguments:
a_matrix (numpy matrix): input contact matrix to be saved
output_file (str): output file name in txt format
Output:
txt file containing the formatted data
"""
import numpy as np
n_row = np.shape(a_matrix)[0]
n_col = np.shape(a_matrix)[1]
vect = np.reshape(a_matrix,[1,n_row*n_col]).tolist()[0]
with open (output_file,'w') as fout:
k = len(vect)
i = 0
count = 0
flag = False # flag to set if the end of the vector has been reached
while i < k and flag == False:
if vect[i] == 0:
count+=1
if (i+count == k):
w_out = str(0) + str(count)
fout.write('%s\n' %w_out)
flag = True
break
while vect[i+count] == 0 and flag == False:
count+=1
if (i+count == k):
w_out = str(0) + str(count)
fout.write('%s\n' %w_out)
flag = True
break
if flag == False:
w_out = str(0) + str(count)
fout.write('%s\n' %w_out)
i+=count
count = 0
else:
fout.write('%s\n' %vect[i])
i+=1
def load_matrix_rectangular(input_file, n_row, n_col):
"""
Load an HiCtool compressed rectangular contact matrix from a txt file and parse it.
Arguments:
input_file (str): input file name in txt format (generated by the function
"save_matrix_rectangular")
n_row (int): number of rows of the matrix.
n_col (int): number of columns of the matrix.
Return:
output_matrix: numpy array the parsed values stored in the input txt file to build a contact matrix.
"""
import numpy as np
print "Loading " + input_file + "..."
with open (input_file,'r') as infile:
matrix_vect = []
for i in infile:
if i[0] == "0" and i[1] != ".":
for k in xrange(int(i[1:-1])):
matrix_vect.append(0)
else:
j = i[:-1]
matrix_vect.append(float(j))
output_matrix = np.reshape(np.array(matrix_vect),[n_row,n_col])
print "Done!"
return output_matrix
def save_matrix_tab(input_matrix, output_filename):
"""
Save a contact matrix in a txt file in a tab separated format. Columns are
separated by tabs, rows are in different lines.
Arguments:
input_matrix (numpy matrix): input contact matrix to be saved
output_filename (str): output file name in txt format
Output:
txt file containing the tab separated data
"""
with open (output_filename, 'w') as f:
for i in xrange(len(input_matrix)):
row = [str(j) for j in input_matrix[i]]
f.write('\t'.join(row) + '\n')
def load_matrix_tab(input_file):
"""
Load a contact matrix saved in a tab separated format using the function
"save_matrix_tab".
Arguments:
input_file (str): input contact matrix to be loaded.
Return:
numpy array containing the parsed values stored in the input tab separated txt file to build a contact matrix.
"""
import numpy as np
print "Loading " + input_file + "..."
with open (input_file, 'r') as infile:
lines = infile.readlines()
temp = []
for line in lines:
row = [float(i) for i in line.strip().split('\t')]
temp.append(row)
output_matrix = np.array(temp)
print "Done!"
return output_matrix
def load_DI_values(input_file):
"""
Load a DI txt file generated with "calculate_chromosome_DI".
Arguments:
input_file (str): input file name in txt format.
Return:
List of the DI values.
"""
import numpy as np
fp = open(input_file,'r+')
lines = fp.read().split('\n')
lines = lines[:-1]
di_values = (np.nan_to_num(np.array(map(float, lines)))).tolist()
return di_values
def load_hmm_states(input_file):
"""
Load an HMM txt file generated with "calculate_chromosome_hmm_states".
Arguments:
input_file (str): input file name in txt format.
Returns:
List of the HMM states.
"""
fp = open(input_file,'r+')
lines = fp.read().split('\n')
lines = lines[:-1]
likelystates = map(int,lines)
return likelystates
def save_topological_domains(a_matrix, output_file):
"""
Function to save the topological domains coordinates to text file.
Each topological domain coordinates (start and end) occupy one row and are
tab separated.
Arguments:
a_matrix (numpy matrix): file to be saved with topological domains coordinates.
output_file (str): output file name in txt format.
Output:
Tab separated txt file with topological domain start and end coordinates.
"""
def compile_row_string(a_row):
return str(a_row).strip(']').strip('[').lstrip().replace(' ','\t')
with open(output_file, 'w') as f:
for row in a_matrix:
f.write(compile_row_string(row)+'\n')
def load_topological_domains(input_file):
"""
Function to load the topological domains coordinates from txt file.
Arguments:
input_file (str): input file name generated with "calculate_topological_domains" in txt format.
Return:
List of lists with topological domain coordinates.
"""
import csv
print "Loading topological domain coordinates..."
with open(input_file, 'r') as f:
reader = csv.reader(f, dialect='excel', delimiter='\t')
topological_domains = []
for row in reader:
row_int = [int(x) for x in row]
topological_domains.append(row_int)
print "Done!"
return topological_domains