Difference between revisions of "Team:EPF Lausanne/Software"

 
(40 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{:Team:EPF_Lausanne/Test/Top_Nav}}
+
{{:Team:EPF_Lausanne/Top_Nav}}
 
<html>
 
<html>
 
<!--DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"-->
 
<!--DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"-->
Line 113: Line 113:
 
   <div class="container">
 
   <div class="container">
 
     <div class="row">
 
     <div class="row">
       <div class="col-md-12 col-centered">
+
       <div class="col-md-10 col-centered col-md-offset-1">
  
           <table cellspacing='0' style="width:75%">
+
          <p>The following table resumes the programs written by members of our team. All the software is released under the GNU General Public License (<a href="https://gnu.org/licenses/gpl.html"target="blank">GPLv3</a>). This means that the software is free, open source and can be modified and redistributed under the terms of the license.</p>
 +
 
 +
          <p>Original source code is downloadable directly from the following table. Below you can find a brief explanation for each program and the source code.</p>
 +
 
 +
           <table cellspacing='0' style="width:90%">
 
               <thead>
 
               <thead>
 
                   <tr>
 
                   <tr>
 
                       <th>Name</th>
 
                       <th>Name</th>
 
                       <th>Description</th>
 
                       <th>Description</th>
 +
                      <th></th>
 
                   </tr>
 
                   </tr>
 
               </thead>
 
               </thead>
Line 125: Line 130:
 
                   <tr class='clickable-row' data-href='#code2html'>
 
                   <tr class='clickable-row' data-href='#code2html'>
 
                       <td>code2html</td>
 
                       <td>code2html</td>
                       <td>Script creating automatically HTML and CSS code from source files in Python, C++ or BASH.</td>
+
                       <td>Script that automatically generates HTML and CSS code from source files in Python, C++ or BASH.</td>
 +
                      <td><a href="https://static.igem.org/mediawiki/2015/d/db/Code2htm.zip" class="btn btn-default"><span class="glyphicon glyphicon-circle-arrow-down"></span> Download</a></td>
 
                   </tr>
 
                   </tr>
  
                   <tr class="even" class='clickable-row' data-href='#ODEgen'>
+
                   <tr class='clickable-row' data-href='#ODEsolv'>
                       <td>ODEs Generator</td>
+
                       <td>ODE Solver</td>
                       <td>Program generating automatically the ODEs governing our system from the circuit structure.</td>
+
                       <td>Class solving a system of non-linear ODEs given the initial condition.</td>
 +
                      <td><a href="https://static.igem.org/mediawiki/2015/3/3a/Integrator.zip" class="btn btn-default"><span class="glyphicon glyphicon-circle-arrow-down"></span> Download</a></td>
 
                   </tr>
 
                   </tr>
  
 +
                  <tr class="even" class='clickable-row' data-href='#ODEfit'>
 +
                      <td>ODE Fit</td>
 +
                      <td>Class fitting the parameter of a system of ODEs to experimental data.</td>
 +
                      <td><a href="https://static.igem.org/mediawiki/2015/b/bf/Fit.zip" class="btn btn-default"><span class="glyphicon glyphicon-circle-arrow-down"></span> Download</a></td>
 +
                  </tr>
 +
                  <tr class="even" class='clickable-row' data-href='#HumanBlaster'>
 +
                      <td>Human Blaster</td>
 +
                      <td>Script blasting gRNAs versus the human genome.</td>
 +
                      <td><a href="https://static.igem.org/mediawiki/2015/5/5b/EPF_Lausanne_HumanBlaster.zip" class="btn btn-default"><span class="glyphicon glyphicon-circle-arrow-down"></span> Download</a></td>
 +
                  </tr>
 
               </tbody>
 
               </tbody>
 
           </table>
 
           </table>
 +
 +
      </br></br>
  
 
       </div>
 
       </div>
Line 141: Line 160:
 
</div>
 
</div>
  
 +
<div class="container">
 +
  <ul class="nav nav-tabs nav-justified">
 +
    <li class="active"><a data-toggle="tab" href="#code2html">code2html</a></li>
 +
    <li><a data-toggle="tab" href="#ODEsolv">ODE Solver</a></li>
 +
    <li><a data-toggle="tab" href="#ODEfit">ODE Fit</a></li>
 +
    <li><a data-toggle="tab" href="#HB">Human Blaster</a></li>
 +
  </ul>
  
 +
  <div class="tab-content background-section">
 +
    <div id="code2html" class="tab-pane fade in active">
 +
      <div class="row">
 +
        <div class="col-md-10 col-md-offset-1 text-justify">
 +
            <h3>code2html</h3>
  
<a class="anchor" id="top" name='code2html'></a>
+
<p>The following Python script allows to generate HTML (and CSS) code from source files in C++ and Python languages. It is based on <a href="http://pygments.org/" target="blank">Pygment</a>, a Python syntax highlighter. All code in our Wiki is formatted using this script.
<div class="soft-section">
+
  <div class="container">
+
    <div class="row">
+
      <div class="col-md-12 col-centered">
+
        <h2>code2html</h2>
+
        <p>The following Python script allows to generate HTML (and CSS) code from source files in C++ and Python languages. It is based on <a href="http://pygments.org/" target="blank">Pygment</a>, a Python syntax highlighter. All code in our Wiki is formatted using this script.
+
  
        <p>This script accepts two command line arguments: the first argument is the name of the file to convert, the second one (optional) is to ask separate HTML and CSS files.</p>
+
<p>This script accepts two command line arguments: the first argument is the name of the file to convert, the second one (optional) is to ask for separate HTML and CSS files.</p>
  
 +
<p>The style is hard coded, but it can be changed easily by modifying the style string. <a href="http://pygments.org/docs/styles/" target="blank">Pygment</a> documentation lists available themes and explains how to create new ones.</p>
 +
</br>
 
<div class="highlight"><pre><span class="sd">&quot;&quot;&quot;</span>
 
<div class="highlight"><pre><span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">Copyright (C) 2015 iGEM Team EPF_Lausanne</span>
 +
 +
<span class="sd">This program is free software: you can redistribute it and/or modify</span>
 +
<span class="sd">it under the terms of the GNU General Public License as published by</span>
 +
<span class="sd">the Free Software Foundation, either version 3 of the License, or</span>
 +
<span class="sd">(at your option) any later version.</span>
 +
 +
<span class="sd">This program is distributed in the hope that it will be useful,</span>
 +
<span class="sd">but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
 +
<span class="sd">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
 +
<span class="sd">GNU General Public License for more details.</span>
 +
 +
<span class="sd">You should have received a copy of the GNU General Public License</span>
 +
<span class="sd">along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;</span>
 +
 +
 +
 
<span class="sd">Command:</span>
 
<span class="sd">Command:</span>
  
Line 318: Line 362:
 
</pre></div>
 
</pre></div>
  
         <center>
+
         </div>
          <div>
+
            <a href="https://static.igem.org/mediawiki/2015/d/db/Code2htm.zip" class="btn btn-default"><span class="glyphicon glyphicon-circle-arrow-down"></span> Download</a>
+
          </div>
+
        </center>
+
 
       </div>
 
       </div>
 
     </div>
 
     </div>
  </div>
 
</div>
 
  
 +
    <div id="ODEsolv" class="tab-pane fade">
 +
      <div class="row">
 +
        <div class="col-md-10 col-md-offset-1 text-justify">
 +
            <h3>ODE Solver</h3>
 +
            <p>Our kinetic model leads to a system of coupled fist-order, nonlinear, ordinary differential equations (ODEs). In order to solve this system we used an explicit Runge-Kutta method of order 4(5) with adaptative step size control and dense output due to Dormand and Prince, implemented by E. Hairer and G. Wanner [1] in the <a href="https://www.scipy.org/" target="blank">SciPy</a> Python library. To facilitate the use of this integrator, we created an utility class which is suited for our needs.</p>
  
<a class="anchor" id="top" name='ODEgen'></a>
+
            <p>The Solver class needs the function defining the system of ODEs we want to solve, an initial condition and  the interval on which we want to integrate. Note that the time step \(\Delta t\) (which is also an argument of the constructor of the Solver class) is not the discretization step, because our algorithm is adaptative: \(\Delta t\) is the maximal allowed step and define the points where the solution of the ODEs system will be computed. </p>
<div class="soft-section">
+
         </br>
  <div class="container">
+
    <div class="row">
+
      <div class="col-md-12 col-centered">
+
         <h2>ODE Generator</h2>
+
  
 +
<div class="highlight"><pre><span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">Copyright (C) 2015 iGEM Team EPF_Lausanne</span>
 +
 +
<span class="sd">This program is free software: you can redistribute it and/or modify</span>
 +
<span class="sd">it under the terms of the GNU General Public License as published by</span>
 +
<span class="sd">the Free Software Foundation, either version 3 of the License, or</span>
 +
<span class="sd">(at your option) any later version.</span>
 +
 +
<span class="sd">This program is distributed in the hope that it will be useful,</span>
 +
<span class="sd">but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
 +
<span class="sd">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
 +
<span class="sd">GNU General Public License for more details.</span>
 +
 +
<span class="sd">You should have received a copy of the GNU General Public License</span>
 +
<span class="sd">along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;</span>
 +
<span class="sd">&quot;&quot;&quot;</span>
 +
 +
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
 +
<span class="kn">from</span> <span class="nn">scipy.integrate</span> <span class="kn">import</span> <span class="o">*</span>
 +
 +
<span class="k">class</span> <span class="nc">Solver</span><span class="p">:</span>
 +
    <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">    Class that allows the solution of a system of non-linear ODEs. The system is specified by the function fun</span>
 +
 +
<span class="sd">        dy/dt = fun(t,y)</span>
 +
 +
<span class="sd">    where t is a number and y and dy/dt are numpy arrays or lists.</span>
 +
 +
<span class="sd">    The solution is performed with the dopri5 method, an explicit Runge-Kutta method of order (4)5.</span>
 +
<span class="sd">    The method is due to Dormand &amp; Prince, and is implemented by E. Hairer and G. Wanner.</span>
 +
 +
<span class="sd">    See</span>
 +
<span class="sd">        http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.integrate.ode.html</span>
 +
<span class="sd">    for more details.</span>
 +
 +
<span class="sd">    NOTE:</span>
 +
<span class="sd">    Our Solver can take a function of the form</span>
 +
 +
<span class="sd">        f(t,y,pars)</span>
 +
 +
<span class="sd">    where PARS are parameters. PARS can be eventually passed to the constructor of the Solver.</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
 +
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="p">,</span><span class="n">fun</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">,</span><span class="n">pars</span><span class="o">=</span><span class="p">[]):</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">dt</span> <span class="o">=</span> <span class="n">dt</span> <span class="c"># Time step</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">fun</span> <span class="o">=</span> <span class="n">fun</span> <span class="c"># Function representing the ODE</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">t0</span> <span class="o">=</span> <span class="n">t0</span> <span class="c"># Initial time</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">T</span> <span class="o">=</span> <span class="n">T</span> <span class="c"># Final time</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">y0</span> <span class="o">=</span> <span class="n">y0</span> <span class="c"># Initial condition</span>
 +
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">pars</span> <span class="o">=</span> <span class="n">pars</span> <span class="c"># Parameters of the system of ODEs</span>
 +
 +
    <span class="k">def</span> <span class="nf">solve</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        Solve the system of ODEs</span>
 +
 +
<span class="sd">            dy/dt = fun(t,y)</span>
 +
 +
<span class="sd">        on the interval [t0,T], with the initial condition y(0)=y0.</span>
 +
 +
<span class="sd">        Returns two lists, time and solution, containing time points and the solution at these time points.</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
 +
        <span class="c"># Choose integrator type</span>
 +
        <span class="n">r</span> <span class="o">=</span> <span class="n">ode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fun</span><span class="p">)</span><span class="o">.</span><span class="n">set_integrator</span><span class="p">(</span><span class="s">&#39;dopri5&#39;</span><span class="p">)</span>
 +
 +
        <span class="c"># Initialize the integrator</span>
 +
        <span class="n">r</span><span class="o">.</span><span class="n">set_initial_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">t0</span><span class="p">)</span>
 +
 +
        <span class="c"># Set parameters for the ODE function</span>
 +
        <span class="n">r</span><span class="o">.</span><span class="n">set_f_params</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">pars</span><span class="p">)</span>
 +
 +
        <span class="c"># Initialize solution list and time points list</span>
 +
        <span class="n">solution</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y0</span><span class="p">)</span>
 +
        <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">t0</span><span class="p">)</span>
 +
 +
        <span class="k">while</span> <span class="n">r</span><span class="o">.</span><span class="n">successful</span><span class="p">()</span> <span class="ow">and</span> <span class="n">r</span><span class="o">.</span><span class="n">t</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">T</span><span class="p">:</span>
 +
            <span class="n">r</span><span class="o">.</span><span class="n">integrate</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">t</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">dt</span><span class="p">)</span> <span class="c"># Perform one integration step, i.e. obtain the solution y at time t+dt</span>
 +
 +
            <span class="n">time</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">time</span><span class="p">,</span><span class="n">r</span><span class="o">.</span><span class="n">t</span><span class="p">)</span> <span class="c"># Append the new time</span>
 +
            <span class="n">solution</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">vstack</span><span class="p">((</span><span class="n">solution</span><span class="p">,</span><span class="n">r</span><span class="o">.</span><span class="n">y</span><span class="p">))</span> <span class="c"># Append the new solution</span>
 +
 +
        <span class="k">return</span> <span class="n">time</span><span class="p">,</span> <span class="n">solution</span> <span class="c"># Return time and solution vectors</span>
 +
 +
    <span class="k">def</span> <span class="nf">solve_for_t</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">t</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        Solve the system of ODEs</span>
 +
 +
<span class="sd">            dy/dt = fun(t,y)</span>
 +
 +
<span class="sd">        on the interval [t0,T], with the initial condition y(0)=y0.</span>
 +
 +
<span class="sd">        Returns two lists, solution and time, containing time points and the solution at these time points.</span>
 +
 +
<span class="sd">        The solution is computed at the points specified in t, i.e. the time step dt is ignored.</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
 +
        <span class="c"># Choose integrator type: dopri5 in this case</span>
 +
        <span class="n">r</span> <span class="o">=</span> <span class="n">ode</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">fun</span><span class="p">)</span><span class="o">.</span><span class="n">set_integrator</span><span class="p">(</span><span class="s">&#39;dopri5&#39;</span><span class="p">)</span>
 +
 +
        <span class="c"># Initialize the integrator</span>
 +
        <span class="n">r</span><span class="o">.</span><span class="n">set_initial_value</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y0</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">t0</span><span class="p">)</span>
 +
 +
        <span class="c"># Set parameters for the ODE function</span>
 +
        <span class="n">r</span><span class="o">.</span><span class="n">set_f_params</span><span class="p">(</span><span class="o">*</span><span class="bp">self</span><span class="o">.</span><span class="n">pars</span><span class="p">)</span>
 +
 +
        <span class="c"># Initialize solution list and time points list</span>
 +
        <span class="n">solution</span> <span class="o">=</span> <span class="p">[]</span>
 +
        <span class="n">time</span> <span class="o">=</span> <span class="p">[]</span>
 +
 +
        <span class="k">for</span> <span class="n">tt</span> <span class="ow">in</span> <span class="n">t</span><span class="p">:</span>
 +
            <span class="n">r</span><span class="o">.</span><span class="n">integrate</span><span class="p">(</span><span class="n">tt</span><span class="p">)</span> <span class="c"># Perform one integration step</span>
 +
 +
            <span class="n">time</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tt</span><span class="p">)</span> <span class="c"># Append the new time</span>
 +
            <span class="n">solution</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span><span class="o">.</span><span class="n">y</span><span class="p">)</span> <span class="c"># Append the new solution</span>
 +
 +
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">time</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">solution</span><span class="p">)</span> <span class="c"># Return time and solution vectors</span>
 +
 +
 +
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
 +
    <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">    Our test functions:</span>
 +
<span class="sd">      rapid_equilibrium (standard function)</span>
 +
<span class="sd">      rapid_equilibrium_from_string() (returns a function compiled from a string)</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
 +
    <span class="kn">import</span> <span class="nn">matplotlib.pylab</span> <span class="kn">as</span> <span class="nn">plt</span>
 +
    <span class="kn">from</span> <span class="nn">test</span> <span class="kn">import</span> <span class="o">*</span> <span class="c"># Import test functions for the ODE integrator</span>
 +
 +
    <span class="n">dt</span> <span class="o">=</span> <span class="mf">0.1</span>
 +
 +
    <span class="n">t0</span> <span class="o">=</span> <span class="mi">0</span>
 +
    <span class="n">T</span> <span class="o">=</span> <span class="mi">100</span>
 +
    <span class="n">y0</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]</span>
 +
 +
    <span class="c"># Store the funtion compiled from a string</span>
 +
    <span class="n">rapid_equilibrium_s</span> <span class="o">=</span> <span class="n">rapid_equilibrium_from_string</span><span class="p">()</span>
 +
 +
    <span class="n">mysolver</span> <span class="o">=</span> <span class="n">Solver</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span><span class="n">rapid_equilibrium</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">)</span>
 +
    <span class="n">mysolver_string</span> <span class="o">=</span> <span class="n">Solver</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span><span class="n">rapid_equilibrium_s</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">)</span>
 +
 +
    <span class="n">t</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="n">mysolver</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
 +
    <span class="n">tt</span><span class="p">,</span><span class="n">yt</span> <span class="o">=</span> <span class="n">mysolver</span><span class="o">.</span><span class="n">solve_for_t</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">linspace</span><span class="p">(</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="mi">10</span><span class="p">))</span>
 +
    <span class="n">ts</span><span class="p">,</span><span class="n">ys</span> <span class="o">=</span> <span class="n">mysolver_string</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
 +
 +
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">y</span><span class="p">)</span>
 +
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">tt</span><span class="p">,</span><span class="n">yt</span><span class="p">,</span><span class="s">&#39;x&#39;</span><span class="p">)</span>
 +
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">ts</span><span class="p">,</span><span class="n">ys</span><span class="p">)</span>
 +
    <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
 +
</pre></div>
 +
 +
 +
 +
            <h4>References</h4>
 +
            [1] E. Hairer et al., Solving Ordinary Differential Equations, 2nd edition, Springer-Verlag, 1993.
 +
        </div>
 
       </div>
 
       </div>
 
     </div>
 
     </div>
 +
 +
    <div id="ODEfit" class="tab-pane fade">
 +
      <div class="row">
 +
        <div class="col-md-10 col-md-offset-1 text-justify">
 +
            <h3>ODE Fit</h3>
 +
            <p>The system of first-order, nonlinear, ordinary differential equations (ODEs) which represents the kinetic model of our system contains a vast variety of parameters. The majority of them has been found on the literature, while the others have simply been estimated. Some parameters, however, are so specific to our system that need to be fitted to experimental data.</p>
 +
 +
            <p>Fitting parameters of a system of ODEs to experimental curves is a non-trivial task: the system of ODEs need to be solved for each value of the parameters during the optimization. In order to solve the system of ODEs for a fixed set of parameters we used our ODE Solver. Parameter optimization is performed by minimizing the square difference between experimental points and the numerical solution.</p>
 +
 +
            <p>Our ODEFit class needs the function defining the system of ODEs we want to optimize, experimental data and initial guess for the initial conditions and for the parameters. Note that in order to have a general code, initial conditions are also free parameters since experimental data does not allow for their exact determination.</p>
 +
 +
            </br>
 +
 +
 +
<div class="highlight"><pre><span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">Copyright (C) 2015 iGEM Team EPF_Lausanne</span>
 +
 +
<span class="sd">This program is free software: you can redistribute it and/or modify</span>
 +
<span class="sd">it under the terms of the GNU General Public License as published by</span>
 +
<span class="sd">the Free Software Foundation, either version 3 of the License, or</span>
 +
<span class="sd">(at your option) any later version.</span>
 +
 +
<span class="sd">This program is distributed in the hope that it will be useful,</span>
 +
<span class="sd">but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
 +
<span class="sd">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
 +
<span class="sd">GNU General Public License for more details.</span>
 +
 +
<span class="sd">You should have received a copy of the GNU General Public License</span>
 +
<span class="sd">along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;</span>
 +
<span class="sd">&quot;&quot;&quot;</span>
 +
 +
<span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
 +
<span class="kn">import</span> <span class="nn">warnings</span> <span class="kn">as</span> <span class="nn">w</span>
 +
<span class="kn">from</span> <span class="nn">integrator</span> <span class="kn">import</span> <span class="n">Solver</span>
 +
<span class="kn">from</span> <span class="nn">scipy.optimize</span> <span class="kn">import</span> <span class="n">minimize</span>
 +
 +
<span class="k">class</span> <span class="nc">ODEFit</span><span class="p">:</span>
 +
    <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">    Class that allows the fit of the solution (experimental data) of a system of ODEs</span>
 +
 +
<span class="sd">        dy/dt = fun(t,y,pars)</span>
 +
 +
<span class="sd">    where PARS are the parameters to fit.</span>
 +
 +
<span class="sd">    This class relay on the ODE Solver class in order to solve the system of ODEs.</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
 +
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">fun</span><span class="p">,</span><span class="n">t</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">y0</span><span class="p">,</span><span class="n">pars0</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        The constructor need the function FUN defining the system of ODEs.</span>
 +
 +
<span class="sd">        t and y are experimental data (y can contain a list with the data of different species).</span>
 +
 +
<span class="sd">        y0 is the initial guess for the initial conditions.</span>
 +
<span class="sd">        pars0 is the initial guess for the parameters.</span>
 +
 +
 +
<span class="sd">        NOTE:</span>
 +
<span class="sd">        The initial condition y0 is free, i.e. it is a parameter as well.</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
 +
        <span class="c"># Function defining the system of ODEs</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">fun</span> <span class="o">=</span> <span class="n">fun</span>
 +
 +
        <span class="c"># Initial guess for parameters and initial conditions</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">pars0</span> <span class="o">=</span>  <span class="n">pars0</span> <span class="o">+</span> <span class="n">y0</span> <span class="c"># Merge all parameters to optimize in a single list</span>
 +
 +
        <span class="c"># Data to fit (convert to Numpy arrays)</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">t</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
 +
 +
        <span class="c"># Number of ODEs composing the system</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">n</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
 +
 +
        <span class="c"># Fitted parameters (initialized to initial guess)</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">pars</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">pars0</span>
 +
 +
        <span class="c"># Index of separation between real parameters and (free) initial conditions</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">index</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pars</span><span class="p">)</span><span class="o">-</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span>
 +
 +
        <span class="c"># Keep trak of FIT routine call (if True, self.pars contains the optimized parameters)</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">fitted</span> <span class="o">=</span> <span class="bp">False</span>
 +
 +
    <span class="k">def</span> <span class="nf">_solve_for_pars</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">pars</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        Solve the system of ODEs for a given set of parameters.</span>
 +
 +
<span class="sd">        Solution is computed at experimental data points.</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
 +
        <span class="c"># pars[:self.index] : fun parameters</span>
 +
        <span class="c"># pars[self.index:] : inital conditions (last entries)</span>
 +
        <span class="n">mysolver</span> <span class="o">=</span> <span class="n">Solver</span><span class="p">(</span><span class="mf">0.1</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">fun</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span><span class="n">pars</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">:],</span><span class="n">pars</span><span class="p">[:</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">])</span>
 +
 +
        <span class="c"># Solve the system of ODEs at experimental data points</span>
 +
        <span class="n">t</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="n">mysolver</span><span class="o">.</span><span class="n">solve_for_t</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">)</span>
 +
 +
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">t</span><span class="p">),</span><span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
 +
 +
    <span class="k">def</span> <span class="nf">_resid</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">y_model</span><span class="p">,</span><span class="n">y_data</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        Compute the square of the difference between the ODE solution and the data to fit.</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
        <span class="k">return</span> <span class="p">((</span><span class="n">y_model</span><span class="o">-</span><span class="n">y_data</span><span class="p">)</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">sum</span><span class="p">()</span>
 +
 +
    <span class="k">def</span> <span class="nf">_score</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">pars</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        The score is defined as the sum of residuals for each ODE composing the system.</span>
 +
 +
<span class="sd">        The score function is minimized in order to optimize the parameters (least squares of residuals).</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
 +
        <span class="c"># Solve the system of ODEs for a given set of parameters</span>
 +
        <span class="n">t</span><span class="p">,</span><span class="n">y_model</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_solve_for_pars</span><span class="p">(</span><span class="n">pars</span><span class="p">)</span>
 +
 +
        <span class="n">score</span> <span class="o">=</span> <span class="mi">0</span>
 +
 +
        <span class="c"># Compute the score of the current set of parameters</span>
 +
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">n</span><span class="p">):</span>
 +
            <span class="n">score</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_resid</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[:,</span><span class="n">i</span><span class="p">],</span><span class="n">y_model</span><span class="p">[:,</span><span class="n">i</span><span class="p">])</span> <span class="c"># The score is the sum of residuals for each ODE</span>
 +
 +
        <span class="k">return</span> <span class="n">score</span>
 +
 +
    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        Fit data in order to obtain the best parameter estimation.</span>
 +
 +
<span class="sd">        The best parameter set is obtained by minimizing the score function, i.e. the sum of residuals.</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
 +
        <span class="n">opt</span> <span class="o">=</span> <span class="n">minimize</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_score</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">pars0</span><span class="p">)</span>
 +
 +
        <span class="c"># Extract the optimized parameters</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">pars</span> <span class="o">=</span> <span class="n">opt</span><span class="o">.</span><span class="n">x</span>
 +
 +
        <span class="c"># The fit function has been called</span>
 +
        <span class="bp">self</span><span class="o">.</span><span class="n">fitted</span> <span class="o">=</span> <span class="bp">True</span>
 +
 +
        <span class="k">if</span> <span class="n">opt</span><span class="o">.</span><span class="n">success</span> <span class="o">==</span> <span class="bp">True</span><span class="p">:</span> <span class="c"># Check the success of the minimization process</span>
 +
            <span class="k">return</span> <span class="n">opt</span><span class="o">.</span><span class="n">x</span><span class="p">[:</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">],</span> <span class="n">opt</span><span class="o">.</span><span class="n">x</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">:]</span>
 +
        <span class="k">else</span><span class="p">:</span>
 +
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="n">opt</span><span class="o">.</span><span class="n">message</span><span class="p">)</span>
 +
 +
    <span class="k">def</span> <span class="nf">solve_fitted</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dt</span><span class="o">=</span><span class="mf">0.1</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        Solve the system of ODEs with the optimal parameters with a dense output.</span>
 +
 +
<span class="sd">        dt is the discretization step, which can be changed manually.</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
 +
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">fitted</span> <span class="o">==</span> <span class="bp">False</span><span class="p">:</span> <span class="c"># Warning: the FIT function has never been called</span>
 +
            <span class="n">w</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s">&quot;Warning: model not fitted, parameters are the initial ones.&quot;</span><span class="p">)</span>
 +
 +
        <span class="c"># Solve for the optimal set of parameters</span>
 +
        <span class="n">mysolver</span> <span class="o">=</span> <span class="n">Solver</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">fun</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">t</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">],</span><span class="bp">self</span><span class="o">.</span><span class="n">pars</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">:],</span><span class="bp">self</span><span class="o">.</span><span class="n">pars</span><span class="p">[:</span><span class="bp">self</span><span class="o">.</span><span class="n">index</span><span class="p">])</span>
 +
 +
        <span class="c"># Solve for a dense grid, not for data points</span>
 +
        <span class="k">return</span> <span class="n">mysolver</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
 +
 +
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;__main__&quot;</span><span class="p">:</span>
 +
    <span class="kn">from</span> <span class="nn">integrator</span> <span class="kn">import</span> <span class="o">*</span>
 +
    <span class="kn">from</span> <span class="nn">test</span> <span class="kn">import</span> <span class="o">*</span>
 +
    <span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
 +
    <span class="kn">from</span> <span class="nn">numpy.random</span> <span class="kn">import</span> <span class="n">randn</span>
 +
    <span class="kn">import</span> <span class="nn">matplotlib.pylab</span> <span class="kn">as</span> <span class="nn">plt</span>
 +
 +
    <span class="k">def</span> <span class="nf">randomize</span><span class="p">(</span><span class="n">lst</span><span class="p">,</span><span class="n">scale</span><span class="o">=</span><span class="mf">100.</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        Randomize the data contained in lst (displacement along y axis)</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">lst</span><span class="p">)):</span>
 +
            <span class="n">lst</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="n">randn</span><span class="p">()</span> <span class="o">/</span> <span class="n">scale</span>
 +
 +
        <span class="k">return</span> <span class="n">lst</span>
 +
 +
    <span class="k">def</span> <span class="nf">random_data</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span><span class="n">fun</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">,</span><span class="n">sel</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
 +
        <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">        Create random data for the system of ODEs</span>
 +
<span class="sd">        &quot;&quot;&quot;</span>
 +
 +
        <span class="n">mysolver</span> <span class="o">=</span> <span class="n">Solver</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span><span class="n">fun</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">)</span>
 +
 +
        <span class="c"># Solve the system of ODEs</span>
 +
        <span class="n">t</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="n">mysolver</span><span class="o">.</span><span class="n">solve</span><span class="p">()</span>
 +
 +
        <span class="n">t_stripped</span> <span class="o">=</span> <span class="p">[]</span>
 +
        <span class="n">y_stripped</span> <span class="o">=</span> <span class="p">[]</span>
 +
 +
        <span class="c"># Select one out of SEL points from the solution</span>
 +
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">y</span><span class="p">)):</span>
 +
            <span class="k">if</span> <span class="n">i</span> <span class="o">%</span> <span class="n">sel</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
 +
                <span class="n">t_stripped</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">t</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
 +
                <span class="n">y_stripped</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">y</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
 +
 +
        <span class="n">y_random</span> <span class="o">=</span> <span class="p">[]</span>
 +
 +
        <span class="c"># Randomize the stripped set of data points (mimiks experimental noise)</span>
 +
        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">y_stripped</span><span class="p">:</span>
 +
            <span class="n">y_random</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">randomize</span><span class="p">(</span><span class="n">i</span><span class="p">))</span>
 +
 +
        <span class="k">return</span> <span class="n">t_stripped</span><span class="p">,</span> <span class="n">y_random</span>
 +
 +
    <span class="c"># Test function representing the system of ODEs</span>
 +
    <span class="n">test_function</span> <span class="o">=</span> <span class="n">rapid_equilibrium</span>
 +
 +
    <span class="n">dt</span> <span class="o">=</span> <span class="mf">0.1</span>
 +
 +
    <span class="n">t0</span> <span class="o">=</span> <span class="mi">0</span>
 +
    <span class="n">T</span> <span class="o">=</span> <span class="mi">100</span>
 +
    <span class="n">y0</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">0</span><span class="p">]</span>
 +
 +
    <span class="c"># Creates noisy data</span>
 +
    <span class="n">t</span><span class="p">,</span><span class="n">y</span> <span class="o">=</span> <span class="n">random_data</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span><span class="n">test_function</span><span class="p">,</span><span class="n">t0</span><span class="p">,</span><span class="n">T</span><span class="p">,</span><span class="n">y0</span><span class="p">)</span>
 +
 +
    <span class="n">y</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
 +
 +
    <span class="c"># Initial guess for the parameters</span>
 +
    <span class="n">pars0</span> <span class="o">=</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mf">0.01</span><span class="p">]</span>
 +
 +
    <span class="c"># Crate a ODEFit object: data points are the noisy solution</span>
 +
    <span class="n">myfit</span> <span class="o">=</span> <span class="n">ODEFit</span><span class="p">(</span><span class="n">test_function</span><span class="p">,</span><span class="n">t</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">y0</span><span class="p">,</span><span class="n">pars0</span><span class="p">)</span>
 +
 +
    <span class="c"># Fit noisy data points</span>
 +
    <span class="n">pars</span> <span class="o">=</span> <span class="n">myfit</span><span class="o">.</span><span class="n">fit</span><span class="p">()[</span><span class="mi">0</span><span class="p">]</span> <span class="c"># Extracts only the real parameters and not initial conditions</span>
 +
 +
    <span class="c"># Print parameters</span>
 +
    <span class="k">print</span><span class="p">(</span><span class="n">pars</span><span class="p">)</span>
 +
 +
    <span class="c"># Solve the system of ODEs using optimal parameters</span>
 +
    <span class="n">tt</span><span class="p">,</span><span class="n">yy</span> <span class="o">=</span> <span class="n">myfit</span><span class="o">.</span><span class="n">solve_fitted</span><span class="p">()</span>
 +
 +
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="s">&#39;x&#39;</span><span class="p">)</span> <span class="c"># Plot noisy data</span>
 +
    <span class="n">plt</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">tt</span><span class="p">,</span><span class="n">yy</span><span class="p">)</span> <span class="c"># Plot fiting function</span>
 +
    <span class="n">plt</span><span class="o">.</span><span class="n">show</span><span class="p">()</span>
 +
</pre></div>
 +
 +
 +
        </div>
 +
      </div>
 +
    </div>
 +
 +
 +
    <div id="HB" class="tab-pane fade">
 +
      <div class="row">
 +
        <div class="col-md-10 col-md-offset-1 text-justify">
 +
            <h3>Human Blaster</h3>
 +
 +
            <p>The Human blaster allows the user to check if a gRNA complementary sequence is present in the human genome.</p>
 +
 +
            <p>This program can help iGEM teams that seek to implement the use of gRNAs in their project.</p>
 +
 +
            </br>
 +
 +
<div class="highlight"><pre><span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">Copyright (C) 2015 iGEM Team EPF_Lausanne</span>
 +
 +
<span class="sd">This program is free software: you can redistribute it and/or modify</span>
 +
<span class="sd">it under the terms of the GNU General Public License as published by</span>
 +
<span class="sd">the Free Software Foundation, either version 3 of the License, or</span>
 +
<span class="sd">(at your option) any later version.</span>
 +
 +
<span class="sd">This program is distributed in the hope that it will be useful,</span>
 +
<span class="sd">but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
 +
<span class="sd">MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
 +
<span class="sd">GNU General Public License for more details.</span>
 +
 +
<span class="sd">You should have received a copy of the GNU General Public License</span>
 +
<span class="sd">along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;</span>
 +
 +
 +
 +
<span class="sd">HumanBlaster.py -- blasts gRNAs against human genome</span>
 +
 +
<span class="sd">Requirements :</span>
 +
<span class="sd">    - Install selenium on your computer:</span>
 +
<span class="sd">        http://selenium-python.readthedocs.org/en/latest/installation.html</span>
 +
<span class="sd">    - Save the gRNAs in a file named gRNAs like this : name:sequence</span>
 +
<span class="sd">&quot;&quot;&quot;</span>
 +
 +
<span class="kn">from</span> <span class="nn">selenium</span> <span class="kn">import</span> <span class="n">webdriver</span>
 +
<span class="kn">from</span> <span class="nn">selenium.webdriver.common.keys</span> <span class="kn">import</span> <span class="n">Keys</span>
 +
<span class="kn">from</span> <span class="nn">selenium.webdriver.support.ui</span> <span class="kn">import</span> <span class="n">Select</span>
 +
<span class="kn">from</span> <span class="nn">selenium.webdriver.common.by</span> <span class="kn">import</span> <span class="n">By</span>
 +
<span class="kn">from</span> <span class="nn">selenium.webdriver.support.ui</span> <span class="kn">import</span> <span class="n">WebDriverWait</span>
 +
<span class="kn">from</span> <span class="nn">selenium.webdriver.support</span> <span class="kn">import</span> <span class="n">expected_conditions</span> <span class="k">as</span> <span class="n">EC</span>
 +
<span class="kn">import</span> <span class="nn">os</span>
 +
 +
<span class="k">def</span> <span class="nf">skip</span><span class="p">(</span><span class="n">inp</span><span class="p">,</span> <span class="n">nb</span><span class="p">):</span>
 +
    <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">    Skip NB lines on INP file.</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
    <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">nb</span><span class="p">):</span>
 +
        <span class="n">inp</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
 +
 +
<span class="k">def</span> <span class="nf">seqReader</span><span class="p">(</span><span class="n">inp</span><span class="p">,</span> <span class="n">initSkip</span><span class="p">):</span>
 +
    <span class="sd">&quot;&quot;&quot;</span>
 +
<span class="sd">    Analyse the blast sequence.</span>
 +
 +
<span class="sd">    This function will display the percentage of similarity with a sequence in</span>
 +
<span class="sd">    the human genome.</span>
 +
 +
<span class="sd">    You can change the sensibility of the script by changing the conditions</span>
 +
<span class="sd">        cond = (int(k[6]) == 1 and int(k[7]) &gt;= 23)</span>
 +
<span class="sd">        cond = (int(k[7]) == 23 and int(k[6]) &lt;= 1)</span>
 +
<span class="sd">    &quot;&quot;&quot;</span>
 +
 +
    <span class="n">skip</span><span class="p">(</span><span class="n">inp</span><span class="p">,</span> <span class="n">initSkip</span><span class="p">)</span> <span class="c"># Skip initial lines</span>
 +
 +
    <span class="n">line</span> <span class="o">=</span> <span class="n">inp</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
 +
 +
    <span class="n">name</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;:&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">][</span><span class="mi">1</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span>
 +
 +
    <span class="n">skip</span><span class="p">(</span><span class="n">inp</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span> <span class="c"># Skip four lines</span>
 +
 +
    <span class="n">l</span> <span class="o">=</span> <span class="n">inp</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
 +
 +
    <span class="n">war</span> <span class="o">=</span> <span class="mi">0</span>
 +
    <span class="k">while</span> <span class="n">l</span> <span class="o">!=</span> <span class="s">&quot;&quot;</span> <span class="ow">and</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">!=</span> <span class="s">&quot;#&quot;</span><span class="p">:</span>
 +
        <span class="n">k</span> <span class="o">=</span> <span class="n">l</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;</span><span class="se">\t</span><span class="s">&quot;</span><span class="p">)</span>
 +
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="o">&lt;</span> <span class="mi">7</span><span class="p">:</span>
 +
            <span class="k">return</span> <span class="s">&quot;&quot;</span>
 +
        <span class="k">if</span> <span class="p">(</span><span class="n">name</span><span class="p">[:</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;CC&quot;</span><span class="p">):</span>
 +
            <span class="n">cond</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="p">[</span><span class="mi">6</span><span class="p">])</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="p">[</span><span class="mi">7</span><span class="p">])</span> <span class="o">&gt;=</span> <span class="mi">23</span><span class="p">)</span>
 +
        <span class="k">else</span><span class="p">:</span>
 +
            <span class="n">cond</span> <span class="o">=</span> <span class="p">(</span><span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="p">[</span><span class="mi">7</span><span class="p">])</span> <span class="o">==</span> <span class="mi">23</span> <span class="ow">and</span> <span class="nb">int</span><span class="p">(</span><span class="n">k</span><span class="p">[</span><span class="mi">6</span><span class="p">])</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">)</span>
 +
 +
        <span class="k">if</span> <span class="n">cond</span><span class="p">:</span>
 +
            <span class="k">print</span> <span class="p">(</span><span class="n">name</span> <span class="o">+</span> <span class="s">&quot;: &quot;</span> <span class="o">+</span> <span class="n">k</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;|&quot;</span><span class="p">)[</span><span class="mi">3</span><span class="p">]</span> <span class="o">+</span> <span class="s">&quot; with &quot;</span> <span class="o">+</span> <span class="n">k</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="s">&quot;</span><span class="si">% s</span><span class="s">imilarity, &quot;</span> <span class="o">+</span> <span class="n">k</span><span class="p">[</span><span class="mi">4</span><span class="p">]</span> <span class="o">+</span> <span class="s">&quot; mismatches.&quot;</span><span class="p">)</span>
 +
 +
        <span class="n">l</span> <span class="o">=</span> <span class="n">inp</span><span class="o">.</span><span class="n">readline</span><span class="p">()</span>
 +
 +
    <span class="k">return</span> <span class="n">l</span>
 +
 +
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&quot;gRNAs&quot;</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">input1</span><span class="p">,</span> <span class="nb">open</span><span class="p">(</span><span class="s">&quot;FASTA_gRNAs&quot;</span><span class="p">,</span> <span class="s">&#39;w&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">output</span><span class="p">:</span>
 +
    <span class="n">text</span> <span class="o">=</span> <span class="s">&quot;&quot;</span>
 +
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">input1</span><span class="p">:</span>
 +
        <span class="n">name</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;:&quot;</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span>
 +
        <span class="n">seq</span> <span class="o">=</span> <span class="n">i</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s">&quot;:&quot;</span><span class="p">)[</span><span class="mi">1</span><span class="p">][:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
 +
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;_TGG&quot;</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;TGG</span><span class="se">\n</span><span class="s">&quot;</span>
 +
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;_GGG&quot;</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;GGG</span><span class="se">\n</span><span class="s">&quot;</span>
 +
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;_AGG&quot;</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;AGG</span><span class="se">\n</span><span class="s">&quot;</span>
 +
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;_CGG&quot;</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;CGG</span><span class="se">\n</span><span class="s">&quot;</span>
 +
        <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;_TGG&quot;</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;TGG</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
 +
        <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;_GGG&quot;</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;GGG</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
 +
        <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;_AGG&quot;</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;AGG</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
 +
        <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;_CGG&quot;</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;CGG</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
 +
 +
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="s">&quot;CCG_&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&quot;CCG&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span>
 +
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="s">&quot;CCC_&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&quot;CCC&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span>
 +
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="s">&quot;CCA_&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&quot;CCA&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span>
 +
        <span class="n">text</span> <span class="o">+=</span> <span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="s">&quot;CCT_&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&quot;CCT&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span>
 +
        <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="s">&quot;CCG_&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&quot;CCG&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
 +
        <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="s">&quot;CCC_&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&quot;CCC&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
 +
        <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="s">&quot;CCA_&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&quot;CCA&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
 +
        <span class="n">output</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;&gt;&quot;</span> <span class="o">+</span> <span class="s">&quot;CCT_&quot;</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span> <span class="o">+</span> <span class="s">&quot;CCT&quot;</span> <span class="o">+</span> <span class="n">seq</span> <span class="o">+</span> <span class="s">&quot;</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">)</span>
 +
 +
 +
 +
    <span class="n">driver</span> <span class="o">=</span> <span class="n">webdriver</span><span class="o">.</span><span class="n">Firefox</span><span class="p">()</span> <span class="c"># Will open Firefox</span>
 +
    <span class="n">driver</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&quot;http://blast.ncbi.nlm.nih.gov/Blast.cgi?PROGRAM=blastn&amp;PAGE_TYPE=BlastSearch&amp;LINK_LOC=blasthome&quot;</span><span class="p">)</span> <span class="c">#Address</span>
 +
    <span class="k">assert</span> <span class="s">&quot;Nucleotide BLAST: Search nucleotide databases using a nucleotide query&quot;</span> <span class="ow">in</span> <span class="n">driver</span><span class="o">.</span><span class="n">title</span>
 +
    <span class="n">elem</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s">&quot;seq&quot;</span><span class="p">)</span> <span class="c">#Selects the place where the sequences will be written</span>
 +
    <span class="n">elem</span><span class="o">.</span><span class="n">send_keys</span><span class="p">(</span><span class="n">text</span><span class="p">)</span> <span class="c">#Writes them</span>
 +
    <span class="n">elem</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s">&quot;qtitle&quot;</span><span class="p">)</span>
 +
    <span class="n">elem</span><span class="o">.</span><span class="n">send_keys</span><span class="p">(</span><span class="s">&quot;Our Blast&quot;</span><span class="p">)</span> <span class="c">#Change here if you want to have another name for the BLAST</span>
 +
    <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s">&quot;Rhc&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">click</span><span class="p">()</span> <span class="c">#Chooses human genome</span>
 +
    <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s">&quot;b1&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">click</span><span class="p">()</span> <span class="c">#Starts the BLAST</span>
 +
    <span class="c">#Will wait 500000 seconds or until the download page is ready</span>
 +
    <span class="k">try</span><span class="p">:</span>
 +
        <span class="n">element</span> <span class="o">=</span> <span class="n">WebDriverWait</span><span class="p">(</span><span class="n">driver</span><span class="p">,</span> <span class="mi">500000</span><span class="p">)</span><span class="o">.</span><span class="n">until</span><span class="p">(</span><span class="n">EC</span><span class="o">.</span><span class="n">presence_of_element_located</span><span class="p">((</span><span class="n">By</span><span class="o">.</span><span class="n">XPATH</span><span class="p">,</span> <span class="s">&quot;/html/body/div[1]/div[2]/div[3]/div[2]/form/table/tbody/tr[2]/td[1]/div[2]/a[5]&quot;</span><span class="p">)))</span>
 +
    <span class="k">finally</span><span class="p">:</span>
 +
        <span class="n">driver</span><span class="o">.</span><span class="n">close</span><span class="p">()</span> <span class="c">#Will leave if it takes too long (13 hours max, should be enough)</span>
 +
    <span class="n">download</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s">&quot;showDownload&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">click</span><span class="p">()</span> <span class="c">#Will start the download</span>
 +
    <span class="n">download</span> <span class="o">=</span> <span class="n">driver</span><span class="o">.</span><span class="n">find_element_by_id</span><span class="p">(</span><span class="s">&quot;hitText&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">click</span><span class="p">()</span>
 +
 +
<span class="k">print</span><span class="p">(</span><span class="s">&quot;You need to save the file in this folder using the name resultat.txt&quot;</span><span class="p">)</span>
 +
<span class="n">continuation</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s">&#39;When you are done press 1</span><span class="se">\n</span><span class="s">&#39;</span><span class="p">)</span>
 +
 +
<span class="k">if</span> <span class="n">continuation</span><span class="p">:</span>
 +
    <span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&quot;resultat.txt&quot;</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">inp</span><span class="p">:</span>
 +
        <span class="n">l</span> <span class="o">=</span> <span class="n">seqReader</span><span class="p">(</span><span class="n">inp</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
 +
        <span class="k">while</span> <span class="p">(</span><span class="n">l</span> <span class="o">!=</span> <span class="s">&quot;&quot;</span> <span class="ow">and</span> <span class="n">l</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">==</span> <span class="s">&quot;#&quot;</span><span class="p">):</span>
 +
            <span class="n">l</span> <span class="o">=</span> <span class="n">seqReader</span><span class="p">(</span><span class="n">inp</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
 +
</pre></div>
 +
 +
 +
        </div>
 +
      </div>
 +
    </div>
 +
 
   </div>
 
   </div>
 
</div>
 
</div>
  
<script>
 
jQuery(document).ready(function($) {
 
    $(".clickable-row").click(function() {
 
        window.document.location = $(this).data("href");
 
    });
 
});
 
</script>
 
  
 
</body>
 
</body>
 +
 +
<script src="https://2015.igem.org/common/MathJax-2.5-latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
 +
 
</html>
 
</html>
{{:Team:EPF_Lausanne/Test/footer}}
+
{{:Team:EPF_Lausanne/Footer}}

Latest revision as of 08:38, 17 September 2015

EPFL 2015 iGEM bioLogic Logic Orthogonal gRNA Implemented Circuits EPFL 2015 iGEM bioLogic Logic Orthogonal gRNA Implemented Circuits

Software

The following table resumes the programs written by members of our team. All the software is released under the GNU General Public License (GPLv3). This means that the software is free, open source and can be modified and redistributed under the terms of the license.

Original source code is downloadable directly from the following table. Below you can find a brief explanation for each program and the source code.

Name Description
code2html Script that automatically generates HTML and CSS code from source files in Python, C++ or BASH. Download
ODE Solver Class solving a system of non-linear ODEs given the initial condition. Download
ODE Fit Class fitting the parameter of a system of ODEs to experimental data. Download
Human Blaster Script blasting gRNAs versus the human genome. Download


code2html

The following Python script allows to generate HTML (and CSS) code from source files in C++ and Python languages. It is based on Pygment, a Python syntax highlighter. All code in our Wiki is formatted using this script.

This script accepts two command line arguments: the first argument is the name of the file to convert, the second one (optional) is to ask for separate HTML and CSS files.

The style is hard coded, but it can be changed easily by modifying the style string. Pygment documentation lists available themes and explains how to create new ones.


"""
Copyright (C) 2015 iGEM Team EPF_Lausanne

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>



Command:

######################################

    python code2html INPUTFILE [CSS]

######################################

INPUTFILE: name (with path) of the file to convert to html
CSS: write "true" (ot "t", "yes", "y") in order to obtain separate .html and .css files ("false" by default)
"""

from pygments import highlight
from pygments.lexers import PythonLexer
from pygments.lexers import CppLexer
from pygments.lexers import BashLexer
from pygments.formatters import HtmlFormatter

# Code formatting style
style = "monokai"

# C++ extensions
cpp = ["cpp","cxx","cc","h"]

# Python extensions
py = ["py"]

# Bash extensions
bash = ["sh","bash"]

def load_file_as_sting(fname):
    """
    Open the file FNAME and save all its content in an unformatted string
    """

    content = ""

    with open(fname,'r') as f: # Open the file (read only)
        content = f.read() # Read file and store it in an unformatted string
        # The file is automatically closed

    return content

def save_string_as_file(fname,string):
    """
    Save the unformatted string STRING into the file FNAME
    """

    with open(fname,'w') as f: # Open the file (write only)
        f.write(string)
        # The file is automatically closed

def lexer_formatter(language,css=False):
    """
    Return the lexer for the appropriate language and the HTML formatter
    """

    L = None

    if language in py:
        # Python Lexer
        L = PythonLexer()

    elif language in cpp:
        # C++ Lexer
        L = CppLexer()

    elif language in bash:
        # Bash Lexer
        L = BashLexer()

    else:
        raise NameError("Invalid language.")

    HF = HtmlFormatter(full=not css,style=style)

    return L, HF


def code_to_htmlcss(code,language):
    """
    Transform CODE into html and css (separate files)
    """

    # Obtain lexer and HtmlFormatter
    L, HF = lexer_formatter(language,css=True)

    # Create html code
    html = highlight(code,L,HF)

    # Create css code
    css = HF.get_style_defs('.highlight')

    return html,css

def code_to_html(code,language):
    """
    Transform CODE into html and css (all in the same file)
    """

    # Obtain lexer and HtmlFormatter
    L, HF = lexer_formatter(language)

    # Create fill html code
    html = highlight(code,L,HF)

    return html

import sys

if __name__ == "__main__":
    """
    Command:

    ######################################

        python code2html INPUTFILE [CSS]

    ######################################

    INPUTFILE: name (with path) of the file to convert to html
    CSS: write "true" (ot "t", "yes", "y") in order to obtain separate .html and .css files ("false" by default)
    """

    # Command line arguments
    args = sys.argv

    # Check command line arguments
    ncla = len(args) # number of command line arguments

    if ncla != 2 and ncla != 3 :
        raise TypeError("Invalid number of command line arguments.")

    css_bool = False

    if ncla == 3 and args[-1].lower() in ["true",'t',"yes",'y']:
        css_bool = True # Export css separately

    # Input file
    fname_code = sys.argv[1] # Name of the file containing the code to convert in html

    # Input file extension
    language = fname_code.split('.')[-1]

    # Output files
    fname_html = fname_code.split('.')[0] + ".html" # Name of the file where the html code will be stored
    fname_css = fname_code.split('.')[0] + ".css" # Name of the file where the css code will be stored

    # Save code into a unformatted string
    code = load_file_as_sting(fname_code)

    if css_bool == False: # Convert to standalone html
        html = code_to_html(code,language)
    else: # Convert to html and css separately
        html,css = code_to_htmlcss(code,language)

    # Save html
    save_string_as_file(fname_html,html)

    if css_bool == True:
        # Save css
        save_string_as_file(fname_css,css)

ODE Solver

Our kinetic model leads to a system of coupled fist-order, nonlinear, ordinary differential equations (ODEs). In order to solve this system we used an explicit Runge-Kutta method of order 4(5) with adaptative step size control and dense output due to Dormand and Prince, implemented by E. Hairer and G. Wanner [1] in the SciPy Python library. To facilitate the use of this integrator, we created an utility class which is suited for our needs.

The Solver class needs the function defining the system of ODEs we want to solve, an initial condition and the interval on which we want to integrate. Note that the time step \(\Delta t\) (which is also an argument of the constructor of the Solver class) is not the discretization step, because our algorithm is adaptative: \(\Delta t\) is the maximal allowed step and define the points where the solution of the ODEs system will be computed.


"""
Copyright (C) 2015 iGEM Team EPF_Lausanne

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
"""

import numpy as np
from scipy.integrate import *

class Solver:
    """
    Class that allows the solution of a system of non-linear ODEs. The system is specified by the function fun

        dy/dt = fun(t,y)

    where t is a number and y and dy/dt are numpy arrays or lists.

    The solution is performed with the dopri5 method, an explicit Runge-Kutta method of order (4)5.
    The method is due to Dormand & Prince, and is implemented by E. Hairer and G. Wanner.

    See
        http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.integrate.ode.html
    for more details.

    NOTE:
    Our Solver can take a function of the form

        f(t,y,pars)

    where PARS are parameters. PARS can be eventually passed to the constructor of the Solver.
    """

    def __init__(self,dt,fun,t0,T,y0,pars=[]):
        self.dt = dt # Time step
        self.fun = fun # Function representing the ODE
        self.t0 = t0 # Initial time
        self.T = T # Final time
        self.y0 = y0 # Initial condition

        self.pars = pars # Parameters of the system of ODEs

    def solve(self):
        """
        Solve the system of ODEs

            dy/dt = fun(t,y)

        on the interval [t0,T], with the initial condition y(0)=y0.

        Returns two lists, time and solution, containing time points and the solution at these time points.
        """

        # Choose integrator type
        r = ode(self.fun).set_integrator('dopri5')

        # Initialize the integrator
        r.set_initial_value(self.y0, self.t0)

        # Set parameters for the ODE function
        r.set_f_params(*self.pars)

        # Initialize solution list and time points list
        solution = np.asarray(self.y0)
        time = np.asarray(self.t0)

        while r.successful() and r.t < self.T:
            r.integrate(r.t + self.dt) # Perform one integration step, i.e. obtain the solution y at time t+dt

            time = np.append(time,r.t) # Append the new time
            solution = np.vstack((solution,r.y)) # Append the new solution

        return time, solution # Return time and solution vectors

    def solve_for_t(self,t):
        """
        Solve the system of ODEs

            dy/dt = fun(t,y)

        on the interval [t0,T], with the initial condition y(0)=y0.

        Returns two lists, solution and time, containing time points and the solution at these time points.

        The solution is computed at the points specified in t, i.e. the time step dt is ignored.
        """

        # Choose integrator type: dopri5 in this case
        r = ode(self.fun).set_integrator('dopri5')

        # Initialize the integrator
        r.set_initial_value(self.y0, self.t0)

        # Set parameters for the ODE function
        r.set_f_params(*self.pars)

        # Initialize solution list and time points list
        solution = []
        time = []

        for tt in t:
            r.integrate(tt) # Perform one integration step

            time.append(tt) # Append the new time
            solution.append(r.y) # Append the new solution

        return np.asarray(time), np.asarray(solution) # Return time and solution vectors


if __name__ == "__main__":
    """
    Our test functions:
       rapid_equilibrium (standard function)
       rapid_equilibrium_from_string() (returns a function compiled from a string)
    """

    import matplotlib.pylab as plt
    from test import * # Import test functions for the ODE integrator

    dt = 0.1

    t0 = 0
    T = 100
    y0 = [1,0,0]

    # Store the funtion compiled from a string
    rapid_equilibrium_s = rapid_equilibrium_from_string()

    mysolver = Solver(dt,rapid_equilibrium,t0,T,y0)
    mysolver_string = Solver(dt,rapid_equilibrium_s,t0,T,y0)

    t,y = mysolver.solve()
    tt,yt = mysolver.solve_for_t(np.linspace(t0,T,10))
    ts,ys = mysolver_string.solve()

    plt.plot(t,y)
    plt.plot(tt,yt,'x')
    plt.plot(ts,ys)
    plt.show()

References

[1] E. Hairer et al., Solving Ordinary Differential Equations, 2nd edition, Springer-Verlag, 1993.

ODE Fit

The system of first-order, nonlinear, ordinary differential equations (ODEs) which represents the kinetic model of our system contains a vast variety of parameters. The majority of them has been found on the literature, while the others have simply been estimated. Some parameters, however, are so specific to our system that need to be fitted to experimental data.

Fitting parameters of a system of ODEs to experimental curves is a non-trivial task: the system of ODEs need to be solved for each value of the parameters during the optimization. In order to solve the system of ODEs for a fixed set of parameters we used our ODE Solver. Parameter optimization is performed by minimizing the square difference between experimental points and the numerical solution.

Our ODEFit class needs the function defining the system of ODEs we want to optimize, experimental data and initial guess for the initial conditions and for the parameters. Note that in order to have a general code, initial conditions are also free parameters since experimental data does not allow for their exact determination.


"""
Copyright (C) 2015 iGEM Team EPF_Lausanne

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>
"""

import numpy as np
import warnings as w
from integrator import Solver
from scipy.optimize import minimize

class ODEFit:
    """
    Class that allows the fit of the solution (experimental data) of a system of ODEs

        dy/dt = fun(t,y,pars)

    where PARS are the parameters to fit.

    This class relay on the ODE Solver class in order to solve the system of ODEs.
    """

    def __init__(self,fun,t,y,y0,pars0):
        """
        The constructor need the function FUN defining the system of ODEs.

        t and y are experimental data (y can contain a list with the data of different species).

        y0 is the initial guess for the initial conditions.
        pars0 is the initial guess for the parameters.


        NOTE:
        The initial condition y0 is free, i.e. it is a parameter as well.
        """

        # Function defining the system of ODEs
        self.fun = fun

        # Initial guess for parameters and initial conditions
        self.pars0 =  pars0 + y0 # Merge all parameters to optimize in a single list

        # Data to fit (convert to Numpy arrays)
        self.t = np.asarray(t)
        self.y = np.asarray(y)

        # Number of ODEs composing the system
        self.n = self.y.shape[-1]

        # Fitted parameters (initialized to initial guess)
        self.pars = self.pars0

        # Index of separation between real parameters and (free) initial conditions
        self.index = len(self.pars)-self.n

        # Keep trak of FIT routine call (if True, self.pars contains the optimized parameters)
        self.fitted = False

    def _solve_for_pars(self,pars):
        """
        Solve the system of ODEs for a given set of parameters.

        Solution is computed at experimental data points.
        """

        # pars[:self.index] : fun parameters
        # pars[self.index:] : inital conditions (last entries)
        mysolver = Solver(0.1,self.fun,self.t[0],self.t[-1],pars[self.index:],pars[:self.index])

        # Solve the system of ODEs at experimental data points
        t,y = mysolver.solve_for_t(self.t)

        return np.asarray(t),np.asarray(y)

    def _resid(self,y_model,y_data):
        """
        Compute the square of the difference between the ODE solution and the data to fit.
        """
        return ((y_model-y_data)**2).sum()

    def _score(self,pars):
        """
        The score is defined as the sum of residuals for each ODE composing the system.

        The score function is minimized in order to optimize the parameters (least squares of residuals).
        """

        # Solve the system of ODEs for a given set of parameters
        t,y_model = self._solve_for_pars(pars)

        score = 0

        # Compute the score of the current set of parameters
        for i in range(self.n):
            score += self._resid(self.y[:,i],y_model[:,i]) # The score is the sum of residuals for each ODE

        return score

    def fit(self):
        """
        Fit data in order to obtain the best parameter estimation.

        The best parameter set is obtained by minimizing the score function, i.e. the sum of residuals.
        """

        opt = minimize(self._score,self.pars0)

        # Extract the optimized parameters
        self.pars = opt.x

        # The fit function has been called
        self.fitted = True

        if opt.success == True: # Check the success of the minimization process
            return opt.x[:self.index], opt.x[self.index:]
        else:
            raise RuntimeError(opt.message)

    def solve_fitted(self,dt=0.1):
        """
        Solve the system of ODEs with the optimal parameters with a dense output.

        dt is the discretization step, which can be changed manually.
        """

        if self.fitted == False: # Warning: the FIT function has never been called
            w.warn("Warning: model not fitted, parameters are the initial ones.")

        # Solve for the optimal set of parameters
        mysolver = Solver(dt,self.fun,self.t[0],self.t[-1],self.pars[self.index:],self.pars[:self.index])

        # Solve for a dense grid, not for data points
        return mysolver.solve()

if __name__ == "__main__":
    from integrator import *
    from test import *
    import numpy as np
    from numpy.random import randn
    import matplotlib.pylab as plt

    def randomize(lst,scale=100.):
        """
        Randomize the data contained in lst (displacement along y axis)
        """
        for i in range(len(lst)):
            lst[i] += randn() / scale

        return lst

    def random_data(dt,fun,t0,T,y0,sel=10):
        """
        Create random data for the system of ODEs
        """

        mysolver = Solver(dt,fun,t0,T,y0)

        # Solve the system of ODEs
        t,y = mysolver.solve()

        t_stripped = []
        y_stripped = []

        # Select one out of SEL points from the solution
        for i in range(len(y)):
            if i % sel == 0:
                t_stripped.append(t[i])
                y_stripped.append(y[i])

        y_random = []

        # Randomize the stripped set of data points (mimiks experimental noise)
        for i in y_stripped:
            y_random.append(randomize(i))

        return t_stripped, y_random

    # Test function representing the system of ODEs
    test_function = rapid_equilibrium

    dt = 0.1

    t0 = 0
    T = 100
    y0 = [1,0,0]

    # Creates noisy data
    t,y = random_data(dt,test_function,t0,T,y0)

    y = np.asarray(y)

    # Initial guess for the parameters
    pars0 = [1,0.01]

    # Crate a ODEFit object: data points are the noisy solution
    myfit = ODEFit(test_function,t,y,y0,pars0)

    # Fit noisy data points
    pars = myfit.fit()[0] # Extracts only the real parameters and not initial conditions

    # Print parameters
    print(pars)

    # Solve the system of ODEs using optimal parameters
    tt,yy = myfit.solve_fitted()

    plt.plot(t,y,'x') # Plot noisy data
    plt.plot(tt,yy) # Plot fiting function
    plt.show()

Human Blaster

The Human blaster allows the user to check if a gRNA complementary sequence is present in the human genome.

This program can help iGEM teams that seek to implement the use of gRNAs in their project.


"""
Copyright (C) 2015 iGEM Team EPF_Lausanne

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>



HumanBlaster.py -- blasts gRNAs against human genome

Requirements :
    - Install selenium on your computer:
        http://selenium-python.readthedocs.org/en/latest/installation.html
    - Save the gRNAs in a file named gRNAs like this : name:sequence
"""

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import os

def skip(inp, nb):
    """
    Skip NB lines on INP file.
    """
    for _ in range(nb):
        inp.readline()

def seqReader(inp, initSkip):
    """
    Analyse the blast sequence.

    This function will display the percentage of similarity with a sequence in
    the human genome.

    You can change the sensibility of the script by changing the conditions
        cond = (int(k[6]) == 1 and int(k[7]) >= 23)
        cond = (int(k[7]) == 23 and int(k[6]) <= 1)
    """

    skip(inp, initSkip) # Skip initial lines

    line = inp.readline()

    name = str(line.split(":")[1][1:-1])

    skip(inp, 4) # Skip four lines

    l = inp.readline()

    war = 0
    while l != "" and l[0] != "#":
        k = l.split("\t")
        if len(k) < 7:
            return ""
        if (name[:2] == "CC"):
            cond = (int(k[6]) == 1 and int(k[7]) >= 23)
        else:
            cond = (int(k[7]) == 23 and int(k[6]) <= 1)

        if cond:
            print (name + ": " + k[1].split("|")[3] + " with " + k[2] + "% similarity, " + k[4] + " mismatches.")

        l = inp.readline()

    return l

with open("gRNAs", 'r') as input1, open("FASTA_gRNAs", 'w') as output:
    text = ""
    for i in input1:
        name = i.split(":")[0]
        seq = i.split(":")[1][:-1]
        text += ">" + name + "_TGG" + "\n" + seq + "TGG\n"
        text += ">" + name + "_GGG" + "\n" + seq + "GGG\n"
        text += ">" + name + "_AGG" + "\n" + seq + "AGG\n"
        text += ">" + name + "_CGG" + "\n" + seq + "CGG\n"
        output.write(">" + name + "_TGG" + "\n" + seq + "TGG\n")
        output.write(">" + name + "_GGG" + "\n" + seq + "GGG\n")
        output.write(">" + name + "_AGG" + "\n" + seq + "AGG\n")
        output.write(">" + name + "_CGG" + "\n" + seq + "CGG\n")

        text += ">" + "CCG_" + name + "\n" + "CCG" + seq + "\n"
        text += ">" + "CCC_" + name + "\n" + "CCC" + seq + "\n"
        text += ">" + "CCA_" + name + "\n" + "CCA" + seq + "\n"
        text += ">" + "CCT_" + name + "\n" + "CCT" + seq + "\n"
        output.write(">" + "CCG_" + name + "\n" + "CCG" + seq + "\n")
        output.write(">" + "CCC_" + name + "\n" + "CCC" + seq + "\n")
        output.write(">" + "CCA_" + name + "\n" + "CCA" + seq + "\n")
        output.write(">" + "CCT_" + name + "\n" + "CCT" + seq + "\n")



    driver = webdriver.Firefox() # Will open Firefox
    driver.get("http://blast.ncbi.nlm.nih.gov/Blast.cgi?PROGRAM=blastn&PAGE_TYPE=BlastSearch&LINK_LOC=blasthome") #Address
    assert "Nucleotide BLAST: Search nucleotide databases using a nucleotide query" in driver.title
    elem = driver.find_element_by_id("seq") #Selects the place where the sequences will be written
    elem.send_keys(text) #Writes them
    elem = driver.find_element_by_id("qtitle")
    elem.send_keys("Our Blast") #Change here if you want to have another name for the BLAST
    driver.find_element_by_id("Rhc").click() #Chooses human genome
    driver.find_element_by_id("b1").click() #Starts the BLAST
    #Will wait 500000 seconds or until the download page is ready
    try:
        element = WebDriverWait(driver, 500000).until(EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div[2]/div[3]/div[2]/form/table/tbody/tr[2]/td[1]/div[2]/a[5]")))
    finally:
        driver.close() #Will leave if it takes too long (13 hours max, should be enough)
    download = driver.find_element_by_id("showDownload").click() #Will start the download
    download = driver.find_element_by_id("hitText").click()

print("You need to save the file in this folder using the name resultat.txt")
continuation = input('When you are done press 1\n')

if continuation:
    with open("resultat.txt", 'r') as inp:
        l = seqReader(inp, 2)
        while (l != "" and l[0] == "#"):
            l = seqReader(inp, 1)
EPFL 2015 iGEM bioLogic Logic Orthogonal gRNA Implemented Circuits