Skip to content

Commit b6ef9c9

Browse files
run 2to3 (only changes one import and adds '()' to a lot of print statement
1 parent a7a5400 commit b6ef9c9

File tree

1 file changed

+49
-48
lines changed

1 file changed

+49
-48
lines changed

src/CombineNearbyInteraction.py

Lines changed: 49 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@
2626
Vijay-Ay lab, LJI
2727
"""
2828

29+
import heapq
30+
import math
2931
import os
3032
import sys
31-
import math
3233
from optparse import OptionParser
34+
3335
import networkx as nx
34-
from Queue import heapq
36+
3537

3638
#==========================================================
3739
# this function returns the element below which top K % of
@@ -145,21 +147,21 @@ def main():
145147

146148
# print the parameters
147149
if 1:
148-
print '****** Merge filtering of adjacent loops is enabled *****'
149-
print '***** within function of merged filtering - printing the parameters ***'
150-
print '*** bin_size: ', bin_size
151-
print '*** headerInp: ', headerInp
152-
print '*** connectivity_rule: ', connectivity_rule
153-
print '*** TopPctElem: ', TopPctElem
154-
print '*** NeighborHoodBinThr: ', NeighborHoodBinThr
155-
print '*** QValCol: ', QValCol
156-
print '*** PValCol: ', PValCol
157-
print '*** SortOrder: ', SortOrder
150+
print('****** Merge filtering of adjacent loops is enabled *****')
151+
print('***** within function of merged filtering - printing the parameters ***')
152+
print('*** bin_size: ', bin_size)
153+
print('*** headerInp: ', headerInp)
154+
print('*** connectivity_rule: ', connectivity_rule)
155+
print('*** TopPctElem: ', TopPctElem)
156+
print('*** NeighborHoodBinThr: ', NeighborHoodBinThr)
157+
print('*** QValCol: ', QValCol)
158+
print('*** PValCol: ', PValCol)
159+
print('*** SortOrder: ', SortOrder)
158160

159161
# output directory
160162
OutDir = os.path.dirname(os.path.realpath(OutFile))
161163
if 1:
162-
print 'OutDir: ', str(OutDir)
164+
print('OutDir: ', str(OutDir))
163165

164166
# open the output file
165167
# if input interaction file has header information,
@@ -196,7 +198,7 @@ def main():
196198
for l in fp_in.readlines():
197199
currchrname = (l.rstrip()).split()[0]
198200
TargetChrList.append(currchrname)
199-
print 'list of chromosomes: ', str(TargetChrList)
201+
print('list of chromosomes: ', str(TargetChrList))
200202
# remove temporary files
201203
sys_cmd = "rm " + temp_chr_log_file
202204
os.system(sys_cmd)
@@ -207,7 +209,7 @@ def main():
207209
for chridx in range(len(TargetChrList)):
208210
curr_chr = TargetChrList[chridx]
209211
if 1:
210-
print 'Processing the chromosome: ', str(curr_chr)
212+
print('Processing the chromosome: ', str(curr_chr))
211213

212214
# extract the interactions of current chromosome from the complete set of interactions
213215
tempchrdumpfile = OutDir + '/Temp_chr_Dump.bed'
@@ -222,11 +224,11 @@ def main():
222224

223225
if (num_Int == 0):
224226
if 0:
225-
print 'Number of interactions for this chromosome = 0 --- continue'
227+
print('Number of interactions for this chromosome = 0 --- continue')
226228
continue
227229

228230
if 0:
229-
print 'Extracted interactions for the current chromosome'
231+
print('Extracted interactions for the current chromosome')
230232

231233
# # extract also the max span of interactions (6th column maximum element)
232234
# # so as to estimate the matrix size
@@ -271,7 +273,7 @@ def main():
271273
# add the node to the given graph as well
272274
G.add_node(curr_key)
273275
if 0:
274-
print 'Current interaction: ', str(line), ' ------ curr_key: ', curr_key
276+
print('Current interaction: ', str(line), ' ------ curr_key: ', curr_key)
275277

276278
# now check the nodes of G
277279
# assign edges of G according to the 8 / 4 connectivity rule (according to the input parameter)
@@ -282,35 +284,35 @@ def main():
282284
for j in range(i+1, len(nodelist)):
283285
node2 = nodelist[j]
284286
if 0:
285-
print 'Checking the edge between node 1: ', node1, ' and node 2: ', node2
287+
print('Checking the edge between node 1: ', node1, ' and node 2: ', node2)
286288
# check if there should be an edge between node1 and node2
287289
# according to the desired connectivity rule
288290
if (connectivity_rule == 8):
289291
if (abs(node1[0] - node2[0]) <= 1) and (abs(node1[1] - node2[1]) <= 1):
290292
G.add_edge(node1, node2)
291293
if 0:
292-
print '8 connectivity Edge between node 1: ', node1, ' and node 2: ', node2
294+
print('8 connectivity Edge between node 1: ', node1, ' and node 2: ', node2)
293295
if (connectivity_rule == 4):
294296
if ((abs(node1[0] - node2[0]) + abs(node1[1] - node2[1])) <= 1):
295297
G.add_edge(node1, node2)
296298
if 0:
297-
print '4 connectivity Edge between node 1: ', node1, ' and node 2: ', node2
299+
print('4 connectivity Edge between node 1: ', node1, ' and node 2: ', node2)
298300

299301
# check the edges of G
300302
edgelist = list(G.edges())
301303

302304
if 1:
303-
print 'No of nodes of G: ', G.number_of_nodes()
304-
print 'No of edges of G: ', G.number_of_edges()
305-
print 'Number of connected components of G: ', nx.number_connected_components(G)
305+
print('No of nodes of G: ', G.number_of_nodes())
306+
print('No of edges of G: ', G.number_of_edges())
307+
print('Number of connected components of G: ', nx.number_connected_components(G))
306308

307309
# scan through individual connected components
308310
# for each such connected component (list of interactions)
309311
# we find a representative interaction and print it in the final output file
310312
list_conn_comp = sorted(nx.connected_components(G), key = len, reverse=True)
311313

312314
if 0:
313-
print '\n\n**** Number of connected components: ', len(list_conn_comp), ' ****\n\n'
315+
print('\n\n**** Number of connected components: ', len(list_conn_comp), ' ****\n\n')
314316

315317
#====================
316318
# process individual connected components
@@ -319,31 +321,31 @@ def main():
319321
# a connected component - a particular list of connected nodes
320322
curr_comp_list = list(list_conn_comp[i])
321323
if 0:
322-
print '\n\n\n ===>>>>> Processing the connected component no: ', i, ' list: ', str(curr_comp_list), ' number of elements: ', len(curr_comp_list)
324+
print('\n\n\n ===>>>>> Processing the connected component no: ', i, ' list: ', str(curr_comp_list), ' number of elements: ', len(curr_comp_list))
323325

324326
# from the first interacting bin set, get the lower and higher bin index
325327
min_idx_bin1 = min([x[0] for x in curr_comp_list])
326328
max_idx_bin1 = max([x[0] for x in curr_comp_list])
327329
if 0:
328-
print 'min_idx_bin1: ', min_idx_bin1, ' max_idx_bin1: ', max_idx_bin1
330+
print('min_idx_bin1: ', min_idx_bin1, ' max_idx_bin1: ', max_idx_bin1)
329331

330332
# get the span of coordinates for the first interacting bin (set)
331333
span_low_bin1 = (min_idx_bin1 - 1) * bin_size
332334
span_high_bin1 = max_idx_bin1 * bin_size
333335
if 0:
334-
print 'span_low_bin1: ', span_low_bin1, ' span_high_bin1: ', span_high_bin1
336+
print('span_low_bin1: ', span_low_bin1, ' span_high_bin1: ', span_high_bin1)
335337

336338
# from the second interacting bin set, get the lower and higher bin index
337339
min_idx_bin2 = min([x[1] for x in curr_comp_list])
338340
max_idx_bin2 = max([x[1] for x in curr_comp_list])
339341
if 0:
340-
print 'min_idx_bin2: ', min_idx_bin2, ' max_idx_bin2: ', max_idx_bin2
342+
print('min_idx_bin2: ', min_idx_bin2, ' max_idx_bin2: ', max_idx_bin2)
341343

342344
# get the span of coordinates for the first interacting bin (set)
343345
span_low_bin2 = (min_idx_bin2 - 1) * bin_size
344346
span_high_bin2 = max_idx_bin2 * bin_size
345347
if 0:
346-
print 'span_low_bin2: ', span_low_bin2, ' span_high_bin2: ', span_high_bin2
348+
print('span_low_bin2: ', span_low_bin2, ' span_high_bin2: ', span_high_bin2)
347349

348350
# sum of contact counts for all the interacting bins
349351
# within this set of connected nodes
@@ -365,7 +367,7 @@ def main():
365367
Percent_Significant_BinPair = (possible_bin_pairs * 1.0) / total_possible_bin_pairs
366368

367369
if 0:
368-
print ' ==>>> total_possible_bin_pairs: ', total_possible_bin_pairs, ' possible_bin_pairs: ', possible_bin_pairs, ' % clique: ', Percent_Significant_BinPair
370+
print(' ==>>> total_possible_bin_pairs: ', total_possible_bin_pairs, ' possible_bin_pairs: ', possible_bin_pairs, ' % clique: ', Percent_Significant_BinPair)
369371

370372

371373
#==================================================
@@ -387,7 +389,7 @@ def main():
387389
curr_key_bin1_mid = (((curr_key[0] - 1) * bin_size) + (curr_key[0] * bin_size)) / 2
388390
curr_key_bin2_mid = (((curr_key[1] - 1) * bin_size) + (curr_key[1] * bin_size)) / 2
389391
if 0:
390-
print ' Connected component index: ', j, ' curr_key: ', curr_key, ' bin 1 mid: ', curr_key_bin1_mid, ' bin 2 mid: ', curr_key_bin2_mid, ' CC: ', curr_cc, ' Pval: ', curr_pval, ' Qval: ', curr_qval
392+
print(' Connected component index: ', j, ' curr_key: ', curr_key, ' bin 1 mid: ', curr_key_bin1_mid, ' bin 2 mid: ', curr_key_bin2_mid, ' CC: ', curr_cc, ' Pval: ', curr_pval, ' Qval: ', curr_qval)
391393
if (j == 0):
392394
# first index
393395
rep_bin_key = curr_key
@@ -412,7 +414,7 @@ def main():
412414
qval = CurrChrDict[rep_bin_key]._GetQVal()
413415

414416
if 0:
415-
print '**** Selected bin key: ', rep_bin_key, ' start bin mid: ', (rep_bin1_low + rep_bin1_high)/2, ' end bin mid: ', (rep_bin2_low + rep_bin2_high) / 2, ' cc: ', cc, ' pval: ', pval, ' qval: ', qval
417+
print('**** Selected bin key: ', rep_bin_key, ' start bin mid: ', (rep_bin1_low + rep_bin1_high)/2, ' end bin mid: ', (rep_bin2_low + rep_bin2_high) / 2, ' cc: ', cc, ' pval: ', pval, ' qval: ', qval)
416418

417419
# write the interaction in the specified output file
418420
fp_outInt.write('\n' + str(curr_chr) + '\t' + str(rep_bin1_low) + '\t' + str(rep_bin1_high) + '\t' + str(curr_chr) + '\t' + str(rep_bin2_low) + '\t' + str(rep_bin2_high) + '\t' + str(cc) + '\t' + str(pval) + '\t' + str(qval) + '\t' + str(span_low_bin1) + '\t' + str(span_high_bin1) + '\t' + str(span_low_bin2) + '\t' + str(span_high_bin2) + '\t' + str(sum_cc) + '\t' + str(Percent_Significant_BinPair))
@@ -468,7 +470,7 @@ def main():
468470
custom_qval = custom_percent(curr_conn_comp_QValList, TopPctElem, (SortOrder+1))
469471

470472
if 0:
471-
print ' --> current connected component: max CC: ', max_cc, ' min Q val: ', min_qval, ' top K (TopPctElem): ', TopPctElem, ' custom_cc threshold: ', custom_cc, ' custom_qval threshold: ', custom_qval
473+
print(' --> current connected component: max CC: ', max_cc, ' min Q val: ', min_qval, ' top K (TopPctElem): ', TopPctElem, ' custom_cc threshold: ', custom_cc, ' custom_qval threshold: ', custom_qval)
472474

473475
# this list stores the candidate interactions
474476
# from this particular connected component
@@ -479,7 +481,7 @@ def main():
479481
while (len(Curr_Comp_Tuple_List) > 0):
480482
curr_elem = heapq.heappop(Curr_Comp_Tuple_List)
481483
if 0:
482-
print 'extracted element from heap: ', curr_elem
484+
print('extracted element from heap: ', curr_elem)
483485

484486
#===================================
485487
# earlier condition - 1 - sourya
@@ -509,7 +511,7 @@ def main():
509511
subl = [curr_elem[2], curr_elem[3]]
510512
Final_Rep_Key_List.append(subl)
511513
if 0:
512-
print '\t\t *** inserted element in the final list: ', str(subl), ' generated Final_Rep_Key_List: ', str(Final_Rep_Key_List)
514+
print('\t\t *** inserted element in the final list: ', str(subl), ' generated Final_Rep_Key_List: ', str(Final_Rep_Key_List))
513515
continue
514516

515517
# otherwise, check with the existing interactions
@@ -523,7 +525,7 @@ def main():
523525
if (((abs(Final_Rep_Key_List[i][0] - curr_elem[2])) * bin_size) <= NeighborHoodBinThr) and (((abs(Final_Rep_Key_List[i][1] - curr_elem[3])) * bin_size) <= NeighborHoodBinThr):
524526
flag = True
525527
if 0:
526-
print ' --- current element is within neighborhood of the bins indexed by ', i, ' of Final_Rep_Key_List'
528+
print(' --- current element is within neighborhood of the bins indexed by ', i, ' of Final_Rep_Key_List')
527529
break
528530

529531
if (flag == False):
@@ -532,7 +534,7 @@ def main():
532534
subl = [curr_elem[2], curr_elem[3]]
533535
Final_Rep_Key_List.append(subl)
534536
if 0:
535-
print '\t\t *** inserted element in the final list: ', str(subl), ' generated Final_Rep_Key_List: ', str(Final_Rep_Key_List)
537+
print('\t\t *** inserted element in the final list: ', str(subl), ' generated Final_Rep_Key_List: ', str(Final_Rep_Key_List))
536538

537539
# now print the candidate interactions
538540
# of the current component
@@ -549,7 +551,7 @@ def main():
549551
qval = CurrChrDict[rep_bin_key]._GetQVal()
550552

551553
if 0:
552-
print '**** Selected bin key: ', rep_bin_key, ' start bin mid: ', (rep_bin1_low + rep_bin1_high)/2, ' end bin mid: ', (rep_bin2_low + rep_bin2_high) / 2, ' cc: ', cc, ' pval: ', pval, ' qval: ', qval
554+
print('**** Selected bin key: ', rep_bin_key, ' start bin mid: ', (rep_bin1_low + rep_bin1_high)/2, ' end bin mid: ', (rep_bin2_low + rep_bin2_high) / 2, ' cc: ', cc, ' pval: ', pval, ' qval: ', qval)
553555

554556
# write the interaction in the specified output file
555557
fp_outInt.write('\n' + str(curr_chr) + '\t' + str(rep_bin1_low) + '\t' + str(rep_bin1_high) + '\t' + str(curr_chr) + '\t' + str(rep_bin2_low) + '\t' + str(rep_bin2_high) + '\t' + str(cc) + '\t' + str(pval) + '\t' + str(qval) + '\t' + str(span_low_bin1) + '\t' + str(span_high_bin1) + '\t' + str(span_low_bin2) + '\t' + str(span_high_bin2) + '\t' + str(sum_cc) + '\t' + str(Percent_Significant_BinPair))
@@ -599,23 +601,23 @@ def main():
599601
Final_Rep_Key_List = []
600602

601603
if 0:
602-
print ' **** Processing the connected component ===== number of elements: ', len(Curr_Comp_Tuple_List)
604+
print(' **** Processing the connected component ===== number of elements: ', len(Curr_Comp_Tuple_List))
603605

604606
# now extract elements from the constructed queue
605607
while (len(Curr_Comp_Tuple_List) > 0):
606608
# extract the first element from the min-heap
607609
# element with the lowest value
608610
curr_elem = heapq.heappop(Curr_Comp_Tuple_List)
609611
if 0:
610-
print 'extracted element from heap: ', curr_elem
612+
print('extracted element from heap: ', curr_elem)
611613

612614
# if this is the first element
613615
# then insert they key in the candidate set of interactions
614616
if (len(Final_Rep_Key_List) == 0):
615617
subl = [curr_elem[2], curr_elem[3]]
616618
Final_Rep_Key_List.append(subl)
617619
if 0:
618-
print '*** inserted element in the final list: ', str(subl)
620+
print('*** inserted element in the final list: ', str(subl))
619621
continue
620622

621623
# otherwise, check with the existing interactions
@@ -629,7 +631,7 @@ def main():
629631
if (((abs(Final_Rep_Key_List[i][0] - curr_elem[2])) * bin_size) <= NeighborHoodBinThr) and (((abs(Final_Rep_Key_List[i][1] - curr_elem[3])) * bin_size) <= NeighborHoodBinThr):
630632
flag = True
631633
if 0:
632-
print ' --- current element is within neighborhood of the existing (included) bin ', Final_Rep_Key_List[i]
634+
print(' --- current element is within neighborhood of the existing (included) bin ', Final_Rep_Key_List[i])
633635
break
634636

635637
if (flag == False):
@@ -638,12 +640,12 @@ def main():
638640
subl = [curr_elem[2], curr_elem[3]]
639641
Final_Rep_Key_List.append(subl)
640642
if 0:
641-
print '*** inserted element in the final list: ', str(subl)
643+
print('*** inserted element in the final list: ', str(subl))
642644

643645
# now print the candidate interactions
644646
# of the current component
645647
if 0:
646-
print '\n\n**** Printing selected loops of the connected component ***\n\n'
648+
print('\n\n**** Printing selected loops of the connected component ***\n\n')
647649

648650
for i in range(len(Final_Rep_Key_List)):
649651
rep_bin_key = (Final_Rep_Key_List[i][0], Final_Rep_Key_List[i][1])
@@ -657,7 +659,7 @@ def main():
657659
pval = CurrChrDict[rep_bin_key]._GetPVal()
658660
qval = CurrChrDict[rep_bin_key]._GetQVal()
659661
if 0:
660-
print 'Selected bin key: ', rep_bin_key, ' start bin mid: ', (rep_bin1_low + rep_bin1_high)/2, ' end bin mid: ', (rep_bin2_low + rep_bin2_high) / 2, ' cc: ', cc, ' pval: ', pval, ' qval: ', qval
662+
print('Selected bin key: ', rep_bin_key, ' start bin mid: ', (rep_bin1_low + rep_bin1_high)/2, ' end bin mid: ', (rep_bin2_low + rep_bin2_high) / 2, ' cc: ', cc, ' pval: ', pval, ' qval: ', qval)
661663

662664
# write the interaction in the specified output file
663665
fp_outInt.write('\n' + str(curr_chr) + '\t' + str(rep_bin1_low) + '\t' + str(rep_bin1_high) + '\t' + str(curr_chr) + '\t' + str(rep_bin2_low) + '\t' + str(rep_bin2_high) + '\t' + str(cc) + '\t' + str(pval) + '\t' + str(qval) + '\t' + str(span_low_bin1) + '\t' + str(span_high_bin1) + '\t' + str(span_low_bin2) + '\t' + str(span_high_bin2) + '\t' + str(sum_cc) + '\t' + str(Percent_Significant_BinPair))
@@ -677,9 +679,8 @@ def main():
677679
os.system(sys_cmd)
678680

679681
if 1:
680-
print '==================== End of merge filtering adjacent interactions !!! ======================'
682+
print('==================== End of merge filtering adjacent interactions !!! ======================')
681683

682684
#===============================================
683685
if __name__ == "__main__":
684686
main()
685-

0 commit comments

Comments
 (0)