<s> """<STR_LIT>""" <EOL> import pybedtools <EOL> from pybedtools import featurefuncs <EOL> from gffutils import helpers <EOL> import six <EOL> def to_bedtool ( iterator ) : <EOL> """<STR_LIT>""" <EOL> def gen ( ) : <EOL> for i in iterator : <EOL> yield helpers . asinterval ( i ) <EOL> return pybedtools . BedTool ( gen ( ) ) <EOL> def tsses ( db , merge_overlapping = False , attrs = None , attrs_sep = "<STR_LIT::>" , <EOL> merge_kwargs = dict ( o = '<STR_LIT>' , s = True , c = <NUM_LIT:4> ) , as_bed6 = False ) : <EOL> """<STR_LIT>""" <EOL> _merge_kwargs = dict ( o = '<STR_LIT>' , s = True , c = <NUM_LIT:4> ) <EOL> if merge_kwargs is not None : <EOL> _merge_kwargs . update ( merge_kwargs ) <EOL> def gen ( ) : <EOL> """<STR_LIT>""" <EOL> for gene in db . features_of_type ( '<STR_LIT>' ) : <EOL> for transcript in db . children ( gene , level = <NUM_LIT:1> ) : <EOL> if transcript . strand == '<STR_LIT:->' : <EOL> transcript . start = transcript . stop <EOL> else : <EOL> transcript . stop = transcript . start <EOL> transcript . featuretype = transcript . featuretype + '<STR_LIT>' <EOL> yield helpers . asinterval ( transcript ) <EOL> x = pybedtools . BedTool ( gen ( ) ) . sort ( ) <EOL> if attrs is None : <EOL> if db . dialect [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> attrs = '<STR_LIT>' <EOL> else : <EOL> attrs = '<STR_LIT>' <EOL> if merge_overlapping or as_bed6 : <EOL> if isinstance ( attrs , six . string_types ) : <EOL> attrs = [ attrs ] <EOL> def to_bed ( f ) : <EOL> """<STR_LIT>""" <EOL> name = attrs_sep . join ( [ f . attrs [ i ] for i in attrs ] ) <EOL> return pybedtools . Interval ( <EOL> f . chrom , <EOL> f . start , <EOL> f . stop , <EOL> name , <EOL> str ( f . score ) , <EOL> f . strand ) <EOL> x = x . each ( to_bed ) . saveas ( ) <EOL> if merge_overlapping : <EOL> def fix_merge ( f ) : <EOL> f = featurefuncs . extend_fields ( f , <NUM_LIT:6> ) <EOL> return pybedtools . Interval ( <EOL> f . chrom , <EOL> f . start , <EOL> f . stop , <EOL> f [ <NUM_LIT:4> ] , <EOL> '<STR_LIT:.>' , <EOL> f [ <NUM_LIT:3> ] ) <EOL> x = x . merge ( ** _merge_kwargs ) . each ( fix_merge ) . saveas ( ) <EOL> return x </s>
