extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/pdf/GeneReportWorker.java

Code
Comments
Other
Rev Date Author Line
3506 23 Sep 15 nicklas 1 package net.sf.basedb.reggie.pdf;
3506 23 Sep 15 nicklas 2
3506 23 Sep 15 nicklas 3 import java.io.File;
3506 23 Sep 15 nicklas 4 import java.io.FileInputStream;
3506 23 Sep 15 nicklas 5 import java.io.IOException;
3506 23 Sep 15 nicklas 6 import java.io.OutputStream;
3506 23 Sep 15 nicklas 7 import java.util.Date;
3506 23 Sep 15 nicklas 8 import java.util.Map;
3506 23 Sep 15 nicklas 9
7024 07 Feb 23 nicklas 10 import org.apache.commons.lang3.time.FastDateFormat;
7024 07 Feb 23 nicklas 11
3506 23 Sep 15 nicklas 12 import net.sf.basedb.core.BioMaterial;
3506 23 Sep 15 nicklas 13 import net.sf.basedb.core.BioSource;
3506 23 Sep 15 nicklas 14 import net.sf.basedb.core.DbControl;
3506 23 Sep 15 nicklas 15 import net.sf.basedb.core.Sample;
6031 29 Oct 20 nicklas 16 import net.sf.basedb.core.plugin.ExportOutputStream;
3506 23 Sep 15 nicklas 17 import net.sf.basedb.reggie.Reggie;
3506 23 Sep 15 nicklas 18 import net.sf.basedb.reggie.Site;
3506 23 Sep 15 nicklas 19 import net.sf.basedb.reggie.converter.DateToStringConverter;
3506 23 Sep 15 nicklas 20 import net.sf.basedb.reggie.dao.Rawbioassay;
3506 23 Sep 15 nicklas 21 import net.sf.basedb.reggie.dao.Subtype;
3506 23 Sep 15 nicklas 22 import net.sf.basedb.reggie.logo.Logo;
6060 17 Nov 20 nicklas 23 import net.sf.basedb.reggie.pdf.PdfUtil7.Align;
6070 20 Nov 20 nicklas 24 import net.sf.basedb.reggie.pdf.PdfUtil7.Options;
6035 29 Oct 20 nicklas 25 import net.sf.basedb.reggie.script.GeneReport;
6036 02 Nov 20 nicklas 26 import net.sf.basedb.reggie.script.ScriptResult;
3506 23 Sep 15 nicklas 27
3506 23 Sep 15 nicklas 28
3506 23 Sep 15 nicklas 29 /**
3506 23 Sep 15 nicklas 30   Worker implementation that runs the 'GeneReport' R script and
3506 23 Sep 15 nicklas 31   generates a PDF document with the plots.
3506 23 Sep 15 nicklas 32
3506 23 Sep 15 nicklas 33   @author nicklas
3506 23 Sep 15 nicklas 34   @since 2.18, 3.7
3506 23 Sep 15 nicklas 35 */
3506 23 Sep 15 nicklas 36 public class GeneReportWorker 
3506 23 Sep 15 nicklas 37   extends PdfReportWorker
3506 23 Sep 15 nicklas 38 {
3506 23 Sep 15 nicklas 39
3506 23 Sep 15 nicklas 40   /**
3506 23 Sep 15 nicklas 41     The default file name for the generated pdf.
3506 23 Sep 15 nicklas 42   */
3506 23 Sep 15 nicklas 43   public static final String DEFAULT_PDF_NAME = "genereport.pdf";
3506 23 Sep 15 nicklas 44   
3506 23 Sep 15 nicklas 45   public static final DateToStringConverter DATE_FORMAT = 
7024 07 Feb 23 nicklas 46       new DateToStringConverter(FastDateFormat.getInstance("yyyy-MM-dd"));
3506 23 Sep 15 nicklas 47
3506 23 Sep 15 nicklas 48   
3506 23 Sep 15 nicklas 49   // Bottom Y of first plot
3506 23 Sep 15 nicklas 50   private static final float PLOT_START_Y = 588;
3506 23 Sep 15 nicklas 51   
3506 23 Sep 15 nicklas 52   // Distance between plots
3506 23 Sep 15 nicklas 53   private static final float PLOT_DELTA_Y = 124;
3506 23 Sep 15 nicklas 54   
3506 23 Sep 15 nicklas 55   // Left X of left plot set
3506 23 Sep 15 nicklas 56   private static final float PLOT_X1 = 15;
3506 23 Sep 15 nicklas 57   
3506 23 Sep 15 nicklas 58   // Left X of right plot set
3506 23 Sep 15 nicklas 59   private static final float PLOT_X2 = 309;
3506 23 Sep 15 nicklas 60   
3506 23 Sep 15 nicklas 61   // Size of plots
3506 23 Sep 15 nicklas 62   private static final float PLOT_WIDTH = 270;
3506 23 Sep 15 nicklas 63   private static final float PLOT_HEIGHT = 97;
3506 23 Sep 15 nicklas 64   
3506 23 Sep 15 nicklas 65   // Top right corner of site logo
3506 23 Sep 15 nicklas 66   private static final float LOGO_X = 576;
3506 23 Sep 15 nicklas 67   private static final float LOGO_Y = 824;
3506 23 Sep 15 nicklas 68   // Max size of logo
3506 23 Sep 15 nicklas 69   private static final float LOGO_HEIGHT = 72;
3506 23 Sep 15 nicklas 70   private static final float LOGO_WIDTH = 180;
3506 23 Sep 15 nicklas 71   
3506 23 Sep 15 nicklas 72   private static final float DATA_X = 298;
3506 23 Sep 15 nicklas 73   private static final float DATA_START_Y = 786.8f;
3506 23 Sep 15 nicklas 74   private static final float DATA_DELTA_Y = 15.2f;
3506 23 Sep 15 nicklas 75   
3620 24 Nov 15 nicklas 76   private final String config;
3620 24 Nov 15 nicklas 77   private final String parameterSet;
3506 23 Sep 15 nicklas 78   private GeneReport script;
3506 23 Sep 15 nicklas 79   private String pdfTemplatePath;
3506 23 Sep 15 nicklas 80   
3620 24 Nov 15 nicklas 81   GeneReportWorker(PdfReportTemplate template, String config, String parameterSet)
3506 23 Sep 15 nicklas 82   {
3506 23 Sep 15 nicklas 83     super(template);
3506 23 Sep 15 nicklas 84     this.config = config;
3620 24 Nov 15 nicklas 85     this.parameterSet = parameterSet;
3506 23 Sep 15 nicklas 86   }
3506 23 Sep 15 nicklas 87   
3506 23 Sep 15 nicklas 88   /**
3506 23 Sep 15 nicklas 89     Run the gene report script for the given raw bioassay and then
3506 23 Sep 15 nicklas 90     create the pdf.
3506 23 Sep 15 nicklas 91   */
3506 23 Sep 15 nicklas 92   @Override
6036 02 Nov 20 nicklas 93   public ScriptResult runAndCreatePdf(DbControl dc, Rawbioassay raw, ExportOutputStream out, ReportOptions options)
3506 23 Sep 15 nicklas 94     throws IOException
3506 23 Sep 15 nicklas 95   {
3506 23 Sep 15 nicklas 96     if (script == null) 
3506 23 Sep 15 nicklas 97     {
3506 23 Sep 15 nicklas 98       // Initialize script in the first call to this method
3620 24 Nov 15 nicklas 99       script = new GeneReport(config, parameterSet, PLOT_WIDTH, PLOT_HEIGHT, "ESR1", "PGR", "ERBB2", "MKI67", "AURKA");
3620 24 Nov 15 nicklas 100       pdfTemplatePath = Reggie.getConfig().getConfig(config+"/template", parameterSet, script.getScriptDir() + "/template.pdf");
3506 23 Sep 15 nicklas 101       Reggie.checkFile(pdfTemplatePath, false);
3506 23 Sep 15 nicklas 102     }
3506 23 Sep 15 nicklas 103     GeneReport.Result result = script.run(dc, raw);
3506 23 Sep 15 nicklas 104     if (result.getExitStatus() == 0)
3506 23 Sep 15 nicklas 105     {
4537 21 Jun 17 nicklas 106       toPdf(dc, result, out, options);
3506 23 Sep 15 nicklas 107     }
3506 23 Sep 15 nicklas 108     return result;
3506 23 Sep 15 nicklas 109   }
3506 23 Sep 15 nicklas 110   
4537 21 Jun 17 nicklas 111   private void toPdf(DbControl dc, GeneReport.Result result, OutputStream out, ReportOptions options)
3506 23 Sep 15 nicklas 112     throws IOException 
3506 23 Sep 15 nicklas 113   {
3506 23 Sep 15 nicklas 114     Rawbioassay raw = result.raw;
3506 23 Sep 15 nicklas 115     
3506 23 Sep 15 nicklas 116     Map<Subtype, BioMaterial> parents = raw.findParentBioMaterial(dc, Subtype.PATIENT, Subtype.SPECIMEN);
3506 23 Sep 15 nicklas 117     Sample specimen = (Sample)parents.get(Subtype.SPECIMEN);
3506 23 Sep 15 nicklas 118     BioSource patient = (BioSource)parents.get(Subtype.PATIENT);
3506 23 Sep 15 nicklas 119
6060 17 Nov 20 nicklas 120     PdfUtil7 pdfUtil = null;
3506 23 Sep 15 nicklas 121     try
3506 23 Sep 15 nicklas 122     {
6060 17 Nov 20 nicklas 123       pdfUtil = new PdfUtil7("Gene report: " + raw.getName(), result.getScript());
3506 23 Sep 15 nicklas 124       pdfUtil.open(out);
3506 23 Sep 15 nicklas 125       
6061 18 Nov 20 nicklas 126       pdfUtil.importPdf(new FileInputStream(pdfTemplatePath), 1, 0, 0, Float.NaN, Float.NaN, Align.LEFT);
3506 23 Sep 15 nicklas 127       //pdfUtil.drawGrid();
3506 23 Sep 15 nicklas 128       
3506 23 Sep 15 nicklas 129       float y = PLOT_START_Y;
3506 23 Sep 15 nicklas 130       File workDir = result.getWorkDir();
3506 23 Sep 15 nicklas 131       for (String gene : result.genes)
3506 23 Sep 15 nicklas 132       {
3506 23 Sep 15 nicklas 133         File f1 = new File(workDir, "valid_"+gene+".pdf");
3506 23 Sep 15 nicklas 134         File f2 = new File(workDir, "scanb_"+gene+".pdf");
3506 23 Sep 15 nicklas 135         if (f1.exists())
3506 23 Sep 15 nicklas 136         {
6061 18 Nov 20 nicklas 137           pdfUtil.importPdf(new FileInputStream(f1), 1, PLOT_X1, y, Float.NaN, Float.NaN, Align.LEFT);
6061 18 Nov 20 nicklas 138           pdfUtil.importPdf(new FileInputStream(f2), 1, PLOT_X2, y, Float.NaN, Float.NaN, Align.LEFT);
3506 23 Sep 15 nicklas 139           y -= PLOT_DELTA_Y;
3506 23 Sep 15 nicklas 140         }
3506 23 Sep 15 nicklas 141       }
3506 23 Sep 15 nicklas 142       
3506 23 Sep 15 nicklas 143       // Include site logo
3506 23 Sep 15 nicklas 144       Site site = Site.findByCaseName(raw.getName());
3506 23 Sep 15 nicklas 145       if (site.getLogo() != null)
3506 23 Sep 15 nicklas 146       {
3506 23 Sep 15 nicklas 147         Logo logo = site.getLogo();
6060 17 Nov 20 nicklas 148         pdfUtil.importImage(logo.getLogo(), LOGO_X, LOGO_Y, LOGO_WIDTH, LOGO_HEIGHT, Align.TOP_RIGHT);
3506 23 Sep 15 nicklas 149       }
3506 23 Sep 15 nicklas 150       
3506 23 Sep 15 nicklas 151       // Patient and sample information
6070 20 Nov 20 nicklas 152       pdfUtil.addText(DATE_FORMAT.convert(new Date()), 12, DATA_X, DATA_START_Y);
3506 23 Sep 15 nicklas 153       if (site != Site.UNKNOWN)
3506 23 Sep 15 nicklas 154       {
6070 20 Nov 20 nicklas 155         pdfUtil.addText(site.getName(), 12,  DATA_X, DATA_START_Y - DATA_DELTA_Y);
3506 23 Sep 15 nicklas 156       }
3506 23 Sep 15 nicklas 157       if (patient != null)
3506 23 Sep 15 nicklas 158       {
6070 20 Nov 20 nicklas 159         pdfUtil.addText(patient.getName(), 12, DATA_X, DATA_START_Y - 2 * DATA_DELTA_Y);
3506 23 Sep 15 nicklas 160       }
6070 20 Nov 20 nicklas 161       pdfUtil.addText(specimen != null ? specimen.getName() : raw.getName().split("\\.(?!\\d)")[0], 12, DATA_X, DATA_START_Y - 3 * DATA_DELTA_Y);
3506 23 Sep 15 nicklas 162       
3506 23 Sep 15 nicklas 163       // Extra info at bottom
6070 20 Nov 20 nicklas 164       pdfUtil.addText(raw.getName(), 8, PLOT_X2+PLOT_WIDTH, 10, Options.ALIGN_RIGHT);
3506 23 Sep 15 nicklas 165     }
3506 23 Sep 15 nicklas 166     finally
3506 23 Sep 15 nicklas 167     {
3506 23 Sep 15 nicklas 168       if (pdfUtil != null) pdfUtil.close();
3506 23 Sep 15 nicklas 169     }    
3506 23 Sep 15 nicklas 170     
3506 23 Sep 15 nicklas 171   }
3506 23 Sep 15 nicklas 172
3506 23 Sep 15 nicklas 173
3506 23 Sep 15 nicklas 174 }