<s> '''<STR_LIT>''' <EOL> import sys <EOL> import collections <EOL> try : <EOL> import pybedtools <EOL> except ImportError : <EOL> pass <EOL> import pysam <EOL> import CGAT . Experiment as E <EOL> import CGAT . IOTools as IOTools <EOL> import pyximport <EOL> pyximport . install ( build_in_temp = False ) <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if not argv : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( version = "<STR_LIT>" , <EOL> usage = globals ( ) [ "<STR_LIT>" ] ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( <EOL> "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> ( options , args ) = E . Start ( parser , argv = argv ) <EOL> outf = open ( options . outfile , "<STR_LIT:w>" ) <EOL> outf . write ( "<STR_LIT:\t>" . join ( [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] ) + "<STR_LIT:\n>" ) <EOL> pybedbamfile = pybedtools . BedTool ( options . bam_file ) <EOL> E . info ( "<STR_LIT>" ) <EOL> total_alignments = <NUM_LIT:0> <EOL> spliced_alignments = <NUM_LIT:0> <EOL> for alignment in pybedbamfile : <EOL> cigar = alignment [ <NUM_LIT:5> ] <EOL> if cigar . find ( "<STR_LIT:N>" ) != - <NUM_LIT:1> : <EOL> total_alignments += <NUM_LIT:1> <EOL> spliced_alignments += <NUM_LIT:1> <EOL> else : <EOL> total_alignments += <NUM_LIT:1> <EOL> gtffile = pybedtools . BedTool ( options . gtf_file ) . merge ( ) <EOL> E . info ( "<STR_LIT>" % <EOL> ( options . bam_file , options . gtf_file ) ) <EOL> cover = pybedbamfile . coverage ( gtffile ) <EOL> E . info ( "<STR_LIT>" ) <EOL> c = <NUM_LIT:0> <EOL> for entry in cover : <EOL> coverage = int ( entry [ <NUM_LIT:3> ] ) <EOL> if coverage > <NUM_LIT:0> : <EOL> c += coverage <EOL> coverage_in_transcripts = c <EOL> allreads = pysam . Samfile ( options . bam_file ) <EOL> spliced_bamname = IOTools . snip ( options . bam_file , "<STR_LIT>" ) + "<STR_LIT>" <EOL> splicedreads = pysam . Samfile ( spliced_bamname , "<STR_LIT:wb>" , template = allreads ) <EOL> spliced = collections . defaultdict ( list ) <EOL> for read in allreads : <EOL> for cigar_tag in read . cigar : <EOL> if cigar_tag [ <NUM_LIT:0> ] == <NUM_LIT:3> : <EOL> spliced [ read ] . append ( cigar_tag ) <EOL> for read in spliced . keys ( ) : <EOL> splicedreads . write ( read ) <EOL> splicedreads . close ( ) <EOL> allreads . close ( ) <EOL> pysam . sort ( spliced_bamname , P . snip ( spliced_bamname , "<STR_LIT>" ) ) <EOL> pysam . index ( spliced_bamname ) <EOL> splicedbam = pybedtools . BedTool ( spliced_bamname ) <EOL> spliced_coverage = splicedbam . coverage ( gtffile ) <EOL> E . info ( "<STR_LIT>" ) <EOL> spliced_exons = { } <EOL> c = <NUM_LIT:0> <EOL> for entry in spliced_coverage : <EOL> coverage = int ( entry [ <NUM_LIT:3> ] ) <EOL> if coverage > <NUM_LIT:0> : <EOL> c += coverage <EOL> spliced_coverage_in_transcripts = c <EOL> spliced_coverage_in_transcripts = float ( <EOL> spliced_coverage_in_transcripts ) / <NUM_LIT:2> <EOL> outf . write ( str ( int ( total_alignments ) ) + "<STR_LIT:\t>" ) <EOL> coverage_in_transcripts = ( <EOL> coverage_in_transcripts ) - ( spliced_coverage_in_transcripts ) <EOL> outf . write ( <EOL> str ( int ( coverage_in_transcripts ) - int ( spliced_coverage_in_transcripts ) ) + "<STR_LIT:\t>" ) <EOL> outf . write ( <EOL> str ( int ( ( coverage_in_transcripts / total_alignments ) * <NUM_LIT:100> ) ) + "<STR_LIT:\t>" ) <EOL> outf . write ( str ( int ( spliced_alignments ) ) + "<STR_LIT:\t>" ) <EOL> outf . write ( str ( int ( spliced_coverage_in_transcripts ) ) + "<STR_LIT:\t>" ) <EOL> outf . write ( <EOL> str ( int ( ( spliced_coverage_in_transcripts / spliced_alignments ) * <NUM_LIT:100> ) ) ) <EOL> outf . close ( ) <EOL> if options . reference_gtf : <EOL> context_summary = open ( <EOL> IOTools . snip ( options . bam_file , "<STR_LIT>" ) + "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> context_summary . write ( "<STR_LIT:\t>" . join ( [ "<STR_LIT>" , "<STR_LIT>" ] ) + "<STR_LIT:\n>" ) <EOL> context_file = open ( <EOL> IOTools . snip ( options . bam_file , "<STR_LIT>" ) + "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> context_dict = collections . defaultdict ( int ) <EOL> context = pybedbamfile . intersect ( gtffile , v = True , bed = True ) . intersect ( <EOL> pybedtools . BedTool ( options . reference_gtf ) , wb = True ) <EOL> for entry in context : <EOL> feature = entry [ <NUM_LIT:8> ] <EOL> context_dict [ feature ] += <NUM_LIT:1> <EOL> context_file . write ( "<STR_LIT:\t>" . join ( [ e for e in entry ] ) + "<STR_LIT:\n>" ) <EOL> for feature , value in context_dict . iteritems ( ) : <EOL> context_summary . write ( "<STR_LIT:\t>" . join ( [ feature , str ( value ) ] ) + "<STR_LIT:\n>" ) <EOL> context_file . close ( ) <EOL> context_summary . close ( ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
