Changeset 3525


Ignore:
Timestamp:
Oct 5, 2015, 8:52:36 AM (7 years ago)
Author:
Nicklas Nordborg
Message:

References #812: Pilot report wizard

The pilot report is updated to the new template pdf. Information about the patient, case, etc. is loaded and written to the header section.

Images are still the output from the GeneReport script and.

Other values are still left blank.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/pdf/PilotReportWorker.java

    r3506 r3525  
    55import java.io.IOException;
    66import java.io.OutputStream;
     7import java.text.DecimalFormat;
     8import java.text.DecimalFormatSymbols;
     9import java.text.NumberFormat;
    710import java.text.SimpleDateFormat;
    811import java.util.Date;
     12import java.util.HashMap;
     13import java.util.List;
    914import java.util.Map;
    1015
     
    1217import com.itextpdf.text.Element;
    1318import com.itextpdf.text.pdf.PdfContentByte;
     19import com.itextpdf.text.pdf.StringUtils;
    1420
    1521import net.sf.basedb.core.BioMaterial;
    1622import net.sf.basedb.core.BioSource;
    1723import net.sf.basedb.core.DbControl;
     24import net.sf.basedb.core.Extract;
    1825import net.sf.basedb.core.Sample;
     26import net.sf.basedb.core.StringUtil;
    1927import net.sf.basedb.reggie.Reggie;
    2028import net.sf.basedb.reggie.Site;
     
    2230import net.sf.basedb.reggie.dao.Annotationtype;
    2331import net.sf.basedb.reggie.dao.Rawbioassay;
     32import net.sf.basedb.reggie.dao.Rna;
     33import net.sf.basedb.reggie.dao.RnaQc;
    2434import net.sf.basedb.reggie.dao.Subtype;
    2535import net.sf.basedb.reggie.logo.Logo;
    2636import net.sf.basedb.reggie.r.PilotReport;
    2737import net.sf.basedb.reggie.r.RResult;
     38import net.sf.basedb.util.MD5;
     39import net.sf.basedb.util.NumberFormatUtil;
     40import net.sf.basedb.util.Values;
     41import net.sf.basedb.util.formatter.NumberFormatter;
    2842
    2943/**
     
    4862      new DateToStringConverter(new SimpleDateFormat("yyyy-MM-dd"));
    4963
    50  
     64  private static final float MARGIN_RIGHT = 579;
     65  private static final float MARGIN_LEFT = 14;
     66 
     67  /* PLOT POSITIONS */
     68  // Left X of plots
     69  private static final float PLOT_X = 355; //309;
    5170  // Bottom Y of first plot
    52   private static final float PLOT_START_Y = 588;
    53  
     71  private static final float PLOT_START_Y = 474.5f; //588;
    5472  // Distance between plots
    55   private static final float PLOT_DELTA_Y = 124;
    56  
    57   // Left X of left plot set
    58   private static final float PLOT_X1 = 15;
    59  
    60   // Left X of right plot set
    61   private static final float PLOT_X2 = 309;
    62  
     73  private static final float PLOT_DELTA_Y = 89.5f; //124;
    6374  // Size of plots
    64   private static final float PLOT_WIDTH = 270;
    65   private static final float PLOT_HEIGHT = 97;
    66  
    67   // Top right corner of site logo
    68   private static final float LOGO_X = 576;
    69   private static final float LOGO_Y = 824;
    70   // Max size of logo
    71   private static final float LOGO_HEIGHT = 72;
    72   private static final float LOGO_WIDTH = 180;
    73  
    74  
    75   private static final float DATA_X = 298;
    76   private static final float DATA_START_Y = 786.8f;
    77   private static final float DATA_DELTA_Y = 15.2f;
    78  
     75  private static final float PLOT_WIDTH = 160; //270;
     76  private static final float PLOT_HEIGHT = 80; //97;
     77
     78  /* TEXT POSITIONS */
     79  // Left X of text columns
     80  private static final float TEXT_X1 = 99;
     81  private static final float TEXT_X2 = 191;
     82  private static final float TEXT_X3 = 283;
     83  private static final float TEXT_X4 = 404;
     84  private static final float TEXT_X5 = 495;
     85 
     86  // Bottom Y of text rows
     87  private static final float TEXT_Y1 = 801;
     88  private static final float TEXT_Y2 = 773;
     89  private static final float TEXT_Y3 = 746;
     90  private static final float TEXT_Y4 = 718;
     91  private static final float TEXT_Y5 = 690;
     92     
    7993  private PilotReport script;
    8094  private String config;
    8195  private String pdfTemplatePath;
     96  private Map<String, String> translations;
     97  private NumberFormat oneDecimal;
     98  private NumberFormat twoDecimals;
    8299
    83100  PilotReportWorker(PdfReportTemplate template, String config)
     
    101118      pdfTemplatePath = Reggie.getConfig().getConfig(config+"/template", null, script.getScriptDir() + "/template.pdf");
    102119      Reggie.checkFile(pdfTemplatePath, false);
     120      oneDecimal = createNumberFormat(1);
     121      twoDecimals = createNumberFormat(2);
    103122    }
    104123    PilotReport.Result result = script.run(dc, raw);
     
    116135    Rawbioassay raw = result.raw;
    117136   
    118     Map<Subtype, BioMaterial> parents = raw.findParentBioMaterial(dc, Subtype.PATIENT, Subtype.SPECIMEN);
     137    Map<Subtype, BioMaterial> parents = raw.findParentBioMaterial(dc, Subtype.PATIENT, Subtype.SPECIMEN, Subtype.CASE, Subtype.RNA);
     138    Extract rna = (Extract)parents.get(Subtype.RNA);
    119139    Sample specimen = (Sample)parents.get(Subtype.SPECIMEN);
     140    Sample theCase = (Sample)parents.get(Subtype.CASE);
    120141    BioSource patient = (BioSource)parents.get(Subtype.PATIENT);
     142    Site site = Site.findByCaseName(raw.getName());
    121143   
    122144    PdfUtil pdfUtil = null;
     
    136158        if (f2.exists())
    137159        {
    138           pdfUtil.importPdf(new FileInputStream(f2), PLOT_X2, y, 1.0f, 1.0f);
     160          pdfUtil.importPdf(new FileInputStream(f2), PLOT_X, y, 1.0f, 1.0f);
    139161          y -= PLOT_DELTA_Y;
    140162        }
    141163      }
    142164     
    143       // Include site logo
    144       Site site = Site.findByCaseName(raw.getName());
    145       if (site.getLogo() != null)
     165     
     166      // Patient and sample information
     167      // Row 2
     168      if (patient != null) pdfUtil.addText(patient.getName(), 12, Element.ALIGN_LEFT, TEXT_X1, TEXT_Y2);
     169      if (theCase != null) pdfUtil.addText(theCase.getName(), 12, Element.ALIGN_LEFT, TEXT_X2, TEXT_Y2);
     170      if (specimen != null) pdfUtil.addText(specimen.getName(), 12, Element.ALIGN_LEFT, TEXT_X3, TEXT_Y2);
     171
     172      // Row 3
     173      String consent = (String)Annotationtype.CONSENT.getAnnotationValue(dc, theCase);
     174      Date consentDate = (Date)Annotationtype.CONSENT_DATE.getAnnotationValue(dc, theCase);
     175      pdfUtil.addText(translate(consent), 12, Element.ALIGN_LEFT, TEXT_X1, TEXT_Y3);
     176      pdfUtil.addText(DATE_FORMAT.convert(consentDate), 12, Element.ALIGN_LEFT, TEXT_X2, TEXT_Y3);
     177      if (rna != null)
    146178      {
    147         Logo logo = site.getLogo();
    148         pdfUtil.importImage(logo.getLogo(), LOGO_X, LOGO_Y, LOGO_WIDTH, LOGO_HEIGHT, Element.ALIGN_RIGHT+Element.ALIGN_TOP);
     179        List<RnaQc> rnaqc = RnaQc.findByRna(dc, Rna.get(rna));
     180        RnaQc lastRnaQc = RnaQc.findLast(dc, null, rnaqc);
     181        if (lastRnaQc != null)
     182        {
     183          Float rqs = (Float)Annotationtype.CA_RQS.getAnnotationValue(dc, lastRnaQc.getItem());
     184          Float rin = (Float)Annotationtype.BA_RIN.getAnnotationValue(dc, lastRnaQc.getItem());
     185          if (rqs != null || rin != null)
     186          {
     187            pdfUtil.addText(oneDecimal.format((double)(rqs != null ? rqs : rin)), 12, Element.ALIGN_LEFT, TEXT_X3, TEXT_Y3);
     188          }
     189        }
    149190      }
    150191     
    151       // TODO -- remove when we have a real pilot report
    152       // Write white rectangle to cover the "Genrapport" text
    153       // and then write "Pilotrapport" on top of it
    154       PdfContentByte canvas = pdfUtil.getCanvas();
    155       canvas.saveState();
    156       canvas.setColorFill(BaseColor.WHITE);
    157       canvas.rectangle(DATA_X - 100, DATA_START_Y+DATA_DELTA_Y, 200, DATA_DELTA_Y*2);
    158       canvas.fillStroke();
    159       canvas.restoreState();
    160       pdfUtil.addText("Pilotrapport", 24, Element.ALIGN_CENTER, DATA_X, DATA_START_Y + 1.5f*DATA_DELTA_Y);
    161       // ------
    162      
    163       // Patient and sample information
    164       pdfUtil.addText(DATE_FORMAT.convert(new Date()), 12, Element.ALIGN_LEFT, DATA_X, DATA_START_Y);
    165       if (site != Site.UNKNOWN)
    166       {
    167         pdfUtil.addText(site.getName(), 12, Element.ALIGN_LEFT, DATA_X, DATA_START_Y - DATA_DELTA_Y);
    168       }
    169       if (patient != null)
    170       {
    171         pdfUtil.addText(patient.getName(), 12, Element.ALIGN_LEFT, DATA_X, DATA_START_Y - 2 * DATA_DELTA_Y);
    172       }
    173       pdfUtil.addText(specimen != null ? specimen.getName() : raw.getName().split("\\.(?!\\d)")[0], 12, Element.ALIGN_LEFT, DATA_X, DATA_START_Y - 3 * DATA_DELTA_Y);
     192      // Row 4
     193      pdfUtil.addText(DATE_FORMAT.convert((Date)Annotationtype.SAMPLING_DATETIME.getAnnotationValue(dc, specimen)), 12, Element.ALIGN_LEFT, TEXT_X1, TEXT_Y4);
     194      pdfUtil.addText(DATE_FORMAT.convert((Date)Annotationtype.ARRIVAL_DATE.getAnnotationValue(dc, specimen)), 12, Element.ALIGN_LEFT, TEXT_X2, TEXT_Y4);
     195      pdfUtil.addText(DATE_FORMAT.convert(new Date()), 12, Element.ALIGN_LEFT, TEXT_X3, TEXT_Y4);
     196     
     197      // Row 5
     198      String laterality = (String)Annotationtype.LATERALITY.getAnnotationValue(dc, specimen);
     199      pdfUtil.addText(translate(laterality), 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y5);
     200      if (site != Site.UNKNOWN) pdfUtil.addText(site.getName(), 12, Element.ALIGN_LEFT, TEXT_X5, TEXT_Y5);
    174201     
    175202      // Extra info at bottom
    176       pdfUtil.addText(raw.getName(), 8, Element.ALIGN_RIGHT, PLOT_X2+PLOT_WIDTH, 10);
     203      pdfUtil.addText(raw.getName(), 8, Element.ALIGN_RIGHT, MARGIN_RIGHT, 10);
    177204    }
    178205    finally
     
    193220    Map<Subtype, BioMaterial> parents = raw.findParentBioMaterial(dc, Subtype.SPECIMEN, Subtype.PATIENT);
    194221    BioSource patient = (BioSource)parents.get(Subtype.PATIENT);
    195     Sample speciemen = (Sample)parents.get(Subtype.SPECIMEN);
    196    
    197    
    198     float X = PilotReportWorker.PLOT_X1 + PilotReportWorker.PLOT_WIDTH / 2;
    199     float Y = PilotReportWorker.PLOT_START_Y + PilotReportWorker.PLOT_DELTA_Y / 2;
    200     PdfContentByte canvas = pdf.getCanvas();
    201     canvas.saveState();
    202     canvas.setColorFill(new BaseColor(255, 255, 204));
    203     canvas.rectangle(PilotReportWorker.PLOT_X1, PilotReportWorker.PLOT_START_Y, PilotReportWorker.PLOT_WIDTH, PilotReportWorker.PLOT_HEIGHT);
    204     canvas.fillStroke();
    205     canvas.restoreState();
     222    Sample specimen = (Sample)parents.get(Subtype.SPECIMEN);
     223   
     224    pdf.addText(DATE_FORMAT.convert(new Date()), 8, Element.ALIGN_LEFT, MARGIN_LEFT, 10);
    206225   
    207226    if (patient != null)
    208227    {
    209       pdf.addText(patient.getName(), 12, Element.ALIGN_CENTER, X, Y);
    210       pdf.addText((String)Annotationtype.ALL_FIRST_NAMES.getAnnotationValue(dc, patient)+" "+Annotationtype.FAMILY_NAME.getAnnotationValue(dc, patient), 12, Element.ALIGN_CENTER, X, Y-12);
    211       pdf.addText((String)Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, patient), 12, Element.ALIGN_CENTER, X, Y-24);
     228      pdf.addText((String)Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, patient), 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y1);
     229      pdf.addText((String)Annotationtype.FAMILY_NAME.getAnnotationValue(dc, patient), 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y2);
     230      pdf.addText((String)Annotationtype.ALL_FIRST_NAMES.getAnnotationValue(dc, patient), 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y3);
     231      pdf.addText((String)Annotationtype.PAD.getAnnotationValue(dc, specimen), 12, Element.ALIGN_LEFT, TEXT_X3, TEXT_Y5);
    212232    }
    213233    else
    214234    {
    215       pdf.addText("No patient information", 12, Element.ALIGN_CENTER, X, Y);
    216     }
    217   }
    218 
     235      pdf.addText("No patient information", 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y1);
     236    }
     237  }
     238
     239  private String translate(String word)
     240  {
     241    if (word == null) return null;
     242    if (translations == null)
     243    {
     244      translations = new HashMap<String, String>();
     245      translations.put("left", "Vänster");
     246      translations.put("right", "Höger");
     247      translations.put("yes", "Ja");
     248      translations.put("no", "Nej");
     249    }
     250   
     251    String translatedWord = translations.get(word.toLowerCase());
     252    return translatedWord == null ? word : translatedWord;
     253  }
     254 
     255  private NumberFormat createNumberFormat(int numDecimals)
     256  {
     257    DecimalFormatSymbols sym = new DecimalFormatSymbols();
     258    sym.setDecimalSeparator(',');
     259    String format = "0";
     260    if (numDecimals > 0) format += "." + MD5.leftPad("", '0', numDecimals);
     261    DecimalFormat df = new DecimalFormat(format, sym);
     262    return df;
     263  }
    219264 
    220265}
Note: See TracChangeset for help on using the changeset viewer.