File: molcif2sdf

package info (click to toggle)
cod-tools 3.7.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 154,792 kB
  • sloc: perl: 57,588; sh: 36,842; ansic: 6,402; xml: 1,982; yacc: 1,117; makefile: 727; python: 166
file content (171 lines) | stat: -rwxr-xr-x 4,858 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#! /bin/sh
#------------------------------------------------------------------------------
#$Author: antanas $
#$Date: 2022-04-20 23:00:32 +0300 (Wed, 20 Apr 2022) $
#$Revision: 9271 $
#$URL: svn+ssh://www.crystallography.net/home/coder/svn-repositories/cod-tools/tags/v3.7.0/scripts/molcif2sdf $
#------------------------------------------------------------------------------
#*
#* Convert CIFs with multiple molecules (similar to those obtained from
#* the 'cif_molecule' script) to an SDF file with a molecule description.
#* Intended to be suitable for upload to PubChem.
#*
#* USAGE:
#*   $0 --options < file1.cif
#*   $0 --options file1.cif
#*   $0 --options file1.cif file2*.cif
#**

TMP_DIR="${TMPDIR}"

set -ue
## set -x

script() { echo "# $*"; cat; }
setvar() { eval $1="'$3'"; }

setvar FILES = ""

setvar BASENAME = "`basename $0`"

setvar COD_CIF = ""

#* OPTIONS:
#*   -C, --cod-cif 1000000.cif
#*                     Provide the original COD CIF to extract structure metadata.
#*
#*   --tmp-dir /tmp
#*                     Use the specified temporary directory (default: '/tmp').
#*
#*   --help, --usage
#*                     Output a short help message (this message) and exit.
#*   --version
#*                     Output version information and exit.
#**
while [ $# -gt 0 ]
do
  case $1 in
      -C|--cod-cif|--cod-ci|--cod-c|--cod|--co|--c)
          COD_CIF="$2"
          shift
          ;;
      --tmp-dir|--tmp-di|--tmp-d|--tmp|--tm|--t)
          TMP_DIR="$2"
          shift
          ;;
      --options|--option|--optio|--opti|--opt|--op|--o)
          echo "$(basename "$0"):: The '--options' option is a placeholder."
          echo "$(basename "$0"):: It should be replaced by one of the following options:"
          awk '/#\* OPTIONS:/,/#\*\*/ {
                  sub("OPTIONS:", "");
                  sub("^ *#[*]?[*]?", "");
                  gsub("\\$0","'"$0"'");
                  print $0
              }' "$0"
          exit
          ;;
      --help|--hel|--he|--h|--usage)
          awk '/#\*/,/#\*\*/ {
                  sub("^ *#[*]?[*]?", "");
                  gsub("\\$0","'"$0"'");
                  print $0
              }' "$0"
          exit
          ;;
      --version)
          $(dirname $0)/cod-tools-version
          exit
          ;;
      -*) echo "`basename $0`:: ERROR, unknown option '$1'." >&2 ; exit 1 ;;
      *)  FILES="$FILES '$1'" ;;
    esac
    shift
done

## echo ${FILES}
eval set -- "${FILES}"

test -z "${TMP_DIR}" && TMP_DIR="/tmp"
TMP_DIR="${TMP_DIR}/tmp-${BASENAME}-$$"
mkdir "${TMP_DIR}"

TMP_SPLIT_DIR="${TMP_DIR}/split"
mkdir ${TMP_SPLIT_DIR}

cat ${1+"$@"} \
| cif_split_primitive --quiet -o ${TMP_SPLIT_DIR}

## ls -l ${TMP_SPLIT_DIR}

cif_values='cif_values --no-header --no-dataname --no-filename --dont-replace-spaces --tags'

## set -x

NMOLS=$(find ${TMP_SPLIT_DIR}/ -name '*.cif' | wc -l)

N=0

for MOL in ${TMP_SPLIT_DIR}/*
do
    (
        cd ${TMP_SPLIT_DIR}
        obabel ---errorlevel 1 -iCIF $(basename $MOL) -o SDF \
            | grep -v '^\$\$\$\$'
    )
    if [ "${COD_CIF}" != "" ]
    then
        CIF="${COD_CIF}"
    else
        CIF="${MOL}"
    fi
    DATABASE_ID=$(${cif_values} _cod_database_code $CIF)
    test "${DATABASE_ID}" = "?" && \
        DATABASE_ID=$(${cif_values} _cod_data_source_block $MOL)
    echo '> <PUBCHEM_EXT_DATASOURCE_REGID>'
    if [ $NMOLS -gt 1 ]
    then
        echo ${DATABASE_ID}_${N}
    else
        echo ${DATABASE_ID}
    fi
    echo ""
    echo '> <PUBCHEM_SUBSTANCE_SYNONYM>'
    (
        ${cif_values} _chemical_name_systematic $CIF
        ${cif_values} _chemical_name_common $CIF
    ) \
    | ( grep -v '^\?' || true ) \
    | perl -0777 -pe 's/[ \t]+/ /g; s/^\s*|\s*$//g'
    echo ""
    echo ""
    echo '> <PUBCHEM_SUBSTANCE_COMMENT>'
    (
        ${cif_values} _publ_author_name --value-separator "; " $CIF
        echo "("$(${cif_values} _journal_year $CIF)")"
        ${cif_values} _publ_section_title $CIF
        ${cif_values} _journal_name_full $CIF
        ${cif_values} _journal_volume $CIF
        ${cif_values} _journal_issue $CIF
        echo $(${cif_values} _journal_page_first $CIF)-$(${cif_values} _journal_page_last $CIF)
    ) \
    | perl -pe 's/-\?//; s/\(\?\)/?/; s/\?-/?/; s/\?+/?/g' \
    | ( grep -v '^\?' || true ) \
    | perl -0777 -pe 's/\n(.)/, $1/g; s/[ \t]+/ /g'
    echo ""
    echo '> <PUBCHEM_EXT_DATASOURCE_URL>'
    echo 'https://www.crystallography.net/cod/'
    echo ""
    echo '> <PUBCHEM_EXT_SUBSTANCE_URL>'
    echo "https://www.crystallography.net/cod/${DATABASE_ID}.html"
    echo ""
    echo '$$$$'
    N=$(expr $N + 1)
done \
| perl -0777 -pe 's/^> <.*?>\n\s*\n//mg' \
| cif-to-utf8 \
| perl -CS -MUnicode::Normalize -pe \
    "# from http://ahinea.com/en/tech/accented-translate.html:
     # 2011.12.10
     \$_ = NFD(\$_); s/\\pM//g;"

rm -rf "${TMP_DIR}"