Donnerstag, 3. Juli 2014

Flask-SQLalchemy

So I am currently trying to set up a small web app using Flask. In order to connect to the MySQL database with some of the data that I want to administer  Flask-SQLAlchemy seemed to be a natural choice as I already had the SQLAlchemy ORM set up for some scripts and only needed to change a few imports.

If I understood correctly, the flask extension provides a few wrappers for querying the database that are supposed to be easier than using the session.

Now here's what I observed:
Depending on what filter-function you are using it is or is not possible to combine multiple filters using logical operators such as or_, and_ etc.

If I only wanted to query a single column in one of my tables called gene1 in may GeneToGene object, the following code does that beautifully:

results = GToG.query.filter_by(gene1 = genesymbol).all()                         

However, when trying to add a second column gene2 to the same filter (with a logical OR) I only get an error message

from flask.ext.sqlalchemy import SQLAlchemy                                      
from sqlalchemy import or_                                                       
results = GToG.query.filter_by(or_(gene1 = genesymbol, gene2 = genesymbol)).all()

TypeError: or_() got an unexpected keyword argument 'gene1'                      

Next try:



results = GToG.query.filter_by(or_(GToG.gene1 == genesymbol,                     
                                    GToG.gene2 == genesymbol)).all()             

resulted in the following error:


TypeError: filter_by() takes exactly 1 argument (2 given)                        

Turns out that only the function filter(), but not filter_by supports logical operators.
This is what finally worked and produced the output I was hoping for.

results = GToG.query.filter(or_(GToG.gene1 == genesymbol,                        
                                GToG.gene2 == genesymbol)).all()                 

Donnerstag, 10. Oktober 2013

Screen

A quick reminder (as can be found plentiful on the web) for the important commands for the tool screen. This tool allows you to start jobs that can be easily detached from the terminal/shell.

Start the screen session:
screen, then enter

After that you can use the session as any other terminal window.
To create new screens:
ctrl + a + c (to browse them use ctrl + a + n / ctrl + a + p)

Detach a screen:
ctrl + a + d

Show screens:
screen -ls

Output of this would be something like:
There are screens on:  
     20751.host  (Detached) 

Reattach a screen:
screen -r <job>

Kill a screen:
screen -r <job>
ctrl + a
quit

Mittwoch, 17. Juli 2013

Open Babel Installation

The installation from source is described with a lot of detail at the openbabel website, however to give a quick reference here are a few cmake parameters that may be of use when building the package:

-DCMAKE_INSTALL_PREFIX=/...
-DPYTHON_BINDINGS=ON
-DEIGEN2_INCLUDE_DIR=<path to eigen2 folder>

Beware: cmake can get confused on your system thus it you should not make the install path the same as the build path, i.e. if you call cmake from within a folder called 'build', don't use the path to this folder for DCMAKE_INSTALL_PREFIX!

Also it may be that cmake does not recognize the correct python compiler. Here it is then useful to also add the following parameters to the cmake command:
-DPYTHON_EXECUTABLE=<path to python executable, e.g. /usr/opt/python-x.y.z/bin/python>
-DPYTHON_LIBRARY=<path to python lib, e.g. python-x.y.z/lib>
-DPYTHON_INCLUDE_DIR=<path to include folder of python, e.g. python-x.y.z/include>

It is noteworthy that for me compiling openbabel with eigen3 did not work out so I had to downgrade to eigen2, then everything worked fine.

The general install procedure is then in the downloaded and unpacked openbabel directory
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=/... -D<other parameters>
make -j8 (-j parameter is used for parallelization of make process)
(sudo) make install

More information about how to use openbabel can be found in the documentation.


Freitag, 7. Juni 2013

Installing pgchem with postgres 9 and openbabel 2.3.2

pgchem is an extension for the postgreSQL database system that allows in-database molecule handling. It was developed by Ernst-Georg Schmid and allows rather quick handling of chemical molecules in a relational database system.
The repository of the developer can be found at github.

Because the code has been developed a few years ago there are some problems if you try to install pgchem with the current (or at least close to current) postgres and openbabel releases.
It took me at least a few weeks and a lot of help from some colleagues at Freiburg university to get a postgres/pgchem system up and running on my local linux x64 machine (Ubuntu 12.10).
In the end it proved to be easier than expected due to the big help of Björn Grüning and Kiran Telukunta who fixed the x64 Makefile and thus allowed the pgchem code to compile on a modern system.
Björn's repository with the updated code can be found here:

http://github.com/bgruening/pgchem_tigress

and they also included a nice shell script that handles the installation of postgres and openbabel as well as the actual pgchem and barsoi installation. It can also be found in Björn's repository (install_debian-ubuntu.sh). The current version of the repo also contains changed SQL function definitions of the pgchem functions because two of them did not work with the new versions of openbabel and pgchem.

However, if you don't want to use this script, you have to do the following:
  1. Install postgres
    1. this could be done from source code or using a packet manager such as apt-get
    2. Create a new user account for your database
  2. Install openbabel (not necessary if you don't want a global ob installation)
    1. If you install it globally, you can also use the command line tools
    2. If you install python bindings as well you can use openbabel/pybel in python scrips
    3. The shared-flag has to be switched on for the libraries to work with pgchem:
  3. Install pgchem
    • Download the package from Björn's gibthub
    • Install the included openbabel version 
    • cmake .. -DBUILD_SHARED=ON -DBUILD_GUI=OFF -DBUILD_TESTING=OFF -DCMAKE_INSTALL_PREFIX=$OB_INSTALL_DIR
    • Install barsoi in the pgchem directory:
      cd barsoi make -f Makefile.linux
    • Now some re-naming has to take place for the makefile to work:
      1. locale.h in openbabel-2.3.2/include/openbabel/locale.h has to be renamed to _locale.h (in the same dir as locale.h) 
      2. In openbabel-2.3.2/build/lib there has to be a symbolic link from inchiformat.so to libinchiformat.so
    • change into the pgchem directory 
    • Have a look at the makefile (only x64 is updated!) and change it if needed (should not be the case)
    • Compilemake -f Makefile.linux.x64
    • Then again some copying needs to be done:
      1. copy libpgchem.so, barsoi/libbarsoi.so, openbabel-2.3.2/build/lib/libinchi.so.0.4.1, openbabel-2.3.2/build/lib/libopenbabel.so.4.0.2, openbabel-2.3.2/build/lib/inchiformat.so to your postgres library directory
      2. copy setup/tigress/obdata/dictionary* to your portgres lib dir at openbabel/share/openbabel/2.3.2
      3. Create the following links in the porstgres lib dir:
        sudo ln -s libinchi.so.0.4.1 libinchi.so.0sudo ln -s libinchi.so.0 libinchi.sosudo ln -s libopenbabel.so.4.0.2 libopenbabel.so.4sudo ln -s libopenbabel.so.4 libopenbabel.sosudo ln -s inchiformat.so libinchiformat.so
    • Because only the openbabel library files are needed you can then remove the bin and include folders from your pgchem openbabel installtion
    • Restart postgres
  4. Maybe you need to copy the content of tigressdata.zip to /usr/local/share
  5. Now you have to import the new function into your postgres
    1. Create a new database, etc.
    2. Run the pgchem_setup.sql script in your database
      In the interactive command prompt you have to use the "\i" command:
      <your_db># \i /$POSTGRES_DIR/contrib/pgchem/setup/pgchem_setup.sql
Examples for how to use pgchem are given in the Documentation that can be accessed from the any of the repositories or found here.
Yay. First post. I'm probably expected to write something like "Hello world" which I've hereby done. Good. Now lets get started with more interesting stuff - at least interesting to me as a poor PhD student struggling with lack of certain up-to-date information on certain bio- and cheminformatics topics on the web.
I will use this blog from now on as an online "brain prosthesis" for all that stuff that has cost me hours of googling. If it is of interest of other like-minded - even better!