extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/script/PilotReport.java

Code
Comments
Other
Rev Date Author Line
6035 29 Oct 20 nicklas 1 package net.sf.basedb.reggie.script;
3492 18 Sep 15 nicklas 2
3531 05 Oct 15 nicklas 3 import java.io.FileOutputStream;
3492 18 Sep 15 nicklas 4 import java.io.IOException;
3531 05 Oct 15 nicklas 5 import java.io.InputStream;
3531 05 Oct 15 nicklas 6 import java.io.OutputStream;
3492 18 Sep 15 nicklas 7
3492 18 Sep 15 nicklas 8 import net.sf.basedb.core.DbControl;
3531 05 Oct 15 nicklas 9 import net.sf.basedb.core.File;
3531 05 Oct 15 nicklas 10 import net.sf.basedb.core.ItemNotFoundException;
3492 18 Sep 15 nicklas 11 import net.sf.basedb.reggie.Reggie;
3492 18 Sep 15 nicklas 12 import net.sf.basedb.reggie.XmlConfig;
3531 05 Oct 15 nicklas 13 import net.sf.basedb.reggie.dao.Datafiletype;
3492 18 Sep 15 nicklas 14 import net.sf.basedb.reggie.dao.Rawbioassay;
3531 05 Oct 15 nicklas 15 import net.sf.basedb.util.FileUtil;
3492 18 Sep 15 nicklas 16
3492 18 Sep 15 nicklas 17 /**
3492 18 Sep 15 nicklas 18   Helper class for running the 'PilotReport' R script and
3492 18 Sep 15 nicklas 19   generating a PDF document with the plots.
3492 18 Sep 15 nicklas 20   
3492 18 Sep 15 nicklas 21   @author nicklas
3492 18 Sep 15 nicklas 22   @since 3.7
3492 18 Sep 15 nicklas 23 */
3492 18 Sep 15 nicklas 24 public class PilotReport 
3492 18 Sep 15 nicklas 25   extends RScriptDefinition
3492 18 Sep 15 nicklas 26 {
3492 18 Sep 15 nicklas 27
3533 08 Oct 15 nicklas 28   private RFunction pilotReport;
3492 18 Sep 15 nicklas 29   
3492 18 Sep 15 nicklas 30   /**
3492 18 Sep 15 nicklas 31     Create a new PilotReport instance. It will be initialized with default
3492 18 Sep 15 nicklas 32     parameters. Path to R script must be in configuration file at
3506 23 Sep 15 nicklas 33     <cfg>/path. Directory with reference data can optionally be
3533 08 Oct 15 nicklas 34     specified by <cfg>/ref-dir
3533 08 Oct 15 nicklas 35     or it will be assumed that it is found in the 'referenceData' subdirectory 
3533 08 Oct 15 nicklas 36     to the R script.
3492 18 Sep 15 nicklas 37   */
3620 24 Nov 15 nicklas 38   public PilotReport(String cfg, String parameterSet)
3492 18 Sep 15 nicklas 39     throws IOException
3492 18 Sep 15 nicklas 40   {
3492 18 Sep 15 nicklas 41     XmlConfig config = Reggie.getConfig();
3506 23 Sep 15 nicklas 42     // Get and check path to script file
3620 24 Nov 15 nicklas 43     String script_path = config.getRequiredConfig(cfg+"/path", parameterSet);
3506 23 Sep 15 nicklas 44     Reggie.checkFile(script_path, false);
3506 23 Sep 15 nicklas 45     setScript(script_path);
3620 24 Nov 15 nicklas 46     String ref_dir = config.getConfig(cfg+"/ref-dir", parameterSet, getScriptDir() + "/referenceData");
3620 24 Nov 15 nicklas 47     String source_dir = config.getConfig(cfg+"/source-dir", parameterSet, getScriptDir() + "/source");
3492 18 Sep 15 nicklas 48
3492 18 Sep 15 nicklas 49     // Check that files and directories exists
3533 08 Oct 15 nicklas 50     Reggie.checkFile(ref_dir, true);
3533 08 Oct 15 nicklas 51     Reggie.checkFile(source_dir, true);
3492 18 Sep 15 nicklas 52
3533 08 Oct 15 nicklas 53     pilotReport = addFunction("pilotReport");
3533 08 Oct 15 nicklas 54     setDefaultParameters(pilotReport);
3533 08 Oct 15 nicklas 55     pilotReport.setParameter("datadir", "'" + ref_dir + "'");
3533 08 Oct 15 nicklas 56     pilotReport.setParameter("sourcedir", "'" + source_dir + "'");
3492 18 Sep 15 nicklas 57   }
3492 18 Sep 15 nicklas 58
3533 08 Oct 15 nicklas 59   private void setDefaultParameters(RFunction f)
3533 08 Oct 15 nicklas 60   {}
3492 18 Sep 15 nicklas 61   
3492 18 Sep 15 nicklas 62   /**
3492 18 Sep 15 nicklas 63     Run the gene report script for the given raw bioassay. It will calculate
3492 18 Sep 15 nicklas 64     the sum of fpkm for the specified genes and then submit those values to 
3492 18 Sep 15 nicklas 65     the R script.
3492 18 Sep 15 nicklas 66     @see Rawbioassay#getFpkmSum(DbControl, java.util.Collection)
3492 18 Sep 15 nicklas 67   */
3492 18 Sep 15 nicklas 68   public Result run(DbControl dc, Rawbioassay raw)
3492 18 Sep 15 nicklas 69   {
3531 05 Oct 15 nicklas 70     File fpkmFile = raw.getFile(dc, Datafiletype.FPKM);
3531 05 Oct 15 nicklas 71     if (fpkmFile == null)
3531 05 Oct 15 nicklas 72     {
3531 05 Oct 15 nicklas 73       throw new ItemNotFoundException(Datafiletype.FPKM.getName() + " for raw bioassay " + raw.getName());
3531 05 Oct 15 nicklas 74     }
3492 18 Sep 15 nicklas 75
6036 02 Nov 20 nicklas 76     pilotReport.setParameter("cufflinksfile", "'./"+fpkmFile.getName()+"'");
3531 05 Oct 15 nicklas 77     
3533 08 Oct 15 nicklas 78     Result result = run(new Result(raw, fpkmFile));
3492 18 Sep 15 nicklas 79     return result;
3492 18 Sep 15 nicklas 80   }
3492 18 Sep 15 nicklas 81   
3492 18 Sep 15 nicklas 82   public class Result
6036 02 Nov 20 nicklas 83     extends ScriptResult
3492 18 Sep 15 nicklas 84   {
3506 23 Sep 15 nicklas 85     public final Rawbioassay raw;
3531 05 Oct 15 nicklas 86     public final File fpkmFile;
3506 23 Sep 15 nicklas 87     
3492 18 Sep 15 nicklas 88     /**
3492 18 Sep 15 nicklas 89       Creates a new result object for the given raw bioassay.
3492 18 Sep 15 nicklas 90     */
3533 08 Oct 15 nicklas 91     public Result(Rawbioassay raw, File fpkmFile) 
3492 18 Sep 15 nicklas 92     {
3492 18 Sep 15 nicklas 93       super();
3492 18 Sep 15 nicklas 94       this.raw = raw;
3531 05 Oct 15 nicklas 95       this.fpkmFile = fpkmFile;
3492 18 Sep 15 nicklas 96     }
3531 05 Oct 15 nicklas 97
3531 05 Oct 15 nicklas 98     @Override
3531 05 Oct 15 nicklas 99     protected void loadFilesToWorkDir() 
3531 05 Oct 15 nicklas 100       throws IOException 
3531 05 Oct 15 nicklas 101     {
3531 05 Oct 15 nicklas 102       InputStream in = null;
3531 05 Oct 15 nicklas 103       OutputStream out = null;
3531 05 Oct 15 nicklas 104       try
3531 05 Oct 15 nicklas 105       {
3531 05 Oct 15 nicklas 106         in = fpkmFile.getDownloadStream(0);
3531 05 Oct 15 nicklas 107         out = new FileOutputStream(new java.io.File(getWorkDir(), fpkmFile.getName()));
3531 05 Oct 15 nicklas 108         FileUtil.copy(in, out);
3531 05 Oct 15 nicklas 109       }
3531 05 Oct 15 nicklas 110       finally
3531 05 Oct 15 nicklas 111       {
3531 05 Oct 15 nicklas 112         FileUtil.close(in);
3531 05 Oct 15 nicklas 113         FileUtil.close(out);
3531 05 Oct 15 nicklas 114       }
3531 05 Oct 15 nicklas 115     }
3492 18 Sep 15 nicklas 116     
3492 18 Sep 15 nicklas 117   }
3492 18 Sep 15 nicklas 118 }