#!/usr/bin/perl                                                                      
use strict;
use warnings;

#------------------------                                                            
# $Author: saulius $                               
# $Revision: 358 $  
# $Date: 2015-06-01 15:12:23 +0300 (Mon, 01 Jun 2015) $                                                                        
# $URL: svn+ssh://saulius-grazulis.lt/home/saulius/svn-repositories/praktikos-darbai-2015/Rimant%C4%97_%C5%BDalyt%C4%97/trunk/uzd4/bin/pdbxPr-SG $                                                                                                           
#----------------------- 

my $svnid = '# $Id: pdbxPr-SG 358 2015-06-01 12:12:23Z saulius $';
print "$svnid \n";

my @atoms = map {[split]} grep /^(ATOM|HETATM)/, <>;

## print scalar(@atoms), "\n";
## print $atoms[0][10], "\n";

my @com = (0, 0, 0); # Center of Masses

for my $atom (@atoms) {
    $com[0] += $atom->[10]; # X coordinate
    $com[1] += $atom->[11]; # Y coordinate
    $com[2] += $atom->[12]; # Z coordinate
}

my $crazy_functional_style = 0; # By the crazy professor...

if( $crazy_functional_style ) {
    @com = map {$_/@atoms} @com;
} else {
    for my $i (0..$#com) { $com[$i] /= scalar(@atoms) }
}

do {
    local $, = " ";
    local $\ = "\n";
    print @com;
} if 0;

for my $atom (@atoms) {
    my ($x, $y, $z) = @{$atom}[10..12];
    ## print ">>>> $x, $y, $z\n";
    my $dx = $x - $com[0];
    my $dy = $y - $com[1];
    my $dz = $z - $com[2];
    my $distance = sqrt( $dx**2 + $dy**2 + $dz**2 );
    local $, = " ";
    local $\ = "\n";
    print "COMDIST", $distance, $ARGV, @{$atom}[2..7];
}
