-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTADs2.py
322 lines (222 loc) · 9.11 KB
/
TADs2.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
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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from sys import argv
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy.stats import norm
from sklearn.preprocessing import StandardScaler
from scipy import stats
import warnings
warnings.filterwarnings('ignore')
# # Axiliar Functions:
# In[2]:
def filter_chrName(v):
"""Filter string like Yp11.2Yq11.223 to Y """
v = str(v)
#print(v)
chromossome = ''
if 'q' in v:
chromossome = v.split("q")[0]
if 'p' in chromossome:
chromossome = chromossome.split("p")[0]
elif 'p' in v:
chromossome = v.split("p")[0]
if 'q' in chromossome:
chromossome = chromossome.split("q")[0]
elif 'chr' in v:
# add if for 'chr22_KI270739v1_random'
chromossome = v.split("chr")[1]
#if '_' in chromossome:
# chromossome = chromossome.split("_")[0]
return str(chromossome)
# # Function:
def get_Overlap(a,b):
"""
Requires: a= region start and end (list), b= chromStart and chromEnd from rR (list)
"""
if a[0] == b[1] or b[0] == a[1]:
return True
ovl=max(0,min(a[1],b[1])-max(a[0],b[0]))#calcula o overlap
if ovl != 0:
return True
return False #ovl
# In[3]:
def closest_to_region(region_start,region_end,chrom,df_TADs):
"""Associates each region to a CpG_Island
Requieres: stat and end int value of the region, chromossome ,and the CpG_Islands dataframe
Ensures: a list, row, with the data fromthe closest CpG island
"""
selected_island = [np.nan,np.nan,np.nan] #DEBUG #This'll cause missing data!
#Start by each region of df_regions:
list_results = [] # key is a CNV id
df_TADs = df_TADs.loc[df_TADs['chrom'] == chrom]
df_TADs = df_TADs.sort_values(by=['start'])
#display(df_TADs) #DEBUG
dist=999**99 #!!!
#closest_island_dist = np.nan #!!!
for index, row in df_TADs.iterrows():
b = [row['start'],row['end']]
overlap = get_Overlap([region_start,region_end],b) #region overlap
#print([region_start,region_end],'\n',b,'\n',overlap)#DEBUG
if overlap == True:# overlap != 0:
#print(overlap)#DEBUG
#print([region_start,region_end],b)#DEBUG
#dist = 0
if (region_end >= row['start'] and region_start <= row['start']):
dist = 0
selected_island = row
break
elif (region_start <= row['end'] and region_end >= row['end']):
dist = 0
selected_island = row
break
else:
dist = region_start - row['start']
selected_island = row
#print(dist,region_start,row['start'])#DEBUG
#print(region_start,region_end)#DEBUG
#print(row['end',row['end']])#DEBUG
break #?
#Done -----
elif overlap == False and dist !=0:
posA = (region_start - row['end'])
#posB = (row['start'] - region_end) #
if posA >= 0:
new_dist = posA
if new_dist < dist:
dist = new_dist
selected_island = row
else:
break
#break here!!!
if type(selected_island) != list:
selected_island = selected_island.tolist()
#selected_island = selected_island.tolist() #selecteted_item = sub_df_Centro.iloc[position_item]
#distance_log
selected_island.append(dist)
dist_Mb = float(dist) / 1000000 # to Mb
distance_log = np.log10(dist_Mb +1)
selected_island.append(distance_log)
return selected_island #add_list
#____Region_B:______________________________________________________
def closest_to_region_B(region_start,region_end,chrom,df_TADs):
"""Associates each region to a CpG_Island
Requieres: stat and end int value of the region, chromossome ,and the CpG_Islands dataframe
Ensures: a list, row, with the data fromthe closest CpG island
"""
selected_island = [np.nan,np.nan,np.nan] #DEBUG #This'll cause missing data!
#Start by each region of df_regions:
list_results = [] # key is a CNV id
df_TADs = df_TADs.loc[df_TADs['chrom'] == chrom]
df_TADs = df_TADs.sort_values(by=['start'])
#display(df_TADs) #DEBUG
#Since for Region_B its considered the closest from its right, so invert order from 5'-3' to 3'-5', "degrowing_order" :
df_TADs = df_TADs.iloc[::-1] #B!
#
dist=999**99 #!!!
#closest_island_dist = np.nan #!!!
for index, row in df_TADs.iterrows():
b = [row['start'],row['end']]
overlap = get_Overlap([region_start,region_end],b)
#print([region_start,region_end],'\n',b,'\n',overlap)#DEBUG
if overlap == True:# overlap != 0:
#print(overlap)#DEBUG
#dist = 0
if (region_end >= row['start'] and region_start <= row['start']):
dist = 0
selected_island = row
break
elif (region_start <= row['end'] and region_end >= row['end']):
dist = 0
selected_island = row
break
else:
dist = row['end'] - region_end
selected_island = row
break #?
#Done -----
elif overlap == False and dist !=0:
#posA = (region_start - row['end']) # start_CNV_region - repRegion_end #B?
posB = (row['start'] - region_end) # repRegion_start - end_CNV_region #B?
if posB >= 0:
new_dist = posB
#elif posA >= 0:
# new_dist = posA
#elif posA >= 0 & posB >= 0:
# new_dist = min([posA, posB])
if new_dist < dist:
dist = new_dist
selected_island = row
else:
break
#break here!!!
if type(selected_island) != list:
selected_island = selected_island.tolist()
#selected_island = selected_island.tolist() #selecteted_item = sub_df_Centro.iloc[position_item]
#distance_log
selected_island.append(dist)
dist_Mb = float(dist) / 1000000 # to Mb
distance_log = np.log10(dist_Mb +1) #was log e
selected_island.append(distance_log)
return selected_island #add_list
#___________________________________________________________________
#___________________________________________________________________
# In[4]:
def main_get_TADs(df_regions,df_TADs):
"""
"""
list_TADs = []
#Line by line from the A nad B regions:
for index, row in df_regions.iterrows():
#A
rR_A = closest_to_region(row['regionA_stat'],row['regionA_end'],row['chr_A'],df_TADs)
#B
rR_B = closest_to_region_B(row['regionB_stat'],row['regionB_end'],row['chr_B'],df_TADs) #B!
#Prep info by line:
list_add = [row['ID'],row['Cluster_id']] + rR_A + rR_B
list_TADs.append(list_add)
return list_TADs
# # Load Data
# In[5]:
region_path = argv[1]
df_regions = pd.read_csv(region_path, sep= ";" ,header=0,index_col=0)
#df_regions = pd.read_csv('./dataset3/rep_regions/match_CNVs_regions_TP.csv', sep= ";" ,header=0,index_col=0)
df_regions['chr_A'] = df_regions['chr_A'].astype(str)
df_regions['chr_B'] = df_regions['chr_B'].astype(str)
# In[6]:
#df_regions
# ## Tags
# In[7]:
df_TADs= pd.read_csv('./dataset3/TAD_boundaries/'+'IMR90_Rao_2014-raw_TADs.txt', sep= "\t",
names=['chrom','start','end'])
# In[8]:
df_TADs['chrom'] = df_TADs['chrom'].astype(str)
df_TADs['chrom'] = df_TADs['chrom'].replace(['23','24'],['X','Y'])
# In[9]:
#df_TADs
# ### Run
# In[10]:
path_CpG_Island = './dataset3/TAD_boundaries/'
TADs_Data = main_get_TADs(df_regions,df_TADs)
# In[11]:
#print(TADs_Data[:10])
# ### To a dataframe:
# In[12]:
#To a dataframe:
df_TADs_AB_data= pd.DataFrame(TADs_Data, columns=['CNV_ID','Cluster_ID',
'A_chr','A_start','A_end','A_dist_Bp','A_dist_Log',
'B_chr','B_start','B_end','B_dist_Bp','B_dist_Log'
])
# In[13]:
#df_TADs_AB_data
# ## Save to a csv:
# In[14]:
#path_CpG_Island = './dataset3/CpG_islands/'
# In[15]:
#Save to a csv:
output_path = argv[2]
df_TADs_AB_data.to_csv(output_path + 'TAD_boundary_AB_test.csv', sep=';',index_label=False)
#df_TADs_AB_data.to_csv(path_CpG_Island +'TAD_boundary_AB.csv', sep=';',index_label=False)