<s> """<STR_LIT>""" <EOL> import os . path as op <EOL> import sys <EOL> import logging <EOL> import numpy as np <EOL> from jcvi . formats . sizes import Sizes <EOL> from jcvi . graphics . base import plt , savefig , Rectangle , mb_formatter , mb_float_formatter , adjust_spines , get_map <EOL> from jcvi . apps . base import OptionParser , glob <EOL> class XYtrack ( object ) : <EOL> def __init__ ( self , ax , datafile , color = None , ymax = <NUM_LIT> ) : <EOL> self . ax = ax <EOL> self . xy = [ ] <EOL> fp = open ( datafile ) <EOL> for row in fp : <EOL> atoms = row . split ( ) <EOL> self . xy . append ( [ int ( atoms [ <NUM_LIT:0> ] ) , float ( atoms [ <NUM_LIT:1> ] ) ] ) <EOL> fp . close ( ) <EOL> self . x , self . y = zip ( * self . xy ) <EOL> logging . debug ( "<STR_LIT>" . format ( datafile , len ( self . x ) ) ) <EOL> self . color = color or "<STR_LIT:k>" <EOL> self . ymax = ymax <EOL> @ property <EOL> def mapping ( self ) : <EOL> return dict ( zip ( self . x , self . y ) ) <EOL> def interpolate ( self , maxsize , unit = <NUM_LIT> ) : <EOL> maxsize = int ( maxsize ) <EOL> for pos in range ( unit , maxsize + unit , unit ) : <EOL> if pos in self . x : <EOL> continue <EOL> self . xy . append ( [ pos , <NUM_LIT:0> ] ) <EOL> self . xy . sort ( ) <EOL> self . x , self . y = zip ( * self . xy ) <EOL> logging . debug ( "<STR_LIT>" . format ( len ( self . x ) ) ) <EOL> def cap ( self , ymax ) : <EOL> self . xy = [ [ a , <NUM_LIT:0> ] if b > ymax else [ a , b ] for a , b in self . xy ] <EOL> self . x , self . y = zip ( * self . xy ) <EOL> def draw ( self ) : <EOL> ax = self . ax <EOL> color = self . color <EOL> ax . plot ( self . x , self . y , lw = <NUM_LIT:0> ) <EOL> ax . fill_between ( self . x , self . y , color = color , lw = <NUM_LIT:0> ) <EOL> ax . set_ylim ( <NUM_LIT:0> , self . ymax ) <EOL> ax . set_axis_off ( ) <EOL> def import_hlfile ( self , hlfile , chr , unit = <NUM_LIT> , diverge = ( '<STR_LIT:r>' , '<STR_LIT:g>' ) ) : <EOL> rr , gg = diverge <EOL> fp = open ( hlfile ) <EOL> imported = <NUM_LIT:0> <EOL> mapping = self . mapping <EOL> for row in fp : <EOL> if row . strip ( ) == "<STR_LIT>" : <EOL> continue <EOL> seqid , start , end , tag = row . split ( ) <EOL> if seqid != chr : <EOL> continue <EOL> start = int ( start ) <EOL> end = int ( end ) <EOL> if tag == "<STR_LIT>" : <EOL> self . highlight ( mapping , start , end , color = rr , unit = unit ) <EOL> else : <EOL> self . highlight ( mapping , start , end , color = gg , unit = unit ) <EOL> imported += <NUM_LIT:1> <EOL> logging . debug ( "<STR_LIT>" . format ( imported , hlfile ) ) <EOL> def highlight ( self , mapping , start , end , color = "<STR_LIT:r>" , unit = <NUM_LIT> , zorder = <NUM_LIT:10> ) : <EOL> ax = self . ax <EOL> x = range ( start , end + unit , unit ) <EOL> y = [ mapping [ z ] for z in x ] <EOL> for a in self . xy : <EOL> if start <= a [ <NUM_LIT:0> ] <= end : <EOL> a [ <NUM_LIT:1> ] = <NUM_LIT:0> <EOL> self . x , self . y = zip ( * self . xy ) <EOL> ax . plot ( x , y , lw = <NUM_LIT:0> ) <EOL> ax . fill_between ( x , y , color = color , lw = <NUM_LIT:0> , zorder = zorder ) <EOL> def vlines ( self , xs , color = '<STR_LIT:m>' ) : <EOL> for x in xs : <EOL> self . ax . plot ( ( x , x ) , ( <NUM_LIT:0> , self . ymax ) , "<STR_LIT:->" , color = color , lw = <NUM_LIT:2> ) <EOL> class Coverage ( object ) : <EOL> def __init__ ( self , fig , root , canvas , chr , xlim , datadir , <EOL> order = None , hlsuffix = None , palette = None , cap = <NUM_LIT:50> , <EOL> gauge = "<STR_LIT>" , plot_label = True , plot_chr_label = True , <EOL> gauge_step = <NUM_LIT> , vlines = None , labels_dict = { } , <EOL> diverge = ( '<STR_LIT:r>' , '<STR_LIT:g>' ) ) : <EOL> x , y , w , h = canvas <EOL> p = <NUM_LIT> <EOL> root . add_patch ( Rectangle ( ( x - p , y - p ) , w + <NUM_LIT:2> * p , h + <NUM_LIT:2> * p , lw = <NUM_LIT:1> , <EOL> fill = False , ec = "<STR_LIT>" , zorder = <NUM_LIT:10> ) ) <EOL> datafiles = glob ( op . join ( datadir , chr + "<STR_LIT:*>" ) ) <EOL> if order : <EOL> datafiles = [ z for z in datafiles if z . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> ] in order ] <EOL> datafiles . sort ( key = lambda x : order . index ( x . split ( "<STR_LIT:.>" ) [ <NUM_LIT:1> ] ) ) <EOL> ntracks = len ( datafiles ) <EOL> yinterval = h / ntracks <EOL> yy = y + h <EOL> if palette is None : <EOL> set2 = get_map ( '<STR_LIT>' , '<STR_LIT>' , ntracks ) . mpl_colors <EOL> else : <EOL> set2 = [ palette ] * ntracks <EOL> if gauge == "<STR_LIT>" : <EOL> gauge_ax = fig . add_axes ( [ x , yy + p , w , <NUM_LIT> ] ) <EOL> adjust_spines ( gauge_ax , [ "<STR_LIT>" ] ) <EOL> tpos = yy + <NUM_LIT> <EOL> elif gauge == "<STR_LIT>" : <EOL> gauge_ax = fig . add_axes ( [ x , y - p , w , <NUM_LIT> ] ) <EOL> adjust_spines ( gauge_ax , [ "<STR_LIT>" ] ) <EOL> tpos = y - <NUM_LIT> <EOL> start , end = xlim <EOL> if gauge : <EOL> fs = gauge_step < <NUM_LIT> <EOL> setup_gauge_ax ( gauge_ax , start , end , gauge_step , float_formatter = fs ) <EOL> if plot_chr_label : <EOL> root . text ( x + w / <NUM_LIT:2> , tpos , chr , ha = "<STR_LIT>" , va = "<STR_LIT>" , <EOL> color = "<STR_LIT>" , size = <NUM_LIT:16> ) <EOL> yys = [ ] <EOL> for label , datafile , c in zip ( order , datafiles , set2 ) : <EOL> yy -= yinterval <EOL> yys . append ( yy ) <EOL> ax = fig . add_axes ( [ x , yy , w , yinterval * <NUM_LIT> ] ) <EOL> xy = XYtrack ( ax , datafile , color = c ) <EOL> xy . interpolate ( end ) <EOL> xy . cap ( ymax = cap ) <EOL> if vlines : <EOL> xy . vlines ( vlines ) <EOL> if hlsuffix : <EOL> hlfile = op . join ( datadir , "<STR_LIT:.>" . join ( ( label , hlsuffix ) ) ) <EOL> xy . import_hlfile ( hlfile , chr , diverge = diverge ) <EOL> if plot_label : <EOL> label = labels_dict . get ( label , label . capitalize ( ) ) <EOL> label = r"<STR_LIT>" . format ( label ) <EOL> root . text ( x - <NUM_LIT> , yy + yinterval / <NUM_LIT:2> , label , <EOL> ha = "<STR_LIT:right>" , va = "<STR_LIT>" ) <EOL> xy . draw ( ) <EOL> ax . set_xlim ( * xlim ) <EOL> self . yys = yys <EOL> def setup_gauge_ax ( gauge_ax , start , end , gauge_step , float_formatter = False ) : <EOL> gauge_ax . set_xlim ( start , end ) <EOL> formatter = mb_float_formatter if float_formatter else mb_formatter <EOL> gauge_ax . xaxis . set_major_formatter ( formatter ) <EOL> gauge_ax . xaxis . set_ticks ( np . arange ( start + gauge_step , end , gauge_step ) ) <EOL> gauge_ax . yaxis . set_ticks ( [ ] ) <EOL> def main ( ) : <EOL> p = OptionParser ( __doc__ ) <EOL> p . add_option ( "<STR_LIT>" , <EOL> help = "<STR_LIT>" ) <EOL> opts , args , iopts = p . set_image_options ( ) <EOL> if len ( args ) != <NUM_LIT:3> : <EOL> sys . exit ( not p . print_help ( ) ) <EOL> chr , sizes , datadir = args <EOL> order = opts . order <EOL> hlsuffix = opts . hlsuffix <EOL> if order : <EOL> order = order . split ( "<STR_LIT:U+002C>" ) <EOL> sizes = Sizes ( sizes ) <EOL> fig = plt . figure ( <NUM_LIT:1> , ( iopts . w , iopts . h ) ) <EOL> root = fig . add_axes ( [ <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:1> , <NUM_LIT:1> ] ) <EOL> canvas = ( <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ) <EOL> chr_size = sizes . get_size ( chr ) <EOL> Coverage ( fig , root , canvas , chr , ( <NUM_LIT:0> , chr_size ) , datadir , <EOL> order = order , hlsuffix = hlsuffix ) <EOL> root . set_xlim ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> root . set_ylim ( <NUM_LIT:0> , <NUM_LIT:1> ) <EOL> root . set_axis_off ( ) <EOL> image_name = chr + "<STR_LIT:.>" + iopts . format <EOL> savefig ( image_name , dpi = iopts . dpi , iopts = iopts ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
