#!/bin/bash
#####################################################################
#                                                                   #
#                           MODTNKXYZ                               #
#                                                                   #
#                                                                   #
#                                                                   #
# Date: June 6th 2009						                        #
# Author: Andrea Minoia                                             #
# mail: minoiaa@gmail.com                                           #
#####################################################################
#
#1. Intro
# modtnkxyz is a bash script intended for translate a xyz file into
# tinker xyz file (from now txyz) by using a txyz template. In 
# particular, motnkxyz has been written with a well precise situation in
# mind: generate a new txyz from a txyz that has been reoriented in 
# the space using VMD or any other software that save in xyz coordinates.
# 
# The idea is to load the original txyz in the graphical software to be
# manipulated. The new structure is then saved as xyz and converted in
# txyz by modtnkxyz using the original txy structure as template. From 
# the template, all the information but the coordinates are used to 
# generate the final txyz structure, while atomic cartesian coordinates
# are taken from the xyz file.
#
# _NOTE_: to sucessfully generate the txyz file, the manipulation should
# not change the original structure, i.e. bonds cannot be deleted or 
# created, nor atoms, fragments or molecules should be added or deleted.
#
#2. Use
# Use this script is quite straigthforward. You need the xyz structure of
# the manipulated system and its txyz template. 
# 
# Convert the xyz into txyz with the following command:
# 
# $ modtnkxyz <file1> <file2> output_file
#
# file1 and file2 can be either xyz and txyz or txyz and xyz files, 
# respectively; the output file _must_ be passed to the script as
# last parameter.
#
#3. Troubleshooting
# Despite the use of the script is quite straightforward, some controls
# have been implement to manage most of the possible source of errors.
# When an error occurs, the script will fail promping to the user the 
# appropriate error message.
#
# The most common sources of error are:
# 
# 1) The user has not passed to the script all the parameters (xyz, txyz
#    and output files);
#
# 2) One input file (or both) is not found; 
# 
# 3) xyz and txyz files have not the same number of lines;
#
# 4) xyz and/or txyz have not valid xyz/txyz format.
#
#4. Contacts and disclaimer
# License:
#    Freeware. Use it! Modify it! Spread it around the world!
#      
#Disclaimer:
#    Molgeom is a freeware software, therefore is distributed
#    cames with _ABSOLUTE_ NO WARRANTY.
#    I am not responsible if this software will erase
#    your hard disks, empty your bank account,
#    stoles your car, seduces your wife, shaves
#    your dog or make any kind of mess and damages,
#    including data losts or whatsoever.
#    Use at your own risk!
#
#Contacts:
#    Problems? Suggestions?
#    Drop me a mail at minoiaa_at_gmail.com
############################################################

#remove existing tmp file
if [ -e "modtnkxyz.tmp" ]; then
	rm -f "modtnkxyz.tmp"
fi

#check for parameters
err=0
if [ "$1" = "" ] ; then
  	err=1
fi
if [ "$2" = "" ] ; then
  	err=1
fi
if [ "$3" = "" ] ; then
  	err=1
fi
if [ $err -eq 1 ] ; then
	echo ""
 	echo "Use: modtnkxyz <xyz_file> <tnk_file> <out_file>"
	exit 0
fi

#check if files exist
if [ ! -e $1 ]; then
	echo ""
	echo "Error: $1 not found"
	exit 0
fi
if [ ! -e $2 ]; then
	echo ""
        echo "Error: $2 not found"
        exit 0
fi

#init files
fxyz=$1
fout=$3

#check number of words (i.e. columns) in $1 to check if is xyz file
if [ `sed -n 3p $fxyz | wc -w` -eq 4 ]; then
	ftnk=$2
else
	ftnk=$1
	fxyz=$2
fi

#check if ftnk is tinker format using minimum number of colums required by tinker
#i.e. progressive number, symbol, x-, y-, z-coords, atom type, connectivity (at least 1) 
if [ `sed -n 2p $ftnk | wc -w` -lt 7 ]; then
	echo ""
	echo "Error: $ftnk does not seem to be a tinker file"
	exit 0
fi
#check if fxyz is valid XYZ file
if [ `sed -n 3p $ftnk | wc -w` -eq 4 ]; then
	echo ""
	echo "Error: $fxyz does not seem to be a XYZ file"
	exit 0
fi
#create tmp file from fxyz removing the second line
count=0
while read line; do
	count=$((count+1))
	if [ $count -ne 2 ]; then
		echo $line >> modtnkxyz.tmp
	fi
done < $fxyz
fxyz="modtnkxyz.tmp"

#get number of lines
nlines_file1=`wc -l $fxyz | cut -d' ' -f1`
nlines_file2=`wc -l $ftnk | cut -d' ' -f1`

#create new tinker file
if [ $nlines_file1 -eq $nlines_file2 ]; then
	paste $fxyz $ftnk | awk '{print $5,$6,$2,$3,$4,$10,$11,$12,$13,$14}' > $fout
	rm -f $fxyz
	echo ""
	echo "Done. New tinker file saved in $fout"
else
	echo ""
	echo "Error: found $nlines_file1 lines in $fxyz and $nlines_file2 in $ftnk"
fi
