<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import re <EOL> import logging <EOL> from optparse import OptionParser <EOL> import urllib2 <EOL> import tempfile <EOL> import gzip <EOL> import subprocess <EOL> from taolib . CoreLib . Parser import WiggleIO <EOL> from taolib . CoreLib . BasicStat . Func import * <EOL> logging . basicConfig ( level = <NUM_LIT:20> , <EOL> format = '<STR_LIT>' , <EOL> datefmt = '<STR_LIT>' , <EOL> stream = sys . stderr , <EOL> filemode = "<STR_LIT:w>" <EOL> ) <EOL> UCSC_chrom_URL = r'<STR_LIT>' <EOL> error = logging . critical <EOL> warn = logging . warning <EOL> debug = logging . debug <EOL> info = logging . info <EOL> def get_chrom_length ( dbname ) : <EOL> try : <EOL> fhd = open ( dbname , "<STR_LIT:r>" ) <EOL> chrom_len = { } <EOL> for l in fhd : <EOL> fs = l . split ( ) <EOL> try : <EOL> chrom_len [ fs [ <NUM_LIT:0> ] ] = int ( fs [ <NUM_LIT:1> ] ) <EOL> except : <EOL> pass <EOL> fhd . close ( ) <EOL> except : <EOL> f = urllib2 . urlopen ( UCSC_chrom_URL % ( dbname ) ) <EOL> tmpfname = tempfile . mkstemp ( prefix = "<STR_LIT>" ) [ <NUM_LIT:1> ] <EOL> tmpf = open ( tmpfname , '<STR_LIT:w>' ) <EOL> tmpf . write ( f . read ( ) ) <EOL> tmpf . close ( ) <EOL> f . close <EOL> fhd = gzip . open ( tmpfname , '<STR_LIT:r>' ) <EOL> chrom_len = { } <EOL> for l in fhd : <EOL> fs = l . split ( ) <EOL> try : <EOL> chrom_len [ fs [ <NUM_LIT:0> ] ] = int ( fs [ <NUM_LIT:1> ] ) <EOL> except : <EOL> pass <EOL> fhd . close ( ) <EOL> os . unlink ( tmpfname ) <EOL> return chrom_len <EOL> def main ( ) : <EOL> usage = "<STR_LIT>" <EOL> description = "<STR_LIT>" <EOL> optparser = OptionParser ( version = "<STR_LIT>" , description = description , usage = usage , add_help_option = False ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , action = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , type = "<STR_LIT:str>" , dest = "<STR_LIT>" , help = "<STR_LIT>" , default = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" , default = <NUM_LIT:100> ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" , default = <NUM_LIT:10> ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" , default = '<STR_LIT>' ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , default = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , action = "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:float>" , default = - <NUM_LIT> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:float>" , default = <NUM_LIT> , <EOL> help = "<STR_LIT>" ) <EOL> optparser . add_option ( "<STR_LIT>" , "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , default = False , <EOL> help = "<STR_LIT>" ) <EOL> ( options , wigfiles ) = optparser . parse_args ( ) <EOL> imgfmt = options . imgformat . upper ( ) <EOL> if imgfmt != '<STR_LIT>' and imgfmt != '<STR_LIT>' : <EOL> print "<STR_LIT>" % imgfmt <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> method = options . method . lower ( ) <EOL> if method == '<STR_LIT>' : <EOL> medfunc = median <EOL> elif method == '<STR_LIT>' : <EOL> medfunc = mean <EOL> elif method == '<STR_LIT>' : <EOL> medfunc = lambda u : u [ - <NUM_LIT:1> ] <EOL> else : <EOL> print "<STR_LIT>" % ( method ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> wigfilenum = len ( wigfiles ) <EOL> if wigfilenum < <NUM_LIT:2> or not options . rfile : <EOL> optparser . print_help ( ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if options . wiglabel and len ( options . wiglabel ) == wigfilenum : <EOL> wiglabel = options . wiglabel <EOL> else : <EOL> wiglabel = map ( lambda x : os . path . basename ( x ) , wigfiles ) <EOL> if options . step < <NUM_LIT:1> : <EOL> error ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> if options . imgsize < <NUM_LIT:10> : <EOL> error ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> for f in wigfiles : <EOL> if not os . path . isfile ( f ) : <EOL> error ( "<STR_LIT>" % f ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> wigfhds = map ( open , wigfiles ) <EOL> info ( "<STR_LIT>" % wigfilenum ) <EOL> info ( "<STR_LIT>" ) <EOL> try : <EOL> chrom_len = get_chrom_length ( options . dbname ) <EOL> except : <EOL> error ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> chromsdict = { } <EOL> for wigfhd in wigfhds : <EOL> tmpset = set ( [ ] ) <EOL> for l in wigfhd : <EOL> if l . find ( "<STR_LIT>" ) != - <NUM_LIT:1> : <EOL> c = re . search ( "<STR_LIT>" , l ) . group ( <NUM_LIT:1> ) <EOL> tmpset . add ( c ) <EOL> for c in tmpset : <EOL> chromsdict [ c ] = chromsdict . setdefault ( c , <NUM_LIT:0> ) + <NUM_LIT:1> <EOL> chroms = [ ] <EOL> for c in chromsdict . keys ( ) : <EOL> if chromsdict [ c ] == wigfilenum : <EOL> chroms . append ( c ) <EOL> info ( "<STR_LIT>" % "<STR_LIT:U+002C>" . join ( chroms ) ) <EOL> if options . rfile : <EOL> rfhd = open ( options . rfile , "<STR_LIT:w>" ) <EOL> rfhd . write ( '''<STR_LIT>''' ) <EOL> for i in range ( len ( wigfhds ) ) : <EOL> wigfhd = wigfhds [ i ] <EOL> wigfhd . seek ( <NUM_LIT:0> ) <EOL> info ( "<STR_LIT>" % ( i + <NUM_LIT:1> ) ) <EOL> wig = WiggleIO . WiggleIO ( wigfhd ) . build_binKeeper ( chromLenDict = chrom_len , bin = options . step ) <EOL> p = [ ] <EOL> ap = p . append <EOL> for chrom in chroms : <EOL> step_region = [ ] <EOL> step_region_a = step_region . append <EOL> try : <EOL> end_of_chrom = chrom_len [ chrom ] <EOL> except : <EOL> warn ( "<STR_LIT>" % chrom ) <EOL> continue <EOL> bkeeper = wig [ chrom ] <EOL> for ( parray , varray ) in bkeeper . cage : <EOL> if varray : <EOL> ap ( medfunc ( varray ) ) <EOL> else : <EOL> ap ( None ) <EOL> info ( "<STR_LIT>" ) <EOL> rfhd . write ( "<STR_LIT>" % i ) <EOL> if p [ <NUM_LIT:0> ] : <EOL> rfhd . write ( "<STR_LIT>" % p [ <NUM_LIT:0> ] ) <EOL> else : <EOL> rfhd . write ( "<STR_LIT>" ) <EOL> for v in p [ <NUM_LIT:1> : ] : <EOL> if v : <EOL> rfhd . write ( "<STR_LIT>" % v ) <EOL> else : <EOL> rfhd . write ( "<STR_LIT>" ) <EOL> rfhd . write ( "<STR_LIT>" ) <EOL> rfhd . write ( "<STR_LIT>" ) <EOL> for i in range ( wigfilenum - <NUM_LIT:1> ) : <EOL> rfhd . write ( "<STR_LIT>" % ( i + <NUM_LIT:1> ) ) <EOL> rfhd . write ( "<STR_LIT>" ) <EOL> rfhd . write ( "<STR_LIT>" % ( options . maxscore , options . minscore ) ) <EOL> for i in range ( wigfilenum - <NUM_LIT:1> ) : <EOL> rfhd . write ( "<STR_LIT>" % ( i + <NUM_LIT:2> , options . maxscore , i + <NUM_LIT:2> , options . minscore ) ) <EOL> rfhd . write ( "<STR_LIT>" ) <EOL> if imgfmt == '<STR_LIT>' : <EOL> rfhd . write ( "<STR_LIT>" % ( options . rfile , options . imgsize , options . imgsize ) ) <EOL> elif imgfmt == '<STR_LIT>' : <EOL> rfhd . write ( "<STR_LIT>" % ( options . rfile , options . imgsize , options . imgsize ) ) <EOL> if options . heatmap : <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '''<STR_LIT>''' ) <EOL> labels = "<STR_LIT:U+002C>" . join ( map ( lambda x : "<STR_LIT>" + x + "<STR_LIT>" , wiglabel ) ) <EOL> rfhd . write ( "<STR_LIT>" % labels ) <EOL> rfhd . write ( "<STR_LIT>" % labels ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> rfhd . write ( '<STR_LIT>' ) <EOL> else : <EOL> rfhd . write ( '''<STR_LIT>''' ) <EOL> labels = "<STR_LIT:U+002C>" . join ( map ( lambda x : "<STR_LIT>" + x + "<STR_LIT>" , wiglabel ) ) <EOL> rfhd . write ( '''<STR_LIT>''' % ( labels ) ) <EOL> rfhd . write ( "<STR_LIT>" ) <EOL> rfhd . close ( ) <EOL> try : <EOL> subprocess . call ( [ '<STR_LIT>' , options . rfile ] ) <EOL> except : <EOL> info ( "<STR_LIT>" % options . rfile ) <EOL> else : <EOL> info ( "<STR_LIT>" % ( options . rfile + '<STR_LIT>' ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> try : <EOL> main ( ) <EOL> except KeyboardInterrupt : <EOL> sys . stderr . write ( "<STR_LIT>" ) <EOL> sys . exit ( <NUM_LIT:0> ) </s>
