-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhiczin.py
265 lines (209 loc) · 10.5 KB
/
hiczin.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
#########The structure of the main script is modified from bin3C########
from raw_contact import ContactMatrix, ContactMatrix_LC
from hiczin_contact import HiCzinMap, HiCzinMap_LC
from exceptions import ApplicationException
from utils import load_object, save_object, make_dir
import scipy.sparse as scisp
import logging
import sys
import argparse
import os
import time
import warnings
##Ignore the warning information of package deprecation##
warnings.filterwarnings("ignore")
__version__ = '0.1.0, released at 12/2020'
if __name__ == '__main__':
def mk_version():
return 'HiCzin v{}'.format(__version__)
def out_name(base, suffix):
return '{}{}'.format(base, suffix)
def ifelse(arg, default):
if arg is None:
return default
else:
return arg
runtime_defaults = {
'min_len': 1000,
'min_signal': 2,
'min_mapq': 30,
'min_match': 30,
'thres': 0.05
}
global_parser = argparse.ArgumentParser(add_help=False)
global_parser.add_argument('-V', '--version', default=False, action='store_true', help='Show the application version')
global_parser.add_argument('-v', '--verbose', default=False, action='store_true', help='Verbose output')
global_parser.add_argument('--cover', default=False, action='store_true', help='Cover existing files')
global_parser.add_argument('--log', help='Log file path [OUTDIR/hiczin.log]')
parser = argparse.ArgumentParser(description='HiCzin: Normalizing metagenomic Hi-C data and detecting spurious contacts using zero-inflated negative binomial regression')
subparsers = parser.add_subparsers(title='commands', dest='command', description='Valid commands',
help='choose an analysis stage for further options')
cmd_pl = subparsers.add_parser('norm', parents=[global_parser],
description='Normalize contacts and do the binning.')
cmd_test = subparsers.add_parser('test', parents=[global_parser],
description='pipeline testing.')
'''
pipeline subparser input
'''
cmd_pl.add_argument('--min-len', type=int,
help='Minimum acceptable reference length [1000]')
cmd_pl.add_argument('--min-signal', type=int, help='Minimum acceptable signal [2]')
cmd_pl.add_argument('--min-mapq', type=int,
help='Minimum acceptable mapping quality [30]')
cmd_pl.add_argument('--min-match', type=int,
help='Accepted alignments must being N matches [30]')
cmd_pl.add_argument('-e', '--enzyme', metavar='NEB_NAME', action='append',
help='Case-sensitive enzyme name. Use multiple times for multiple enzymes')
cmd_pl.add_argument('--thres', type=float,
help='acceptable fraction of incorrectly identified valid contacts [0.05]')
cmd_pl.add_argument('FASTA', help='Reference fasta sequence')
cmd_pl.add_argument('BAM', help='Input bam file in query order')
cmd_pl.add_argument('TAX', help='Contig labels from TAXAssign')
cmd_pl.add_argument('COV', help='Coverage information of assembled contigs')
cmd_pl.add_argument('OUTDIR', help='Output directory')
'''
Testing of HiCzin software
'''
cmd_test.add_argument('OUTDIR', help='Output directory of testing results')
args = parser.parse_args()
if args.version:
print(mk_version())
sys.exit(0)
try:
make_dir(args.OUTDIR, args.cover)
except IOError:
print('Error: cannot find out directory or the directory already exists')
sys.exit(1)
logging.captureWarnings(True)
logger = logging.getLogger('main')
# root log listens to everything
root = logging.getLogger('')
root.setLevel(logging.DEBUG)
# log message format
formatter = logging.Formatter(fmt='%(levelname)-8s | %(asctime)s | %(name)7s | %(message)s')
# Runtime console listens to INFO by default
ch = logging.StreamHandler()
if args.verbose:
ch.setLevel(logging.DEBUG)
else:
ch.setLevel(logging.INFO)
ch.setFormatter(formatter)
root.addHandler(ch)
# File log listens to all levels from root
if args.log is not None:
log_path = args.log
else:
log_path = os.path.join(args.OUTDIR, 'hiczin.log')
fh = logging.FileHandler(log_path, mode='a')
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
root.addHandler(fh)
# Add some environmental details
logger.debug(mk_version())
logger.debug(sys.version.replace('\n', ' '))
logger.debug('Command line: {}'.format(' '.join(sys.argv)))
try:
start_time = time.time()
if args.command == 'norm':
if args.enzyme is not None:
# Create a contact map for analysis by HiCzin
cm = ContactMatrix(args.BAM,
args.enzyme,
args.FASTA,
args.TAX,
args.COV,
args.OUTDIR,
min_mapq=ifelse(args.min_mapq, runtime_defaults['min_mapq']),
min_len=ifelse(args.min_len, runtime_defaults['min_len']),
min_match=ifelse(args.min_match, runtime_defaults['min_match']),
min_signal=ifelse(args.min_signal, runtime_defaults['min_signal']))
if cm.is_empty():
logger.info('Stopping as the contact matrix is empty')
sys.exit(1)
logger.info('Raw contact matrix construction finished')
logger.info('Normalizing raw contacts by HiCzin...')
from rpy2 import robjects
r = robjects.r
r.source('HiCzin.R')
contig_file = os.path.join(args.OUTDIR ,'contig_info.csv')
valid_file = os.path.join(args.OUTDIR ,'valid_contact.csv')
thres = ifelse(args.thres, runtime_defaults['thres'])
norm_result = r.HiCzin(contig_file , valid_file , thres)
hzmap = HiCzinMap(args.OUTDIR,
cm.seq_info,
cm.seq_map,
norm_result,
ifelse(args.min_signal, runtime_defaults['min_signal']))
else:
cm = ContactMatrix_LC(args.BAM,
args.FASTA,
args.TAX,
args.COV,
args.OUTDIR,
min_mapq=ifelse(args.min_mapq, runtime_defaults['min_mapq']),
min_len=ifelse(args.min_len, runtime_defaults['min_len']),
min_match=ifelse(args.min_match, runtime_defaults['min_match']),
min_signal=ifelse(args.min_signal, runtime_defaults['min_signal']))
if cm.is_empty():
logger.info('Stopping as the contact matrix is empty')
sys.exit(1)
logger.info('Raw contact matrix construction finished')
logger.info('Normalizing by HiCzin_LC because no enzymes are input...')
from rpy2 import robjects
r = robjects.r
r.source('HiCzin.R')
contig_file = os.path.join(args.OUTDIR ,'contig_info.csv')
valid_file = os.path.join(args.OUTDIR ,'valid_contact.csv')
thres = ifelse(args.thres, runtime_defaults['thres'])
norm_result = r.HiCzin(contig_file , valid_file , thres)
hzmap = HiCzinMap_LC(args.OUTDIR,
cm.seq_info,
cm.seq_map,
norm_result,
ifelse(args.min_signal, runtime_defaults['min_signal']))
logger.info('Normalization finished')
end_time = time.time()
logger.info('HiCzin consumes {} seconds in total'.format(str(end_time-start_time)))
logger.info('Saving Normalized Hi-C contact maps')
scisp.save_npz(os.path.join(args.OUTDIR, 'Normalized_contact_matrix.npz'), hzmap.seq_map.tocsr())
save_object(os.path.join(args.OUTDIR, 'HiCzin_normalized_contact'), hzmap)
if args.command == 'test':
logger.info('Begin to test the HiCzin software...')
ENZ = 'HindIII'
FASTA = 'test/contig_assembly_test.fa'
BAM = 'test/align_test.bam'
TAX = 'test/contig_tax_test.csv'
COV = 'test/coverage_test.txt'
logger.info('Begin to test the contact map construction section...')
cm = ContactMatrix(BAM,
ENZ,
FASTA,
TAX,
COV,
args.OUTDIR,
min_mapq=runtime_defaults['min_mapq'],
min_len=runtime_defaults['min_len'],
min_match=runtime_defaults['min_match'],
min_signal=0)
logger.info('Contact map construction section works!')
logger.info('Begin to test the normalization section...')
logger.info('Normalizing by HiCzin...')
from rpy2 import robjects
r = robjects.r
r.source('HiCzin.R')
contig_file = 'test/contig_info_test.csv'
valid_file = 'test/must_contact_test.csv'
thres = runtime_defaults['thres']
norm_result = r.HiCzin(contig_file , valid_file , thres)
hzmap = HiCzinMap(args.OUTDIR,
cm.seq_info,
cm.seq_map,
norm_result,
0)
scisp.save_npz(os.path.join(args.OUTDIR, 'Normalized_contact_matrix.npz'), hzmap.seq_map.tocsr())
save_object(os.path.join(args.OUTDIR, 'HiCzin_normalized_contact'), hzmap)
logger.info('Normalization section works!')
logger.info('Testing finished!')
except ApplicationException:
logger.error('ApplicationException Error')
sys.exit(1)