<commit_before>package gov.nih.nci.ispy.ui.graphing.chart.plot;

import gov.nih.nci.caintegrator.application.graphing.PlotPoint;
import gov.nih.nci.caintegrator.application.service.LevelOfExpressionIHCService;
import gov.nih.nci.caintegrator.application.service.LossOfExpressionIHCService;
import gov.nih.nci.caintegrator.domain.finding.bean.Finding;
import gov.nih.nci.caintegrator.domain.finding.protein.ihc.bean.IHCFinding;
import gov.nih.nci.caintegrator.domain.finding.protein.ihc.bean.LevelOfExpressionIHCFinding;
import gov.nih.nci.caintegrator.enumeration.AxisType;
import gov.nih.nci.caintegrator.ui.graphing.data.DataRange;
import gov.nih.nci.ispy.service.annotation.SampleInfo;
import gov.nih.nci.ispy.service.clinical.ContinuousType;
import gov.nih.nci.ispy.service.clinical.PatientData;
import gov.nih.nci.ispy.ui.graphing.data.ISPYPlotPoint;
import gov.nih.nci.ispy.ui.graphing.data.principalComponentAnalysis.ISPYPCADataPoint;

import gov.nih.nci.ispy.service.clinical.ClinicalResponseType;
import gov.nih.nci.ispy.service.clinical.ClinicalStageType;
import java.awt.geom.Line2D;
import gov.nih.nci.ispy.service.common.TimepointType;
import gov.nih.nci.ispy.service.findings.strategies.CorrelationFindingStrategy3;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.text.NumberFormat;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.LegendItemSource;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;

public class ISPYCorrelationScatterPlot {

	private Collection<ISPYPlotPoint> dataPoints;
	private JFreeChart corrChart = null;
	private String xLabel;
	private String yLabel;
	private ContinuousType ct1;
	private ContinuousType ct2;
	private Double corrValue;
	private NumberFormat nf = NumberFormat.getNumberInstance();
	private static final double glyphSize = 8.0;
	private ColorByType colorBy = ColorByType.CLINICALRESPONSE;
	private static Logger logger = Logger.getLogger(ISPYCorrelationScatterPlot.class);	
	
	public ISPYCorrelationScatterPlot(Collection<ISPYPlotPoint> dataPoints, String xLabel, String yLabel, ContinuousType contType1, ContinuousType contType2, Double correlationValue, ColorByType colorBy) {
		this.dataPoints = dataPoints;
		this.xLabel = xLabel;
		this.yLabel = yLabel;
		this.ct1 = contType1;
		this.ct2 = contType2;
		this.corrValue = correlationValue;
		this.colorBy = colorBy;
		nf.setMinimumFractionDigits(1);
		createChart();
		
	}

	private void createChart() {
		
		String title = "Correlation Scatter Plot  correlationCoefficient=" + nf.format(corrValue) + " N=" + dataPoints.size();
		
		corrChart = ChartFactory.createScatterPlot(title,xLabel, yLabel, null,  PlotOrientation.VERTICAL,
	            true, 
	            true, 
	            false );
		
		XYPlot plot = (XYPlot) corrChart.getPlot();
		
		buildLegend();
		
	    plot.setNoDataMessage(null);
	    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
	    renderer.setToolTipGenerator(new StandardXYToolTipGenerator());
	    renderer.setUseOutlinePaint(true);
        plot.setRangeCrosshairVisible(false);
	    plot.setDomainCrosshairVisible(false);
	        
//	     XYShapeAnnotation annotation = new XYShapeAnnotation(new Rectangle2D.Double(25.0, 25.0, 5, 5));
//	        
//	     plot.addAnnotation(annotation);
	       
	        
        NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
        NumberAxis rangeAxis  =	(NumberAxis) plot.getRangeAxis();
        
        
        //should determine axis range using datapoints.
        List<PlotPoint> plotPoints = new ArrayList<PlotPoint>(dataPoints);
        DataRange Xrange = PlotPoint.getDataRange(plotPoints, AxisType.X_AXIS );
        DataRange Yrange = PlotPoint.getDataRange(plotPoints, AxisType.Y_AXIS);
        
//        Double xAbsMax = Math.max(Math.abs(Xrange.getMaxRange()), Math.abs(Xrange.getMinRange()));
//        Double yAbsMax = Math.max(Math.abs(Yrange.getMaxRange()), Math.abs(Yrange.getMinRange()));
//        
//        Double maxAbsVal = Math.max(xAbsMax, yAbsMax);
           
        domainAxis.setAutoRangeIncludesZero(false);
         
        
        double xTick = 10.0;
        double yTick = 10.0;
        
        double xdist = Math.abs(Xrange.getMaxRange()-Xrange.getMinRange());
        double ydist = Math.abs(Yrange.getMaxRange()-Yrange.getMinRange());
        if (xdist < 10.0) {
          xTick = 1.0;
        }
        
        
        if (ydist < 10.0) {
          yTick = 1.0;
        }
        
//        if (maxAbsVal <= 25.0) {
//          tickUnit =5.0;
//        }
//        else if (maxAbsVal <= 50.0) {
//          tickUnit = 10.0;
//        }
        
        domainAxis.setTickUnit(new NumberTickUnit(xTick));
        rangeAxis.setTickUnit(new NumberTickUnit(yTick));
        
        //double glyphScaleFactor = (maxAbsVal*2.0)/600.0;   //assuming 600 pixels for the graph
        double xScale = xdist / 600.0;
        //double glyphScaleFactor = 1.0;
        double yScale = ydist / 600.0;
        
        
        //double adjAbsVal = Math.ceil(maxAbsVal + (glyphScaleFactor*8.0));
        
        //domainAxis.setRange(-maxAbsVal, maxAbsVal);
        double xMin = Xrange.getMinRange()-xScale*glyphSize;
        double xMax = Xrange.getMaxRange()+xScale*glyphSize;
        double yMin = Yrange.getMinRange()-yScale*glyphSize;
        double yMax = Yrange.getMaxRange()+yScale*glyphSize;
        
        domainAxis.setRange(xMin, xMax);
        
        //rangeAxis.setRange(-maxAbsVal, maxAbsVal);
        rangeAxis.setRange(yMin, yMax);
        
        if ((colorBy == ColorByType.IHC_EXPRESSION_X) || (colorBy == ColorByType.IHC_EXPRESSION_Y)) {
          //Get the IHC data for the samples to be graphed	
          LevelOfExpressionIHCService loeService = LevelOfExpressionIHCService.getInstance();
          LossOfExpressionIHCService lossService = LossOfExpressionIHCService.getInstance();
          Set<String> sampleIds = new HashSet<String>();
          String labtrackId;
          SampleInfo info;
          for (ISPYPlotPoint corrPoint : dataPoints) {
        	  info = corrPoint.getSampleInfo();
        	  if (info != null) {
        		  labtrackId = corrPoint.getSampleInfo().getLabtrackId();
            	  sampleIds.add(labtrackId);
        	  }
        	  else {
        	      logger.warn("Point id=" + corrPoint.getId() + " has no sample info. Skipping point");
        	  }
          }
          
          Collection<? extends Finding> loeFindings = loeService.getFindingsFromSampleIds(sampleIds);
          
          //TEST Case
//          Set<String> testIds = new HashSet<String>();
//          testIds.add("212833");
//          testIds.add("213152");
          
          //Collection<? extends Finding> loeFindings = loeService.getFindingsFromSampleIds(testIds);
          
          
          //Collection<? extends Finding> lossFindings = lossService.getFindingsFromSampleIds(sampleIds);
          
          Map ihcData = new HashMap<String, IHCFinding>();
          IHCFinding loeFinding;
          for (Finding finding : loeFindings) {
        	loeFinding = (IHCFinding) finding;
            ihcData.put(loeFinding.getId(), loeFinding);
          }
//          IHCFinding lossFinding;
//          for (Finding finding : lossFindings) {
//        	lossFinding = (IHCFinding) finding;
//            ihcData.put(lossFinding.getId(), lossFinding);
//          }
          
        }
        
        
	    createGlyphsAndAddToPlot(plot, xScale, yScale); 	 
	    
	   // Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
	    //try and match the UI e9e9e9<commit_msg>*** empty log message ***<commit_after>package gov.nih.nci.ispy.ui.graphing.chart.plot;

import gov.nih.nci.caintegrator.application.graphing.PlotPoint;
import gov.nih.nci.caintegrator.domain.finding.bean.Finding;
import gov.nih.nci.caintegrator.domain.finding.protein.ihc.bean.IHCFinding;
import gov.nih.nci.caintegrator.enumeration.AxisType;
import gov.nih.nci.caintegrator.ui.graphing.data.DataRange;
import gov.nih.nci.ispy.service.annotation.SampleInfo;
import gov.nih.nci.ispy.service.clinical.ClinicalResponseType;
import gov.nih.nci.ispy.service.clinical.ClinicalStageType;
import gov.nih.nci.ispy.service.clinical.ContinuousType;
import gov.nih.nci.ispy.service.clinical.PatientData;
import gov.nih.nci.ispy.service.common.TimepointType;
import gov.nih.nci.ispy.service.ihc.LevelOfExpressionIHCService;
import gov.nih.nci.ispy.service.ihc.LossOfExpressionIHCService;
import gov.nih.nci.ispy.ui.graphing.data.ISPYPlotPoint;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Paint;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.LegendItemSource;
import org.jfree.chart.annotations.XYShapeAnnotation;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;

public class ISPYCorrelationScatterPlot {

	private Collection<ISPYPlotPoint> dataPoints;
	private JFreeChart corrChart = null;
	private String xLabel;
	private String yLabel;
	private ContinuousType ct1;
	private ContinuousType ct2;
	private Double corrValue;
	private NumberFormat nf = NumberFormat.getNumberInstance();
	private static final double glyphSize = 8.0;
	private ColorByType colorBy = ColorByType.CLINICALRESPONSE;
	private static Logger logger = Logger.getLogger(ISPYCorrelationScatterPlot.class);	
	
	public ISPYCorrelationScatterPlot(Collection<ISPYPlotPoint> dataPoints, String xLabel, String yLabel, ContinuousType contType1, ContinuousType contType2, Double correlationValue, ColorByType colorBy) {
		this.dataPoints = dataPoints;
		this.xLabel = xLabel;
		this.yLabel = yLabel;
		this.ct1 = contType1;
		this.ct2 = contType2;
		this.corrValue = correlationValue;
		this.colorBy = colorBy;
		nf.setMinimumFractionDigits(1);
		createChart();
		
	}

	private void createChart() {
		
		String title = "Correlation Scatter Plot  correlationCoefficient=" + nf.format(corrValue) + " N=" + dataPoints.size();
		
		corrChart = ChartFactory.createScatterPlot(title,xLabel, yLabel, null,  PlotOrientation.VERTICAL,
	            true, 
	            true, 
	            false );
		
		XYPlot plot = (XYPlot) corrChart.getPlot();
		
		buildLegend();
		
	    plot.setNoDataMessage(null);
	    XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
	    renderer.setToolTipGenerator(new StandardXYToolTipGenerator());
	    renderer.setUseOutlinePaint(true);
        plot.setRangeCrosshairVisible(false);
	    plot.setDomainCrosshairVisible(false);
	        
//	     XYShapeAnnotation annotation = new XYShapeAnnotation(new Rectangle2D.Double(25.0, 25.0, 5, 5));
//	        
//	     plot.addAnnotation(annotation);
	       
	        
        NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
        NumberAxis rangeAxis  =	(NumberAxis) plot.getRangeAxis();
        
        
        //should determine axis range using datapoints.
        List<PlotPoint> plotPoints = new ArrayList<PlotPoint>(dataPoints);
        DataRange Xrange = PlotPoint.getDataRange(plotPoints, AxisType.X_AXIS );
        DataRange Yrange = PlotPoint.getDataRange(plotPoints, AxisType.Y_AXIS);
        
//        Double xAbsMax = Math.max(Math.abs(Xrange.getMaxRange()), Math.abs(Xrange.getMinRange()));
//        Double yAbsMax = Math.max(Math.abs(Yrange.getMaxRange()), Math.abs(Yrange.getMinRange()));
//        
//        Double maxAbsVal = Math.max(xAbsMax, yAbsMax);
           
        domainAxis.setAutoRangeIncludesZero(false);
         
        
        double xTick = 10.0;
        double yTick = 10.0;
        
        double xdist = Math.abs(Xrange.getMaxRange()-Xrange.getMinRange());
        double ydist = Math.abs(Yrange.getMaxRange()-Yrange.getMinRange());
        if (xdist < 10.0) {
          xTick = 1.0;
        }
        
        
        if (ydist < 10.0) {
          yTick = 1.0;
        }
        
//        if (maxAbsVal <= 25.0) {
//          tickUnit =5.0;
//        }
//        else if (maxAbsVal <= 50.0) {
//          tickUnit = 10.0;
//        }
        
        domainAxis.setTickUnit(new NumberTickUnit(xTick));
        rangeAxis.setTickUnit(new NumberTickUnit(yTick));
        
        //double glyphScaleFactor = (maxAbsVal*2.0)/600.0;   //assuming 600 pixels for the graph
        double xScale = xdist / 600.0;
        //double glyphScaleFactor = 1.0;
        double yScale = ydist / 600.0;
        
        
        //double adjAbsVal = Math.ceil(maxAbsVal + (glyphScaleFactor*8.0));
        
        //domainAxis.setRange(-maxAbsVal, maxAbsVal);
        double xMin = Xrange.getMinRange()-xScale*glyphSize;
        double xMax = Xrange.getMaxRange()+xScale*glyphSize;
        double yMin = Yrange.getMinRange()-yScale*glyphSize;
        double yMax = Yrange.getMaxRange()+yScale*glyphSize;
        
        domainAxis.setRange(xMin, xMax);
        
        //rangeAxis.setRange(-maxAbsVal, maxAbsVal);
        rangeAxis.setRange(yMin, yMax);
        Set<SampleInfo> infoSet = new HashSet<SampleInfo>();
        
        if ((colorBy == ColorByType.IHC_EXPRESSION_X) || (colorBy == ColorByType.IHC_EXPRESSION_Y)) {
          //Get the IHC data for the samples to be graphed	
          LevelOfExpressionIHCService loeService = LevelOfExpressionIHCService.getInstance();
          LossOfExpressionIHCService lossService = LossOfExpressionIHCService.getInstance();
          Set<String> sampleIds = new HashSet<String>();
          String labtrackId;
          SampleInfo info;          
          for (ISPYPlotPoint corrPoint : dataPoints) {
        	  info = corrPoint.getSampleInfo();
        	  if (info != null) {
        		  labtrackId = corrPoint.getSampleInfo().getLabtrackId();
            	  sampleIds.add(labtrackId);
                  infoSet.add(info);
        	  }
        	  else {
        	      logger.warn("Point id=" + corrPoint.getId() + " has no sample info. Skipping point");
        	  }
          }
          
          Collection<? extends Finding> loeFindings = loeService.getFindingsFromSampleInfo(infoSet);
          
          //TEST Case
//          Set<String> testIds = new HashSet<String>();
//          testIds.add("212833");
//          testIds.add("213152");
          
          //Collection<? extends Finding> loeFindings = loeService.getFindingsFromSampleIds(testIds);
          
          
          //Collection<? extends Finding> lossFindings = lossService.getFindingsFromSampleIds(sampleIds);
          
          Map ihcData = new HashMap<String, IHCFinding>();
          IHCFinding loeFinding;
          for (Finding finding : loeFindings) {
        	loeFinding = (IHCFinding) finding;
            ihcData.put(loeFinding.getId(), loeFinding);
          }
//          IHCFinding lossFinding;
//          for (Finding finding : lossFindings) {
//        	lossFinding = (IHCFinding) finding;
//            ihcData.put(lossFinding.getId(), lossFinding);
//          }
          
        }
        
        
	    createGlyphsAndAddToPlot(plot, xScale, yScale); 	 
	    
	   // Paint p = new GradientPaint(0, 0, Color.white, 1000, 0, Color.green);
	    //try and match the UI e9e9e9
{'fraction_non_alphanumeric': 0.10850246187102197, 'fraction_numerical': 0.011348624954965775, 'mean_word_length': 2.8687572590011614, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5046362', 'n_tokens_mistral': 5592, 'n_tokens_neox': 5224, 'n_words': 910}