示例#1
0
def drawData( axes, stats, options ):
    #halfsize = len(stats)/2 + len(stats)%2
    #colors = libplot.getColors2( halfsize )
    #colors = libplot.getColors2( len(stats) )
    #styles = { 0:'-', 1:'--' }

    colors = libplot.getColors1()
    if len(stats) < 1:
        return
    if stats[0].reference == "reference":
        colors.pop(0)
    elif stats[0].reference == 'hg19':
        colors.pop(1)

    #===========

    #dash = 0
    colorindex = -1
    lines = []
    sampleNames = []
    ymin = float('inf')
    ref = ''

    for sample in stats:
        sampleNames.append(sample.name)
        if ref == '':
            ref = sample.reference
        xdata = []
        ydata = []
        for bucket in sample:
            xdata.append( bucket.mid )
            if options.includeCov:
                ydata.append( bucket.correctPerSample )
            else:
                ydata.append( bucket.correctPerAligned )
        
        #if not dash:
        #    colorindex += 1
        #if colorindex == 1:
        #    colorindex += 1
        colorindex +=1
        ymin = min([ymin, min(ydata)])

        l = axes.plot( xdata, ydata, color=colors[colorindex], linewidth=1 )
        #l = axes.plot( xdata, ydata, color=colors[colorindex], linestyle=styles[dash], linewidth=0.5 )
        lines.append(l)
        
        #dash = not dash
    
    libplot.editSpine( axes )
    title = options.title
    if ref != '':
        title += ', %s' % libplot.properName(ref)
    axes.set_title(title)
    pyplot.xlabel("Distance")
    pyplot.ylabel("Correct proportion")
    return lines, sampleNames, ymin
示例#2
0
def drawData( axesList, samples, samplesPerPlot, options, proportion, culm ):
    largeIns = [] #List of proportion of total indel bases that indels >= 1000bp take up, each element is for each sample
    largeDels = []

    if len(axesList) %2 != 0:
        sys.stderr.write( 'Number of axes must be even. Got %d\n' %len(axesList) )
        sys.exit( 1 )

    colors = libplot.getColors1()
    if len(samples) < 1:
        return
    if samples[0].attrib["referenceName"] == "reference":
        colors.pop(0)
    elif samples[0].attrib["referenceName"] == 'hg19':
        colors.pop(1)

    #styles = []

    c = -1
    textsize = 'x-small'
    linesDict = {}
    labelsDict = {}
    xmax = float('-inf')
    ymax = float('-inf')
    xmin = float('inf')
    ymin = float('inf')
    for i in range( len(axesList)/2 ):
        inslines = []
        dellines = []
        sampleNames = []
        insAxes = axesList[ i ]
        delAxes = axesList[ i  + len(axesList)/2 ]
        
        startIndex = i * samplesPerPlot
        endIndex = min( [startIndex + samplesPerPlot, len(samples)] )
        for j in range( startIndex, endIndex ):
            sample = samples[j]
            sampleNames.append( sample.attrib[ 'sampleName' ] )
            insDist = [int(val) for val in sample.attrib[ 'insertionSizeDistribution' ].split()]
            #insXdata, insYdata = getFreq( insDist, options.xlogscale, options.ylogscale )
            insXdata, insYdata = getFreq( insDist, proportion, culm )
            delDist = [int(val) for val in sample.attrib[ 'deletionSizeDistribution' ].split()]
            #delXdata, delYdata = getFreq( delDist, options.xlogscale, options.ylogscale )
            delXdata, delYdata = getFreq( delDist, proportion, culm )

            #LARGE INDELS, FOR paper STATS, not related to the plot:
            if proportion and culm:
                largeIns.append( getLargeIndelProp(insXdata, insYdata) )
                largeDels.append( getLargeIndelProp(delXdata, delYdata) )

            c += 1
            il = insAxes.plot( insXdata, insYdata, color=colors[c] )
            dl = delAxes.plot( delXdata, delYdata, color=colors[c] )

            inslines.append( il )
            dellines.append( dl )
            
            insXmax = xmax
            delXmax = xmax
            if len(insXdata) >0:
                insXmax = max(insXdata)
            if len(delXdata) > 0:
                delXmax = max(delXdata)
            xmax = max( [xmax, insXmax, delXmax] )

            insYmax = ymax
            delYmax = ymax
            if len(insYdata) >0:
                insYmax = max(insYdata)
            if len(delYdata) > 0:
                delYmax = max(delYdata)
            ymax = max( [ymax, insYmax, delYmax] )

            insXmin = xmin
            delXmin = xmin
            if len(insXdata) >0:
                insXmin = min(insXdata)
            if len(delXdata) > 0:
                delXmin = min(delXdata)
            xmin = min( [xmin, insXmin, delXmin] )

            insYmin = ymin
            delYmin = ymin
            if len(insYdata) >0:
                insYmin = min(insYdata)
            if len(delYdata) > 0:
                delYmin = min(delYdata)
            ymin = min( [ymin, insYmin, delYmin] )

            #xmax = max([xmax, max(insXdata), max(delXdata)])
            #ymax = max([ymax, max(insYdata), max(delYdata)])
        
        linesDict[ i ] = inslines
        labelsDict[ i ] = sampleNames
        linesDict[ i + len(axesList)/2 ] = dellines
        labelsDict[ i + len(axesList)/2 ] = sampleNames
        #fontp = FontProperties()
        #fontp.set_size( 'x-small' )
        if i == 0:
            insAxes.set_title( 'Insertions' )    
            delAxes.set_title( 'Deletions' )
        
    for i in range( len(axesList) ):
        axes = axesList[ i ]
        if options.xlogscale == "true":
            axes.set_xscale('log')

        #if options.ylogscale == "true" and not proportion:
        if options.ylogscale == "true":
            axes.set_yscale('log')
        libplot.editSpine( axes )
        
        axes.set_xlabel('Length (bp)', size = textsize)
        if not proportion:
            axes.set_ylabel('Event number', size = textsize)
        else:
            axes.set_ylabel('Number of positions', size = textsize)
        axes.xaxis.grid(b=True, color="#CCCCCC", linestyle='-', linewidth=0.005)
        axes.yaxis.grid(b=True, color="#CCCCCC", linestyle='-', linewidth=0.005)
        #if options.xlogscale == "true":
        #    axes.set_xlabel('Log 2 of length (bp)', size = textsize)
        #else:
        #    axes.set_xlabel('Length (bp)', size = textsize)
        #if options.ylogscale == "true":
        #    axes.set_ylabel('Log 2 of count', size = textsize)
        #else:
        #    axes.set_ylabel('Count', size = textsize)

        #Legend
        legend = axes.legend( linesDict[ i ], [ libplot.properName(n) for n in labelsDict[ i ]], 'upper right', ncol=3 )
        for t in legend.get_texts():
            t.set_fontsize('x-small')
        legend._drawFrame = False

        if options.xlogscale == "true":
            scale = len(str(xmax)) -1
            xticks = [ 10**x for x in range(scale + 1) ]
            axes.set_xticks( xticks )
        #if options.ylogscale == "true" and not proportion:
        if options.ylogscale == "true":
            scale = len(str(ymax)) -1
            yticks = [ 10**y for y in range(scale + 1) ]
            axes.set_yticks( yticks )


        for label in axes.get_xticklabels():
            #label.set_rotation(75)
            label.set_fontsize( textsize )
        for label in axes.get_yticklabels():
            label.set_fontsize( textsize )
        #box = axes.get_position()
        #axes.set_position( [box.x0, box.y0, box.width*0.8, box.height] )
        #legend = pyplot.legend( lines, options.keys, numpoints=1, prop=fontP, loc="best", bbox_to_anchor=(1, 0.9) )
        #legend._drawFrame = False

        #libplot.setTicks( axes )
        #axes.set_xticks( range( 0, len(samples) ) )
        #axes.set_xticklabels( sampleNames )
        #for label in axes.xaxis.get_ticklabels():
        #    label.set_rotation( 90 )

        #axes.xaxis.set_ticks_position( 'bottom' )
        #axes.yaxis.set_ticks_position( 'left' )

        axes.set_ylim( ymin, ymax )
        if proportion and not culm:
            axes.set_xlim( xmin, 100 )
        else:
            axes.set_xlim( xmin, xmax )

    #PRINT THE LARGE INDEL STATS:
    if proportion and culm:
        sys.stderr.write("largeIndelStats\n")
        sys.stderr.write("Large insertions: %f\n" %( sum(largeIns)/len(largeIns) ))
        sys.stderr.write("Large deletions: %f\n" %( sum(largeDels)/len(largeDels) ))
        largeIndels = [ (largeIns[i] + largeDels[i])/2.0 for i in range(len(largeIns)) ]
        sys.stderr.write("IndelsAverage: %f\n" %( sum(largeIndels)/len(largeIndels) ))

    return
示例#3
0
def drawCompareData( axesList, xstats, ystats, options ):
    #Only draw the overlapped samples:
    #colors = libplot.getColors2( len(xstats) )
    colors = libplot.getColors1()
    #colorindex = -1
    #colorindex = 0
    colorindex = 1
    lines = []
    sampleNames = []
    p0axes = axesList[0] #plot 0 axes (see def 'setCompareAxes')
    aggData = [] #data points (buckets) of all samples
   
    minval = float('inf')

    for xsample in xstats:
        ysample = getSample( ystats, xsample.name )
        if ysample is None:
            continue
        xsample, ysample = intersect(xsample, ysample)
        #if len(xsample) != len(ysample): 
        #    xsample, ysample = intersect(xsample, ysample)
        #    sys.stderr.write( "Error: Two xml files do not have the same number of buckets for sample %s\n" % xsample.name )
            #sys.exit( 1 )
        
        data = [] #list of (x,y) tuples
        colorindex += 1
        for i in range( len( xsample ) ): #each bucket
            if xsample[i].mid != ysample[i].mid:
                sys.stderr.write( "Two xml files have different buckets\n " )
                sys.exit( 1 )
            if options.includeCov:
                data.append( (xsample[i].correctPerSample, ysample[i].correctPerSample) )
            else:
                data.append( (xsample[i].correctPerAligned, ysample[i].correctPerAligned) )

        x2data = [ point[0] for point in data ]
        y2data = [ point[1] for point in data ]
        l = p0axes.plot( x2data, y2data, color=colors[colorindex], marker='.', markersize=4.0, linestyle='none' )
        lines.append( l )
        sampleNames.append( xsample.name )
        aggData.extend( data )
        minval = min( [min(x2data), min(y2data)] )

    #Draw the y=x line
    x = [0, 1]
    y = [0, 1]
    p0axes.plot(x, y, color="#919191")

    fontP = FontProperties()
    fontP.set_size('small')
    
    libplot.editSpine( p0axes )
    p0axes.set_title(options.title)
    p0axes.set_xlabel( libplot.properName(xstats.refname) )
    p0axes.set_ylabel( libplot.properName(ystats.refname) )
    libplot.setTicks( p0axes )
    for l in p0axes.xaxis.get_ticklabels():
        l.set_fontsize('small')
    for l in p0axes.yaxis.get_ticklabels():
        l.set_fontsize('small')

    #legend:
    legend = p0axes.legend( lines, [ libplot.properName(n) for n in sampleNames], 'lower right', numpoints = 1, prop=fontP, ncol = 2)
    legend._drawFrame = False
    
    #p0axes.set_xlim( -0.005, 1.005 )
    #p0axes.set_ylim( -0.005, 1.005 )
    ycutoff = minval
    if options.ycutoff:
        ycutoff = options.ycutoff
    p0axes.set_xlim( ycutoff - (1-ycutoff)*0.02, 1 + (1 - ycutoff)*0.01 )
    p0axes.set_ylim( ycutoff - (1-ycutoff)*0.02, 1 + (1 - ycutoff)*0.01 )
   
    #box = p0axes.get_position()
    #p0axes.set_position([box.x0, box.y0, box.width * 0.8, box.height * 0.8])
    #legend = pyplot.legend( lines, sampleNames, numpoints = 1, prop= fontP, loc="best", bbox_to_anchor=(1, 0.6))
    #legend._drawFrame=False
    
    #DRAW AGGREGATE DATA (plot 1 and plot 2):
    nbins = 20
    p1axes = axesList[1]
    y1min, y1max = drawAggData( p1axes, aggData, 0, 0, 1, ycutoff, nbins )
    y1lim = max( abs(y1min), abs(y1max) )
    p1axes.set_ylim( -y1lim*1.1, y1lim*1.1 )
    p1axes.set_xlim( ycutoff - (1-ycutoff)*0.02, 1 + (1-ycutoff)*0.01 )
    #p1axes.set_ylim( y1min*1.1, y1max*1.1 )
    for loc, spine in p1axes.spines.iteritems():
        if loc == 'left':
            spine.set_position( ( 'outward', 10 ) )
        spine.set_color( 'none' )
    p1axes.axhline( 0, color = '#000000' )
    p1axes.xaxis.set_major_locator( NullLocator() )
    p1axes.xaxis.set_major_formatter( NullFormatter() )
    p1axes.yaxis.set_ticks([-y1lim, 0, y1lim])
    for l in p1axes.yaxis.get_ticklabels():
        l.set_fontsize('small')

    p2axes = axesList[2]
    x2min, x2max = drawAggData( p2axes, aggData, 1, 0, 1, ycutoff, nbins )
    x2lim = max( abs(x2min), abs(x2max) )
    p2axes.set_xlim( -x2lim*1.1, x2lim*1.1 )
    p2axes.set_ylim( ycutoff - (1-ycutoff)*0.02, 1 + (1- ycutoff)*0.01 )
    #p2axes.set_xlim( x2min*1.1, x2max*1.1 )
    for loc, spine in p2axes.spines.iteritems():
        if loc == 'bottom':
            spine.set_position( ( 'outward', 10 ) )
        spine.set_color( 'none' )
    p2axes.axvline( 0, color = '#000000' )
    p2axes.yaxis.set_major_locator( NullLocator() )
    p2axes.yaxis.set_major_formatter( NullFormatter() )
    p2axes.xaxis.set_ticks([-x2lim, 0, x2lim])
    for l in p2axes.xaxis.get_ticklabels():
        l.set_fontsize('small')
        l.set_rotation( 45 )
    return