poniedziałek, 26 września 2011

How to count number of molecules in chemical file?

How to count number of molecules in chemical file (mol2, sdf, smi or mol) in bash?


#!/bin/bash

echo
echo "Count number of molecules in a file"
echo

[[ -n "$1" ]] || { echo; echo "Usage:"; 
echo "$0 chemical_database.ext";
exit 0 ; }

ext=${1#*.}

echo -n "Number of molecules in $1 file: "

case $ext in
"sdf" )
        cat $1 | grep "ISIS" | wc -l
        ;;
"smi" )
        cat $1 | wc -l
        ;;
"mol2" )
        cat $1 | grep '@MOLECULE' | wc -l
        ;;
"mol" )
        cat $1 | grep "M  END" | wc -l
        ;;
* )
        echo "Extension not supported :("
        ;;
esac


EDIT 2016.10: there is much easier way to do it:
obabel "filename.sdf" -onul --count 2>&1 | grep "molecules converted" | cut -d" " -f 1

Brak komentarzy:

Prześlij komentarz