#!/usr/bin/perl
#
# mol2tocml.pl
#
# FyD, April 2006, 
# Universite de Picardie - Jules Verne
# http://www.q4md-forcefieldtools.org/
# GPL license
#
# <molecule>
#   <atomArray>
#     <atom id="a1" elementType="O" x3="1.383" y3="3.330" z3="7.669"><scalar dictRef="jmol:charge">-0.682</scalar>
#   </atomArray>
#   <bondArray>
#     <bond atomRefs2="a1 a2" order="1"/>
#   </bondArray>
# </molecule>

use warnings;
# use strict;
use FileHandle;

if (!@ARGV) { @ARGV = <STDIN>; chop(@ARGV); }
$z = 1;
foreach $file (@ARGV)	{
	if (!-f $file)	{ print "Skipping non-regular file: $file\n"; next; }
	if (-B $file)	{ print "Skipping binary file: $file\n"; next; }
	system ("echo \"Name of the file converted = $file\"\n");
	open (TRIPOS,"<$file");
	$i=0;
	foreach (<TRIPOS>){
		if(/\s+(\-\d+|\d+)\.\d+\s+(\-\d+|\d+)\.\d+\s+(\-\d+|\d+)\.\d+\s/){
		#           AtName        Xcoord        Ycoord        Zcoord        Element       Charge
			($T[1][$i][$z],$T[2][$i][$z],$T[3][$i][$z],$T[4][$i][$z],$T[5][$i][$z],$T[6][$i][$z],$T[7][$i][$z],$T[8][$i][$z]) = (split(' '))[1,2,3,4,5,6,7,8];
			$T[0][$i][$z] = $i+1;
			# print "nbA = $T[0][$i][$z]; Atom = $T[1][$i][$z]\n";    # Checking
			$i++;
		}
	}
	$NbA=$i;
	close(TRIPOS);

	open(TRIPOS,"<$file");
	$j=$flag1=$flag2=$flag3=0;
	foreach (<TRIPOS>){
		if(/\@<TRIPOS>BOND/ig)			{ $flag1=1; }
		if(($flag1 == 1) && ($flag2 < 2))	{ $flag2++; }
		if(($flag2 == 2) && ($flag3 == 0)){
			if(/\s+\d+\s+\d+\s+\d+\s+\d+\s/){
				($T[10][$j][$z],$T[11][$j][$z],$T[12][$j][$z]) = (split(' '))[1,2,3];
				# print "nbB = $T[7][$j][$z]\n";    # Checking
				$j++;
			}
			else   { $flag3 = 1; }
			}
	}
	$NbB=$j;
	close(TRIPOS);
	$NbAtom=$NbA-1; $NbBond=$NbB-1;
	open (CML, ">$file.cml");
		printf CML ("<molecule title=\"molecule\" xmlns=\"http://www.xml-cml.org/schema/cml2/core\"
          xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
          xsi:schemaLocation=\"http://www.xml-cml.org/schema/cml2/core cmlAll.xsd\"
          mol2tocml.pl=\"http://www.q4md-forcefieldtools.org/\">\n");
		printf CML ("  <atomArray>\n");
		for ($i=0; $i<=$NbAtom; $i++) { 
			printf (CML "    <atom id=\"$T[7][$i][$z]:$T[6][$i][$z]:$T[1][$i][$z]\" elementType=\"$T[5][$i][$z]\" x3=\"$T[2][$i][$z]\" y3=\"$T[3][$i][$z]\" z3=\"$T[4][$i][$z]\"> <scalar dictRef=\"jmol:charge\">$T[8][$i][$z]</scalar></atom>\n");
#			printf (CML "    <atom id=\"$T[7][$i][$z]:$T[6][$i][$z]:$T[1][$i][$z]\" elementType=\"$T[5][$i][$z]\" x3=\"$T[2][$i][$z]\" y3=\"$T[3][$i][$z]\" z3=\"$T[4][$i][$z]\" residue=\"$T[7][$i][$z]\" resnumber=\"$T[6][$i][$z]\"> <scalar dictRef=\"jmol:charge\">$T[8][$i][$z]</scalar></atom>\n");
		}
		printf CML ("  </atomArray>\n");
		printf CML ("  <bondArray>\n");
		for ($j=0; $j<=$NbBond; $j++) { 
			printf (CML "    <bond atomRefs2=\"");
			for ($i=0; $i<=$NbAtom; $i++) {
                        	if ($T[10][$j][$z] == $T[0][$i][$z]) { printf (CML "$T[7][$i][$z]:$T[6][$i][$z]:$T[1][$i][$z] "); }
                        	if ($T[11][$j][$z] == $T[0][$i][$z]) { printf (CML "$T[7][$i][$z]:$T[6][$i][$z]:$T[1][$i][$z]\""); }
			}
			printf (CML " order=\"$T[12][$j][$z]\"/>\n");
		}
		printf CML ("  </bondArray>\n");
		printf CML ("</molecule>\n");
	close(CML);
	$z++;
}




