-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathimargi_ant.py
293 lines (252 loc) · 10.2 KB
/
imargi_ant.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# modified from original pairtools_restrict
import io
import sys
import click
import re
import HTSeq
# import numpy as np
# use pandas.read_csv to accelerate loading rsite file, much faster than np.genfromtxt
# import pandas as pd
from pairtools import _fileio, _pairsam_format, cli, _headerops, common_io_options
UTIL_NAME = 'imargi_annotate'
@cli.command()
@click.argument(
'pairs_path',
type=str,
required=False)
@click.option(
'-A', '--ant_format',
type=str,
required=True,
help='annotation file format, \'gtf\' or \'bed\'')
@click.option(
'-a', '--ant_file',
type=str,
required=True,
help='annotation file')
@click.option(
'-l', '--ant_level',
type=str,
default='gene',
help='gene annotation level for GTF annotation, default is gene')
@click.option(
'-f', '--ant_attr',
type=str,
default='gene_id,gene_name,gene_type',
help='gene annotation attributes, default is gene_id, gene_name, and gene_type seperated by | in output result')
@click.option(
'-C', '--ant_mode',
type=str,
required=True,
help='which end to be annotated, \'RNA\', \'DNA\' \'both\'')
@click.option(
'-c', '--ant_col',
type=str,
required=True,
help='colnames for anntations in output .pairs file')
@click.option(
'-s', '--strand_type',
type=str,
default='rn',
help='strand-specific settings for RNA and DNA end annotation, default is \'rn\', i.e., reverse-strand-specific \
for RNA end and ignoring strand info for DNA end.')
@click.option(
'-m', '--min_over',
type=str,
default=1,
help='minimum bases of overlapping for annotation, 0 means it must be totally inside of the genomic feature')
@click.option(
'-G', '--cigar_col',
type=str,
default='cigar1,cigar2',
help='CIGAR information columns for annotating, default \'cigar1\' and \'cigar2\'. \'FALSE\' means ignore CIGAR')
@click.option(
'-o', "--output",
type=str,
default="",
help='output .pairs/.pairsam file.'
' If the path ends with .gz/.lz4, the output is compressed by pbgzip/lz4c.'
' By default, the output is printed into stdout.')
@click.option(
'-h', "--help",
type=str,
default="",
help='output .pairs/.pairsam file.'
' If the path ends with .gz/.lz4, the output is compressed by pbgzip/lz4c.'
' By default, the output is printed into stdout.')
@common_io_options
def annotate(pairs_path, ant_format, ant_file, ant_level, ant_attr, ant_mode, ant_col, strand_type,
min_over, cigar_col, output, **kwargs):
'''
Annotate both RNA, DNA ends with gene annotations in GTF/GFF format or any other genomic
features in a simple BED file (each line is a named genomic feature).
'''
if strand_type == 'n' or strand_type == 'nn':
stranded = False
else:
stranded = True
if ant_format.lower() == "gtf":
print("Loading GTF annotation file ... ")
ant = read_gtf(ant_file, ant_level, ant_attr, stranded)
else:
print("Loading BED annotation file ... ")
ant = read_bed(ant_file, stranded)
annotate_pairs(pairs_path, ant, ant_mode, ant_col, strand_type, min_over, cigar_col, output, **kwargs)
def read_gtf(ant_file, ant_level, ant_attr, stranded = True):
gtf = HTSeq.GFF_Reader(ant_file)
ant = HTSeq.GenomicArrayOfSets("auto", stranded = stranded)
ant_attr = ant_attr.split(',')
for feature in gtf:
if feature.type == ant_level:
attrs = []
for i in ant_attr:
attrs += [feature.attr[i]]
ant[feature.iv] += "|".join(attrs)
return ant
def read_bed(ant_file, stranded = True):
bed = HTSeq.BED_Reader(ant_file)
ant = HTSeq.GenomicArrayOfSets("auto", stranded = stranded)
for feature in bed:
ant[ feature.iv ] += feature.name
return ant
def annotate_region(ant, chr_str, pos, strand, match_length, min_over, strand_type):
if chr_str == '!':
return '.'
ant_region = set()
# change 1-based to zero-based genomic coordinate
if strand == "+":
start = pos - 1
end = pos + match_length
else:
start = pos - match_length
end = pos
min_over = int(min_over)
if min_over == 0:
min_over = match_length
ant_stranded = ant.stranded
if ant_stranded == False:
region_iv = HTSeq.GenomicInterval(chr_str, start, end, strand)
for iv, val in ant[region_iv].steps():
if iv.length >= min_over:
ant_region |= val
else:
if strand_type == 'r':
strand = '-' if strand == '+' else '+'
if strand_type == 'n':
strand = '.'
if strand == '.':
region_iv = HTSeq.GenomicInterval(chr_str, start, end, '+')
for iv, val in ant[region_iv].steps():
if iv.length >= min_over:
ant_region |= val
region_iv = HTSeq.GenomicInterval(chr_str, start, end, '-')
for iv, val in ant[region_iv].steps():
if iv.length >= min_over:
ant_region |= val
else:
region_iv = HTSeq.GenomicInterval(chr_str, start, end, strand)
for iv, val in ant[region_iv].steps():
if iv.length >= min_over:
ant_region |= val
if len(ant_region) > 0:
return ','.join(ant_region)
else:
return '.'
def annotate_pairs(pairs_path, ant, ant_mode, ant_col, strand_type, min_over, cigar_col, output, **kwargs):
instream = (_fileio.auto_open(pairs_path, mode='r',
nproc=kwargs.get('nproc_in'),
command=kwargs.get('cmd_in', None))
if pairs_path else sys.stdin)
outstream = (_fileio.auto_open(output, mode='w',
nproc=kwargs.get('nproc_out'),
command=kwargs.get('cmd_out', None))
if output else sys.stdout)
header, body_stream = _headerops.get_header(instream)
header = _headerops.append_new_pg(header, ID=UTIL_NAME, PN=UTIL_NAME)
if len(header) == 0:
sys.stderr.write('.pairs file doesn\'t have header rows!\n')
raise SystemExit(1)
col_names = header[-1].split(' ')
if col_names[0] != '#columns:':
sys.stderr.write('The last row of .pairs header is not a valid col_names row (start with \'#columns:\')!\n')
raise SystemExit(1)
col_names.pop(0)
ant_col = ant_col.split(',')
for i in ant_col:
if i in col_names:
sys.stderr.write('Annotation col names already exist in .pairs file!\n')
raise SystemExit(1)
for i in strand_type:
if i not in ['s', 'r', 'n']:
sys.stderr.write('Invalid strand specific type for annotation!\n')
raise SystemExit(1)
if ant_mode.lower() == 'both':
header[-1] = header[-1] + ' ' + ' '.join(ant_col)
else:
header[-1] = header[-1] + ' ' + ant_col[0]
min_over = [int(i) for i in min_over.split(',')]
cigar_col = cigar_col.split(',')
cigar_idx = []
for i in cigar_col:
if i not in col_names and i.lower() != 'false':
sys.stderr.write('Cigar col names doesn\'t exist in .pairs file!\n')
raise SystemExit(1)
else:
cigar_idx += [col_names.index(i)]
outstream.writelines(l + '\n' for l in header)
count_line = 1
for line in body_stream:
if count_line % 1000000 == 0:
print("%d records processed ..." % count_line)
count_line += 1
cols = line.rstrip().split(_pairsam_format.PAIRSAM_SEP)
if ant_mode.lower() == 'rna':
chrom1, pos1, strand1, cigar1 = cols[_pairsam_format.COL_C1], int(cols[_pairsam_format.COL_P1]), \
cols[_pairsam_format.COL_S1], cols[cigar_idx[0]]
if cigar_idx[0] == 'false':
match_length1 = 1
else:
if cigar1 == '*':
cigar1 = '1M'
match_length1 = sum([i.ref_iv.length for i in HTSeq.parse_cigar(cigar1)])
ant_str = annotate_region(ant, chrom1, pos1, strand1, match_length1, min_over[0], strand_type[0])
elif ant_mode.lower() == 'dna':
chrom2, pos2, strand2, cigar2 = cols[_pairsam_format.COL_C2], int(cols[_pairsam_format.COL_P2]), \
cols[_pairsam_format.COL_S2], cols[cigar_idx[0]]
if cigar_idx[0] == 'false':
match_length2 = 1
else:
if cigar2 == '*':
cigar2 = '1M'
match_length2 = sum([i.ref_iv.length for i in HTSeq.parse_cigar(cigar2)])
ant_str = annotate_region(ant, chrom2, pos2, strand2, match_length2, min_over[0], strand_type[0])
else:
chrom1, pos1, strand1, cigar1 = cols[_pairsam_format.COL_C1], int(cols[_pairsam_format.COL_P1]), \
cols[_pairsam_format.COL_S1], cols[cigar_idx[0]]
if cigar_idx[0] == 'false':
match_length1 = 1
else:
if cigar1 == '*':
cigar1 = '1M'
match_length1 = sum([i.ref_iv.length for i in HTSeq.parse_cigar(cigar1)])
ant_str = annotate_region(ant, chrom1, pos1, strand1, match_length1, min_over[0], strand_type[0])
chrom2, pos2, strand2, cigar2 = cols[_pairsam_format.COL_C2], int(cols[_pairsam_format.COL_P2]), \
cols[_pairsam_format.COL_S2], cols[cigar_idx[1]]
if cigar_idx[1] == 'false':
match_length2 = 1
else:
if cigar2 == '*':
cigar2 = '1M'
match_length2 = sum([i.ref_iv.length for i in HTSeq.parse_cigar(cigar2)])
ant_str += _pairsam_format.PAIRSAM_SEP + \
annotate_region(ant, chrom2, pos2, strand2, match_length2, min_over[1], strand_type[1])
outstream.write(_pairsam_format.PAIRSAM_SEP.join([line.rstrip(), ant_str]))
outstream.write('\n')
if instream != sys.stdin:
instream.close()
if outstream != sys.stdout:
outstream.close()
if __name__ == '__main__':
annotate()