0

I have files in the xyz file format like this:

   55
FINAL HEAT OF FORMATION =     0.000000
 C    -1.602726     0.220926     0.289897
 C    -1.486393     1.490851    -0.581098
 C    -0.269002     2.434576    -0.276060
 C     1.010307     1.687714     0.217781
 C     1.485345     0.603160    -0.764139
 C    -1.564938     1.114872    -2.078306
 O    -2.879135     0.437518    -2.475109
 C    -0.550397     3.726131     0.624425
 C    -1.962009     3.939190     1.255790
 C    -2.367687     2.809316     2.219183
 C     0.020100     4.998947    -0.121715
 C    -0.978418     5.719489    -1.074614
 C    -1.616282     4.795344    -2.118148
 C     2.215398     2.612417     0.464811
 C     0.729644     5.994046     0.844547
 C     2.143005     6.393766     0.406166
 C    -2.045078     5.240181     2.079386
 C    -0.323618     6.897509    -1.813043
 C    -1.401212     2.359346    -2.899572
 H    -2.385338     2.081960    -0.396254
 H     0.010153     2.832497    -1.252999
 H     0.084959     3.605123     1.504509
 H     0.809530     4.617245    -0.774572
 H     0.128704     6.897394     0.976132
 H     0.798102     5.548850     1.839117
 H     2.585871     7.101059     1.112504
 H     2.797551     5.521179     0.355908
 H     2.147260     6.862273    -0.578875
 H    -1.790477     6.132728    -0.470526
 H    -1.045932     7.372355    -2.481810
 H     0.046563     7.666682    -1.135188
 H     0.516095     6.553772    -2.424710
 H    -2.319681     5.356939    -2.738366
 H    -0.857441     4.374340    -2.783635
 H    -2.163844     3.970151    -1.668818
 H    -2.716285     4.004456     0.465662
 H    -3.243256     3.107916     2.800711
 H    -2.619420     1.880831     1.722495
 H    -1.559685     2.604091     2.928503
 H    -3.049833     5.345765     2.495339
 H    -1.345140     5.209610     2.919004
 H    -1.835946     6.139276     1.507938
 H     0.771267     1.206903     1.173131
 H     3.062594     2.025264     0.827489
 H     2.528865     3.099255    -0.462839
 H     2.024108     3.390184     1.201447
 H     2.402236     0.135542    -0.396501
 H     0.758861    -0.189092    -0.920031
 H     1.710054     1.046723    -1.738905
 H    -1.261942     0.377911     1.311479
 H    -2.639613    -0.116300     0.341732
 H    -1.021606    -0.605553    -0.121261
 H    -1.454026     2.110341    -3.938854
 H    -2.181199     3.050993    -2.658440
 H    -0.451621     2.804428    -2.687258

I have this code that converts .xyz-file to molecule input format:

CARBONS=$(grep -ow "C" $1 | wc -l)
HYDROGENS=$(grep -ow "H" $1 | wc -l)
OXYGENS=$(grep -ow "O" $1 | wc -l)

ATYPES=0
ARRAY=($CARBONS $HYDROGENS $OXYGENS)

for i in "${ARRAY[@]}"
do
        if [ $i -gt 0 ]; then
                ((ATYPES+=1))
        fi
done

echo "BASIS"
echo "co2"
echo ""
echo ""
echo "Atomtypes="$ATYPES" Generators=0 Integrals=1.00D-15 Angstrom"
echo "Charge=6.0 Atoms="$CARBONS""
grep "C" $1
echo "Charge=1.0 Atoms="$HYDROGENS""
grep "H" $1
if [ $OXYGENS -gt 0 ]; then
    echo "Charge=8.0 Atoms="$OXYGENS""
    grep "O" $1
fi

The output is:

BASIS
co2


Atomtypes=3 Generators=0 Integrals=1.00D-15 Angstrom
Charge=6.0 Atoms=18
 C    -1.602726     0.220926     0.289897
 C    -1.486393     1.490851    -0.581098
 C    -0.269002     2.434576    -0.276060
 C     1.010307     1.687714     0.217781
 C     1.485345     0.603160    -0.764139
 C    -1.564938     1.114872    -2.078306
 C    -0.550397     3.726131     0.624425
 C    -1.962009     3.939190     1.255790
 C    -2.367687     2.809316     2.219183
 C     0.020100     4.998947    -0.121715
 C    -0.978418     5.719489    -1.074614
 C    -1.616282     4.795344    -2.118148
 C     2.215398     2.612417     0.464811
 C     0.729644     5.994046     0.844547
 C     2.143005     6.393766     0.406166
 C    -2.045078     5.240181     2.079386
 C    -0.323618     6.897509    -1.813043
 C    -1.401212     2.359346    -2.899572
Charge=1.0 Atoms=36
FINAL HEAT OF FORMATION =     0.000000
 H    -2.385338     2.081960    -0.396254
 H     0.010153     2.832497    -1.252999
 H     0.084959     3.605123     1.504509
 H     0.809530     4.617245    -0.774572
 H     0.128704     6.897394     0.976132
 H     0.798102     5.548850     1.839117
 H     2.585871     7.101059     1.112504
 H     2.797551     5.521179     0.355908
 H     2.147260     6.862273    -0.578875
 H    -1.790477     6.132728    -0.470526
 H    -1.045932     7.372355    -2.481810
 H     0.046563     7.666682    -1.135188
 H     0.516095     6.553772    -2.424710
 H    -2.319681     5.356939    -2.738366
 H    -0.857441     4.374340    -2.783635
 H    -2.163844     3.970151    -1.668818
 H    -2.716285     4.004456     0.465662
 H    -3.243256     3.107916     2.800711
 H    -2.619420     1.880831     1.722495
 H    -1.559685     2.604091     2.928503
 H    -3.049833     5.345765     2.495339
 H    -1.345140     5.209610     2.919004
 H    -1.835946     6.139276     1.507938
 H     0.771267     1.206903     1.173131
 H     3.062594     2.025264     0.827489
 H     2.528865     3.099255    -0.462839
 H     2.024108     3.390184     1.201447
 H     2.402236     0.135542    -0.396501
 H     0.758861    -0.189092    -0.920031
 H     1.710054     1.046723    -1.738905
 H    -1.261942     0.377911     1.311479
 H    -2.639613    -0.116300     0.341732
 H    -1.021606    -0.605553    -0.121261
 H    -1.454026     2.110341    -3.938854
 H    -2.181199     3.050993    -2.658440
 H    -0.451621     2.804428    -2.687258
Charge=8.0 Atoms=1
FINAL HEAT OF FORMATION =     0.000000
 O    -2.879135     0.437518    -2.475109

But there shouldn't be these lines FINAL HEAT OF FORMATION = 0.000000. I guess the grep command takes them there because there are words that begin with letters H and O. The correct output is:

BASIS
co2


Atomtypes=3 Generators=0 Integrals=1.00D-15 Angstrom
Charge=6.0 Atoms=18
 C    -1.602726     0.220926     0.289897
 C    -1.486393     1.490851    -0.581098
 C    -0.269002     2.434576    -0.276060
 C     1.010307     1.687714     0.217781
 C     1.485345     0.603160    -0.764139
 C    -1.564938     1.114872    -2.078306
 C    -0.550397     3.726131     0.624425
 C    -1.962009     3.939190     1.255790
 C    -2.367687     2.809316     2.219183
 C     0.020100     4.998947    -0.121715
 C    -0.978418     5.719489    -1.074614
 C    -1.616282     4.795344    -2.118148
 C     2.215398     2.612417     0.464811
 C     0.729644     5.994046     0.844547
 C     2.143005     6.393766     0.406166
 C    -2.045078     5.240181     2.079386
 C    -0.323618     6.897509    -1.813043
 C    -1.401212     2.359346    -2.899572
Charge=1.0 Atoms=36
 H    -2.385338     2.081960    -0.396254
 H     0.010153     2.832497    -1.252999
 H     0.084959     3.605123     1.504509
 H     0.809530     4.617245    -0.774572
 H     0.128704     6.897394     0.976132
 H     0.798102     5.548850     1.839117
 H     2.585871     7.101059     1.112504
 H     2.797551     5.521179     0.355908
 H     2.147260     6.862273    -0.578875
 H    -1.790477     6.132728    -0.470526
 H    -1.045932     7.372355    -2.481810
 H     0.046563     7.666682    -1.135188
 H     0.516095     6.553772    -2.424710
 H    -2.319681     5.356939    -2.738366
 H    -0.857441     4.374340    -2.783635
 H    -2.163844     3.970151    -1.668818
 H    -2.716285     4.004456     0.465662
 H    -3.243256     3.107916     2.800711
 H    -2.619420     1.880831     1.722495
 H    -1.559685     2.604091     2.928503
 H    -3.049833     5.345765     2.495339
 H    -1.345140     5.209610     2.919004
 H    -1.835946     6.139276     1.507938
 H     0.771267     1.206903     1.173131
 H     3.062594     2.025264     0.827489
 H     2.528865     3.099255    -0.462839
 H     2.024108     3.390184     1.201447
 H     2.402236     0.135542    -0.396501
 H     0.758861    -0.189092    -0.920031
 H     1.710054     1.046723    -1.738905
 H    -1.261942     0.377911     1.311479
 H    -2.639613    -0.116300     0.341732
 H    -1.021606    -0.605553    -0.121261
 H    -1.454026     2.110341    -3.938854
 H    -2.181199     3.050993    -2.658440
 H    -0.451621     2.804428    -2.687258
Charge=8.0 Atoms=1
 O    -2.879135     0.437518    -2.475109

I tried changing the grep commands to grep -w "^C" $1 and grep -x "C" $1 but neither of these help. How can I fix this?

1
  • 1
    what output format is that? it looks very similar to dalmol. Are you aware of the Open Babel project, which has an obabel program for converting between chemistry file formats?
    – cas
    Jul 22, 2021 at 11:56

2 Answers 2

2

^C doesn't work because the C is not at the start of your input lines, there's a space before it. grep '^ C' "$1" should do what you want.

(By the way, instead of grep | wc -l you can use grep -c. Oh, and the quoting in your echo lines is kind of weird, you can simply have the variables inside the quotes.)

1

Try doing it entirely in awk. e.g. the following script uses two arrays, atoms to remember every input line for a given atom, and count to keep a count of each atom. Once it has read the entire input file, it outputs the data in the format you want.

/^ [[:alpha:]]/ {
  if (count[$1] == 0) {
    atoms[$1]=$0;
  } else {
    atoms[$1]=atoms[$1] "\n" $0;
  }
    count[$1]++
}

END {
  atypes = length(count);

  print "BASIS\nco2\n\n"

  print "Atomtypes=" atypes " Generators=0 Integrals=1.00D-15 Angstrom"

  print "Charge=6.0 Atoms=" count["C"]
  print atoms["C"]

  print "Charge=1.0 Atoms=" count["H"]
  print atoms["H"]

  if (count["O"] > 0) {
    print "Charge=8.0 Atoms=" count["O"]
    print atoms["O"]
  }
}

This could be greatly improved and turned into a generic conversion script if it had a lookup table for the charges associated with each atom.... but there's no need. A conversion utility already exists. It's called obabel from Open Babel: The Open Source Chemistry Toolbox project, and can convert between many chemistry file formats, e.g. to convert from xyz to dalmol:

obabel -i xyz input.xyz -o dalmol -O output.dalmol

Run olabel -L formats | less for a full list of supported formats.

If you're running Debian or a Debian-deriviative like Ubuntu, Mint, etc, you can install it apt-get install openbabel. The package description says:

Package: openbabel
Version: 3.1.1+dfsg-6
Installed-Size: 630
Maintainer: Debichem Team <[email protected]>
Architecture: amd64
Depends: libc6 (>= 2.14), libgcc-s1 (>= 3.0), libopenbabel7 (>= 3.1.1+dfsg), libstdc++6 (>= 5.2)
Description-en: Chemical toolbox utilities (cli)
 Open Babel is a chemical toolbox designed to speak the many languages of
 chemical data. It allows one to search, convert, analyze, or store data from
 molecular modeling, chemistry, solid-state materials, biochemistry, or related
 areas.  Features include:
 .
  * Hydrogen addition and deletion
  * Support for Molecular Mechanics
  * Support for SMARTS molecular matching syntax
  * Automatic feature perception (rings, bonds, hybridization, aromaticity)
  * Flexible atom typer and perception of multiple bonds from atomic coordinates
  * Gasteiger-Marsili partial charge calculation
 .
 File formats Open Babel supports include PDB, XYZ, CIF, CML, SMILES, MDL
 Molfile, ChemDraw, Gaussian, GAMESS, MOPAC and MPQC.
 .
 This package includes the following utilities:
  * obabel: Convert between various chemical file formats
  * obenergy: Calculate the energy for a molecule
  * obminimize: Optimize the geometry, minimize the energy for a molecule
  * obgrep: Molecular search program using SMARTS pattern
  * obgen: Generate 3D coordinates for a molecule
  * obprop: Print standard molecular properties
  * obfit: Superimpose two molecules based on a pattern
  * obrotamer: Generate conformer/rotamer coordinates
  * obconformer: Generate low-energy conformers
  * obchiral: Print molecular chirality information
  * obrotate: Rotate dihedral angle of molecules in batch mode
  * obprobe: Create electrostatic probe grid

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .