Package simulation

Source Code of simulation.Dad

package simulation;

import java.util.Map;
import java.util.Vector;

import logs.Logs;
import miscellaneous.DirectoryManager;
import miscellaneous.FileUtility;
import miscellaneous.LSVList;
import miscellaneous.StatisticsUtility;

import org.apache.commons.math.MathException;

import reaction.Item;
import reaction.Reaction;
import recorders.Recorder;
import rtools.R;
import rtools.ScriptRepository;
import vsv.Cell;
import vsv.VSVModel;

public class Tester {
 
  public static double templates = 1, transcripts = 0, proteins = 0, polymerases = 2000, ribosomes = 2;
  public static double kms = 0.00001;
  public static double ksp = 0.001;
  public static double transcriptDelay = 30, proteinDelay = 0;
 
  public static double tf = 60;
  public static double dt = 8;
  public static double dtRecord = 10;
 
  public static void main(String[] args)
  {
   
    // testSim();
    // testDelayedIntegration(true);
    // testIntegration(false);
   
    // testStringConversion();
    testR();
   
  }
 
  public static void testR()
  {
    // try
    // {
    // ScriptRepository.runSysCommand(new String[] { "R", "CMD", "Rserve",
    // "--no-save" });
    // ScriptRepository.runSysCommand(new String[] { "open",
    // "/Users/jaywarrick/Desktop/JavaModelOutputs/test.pdf;", "open",
    // "/Users/jaywarrick/Desktop/JavaModelOutputs/temp/VSVSim_current.arff"
    // });
    Map<String,String> env = System.getenv();
    for (String s : env.keySet())
    {
      Logs.log(s + ":" + env.get(s), ScriptRepository.class);
    }
    R.eval("x <- 1:10");
    R.eval("ls()");
    R.eval("x <- 1:10");
    R.eval("y <- x");
    // R.eval("library('RWeka')");
    // R.eval("data <- read.arff('/Users/jaywarrick/Desktop/JavaModelOutputs/temp/VSVSim_current.arff')");
    R.eval("png('/Users/jaywarrick/Desktop/JavaModelOutputs/test34.png')");
    R.eval("plot(x,y)");
    R.eval("dev.off()");
    // R.evalAsString("graphics.off()");
    // R.evalAsString("graphics.off()");
    // R.evalAsString("is.element('RWeka', installed.packages()[,1])");
    // R.evalAsString("find.package('RWeka')");
    // R.startPlot("/Users/jaywarrick/Desktop/JavaModelOutputs/test.pdf", 4,
    // 4, 300, 12, R.FONT_HELVETICA, null);
    // R.eval("plot(x,x)");
    // R.evalAsString("graphics.off()");
    // // R.evalAsString("require('RWeka')");
    // R.evalAsString("is.element('reshape', installed.packages()[,1])");
    // R.evalAsString("library('reshape')");
    // R.evalAsString("library('foreign')");
    // R.evalAsString("search()");
    // R.evalAsString("library('Rserve')");
    // R.evalAsString("library('RWeka', quietly=TRUE)");
    // R.serverEval("library('RWeka')");
   
    //
    R.eval("ls()");
   
    // R.load("RWeka");
   
    // REXP result = R.eval("source(" +
    // R.quotedPath("/Users/jaywarrick/Desktop/JavaModelOutputs/plotScript.R")
    // + ")");
    // R.eval("plotScript(" +
    // R.quotedPath("/Users/jaywarrick/Desktop/JavaModelOutputs/temp/JEXData0000000000.arff")
    // + "," +
    // R.quotedPath("/Users/jaywarrick/Desktop/JavaModelOutputs/temp/VSVSim_current.png")
    // + ")");
   
    // R.serverSource("/Users/jaywarrick/Desktop/JavaModelOutputs/temp.R");
    // R.source("/Users/jaywarrick/Desktop/JavaModelOutputs/temp.R");
    // result = R.eval("temp(4)");
    // Double num = result.asDouble();
    // Recorder.log(num.toString(), Tester.class);
    // }
    // catch (REXPMismatchException e1)
    // {
    // // TODO Auto-generated catch block
    // e1.printStackTrace();
    // }
   
  }
 
  public static void testStringConversion()
  {
    // CSVList csv = new CSVList();
    // csv.add("1e-3");
    // csv.add("1e-4");
    // csv.add("1e-5");
    //
    // // Dim dim = new Dim("Values", csv);
    //
    // List<Double> nums = dim.doubleValues();
    // for (Double d : nums)
    // {
    // Recorder.log("" + d, "Tester");
    // }
  }
 
  public static void testDelayedIntegration(boolean plot)
  {
    Vector<Double> resultsTranscripts = new Vector<Double>();
    Vector<Double> resultsProteins = new Vector<Double>();
    Vector<Double> time = new Vector<Double>();
   
    // Initialize Cell Amounts
    Cell cell = new Cell();
    cell.templates.initialize(new Item("Template", templates, 0.0));
    cell.proteins.initialize(new Item("Rib", ribosomes, 0.0));
    cell.proteins.initialize(new Item("Pol", polymerases, 0.0));
   
    Vector<Reaction> rxns = new Vector<Reaction>();
   
    // Add reactions
    TestPolymeraseRxn transcription = new TestPolymeraseRxn(cell.templates, "Template", cell.proteins, "Pol", kms, transcriptDelay, cell.transcripts);
    TestPolymeraseRxn translation = new TestPolymeraseRxn(cell.transcripts, "Template", cell.proteins, "Rib", ksp, proteinDelay, cell.proteins);
    rxns.add(transcription);
    rxns.add(translation);
    LSVList reactions = new LSVList();
    reactions.add("Reactions");
    for (Reaction r : rxns)
    {
      reactions.add(r.toString());
    }
    Recorder.log(reactions.toString(), "Tester");
   
    double t = 0;
    while (t <= tf)
    {
      // Add matured items to the pool
      cell.mature(t);
     
      if(t % dtRecord == 0)
      {
        resultsTranscripts.add(cell.transcripts.getCount("Template"));
        resultsProteins.add(cell.proteins.getCount("Template"));
        time.add(t);
      }
     
      // calculate rates based on each reaction in the simulation
      for (Reaction rxn : rxns)
      {
        rxn.calculateBaseRate();
        rxn.applyStoichAndRate();
      }
      cell.simpleLinearIntegrate(dt, t, 1);
     
      t = t + dt;
    }
   
    if(plot)
    {
      R.eval("x <- 0;");
      R.makeVector("transcripts", resultsTranscripts);
      R.makeVector("proteins", resultsProteins);
      R.makeVector("time", time);
      String plotTranscripts = R.startPlot("pdf", 10, 8, 300, 12, "Helvetica", null);
      R.eval("plot(time, transcripts, type = 'l', col='green')");
      R.endPlot();
      String plotProteins = R.startPlot("pdf", 10, 8, 300, 12, "Helvetica", null);
      R.eval("plot(time, proteins, type = 'l', col='red')");
      R.endPlot();
     
      try
      {
        FileUtility.openFileDefaultApplication(plotProteins);
        FileUtility.openFileDefaultApplication(plotTranscripts);
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
    }
   
  }
 
  public static void testIntegration(boolean plot)
  {
    Vector<Double> resultsTranscripts = new Vector<Double>();
    Vector<Double> resultsProteins = new Vector<Double>();
    Vector<Double> time = new Vector<Double>();
   
    double t = 0;
    while (t <= tf)
    {
      proteins = proteins + ksp * ribosomes * transcripts * dt;
      transcripts = transcripts + kms * polymerases * templates * dt;
      if(t % dtRecord == 0)
      {
        resultsTranscripts.add(transcripts);
        resultsProteins.add(proteins);
        time.add(t);
      }
      t = t + dt;
    }
   
    if(plot)
    {
      R.eval("x <- 0;");
      R.makeVector("transcripts", resultsTranscripts);
      R.makeVector("proteins", resultsProteins);
      R.makeVector("time", time);
      String plotTranscripts = R.startPlot("pdf", 10, 8, 300, 12, "Helvetica", null);
      R.eval("plot(time, transcripts, type = 'l', col='green')");
      R.endPlot();
      String plotProteins = R.startPlot("pdf", 10, 8, 300, 12, "Helvetica", null);
      R.eval("plot(time, proteins, type = 'l', col='red')");
      R.endPlot();
     
      try
      {
        FileUtility.openFileDefaultApplication(plotProteins);
        FileUtility.openFileDefaultApplication(plotTranscripts);
      }
      catch (Exception e)
      {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
 
  public static void testSim()
  {
    DirectoryManager.setHostDirectory("/Users/jaywarrick/Desktop/JavaModelOutputs");
    // DirectoryManager.setHostDirectory("C:\\Users\\ctimm\\Desktop\\JavaModelOutputs");
    VSVModel model = new VSVModel();
    TestSim sim = new TestSim();
    sim.setParams(model, 5000.0, 1.0, 30.0);
    sim.call();
  }
 
  // public static void runSimpleModel()
  // {
  // Simulator sim = new Simulator();
  //
  // // Transcription
  // Reaction r1 = new Reaction(1e-4, 0);
  // r1.addReactant("Pol", 1, 1);
  // r1.addReactant("Gen", 1, 1);
  // r1.addProduct("Transcript", 1, 1000);
  // r1.addProduct("Pol", 1, 1000);
  // r1.addProduct("Gen", 1, 0.0);
  //
  // // Make AntiGen
  // Reaction r2 = new Reaction((1e-6)/2.5, 0);
  // r2.addReactant("Pol", 1, 1);
  // r2.addReactant("Gen", 1, 1);
  // r2.addProduct("AntiGen", 1, 5000);
  // r2.addProduct("Gen", 1, 0);
  // r2.addProduct("Pol", 1, 5000);
  //
  // // Make Gen
  // Reaction r3 = new Reaction(1e-6, 0);
  // r3.addReactant("Pol", 1, 1);
  // r3.addReactant("AntiGen", 1, 1);
  // r3.addProduct("Gen", 1, 5000);
  // r3.addProduct("Pol", 1, 5000);
  // r3.addProduct("AntiGen", 1, 0.0);
  //
  // // Make Protein
  // Reaction r4 = new Reaction(1e-8, 0);
  // r4.addReactant("Rib", 1, 1);
  // r4.addReactant("Transcript", 1, 1);
  // r4.addProduct("Protein", 3, 300);
  // r4.addProduct("Rib", 1, 300.0);
  // r4.addProduct("Transcript", 1, 0.0);
  //
  //
  // // Make Pol
  // Reaction r5 = new Reaction(1e-16, 0);
  // r5.addReactant("Protein", 2, 1);
  // r5.addProduct("Pol", 1, 0);
  //
  // sim.addReaction(r1);
  // sim.addReaction(r2);
  // sim.addReaction(r3);
  // sim.addReaction(r4);
  // sim.addReaction(r5);
  //
  // SimplePool reactantSupply = new SimplePool();
  // reactantSupply.put("Gen", new Item("Gen",1.0));
  // reactantSupply.put("Pol", new Item("Pol",50.0));
  // reactantSupply.put("AntiGen", new Item("AntiGen",0.0));
  // reactantSupply.put("Rib", new Item("Rib",5e6));
  // reactantSupply.put("Protein", new Item("Protein",0));
  // sim.setPool(reactantSupply);
  //
  // sim.simulateReactions(100000, 1, 3600);
  //
  // System.out.print(sim.getResults());
  // }
 
  public static void runTestOfNormalStats()
  {
    try
    {
      Recorder.log("" + StatisticsUtility.normalInverseCDF(StatisticsUtility.normalCDF(0.3, 0.5, 0.25), 0.5, 0.25), Tester.class.getSimpleName());
    }
    catch (MathException e)
    {
      e.printStackTrace();
    }
  }
}

class Dad {
 
  public static String me = "dad";
 
  public void printMe()
  {
    System.out.println(me);
  }
}

class Son extends Dad {
 
  public static String me = "son";
 
  @Override
  public void printMe()
  {
    System.out.println(me);
  }
}
TOP

Related Classes of simulation.Dad

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.