<s> '''<STR_LIT>''' <EOL> import sys <EOL> import os <EOL> import ConfigParser <EOL> import matplotlib <EOL> import pylab <EOL> import matplotlib . ticker <EOL> import numpy <EOL> import CGAT . Experiment as E <EOL> import CGAT . IOTools as IOTools <EOL> def formatGenomicCoordinate ( value , pos = None ) : <EOL> decimals = <NUM_LIT:0> <EOL> exps = [ '<STR_LIT>' , '<STR_LIT:k>' , '<STR_LIT:m>' , '<STR_LIT:g>' , '<STR_LIT:t>' ] <EOL> suffix = "<STR_LIT>" <EOL> value = int ( value ) <EOL> format = "<STR_LIT>" % ( int ( decimals ) , suffix ) <EOL> for exp in xrange ( len ( exps ) - <NUM_LIT:1> , - <NUM_LIT:1> , - <NUM_LIT:1> ) : <EOL> if value < <NUM_LIT:1000> L ** ( exp ) : <EOL> continue <EOL> else : <EOL> return format % ( float ( value ) / <NUM_LIT:1000> L ** ( exp ) , exps [ exp ] ) <EOL> break <EOL> else : <EOL> return str ( value ) <EOL> def enterParams ( o , params ) : <EOL> """<STR_LIT>""" <EOL> r = { } <EOL> for p in params : <EOL> if isinstance ( p , tuple ) : <EOL> p , f = p <EOL> else : <EOL> f = str <EOL> if hasattr ( o , p ) : <EOL> r [ p ] = f ( getattr ( o , p ) ) <EOL> return r <EOL> def normalizeValuesByWindows ( data , window_size = None ) : <EOL> """<STR_LIT>""" <EOL> if not options . window_size : <EOL> window_size = min ( map ( lambda x : x [ <NUM_LIT:1> ] - x [ <NUM_LIT:0> ] , data ) ) <EOL> new_values = [ ] <EOL> for start , end , value in data : <EOL> if end - start < window_size : <EOL> continue <EOL> start = start - start % window_size <EOL> for z in range ( start , end , window_size ) : <EOL> new_values . append ( ( z , value ) ) <EOL> new_values . sort ( ) <EOL> xvals = [ ] <EOL> yvals = [ ] <EOL> last_x = None <EOL> values = [ ] <EOL> for x , value in new_values : <EOL> if last_x != x : <EOL> if last_x is not None : <EOL> xvals . append ( last_x ) <EOL> yvals . append ( numpy . mean ( values ) ) <EOL> values = [ ] <EOL> last_x = x <EOL> values . append ( value ) <EOL> return xvals , yvals <EOL> def addPlot ( ax , track , contig , nplotted , <EOL> nsubplotted = None , <EOL> nsubplots = None , <EOL> y_min = None , <EOL> y_max = None ) : <EOL> """<STR_LIT>""" <EOL> if contig not in track . mData : <EOL> return <EOL> xvals = map ( lambda x : ( x [ <NUM_LIT:1> ] + x [ <NUM_LIT:0> ] ) / <NUM_LIT> , track . mData [ contig ] ) <EOL> yvals = map ( lambda x : x [ <NUM_LIT:2> ] , track . mData [ contig ] ) <EOL> l = len ( xvals ) <EOL> if nsubplots : <EOL> plotnum = nsubplotted <EOL> else : <EOL> plotnum = nplotted <EOL> if track . style == "<STR_LIT>" : <EOL> matrix = pylab . array ( yvals ) <EOL> matrix . shape = ( <NUM_LIT:1> , len ( yvals ) ) <EOL> if nsubplots is not None : <EOL> y_width = float ( y_max - y_min ) / nsubplots <EOL> extent = ( min ( xvals ) , max ( xvals ) , y_min + y_width * <EOL> nsubplotted , y_min + y_width * ( nsubplotted + <NUM_LIT:1> ) ) <EOL> else : <EOL> extent = ( min ( xvals ) , max ( xvals ) , min ( yvals ) , max ( yvals ) ) <EOL> ax . imshow ( matrix , <EOL> cmap = track . color_scheme , <EOL> extent = extent , <EOL> interpolation = "<STR_LIT>" ) <EOL> symbol = options . symbols [ plotnum % len ( options . symbols ) ] <EOL> plot = ax . plot ( xvals , yvals , symbol , lw = <NUM_LIT:2> ) <EOL> else : <EOL> symbol = options . symbols [ plotnum % len ( options . symbols ) ] <EOL> plot = ax . plot ( xvals , yvals , symbol ) <EOL> return plot <EOL> def plotContig ( contig , tracks , options , plot_legend = False , <EOL> extra_features = None ) : <EOL> """<STR_LIT>""" <EOL> if extra_features and "<STR_LIT>" in extra_features : <EOL> figure = pylab . figure ( ** enterParams ( extra_features [ '<STR_LIT>' ] , <EOL> ( ( "<STR_LIT>" , lambda x : map ( int , x . split ( "<STR_LIT:U+002C>" ) ) ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) ) <EOL> else : <EOL> figure = pylab . figure ( ) <EOL> if plot_legend : <EOL> if extra_features and "<STR_LIT>" in extra_features : <EOL> legend = pylab . figure ( ** enterParams ( extra_features [ '<STR_LIT>' ] , <EOL> ( ( "<STR_LIT>" , lambda x : map ( int , x . split ( "<STR_LIT:U+002C>" ) ) ) , <EOL> ( "<STR_LIT>" , int ) , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" ) ) ) <EOL> else : <EOL> legend = pylab . figure ( ) <EOL> lx = legend . add_axes ( ( <NUM_LIT:0.1> , <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT> ) ) <EOL> lx . set_title ( "<STR_LIT>" ) <EOL> lx . set_axis_off ( ) <EOL> else : <EOL> legend = None <EOL> axes = [ ] <EOL> ywidth = <NUM_LIT> / float ( len ( tracks ) ) <EOL> yoffset = <NUM_LIT:0.1> <EOL> axprops = { } <EOL> ayprops = { } <EOL> min_x , max_x = <NUM_LIT> , <NUM_LIT:0> <EOL> for track in tracks : <EOL> if track . mData : <EOL> min_x = min ( min_x , min ( map ( lambda x : ( x [ <NUM_LIT:0> ] ) , track . mData [ contig ] ) ) ) <EOL> max_x = max ( max_x , max ( map ( lambda x : ( x [ <NUM_LIT:1> ] ) , track . mData [ contig ] ) ) ) <EOL> axprops [ '<STR_LIT>' ] = ( min_x , max_x ) <EOL> nplotted = <NUM_LIT:0> <EOL> for track in tracks : <EOL> labels , plots = [ ] , [ ] <EOL> ax = figure . add_axes ( <EOL> ( <NUM_LIT:0.1> , track . mYOffset , <NUM_LIT> , track . mYWidth ) , ** axprops ) <EOL> if '<STR_LIT>' not in axprops : <EOL> ax . xaxis . set_major_formatter ( <EOL> matplotlib . ticker . FuncFormatter ( formatGenomicCoordinate ) ) <EOL> ax . set_xlabel ( "<STR_LIT>" ) <EOL> axprops [ '<STR_LIT>' ] = ax <EOL> else : <EOL> pylab . setp ( ax . get_xticklabels ( ) , visible = False ) <EOL> ax . set_ylabel ( track . mTitle , ** ayprops ) <EOL> if track . mSubTracks : <EOL> first = True <EOL> for tt in track . mSubTracks : <EOL> if first : <EOL> min_y = min ( map ( lambda x : x [ <NUM_LIT:2> ] , tt . mData [ contig ] ) ) <EOL> max_y = max ( map ( lambda x : x [ <NUM_LIT:2> ] , tt . mData [ contig ] ) ) <EOL> first = False <EOL> else : <EOL> min_y = min ( <EOL> min_y , min ( map ( lambda x : x [ <NUM_LIT:2> ] , tt . mData [ contig ] ) ) ) <EOL> max_y = max ( <EOL> max_y , max ( map ( lambda x : x [ <NUM_LIT:2> ] , tt . mData [ contig ] ) ) ) <EOL> nsubplotted = <NUM_LIT:0> <EOL> for tt in track . mSubTracks : <EOL> plot = addPlot ( ax , tt , contig , nplotted , <EOL> nsubplotted , len ( track . mSubTracks ) , <EOL> min_y , max_y ) <EOL> nsubplotted += <NUM_LIT:1> <EOL> plots . append ( plot ) <EOL> if hasattr ( tt , "<STR_LIT>" ) : <EOL> labels . append ( tt . legend ) <EOL> else : <EOL> labels . append ( tt . mTitle ) <EOL> else : <EOL> min_y = min ( map ( lambda x : x [ <NUM_LIT:2> ] , track . mData [ contig ] ) ) <EOL> max_y = max ( map ( lambda x : x [ <NUM_LIT:2> ] , track . mData [ contig ] ) ) <EOL> if options . global_colours : <EOL> n_for_colour = nplotted <EOL> else : <EOL> n_for_colour = <NUM_LIT:0> <EOL> plot = addPlot ( ax , track , contig , n_for_colour ) <EOL> plots . append ( plot ) <EOL> if hasattr ( track , "<STR_LIT>" ) : <EOL> lables . append ( track . legend ) <EOL> else : <EOL> labels . append ( track . mTitle ) <EOL> old_ticks = ax . get_yticks ( ) <EOL> step_size = ( old_ticks [ <NUM_LIT:1> ] - old_ticks [ <NUM_LIT:0> ] ) * <NUM_LIT:2> <EOL> new_ticks = list ( pylab . arange ( old_ticks [ <NUM_LIT:0> ] , old_ticks [ - <NUM_LIT:1> ] , step_size ) ) <EOL> ax . set_yticks ( new_ticks ) <EOL> if nplotted % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> ax . yaxis . set_ticks_position ( "<STR_LIT:right>" ) <EOL> ax . yaxis . set_label_position ( "<STR_LIT:right>" ) <EOL> else : <EOL> ax . yaxis . set_ticks_position ( "<STR_LIT:left>" ) <EOL> ax . yaxis . set_label_position ( "<STR_LIT:left>" ) <EOL> if extra_features : <EOL> for key , config in extra_features . items ( ) : <EOL> if key == "<STR_LIT>" : <EOL> if contig not in config . mData : <EOL> continue <EOL> lines = [ ] <EOL> for start , end , value in config . mData [ contig ] : <EOL> lines . append ( start ) <EOL> lines . append ( end ) <EOL> ax . vlines ( <EOL> lines , min_y , max_y , ** enterParams ( config , ( "<STR_LIT>" , "<STR_LIT>" ) ) ) <EOL> nplotted += <NUM_LIT:1> <EOL> if legend : <EOL> lx = legend . add_axes ( ( <NUM_LIT:0.1> , track . mYOffset , <NUM_LIT> , track . mYWidth ) ) <EOL> pylab . setp ( lx . get_xticklabels ( ) , visible = False ) <EOL> lx . set_xticks ( [ ] ) <EOL> lx . set_yticks ( [ ] ) <EOL> lx . text ( <NUM_LIT> , <NUM_LIT:0.5> , track . mTitle ) <EOL> lx . legend ( plots , labels , '<STR_LIT>' ) <EOL> if hasattr ( track , "<STR_LIT:text>" ) : <EOL> lx . text ( <NUM_LIT> , <NUM_LIT> , track . text , size = "<STR_LIT>" , <EOL> clip_on = True ) <EOL> ax . set_title ( contig ) <EOL> ax . set_xlim ( min_x , max_x ) <EOL> return figure , legend <EOL> def readData ( infile ) : <EOL> """<STR_LIT>""" <EOL> dd = { } <EOL> for line in infile : <EOL> if line [ <NUM_LIT:0> ] == "<STR_LIT:#>" : <EOL> continue <EOL> d = line [ : - <NUM_LIT:1> ] . split ( "<STR_LIT:\t>" ) <EOL> contig , start , end , score = d [ <NUM_LIT:0> ] , int ( d [ <NUM_LIT:3> ] ) , int ( d [ <NUM_LIT:4> ] ) , float ( d [ <NUM_LIT:5> ] ) <EOL> if contig not in dd : <EOL> dd [ contig ] = [ ] <EOL> dd [ contig ] . append ( ( start , end , score ) ) <EOL> return dd <EOL> class Track : <EOL> def __init__ ( self , title , <EOL> priority = <NUM_LIT:0> , <EOL> data = None , <EOL> subtracks = None , <EOL> config = None ) : <EOL> self . mTitle = title <EOL> self . mData = data <EOL> self . mSubTracks = subtracks <EOL> self . height = <NUM_LIT:1.0> <EOL> self . priority = priority <EOL> self . style = "<STR_LIT:default>" <EOL> self . color_scheme = None <EOL> if config : <EOL> for key , value in config . items ( section ) : <EOL> setattr ( self , key , value ) <EOL> def layoutTracks ( tracks ) : <EOL> """<STR_LIT>""" <EOL> for track in tracks . values ( ) : <EOL> if track . mSubTracks : <EOL> s = [ ] <EOL> for subtrack in track . mSubTracks : <EOL> s . append ( tracks [ subtrack ] ) <EOL> del tracks [ subtrack ] <EOL> track . mSubTracks = s <EOL> total_width = sum ( map ( lambda x : x . height , tracks . values ( ) ) ) <EOL> tracks = list ( tracks . values ( ) ) <EOL> tracks . sort ( lambda x , y : cmp ( x . priority , y . priority ) ) <EOL> yoffset = <NUM_LIT:0.1> <EOL> ymax_width = <NUM_LIT> <EOL> for track in tracks : <EOL> track . mYOffset = yoffset <EOL> width = ymax_width * track . height / total_width <EOL> track . mYWidth = width <EOL> yoffset += width <EOL> return tracks <EOL> def main ( argv = None ) : <EOL> """<STR_LIT>""" <EOL> if argv is None : <EOL> argv = sys . argv <EOL> parser = E . OptionParser ( <EOL> version = "<STR_LIT>" , 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>" , dest = "<STR_LIT>" , type = "<STR_LIT>" , <EOL> choices = ( "<STR_LIT>" , "<STR_LIT:all>" , "<STR_LIT>" ) , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:int>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , type = "<STR_LIT:string>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . add_option ( "<STR_LIT>" , dest = "<STR_LIT>" , action = "<STR_LIT:store_true>" , <EOL> help = "<STR_LIT>" ) <EOL> parser . set_defaults ( <EOL> filenames = None , <EOL> symbols = "<STR_LIT>" , <EOL> output_pattern_image = "<STR_LIT>" , <EOL> slide_show = None , <EOL> window_size = None , <EOL> filename_config = None , <EOL> dpi = None , <EOL> global_colours = False , <EOL> ) <EOL> ( options , args ) = E . Start ( parser ) <EOL> options . symbols = options . symbols . split ( "<STR_LIT:U+002C>" ) <EOL> tracks = { } <EOL> extra_features = { } <EOL> if options . filenames : <EOL> options . filenames = options . filenames . split ( "<STR_LIT:U+002C>" ) <EOL> if len ( args ) > <NUM_LIT:0> : <EOL> options . filenames = args <EOL> if options . filenames : <EOL> for filename in options . filenames : <EOL> if filename == "<STR_LIT:->" : <EOL> infile = sys . stdin <EOL> else : <EOL> infile = IOTools . openFile ( filename ) <EOL> data = readData ( infile ) <EOL> if filename != "<STR_LIT:->" : <EOL> infile . close ( ) <EOL> track [ filename ] = Track ( title = filename , data = data ) <EOL> elif options . filename_config : <EOL> config = ConfigParser . ConfigParser ( ) <EOL> config . read ( os . path . expanduser ( options . filename_config ) ) <EOL> for section in config . sections ( ) : <EOL> if section == "<STR_LIT>" : <EOL> infile = IOTools . openFile ( config . get ( section , "<STR_LIT:filename>" ) , "<STR_LIT:r>" ) <EOL> data = readData ( infile ) <EOL> infile . close ( ) <EOL> extra_features [ section ] = Track ( title = section , <EOL> data = data , <EOL> config = config ) <EOL> config . remove_section ( section ) <EOL> elif section in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> extra_features [ section ] = Track ( title = section , <EOL> data = None , <EOL> config = config ) <EOL> config . remove_section ( section ) <EOL> n = <NUM_LIT:0> <EOL> for section in config . sections ( ) : <EOL> if config . has_option ( section , "<STR_LIT:filename>" ) : <EOL> infile = IOTools . openFile ( config . get ( section , "<STR_LIT:filename>" ) , "<STR_LIT:r>" ) <EOL> data = readData ( infile ) <EOL> infile . close ( ) <EOL> tracks [ section ] = Track ( title = section , <EOL> data = data , <EOL> priority = n , <EOL> config = config ) <EOL> elif config . has_option ( section , "<STR_LIT>" ) : <EOL> subtracks = config . get ( section , "<STR_LIT>" ) <EOL> subtracks = map ( lambda x : x . strip ( ) , subtracks . split ( "<STR_LIT:U+002C>" ) ) <EOL> tracks [ section ] = Track ( title = section , <EOL> data = None , <EOL> config = config , <EOL> priority = n , <EOL> subtracks = subtracks ) <EOL> n += <NUM_LIT:1> <EOL> contigs = set ( ) <EOL> for track in tracks . values ( ) : <EOL> if track . mData : <EOL> contigs = contigs . union ( track . mData . keys ( ) ) <EOL> tracks = layoutTracks ( tracks ) <EOL> nplots = <NUM_LIT:0> <EOL> figures = [ ] <EOL> legend = None <EOL> for contig in contigs : <EOL> figure , l = plotContig ( contig , tracks , options , <EOL> plot_legend = legend is None , <EOL> extra_features = extra_features ) <EOL> figures . append ( figure ) <EOL> if l : <EOL> legend = l <EOL> if options . slide_show : <EOL> if options . slide_show == "<STR_LIT>" : <EOL> pylab . show ( ) <EOL> elif options . slide_show == "<STR_LIT:all>" : <EOL> pylab . show ( ) <EOL> elif options . slide_show == "<STR_LIT>" : <EOL> pylab . show ( ) <EOL> else : <EOL> extra_args = { } <EOL> if options . dpi : <EOL> extra_args [ '<STR_LIT>' ] = options . dpi <EOL> for contig , figure in zip ( contigs , figures ) : <EOL> params = { '<STR_LIT>' : contig } <EOL> filename = options . output_pattern_image % params <EOL> E . info ( "<STR_LIT>" % filename ) <EOL> figure . savefig ( os . path . expanduser ( filename ) , ** extra_args ) <EOL> if legend : <EOL> params = { '<STR_LIT>' : "<STR_LIT>" } <EOL> filename = options . output_pattern_image % params <EOL> E . info ( "<STR_LIT>" % filename ) <EOL> legend . savefig ( os . path . expanduser ( filename ) , ** extra_args ) <EOL> E . info ( "<STR_LIT>" % <EOL> ( len ( tracks ) , nplots , len ( contigs ) ) ) <EOL> E . Stop ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
