Strony

Pokazywanie postów oznaczonych etykietą openbabel. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą openbabel. Pokaż wszystkie posty

czwartek, 10 lipca 2014

How to split any chemical file to single files with file names = compounds' titles

Here is a simple python script for splitting (almost) any chemical multi-structures file (mol2, sdf, smi...) to single ones, with compounds names as new file names. It uses pybel - an openbabel python interface.

Usage:
split_mol.py -i big.mol2

The code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
import os
import getopt
import pybel
from random import randint


### Options

errMsg = "Invalid operation specified:\t%s\nAllowed options are:\n\t-i\tinput file\n"

options, arguments = getopt.getopt( sys.argv[1:], 'i:' )
inputFile = ''

for opt in options:
 if opt[0] == '-i':
  inputFile = opt[1]

  
if not os.path.exists( inputFile ):
 print ( errMsg % "No such file.\n" )
 sys.exit( -1 )

### output names and dirs

basename = os.path.splitext(os.path.basename(inputFile))[0]
ext = os.path.splitext(os.path.basename(inputFile))[1][1:]

outDir = basename + ".splitted"
i = 0

if not os.path.exists(outDir):
    os.makedirs(outDir)

### Splitting

for mol in pybel.readfile(ext, inputFile):
  if mol.title == "":
    i += 1
    title = "%s_%09d" % (basename, i)
  else:
    title = mol.title

  print "%30s" % title,
  
  outFile = "%s/%s.%s" % (outDir, title, ext)
  
  if os.path.exists( outFile ): # if desired file exists
    outFile = "%s/%s_dup%03d.%s" % (outDir, title, randint(0,1000), ext) # attach _dup suffix with 3-digit random number
    print " duplicated: written to file: ", outFile
  else:
    print " ok"

  mol.write(ext, outFile)

wtorek, 10 czerwca 2014

Notes on Open Babel compiling and local install...

Installing openbabel locally sometimes is not as straightforward as it could be. Here are some notes on compiling it on the system without root permissions.

Compiling OB from sources


Fetching / updating sources from git repositories. Please notice the patching eem charges model with eem_fix.patch (optional).

#!/bin/bash

# for the first time use:
#mkdir -p openbabel
# cd openbabel
#git clone git://github.com/openbabel/openbabel.git

cd openbabel
git pull
cd ..

# optional for patching EEM
# echo patching....
# patch openbabel/src/charges/eem.cpp < eem_fix.patch

if [ -d build ]; then
    echo "********* Please delete build dir"
    exit
fi

mkdir build
cd build
cmake ../openbabel -DPYTHON_BINDINGS=ON -DRUN_SWIG=ON \
    -DPYTHON_INCLUDE_DIR=/usr/include/python2.6 \
    -DPYTHON_LIBRARY=/usr/lib/python2.6/config/libpython2.6.so \
    -DCMAKE_INSTALL_PREFIX=~/bin/openbabel-install

make -j14

make test

After successful compilation you should have a complete openbabel structure under your bin/openbabel-install:

~/bin/openbabel-install/
   |-bin
   |-include
   |---inchi
   |---openbabel-2.0
   |-----openbabel
   |-------json
   |-------math
   |-------stereo
   |-lib
   |---cmake
   |-----openbabel2
   |---openbabel
   |-----2.3.90
   |---pkgconfig
   |---python2.6
   |-----site-packages
   |-share
   |---man
   |-----man1
   |---openbabel
   |-----2.3.90


Compiling align-it


Fetch the newest version of silicos align-it, extract it to directory of your choice (eg. align-it-1.0.4). Then you can use following script to setup environmental variables and compile the program:

#!/bin/bash

export BABEL_LIBDIR=/home/fstefaniak/bin/openbabel-install/lib/ 
export BABEL_INCLUDEDIR=/home/fstefaniak/bin/openbabel-install/include/openbabel-2.0/openbabel/
export BABEL_DATADIR=/home/fstefaniak/bin/openbabel-install/share/openbabel/2.3.90/

cd align-it-1.0.4

mkdir build
cd build
cmake ..
make


To be continued


Some more notes


Update 2017/12/01


compiling the older version of OpenBabel (2.3.1), I get the error

Scanning dependencies of target openbabel
[  0%] Building CXX object src/CMakeFiles/openbabel.dir/alias.o
In file included from /home/fstefaniak/sources/openbabel/openbabel-2.3.1/include/openbabel/alias.h:19:0,
                 from /home/fstefaniak/sources/openbabel/openbabel-2.3.1/src/alias.cpp:17:
/home/fstefaniak/sources/openbabel/openbabel-2.3.1/include/openbabel/shared_ptr.h:27:14: error: ‘std::tr1’ has not been declared
   using std::tr1::shared_ptr;
              ^
src/CMakeFiles/openbabel.dir/build.make:62: recipe for target 'src/CMakeFiles/openbabel.dir/alias.o' failed
make[2]: *** [src/CMakeFiles/openbabel.dir/alias.o] Error 1
CMakeFiles/Makefile2:1232: recipe for target 'src/CMakeFiles/openbabel.dir/all' failed
make[1]: *** [src/CMakeFiles/openbabel.dir/all] Error 2
Makefile:138: recipe for target 'all' failed
make: *** [all] Error 2

Thanks to https://diane-be-unique.com/2016/10/19/openbabel-error-in-installing/ the solution is: in openbabel-2.3.1/include/openbabel/shared_ptr.h add:

#include <tr1/memory>

piątek, 28 września 2012

Open Babel with progress bar

I've tried to evaluate progress of Open Babel conversion from sdf (or mol/mol2 etc) to smi. First I've used bar for this, worked fine, but slightly different that I've expected:
obabel in.sdf -osmi | bar -of out.smi
Output was:
44.9KB at   11.2KB/s  elapsed:   0:00:04
only cnversion rate and time elapsed... Then I found a pv program, which can be configured to show the regular progress bar with some additional information (ETA, conversion rate etc):
obabel in.sdf -osmi | pv -lperat -s `count_mol.sh in.sdf` > out.smi
(this script uses count_mol.sh script I've mentioned in one of my previous posts.) The output from this command looks similar to:
0:00:01 [ 267/s] [ 267/s] [====>                   ]  9% ETA 0:00:09
and it does everything I need (ie progress and ETA).

piątek, 4 listopada 2011

OpenBabel compiling error

Frome time to time, during OpenBabel compilation, a strange error occurs:
cc1plus: warning: command line option '-Wstrict-prototypes'
 is valid for Ada/C/ObjC but not for C++ [enabled by default]
In file included from /root/OPENBABEL/openbabel/scripts/python/
openbabel-python.cpp:3358:0:
/root/OPENBABEL/openbabel/scripts/python/../../
include/openbabel/math/align.h:26:22: fatal error: Eigen/Core: No such file or directory
compilation terminated.
error: command 'gcc' failed with exit status 1
make[2]: *** [scripts/CMakeFiles/_openbabel] Error 1
make[1]: *** [scripts/CMakeFiles/_openbabel.dir/all] Error 2
make: *** [all] Error 2
Ant one may assume, that something is wrong in include/openbabel/math/align.h file. To be precise, there is a wrong path to the Eigen/Core files. To fix this problem, you have to open include/openbabel/math/align.h in the source directory, and change the line:
#include <Eigen/Core>
to
#include <your_path_to_Eigen_Core>
eg:
#include </root/OPENBABEL/eigen/eigen-eigen-2.0.16/Eigen/Core>
good luck!