├── README.md └── cool2hic.py /README.md: -------------------------------------------------------------------------------- 1 | # Cooler to hic 2 | 3 | Dependences: 4 | ``` 5 | pip install cooler 6 | pip install pandas 7 | ``` 8 | 9 | 1, cool2hic.py 10 | 11 | Transfer cool to matrix.txt.gz. 12 | ``` 13 | python3 cool2hic.py -i test.mcool/test.cool -r resolution 14 | ``` 15 | 16 | 17 | 2, juicer_tools transfer from matrix text to hic. 18 | 19 | juicer_tools: https://github.com/aidenlab/juicer/wiki/Feature-Annotation 20 | ``` 21 | gzip matrix.txt matrix.txt.gz 22 | java -Xmx20g -jar ~/packages/juicer_tools.jar pre -r resolution -d matrix.txt.gz name.hic genome 23 | ``` 24 | -------------------------------------------------------------------------------- /cool2hic.py: -------------------------------------------------------------------------------- 1 | from optparse import OptionParser 2 | import cooler 3 | import pandas as pd 4 | 5 | desc="Transfer cool to hic." 6 | 7 | parser = OptionParser(description=desc) 8 | 9 | parser.add_option("-i", "--input", action="store", type="string", 10 | dest="input", help="Input cool or mcool file.", metavar="") 11 | 12 | parser.add_option("-r", "--resolution", action="store", type="int", 13 | dest="res", help="Resolution for the output hic file.", metavar="") 14 | 15 | (opt, args) = parser.parse_args() 16 | file = opt.input 17 | resolution = opt.res 18 | 19 | print("Input file: %s" % file) 20 | print("Resolution: %s" % resolution) 21 | 22 | #========================== 23 | 24 | file_type = file.split('.')[len(file.split('.'))-1] 25 | if file_type == 'cool': 26 | c = cooler.Cooler(file) 27 | if file_type == 'mcool': 28 | c = cooler.Cooler(file+'::resolutions/'+str(resolution)) 29 | 30 | chrom = c.chroms()[0:len(c.chroms())] 31 | chrom = chrom[~chrom.name.str.contains('_|M')] 32 | chrom = chrom.sort_values('name') 33 | 34 | for i in chrom.name: 35 | print(i) 36 | hic_chr = c.matrix(balance=False, as_pixels=True, join=True).fetch(i) 37 | hic_chr = hic_chr.iloc[:,[0,1,3,4,6]] 38 | hic_chr['str1'] = 0 39 | hic_chr['str2'] = 0 40 | hic_chr['frag1'] = 0 41 | hic_chr['frag2'] = 1 42 | names = ['str1','chrom1','start1','frag1','str2','chrom2','start2','frag2','count'] 43 | hic_chr = hic_chr.reindex(columns=names) 44 | hic_chr.to_csv('matrix.txt',sep='\t', mode='a', index=None, header=None) 45 | 46 | #=========================== 47 | --------------------------------------------------------------------------------