from CCP4ReportParser import *


"""
    edstats report generator
    Jon Agirre      2014

"""

class edstats_report(Report):
  # Specify which gui task and/or pluginscript this applies to
  TASKNAME = 'edstats'
  CSS_VERSION = '0.1.0'

  def __init__(self,xmlnode=None,jobInfo={},**kw):
    Report. __init__(self,xmlnode=xmlnode,jobInfo=jobInfo,cssVersion=self.CSS_VERSION,**kw)
  
    import CCP4Utils,os
    results = self.addResults()
    
    results.append ( "The RSZD scores are accuracy metrics, <i>i.e.</i> at least in theory they can be improved by adjusting the model (by eliminating the " +\
                     "obvious difference density). The RSZO scores are precision metrics and will be strongly correlated with the B<sub>iso</sub>s " +\
                     "(since that is also a precision metric), <i>i.e.</i> assuming you've fixed any issues with accuracy of that residue there's " +\
                     "nothing you can do about the precision, short of re-collecting the data. The advisable rejection limits are &#60; -3&#963; and &#62; 3&#963; for the residue " +\
                     "RSZD metrics respectively, and &#60; 1&#963; for the residue RSZO metrics." ) 

    graph = results.addFlotGraph(title="Correlation analysis", select="//Edstats", style="height:300px; width:600px; border:0px;" )
    graph.addData ( title="Residue", select="Residue/Number" )
    graph.addData ( title="ZCCm", select="Residue/ZCCm" )
    graph.addData ( title="ZOm", select="Residue/ZOm" )
    graph.addData ( title="ZD-m", select="Residue/ZDmm" )
    graph.addData ( title="ZD+m", select="Residue/ZDpm" )
    graph.addData ( title="ZCCs", select="Residue/ZCCs" )
    graph.addData ( title="ZOs", select="Residue/ZOs" )
    graph.addData ( title="ZD-s", select="Residue/ZDms" )
    graph.addData ( title="ZD+s", select="Residue/ZDps" )
    graph.addData ( title="ZCCa", select="Residue/ZCCa" )
    graph.addData ( title="ZOa", select="Residue/ZOa" )
    graph.addData ( title="ZD-a", select="Residue/ZDma" )
    graph.addData ( title="ZD+a", select="Residue/ZDpa" )



    p = graph.addPlotObject()
    p.append( 'title', 'Main chain analysis' )
    p.append( 'plottype', 'xy' )
    p.append( 'showlegend', 'true' )
    p.append( 'xintegral', 'true' )
    p.append( 'yintegral', 'false' )
#    p.append( 'xlabel', 'Residue' ) # Labels are not represented correctly (Jon Agirre, 17 Oct 2014)
#    p.append( 'ylabel', 'Z-scores' )
    
    l = p.append( 'plotline', xcol = 1, ycol = 3 )
    l.append( 'colour', 'black' )
    l.append( 'linestyle', '.' )
    l.append( 'symbol', '.' )
    l.append( 'symbolsize', '0.1' )

    
    l = p.append( 'plotline', xcol = 1, ycol = 4 )
    l.append( 'colour', 'red' )
    l.append( 'linestyle', '.' )
    l.append( 'symbolsize', '2' )
    l.append( 'symbol', 's' )
    
    l = p.append( 'plotline', xcol = 1, ycol = 5)
    l.append('colour','blue')
    l.append( 'linestyle', '.' )
    l.append( 'symbolsize', '2' )
    l.append( 'symbol', '^' )
    
    p = graph.addPlotObject()
    p.append( 'title', 'Side chain analysis' )
    p.append( 'plottype', 'xy' )
    p.append( 'showlegend', 'true' )
    p.append( 'xintegral', 'true' )
    p.append( 'yintegral', 'false' )
#    p.append( 'xlabel', 'Residue' ) # Labels are not represented correctly (Jon Agirre, 17 Oct 2014)
#    p.append( 'ylabel', 'Z-scores' )
#    l = p.append( 'plotline', xcol = 1, ycol = 6, rightaxis='true')
#    l.append( 'colour', 'red' )
#    l.append( 'linestyle', '.' )
    
    l = p.append( 'plotline', xcol = 1, ycol = 7 )
    l.append( 'colour', 'red' )
    l.append( 'linestyle', '.' )
    l.append( 'symbolsize', '1' )
    l.append( 'symbol', 'x' )

    l = p.append( 'plotline', xcol = 1, ycol = 8 )
    l.append('colour','blue')
    l.append( 'linestyle', '.' )
    l.append( 'symbolsize', '1' )
    l.append( 'symbol', 'x' )


    tableFolder = self.addFold(label='per-residue metrics', initiallyOpen=True)
    table1 = tableFolder.addTable(select="//Residue", downloadable=True, id='edstats_table')
    
    for title,subtitle,select in [ [ "Residue", "Name" , "Name" ],
                                   [ "Residue", "Chain", "Chain"],
                                   [ "Residue", "Number", "Number"],
                                   [ "Main chain", "ZCCm", "ZCCm"],
                                   [ "Main chain", "ZOm", "ZOm"],
                                   [ "Main chain", "ZD-m", "ZDmm"],
                                   [ "Main chain", "ZD+m", "ZDpm"],
                                   [ "Side chain", "ZCCs", "ZCCs"],
                                   [ "Side chain", "ZOs", "ZOs"],
                                   [ "Side chain", "ZD-s", "ZDms"],
                                   [ "Side chain", "ZD+s", "ZDps"],
                                   [ "Average", "ZCCa", "ZCCa"],
                                   [ "Average", "ZOa", "ZOa"],
                                   [ "Average", "ZD-a", "ZDma"],
                                   [ "Average", "ZD+a", "ZDpa"]] :

        
        table1.addData(title=subtitle, subtitle=subtitle, select=select)

    


    self.addTaskReferences()

if __name__ == "__main__":
  import sys
  edstats_report(xmlFile=sys.argv[1],jobId=sys.argv[2])


