-
Notifications
You must be signed in to change notification settings - Fork 2
/
htseq-count_custom.py
370 lines (321 loc) · 14.3 KB
/
htseq-count_custom.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# script written by Simon Anders, part of the package HTSeq (http://www-huber.embl.de/users/anders/HTSeq/doc/count.html)
# every modification made to the script is surrounded by any of the following lines:
# modified ################
# added ###################
# de-indented #############
# finish de-indentation ###
###########################
import sys, optparse, itertools, warnings, traceback, os.path
# added ###################
import random
###########################
import HTSeq
class UnknownChrom( Exception ):
pass
def invert_strand( iv ):
iv2 = iv.copy()
if iv2.strand == "+":
iv2.strand = "-"
elif iv2.strand == "-":
iv2.strand = "+"
else:
raise ValueError, "Illegal strand"
return iv2
def count_reads_in_features( sam_filename, gff_filename, stranded,
overlap_mode, feature_type, id_attribute, quiet, minaqual, samout ):
def write_to_samout( r, assignment ):
if samoutfile is None:
return
if not pe_mode:
r = (r,)
for read in r:
if read is not None:
samoutfile.write( read.original_sam_line.rstrip() +
"\tXF:Z:" + assignment + "\n" )
if quiet:
warnings.filterwarnings( action="ignore", module="HTSeq" )
if samout != "":
samoutfile = open( samout, "w" )
else:
samoutfile = None
features = HTSeq.GenomicArrayOfSets( "auto", stranded != "no" )
counts = {}
# Try to open samfile to fail early in case it is not there
if sam_filename != "-":
open( sam_filename ).close()
gff = HTSeq.GFF_Reader( gff_filename )
i = 0
try:
for f in gff:
if f.type == feature_type:
try:
feature_id = f.attr[ id_attribute ]
except KeyError:
sys.exit( "Feature %s does not contain a '%s' attribute" %
( f.name, id_attribute ) )
if stranded != "no" and f.iv.strand == ".":
sys.exit( "Feature %s at %s does not have strand information but you are "
"running htseq-count in stranded mode. Use '--stranded=no'." %
( f.name, f.iv ) )
features[ f.iv ] += feature_id
counts[ f.attr[ id_attribute ] ] = 0
i += 1
if i % 100000 == 0 and not quiet:
sys.stderr.write( "%d GFF lines processed.\n" % i )
except:
sys.stderr.write( "Error occured in %s.\n" % gff.get_line_number_string() )
raise
if not quiet:
sys.stderr.write( "%d GFF lines processed.\n" % i )
if len( counts ) == 0 and not quiet:
sys.stderr.write( "Warning: No features of type '%s' found.\n" % feature_type )
try:
if sam_filename != "-":
read_seq = HTSeq.SAM_Reader( sam_filename )
first_read = iter(read_seq).next()
else:
read_seq = iter( HTSeq.SAM_Reader( sys.stdin ) )
first_read = read_seq.next()
read_seq = itertools.chain( [ first_read ], read_seq )
pe_mode = first_read.paired_end
except:
sys.stderr.write( "Error occured when reading first line of sam file.\n" )
raise
try:
if pe_mode:
read_seq_pe_file = read_seq
read_seq = HTSeq.pair_SAM_alignments( read_seq )
empty = 0
ambiguous = 0
notaligned = 0
lowqual = 0
nonunique = 0
i = 0
for r in read_seq:
i += 1
if not pe_mode:
if not r.aligned:
notaligned += 1
write_to_samout( r, "not_aligned" )
continue
# modified ###################
#try:
# if r.optional_field( "NH" ) > 1:
# write_to_samout( r, "alignment_not_unique" )
# nonunique += 1
# continue
#except KeyError:
# pass
# added ######################
if r.optional_field( "NH" ) > 1:
#write_to_samout( r, "alignment_not_unique" )
nonunique += 1
##############################
if r.aQual < minaqual:
lowqual += 1
write_to_samout( r, "too_low_aQual" )
continue
if stranded != "reverse":
iv_seq = ( co.ref_iv for co in r.cigar if co.type == "M" )
# added ###################################################
if r.iv.strand == '+':
st = list(iv_seq)[-1].end
iv_start = HTSeq.GenomicInterval(r.iv.chrom, st-1, st, '+')
else:
st = list(iv_seq)[0].start
iv_start = HTSeq.GenomicInterval(r.iv.chrom, st, st+1, '-')
iv_seq = ( co.ref_iv for co in r.cigar if co.type == "M" )
###########################################################
else:
iv_seq = ( invert_strand( co.ref_iv ) for co in r.cigar if co.type == "M" )
# added ###################################################
if r.iv.strand == '+':
st = list(iv_seq)[0].start
iv_start = HTSeq.GenomicInterval(r.iv.chrom, st, st+1, '-')
else:
st = list(iv_seq)[-1].end
iv_start = HTSeq.GenomicInterval(r.iv.chrom, st-1, st, '+')
iv_seq = ( invert_strand( co.ref_iv ) for co in r.cigar if co.type == "M" )
###########################################################
else:
if r[0] is not None and r[0].aligned:
if stranded != "reverse":
iv_seq = ( co.ref_iv for co in r[0].cigar if co.type == "M" )
else:
iv_seq = ( invert_strand( co.ref_iv ) for co in r[0].cigar if co.type == "M" )
else:
iv_seq = tuple()
if r[1] is not None and r[1].aligned:
if stranded != "reverse":
iv_seq = itertools.chain( iv_seq,
( invert_strand( co.ref_iv ) for co in r[1].cigar if co.type == "M" ) )
else:
iv_seq = itertools.chain( iv_seq,
( co.ref_iv for co in r[1].cigar if co.type == "M" ) )
else:
if ( r[0] is None ) or not ( r[0].aligned ):
write_to_samout( r, "not_aligned" )
notaligned += 1
continue
# modified ###################
#try:
# if ( r[0] is not None and r[0].optional_field( "NH" ) > 1 ) or \
# ( r[1] is not None and r[1].optional_field( "NH" ) > 1 ):
# nonunique += 1
# write_to_samout( r, "alignment_not_unique" )
# continue
#except KeyError:
# pass
# added ###################
if ( r[0] is not None and r[0].optional_field( "NH" ) > 1 ) and \
( r[1] is not None and r[1].optional_field( "NH" ) > 1 ):
#write_to_samout( r, "alignment_not_unique" )
nonunique += 1
###########################
if ( r[0] and r[0].aQual < minaqual ) or ( r[1] and r[1].aQual < minaqual ):
lowqual += 1
write_to_samout( r, "too_low_aQual" )
continue
try:
if overlap_mode == "union":
fs = set()
#modified #########
#for iv in iv_seq:
iv = iv_start
###################
# de-indented #####
if iv.chrom not in features.chrom_vectors:
raise UnknownChrom
for iv2, fs2 in features[ iv ].steps():
fs = fs.union( fs2 )
# finish de-indentation #####
#############################
elif overlap_mode == "intersection-strict" or overlap_mode == "intersection-nonempty":
fs = None
#modified #########
#for iv in iv_seq:
iv = iv_start
###################
# de-indented #####
if iv.chrom not in features.chrom_vectors:
raise UnknownChrom
for iv2, fs2 in features[ iv ].steps():
if len(fs2) > 0 or overlap_mode == "intersection-strict":
if fs is None:
fs = fs2.copy()
else:
fs = fs.intersection( fs2 )
# finish de-indentation #####
#############################
else:
sys.exit( "Illegal overlap mode." )
if fs is None or len( fs ) == 0:
write_to_samout( r, "no_feature" )
empty += (1/float(r.optional_field( "NH" )))
elif len( fs ) > 1:
# modified ###################
# in order to randomly assign read overlapping several feature, to one of those feature
# (taking also into account the number of time the read mapped to the genome)
ambiguous += (1/float(r.optional_field( "NH" )))
index = int(random.random() * len(list(fs)))
feat = list(fs)[index]
write_to_samout( r, "ambiguous[" + '+'.join( fs ) + "] randomly Picked: " + feat)
counts[ feat ] += (1/float(r.optional_field( "NH" )))
#############################
else:
write_to_samout( r, list(fs)[0] )
# modified ###################
counts[ list(fs)[0] ] += (1/float(r.optional_field( "NH" )))
#############################
except UnknownChrom:
if not pe_mode:
rr = r
else:
rr = r[0] if r[0] is not None else r[1]
if not quiet:
sys.stderr.write( ( "Warning: Skipping read '%s', because chromosome " +
"'%s', to which it has been aligned, did not appear in the GFF file.\n" ) %
( rr.read.name, iv.chrom ) )
if i % 100000 == 0 and not quiet:
sys.stderr.write( "%d sam %s processed.\n" % ( i, "lines " if not pe_mode else "line pairs" ) )
except:
if not pe_mode:
sys.stderr.write( "Error occured in %s.\n" % read_seq.get_line_number_string() )
else:
sys.stderr.write( "Error occured in %s.\n" % read_seq_pe_file.get_line_number_string() )
raise
if not quiet:
sys.stderr.write( "%d sam %s processed.\n" % ( i, "lines " if not pe_mode else "line pairs" ) )
if samoutfile is not None:
samoutfile.close()
# modified ###################
for fn in sorted( counts.keys() ):
print "%s\t%d" % ( fn, float(counts[fn]))
##############################
print "no_feature\t%d" % empty
print "ambiguous\t%d" % ambiguous
print "too_low_aQual\t%d" % lowqual
print "not_aligned\t%d" % notaligned
print "alignment_not_unique\t%d" % nonunique
def main():
optParser = optparse.OptionParser(
usage = "%prog [options] sam_file gff_file",
description=
"This script takes an alignment file in SAM format and a " +
"feature file in GFF format and calculates for each feature " +
"the number of reads mapping to it. See " +
"http://www-huber.embl.de/users/anders/HTSeq/doc/count.html for details.",
epilog =
"Written by Simon Anders ([email protected]), European Molecular Biology " +
"Laboratory (EMBL). (c) 2010. Released under the terms of the GNU General " +
"Public License v3. Part of the 'HTSeq' framework, version %s." % HTSeq.__version__ )
optParser.add_option( "-m", "--mode", type="choice", dest="mode",
choices = ( "union", "intersection-strict", "intersection-nonempty" ),
default = "union", help = "mode to handle reads overlapping more than one feature" +
"(choices: union, intersection-strict, intersection-nonempty; default: union)" )
optParser.add_option( "-s", "--stranded", type="choice", dest="stranded",
choices = ( "yes", "no", "reverse" ), default = "yes",
help = "whether the data is from a strand-specific assay. Specify 'yes', " +
"'no', or 'reverse' (default: yes). " +
"'reverse' means 'yes' with reversed strand interpretation" )
optParser.add_option( "-a", "--minaqual", type="int", dest="minaqual",
default = 0,
help = "skip all reads with alignment quality lower than the given " +
"minimum value (default: 0)" )
optParser.add_option( "-t", "--type", type="string", dest="featuretype",
default = "exon", help = "feature type (3rd column in GFF file) to be used, " +
"all features of other type are ignored (default, suitable for Ensembl " +
"GTF files: exon)" )
optParser.add_option( "-i", "--idattr", type="string", dest="idattr",
default = "gene_id", help = "GFF attribute to be used as feature ID (default, " +
"suitable for Ensembl GTF files: gene_id)" )
optParser.add_option( "-o", "--samout", type="string", dest="samout",
default = "", help = "write out all SAM alignment records into an output " +
"SAM file called SAMOUT, annotating each line with its feature assignment " +
"(as an optional field with tag 'XF')" )
optParser.add_option( "-q", "--quiet", action="store_true", dest="quiet",
help = "suppress progress report and warnings" )
if len( sys.argv ) == 1:
optParser.print_help()
sys.exit(1)
(opts, args) = optParser.parse_args()
if len( args ) != 2:
sys.stderr.write( sys.argv[0] + ": Error: Please provide two arguments.\n" )
sys.stderr.write( " Call with '-h' to get usage information.\n" )
sys.exit( 1 )
warnings.showwarning = my_showwarning
try:
count_reads_in_features( args[0], args[1], opts.stranded,
opts.mode, opts.featuretype, opts.idattr, opts.quiet, opts.minaqual,
opts.samout )
except:
sys.stderr.write( "Error: %s\n" % str( sys.exc_info()[1] ) )
sys.stderr.write( "[Exception type: %s, raised in %s:%d]\n" %
( sys.exc_info()[1].__class__.__name__,
os.path.basename(traceback.extract_tb( sys.exc_info()[2] )[-1][0]),
traceback.extract_tb( sys.exc_info()[2] )[-1][1] ) )
sys.exit( 1 )
def my_showwarning( message, category, filename, lineno = None, line = None ):
sys.stderr.write( "Warning: %s\n" % message )
if __name__ == "__main__":
main()