2 # Time-stamp: <2024-02-12 16:46:59 Tao Liu>
6 from MACS3
.Signal
.Region
import *
9 # ------------------------------------
11 # ------------------------------------
14 sys
.stderr
.write("Calculate Jaccard Index of two peak files and return the index.\n\nneed 2 paras: %s <peak1> <peak2>\n" % sys
.argv
[0])
17 #options.info("# Read peak file 1...")
18 peak1
= open( sys
.argv
[1] )
21 if l
.startswith("track"):
23 fs
= l
.rstrip().split()
24 regions1
.add_loc( fs
[0].encode(), int(fs
[1]), int(fs
[2]) )
27 #options.info("# Read peak file 2...")
28 peak2
= open( sys
.argv
[2] )
31 if l
.startswith("track"):
33 fs
= l
.rstrip().split()
34 regions2
.add_loc( fs
[0].encode(), int(fs
[1]), int(fs
[2]) )
37 tl1
= regions1
.total_length()
38 tl2
= regions2
.total_length()
39 inter
= regions1
.intersect(regions2
)
40 tl_inter
= inter
.total_length()
42 if (tl1
+ tl2
- tl_inter
) == 0:
43 # this means both of the files are empty
46 jaccard_index
= tl_inter
/ (tl1
+ tl2
- tl_inter
)
47 print ( jaccard_index
)
49 if __name__
== '__main__':