Hello,

I'm a newbie here and also to Perl... Have a big task of finding the lines of the form from a file

ATOM 1 N LYS A 257 -5.036 -29.330 -27.709 1.0041.51 N
ATOM 2 CA LYS A 257 -3.873 -29.331 -26.757 1.00 41.55 C
.
.
.
....n lines
Then, save it in array format and also calculate dist=sqrt[ (xi-xj)^2+(yi-yj)^2+(zi-zj)^2] for atom12, atom 23 and so on...(x= -5.036,y=-29.330,z=-27.709 for Atom 1 values indicated above).
Please help me....Thanks a tonne in adv....

Recommended Answers

All 7 Replies

Reading from a file in array context will get you all the lines in the file:

@array = <$fh> #where $fh is your file handle

If the atoms are in order (1, 2, 3) etc, and you need to get specific values from them, use split:

my @atom1 = split / /, $array[0] #first line is in 0th position of array

There's probably a more efficient way of doing what you want, but that was the first thing that came to my mind.

When the original poster says read all the data for all the atoms into an array it sounds like an array of arrays. There is no such thing in Perl but you can get the same result using an array of references to arrays. There are tutorials such as http://perldoc.perl.org/perlreftut.html which may help.

Or, putting aside my array of references suggestion (because trying it that way gave me a headache) and building on what ItecKid said, you could do it as follows:

#!/usr/bin/perl -w
use strict;
use math::Complex;
#CalcDistAtoms.pl
my (@atoms,@x,@y,@z);

# I appended the data to this program.
# If you open a file instead, @atoms = <whatever you called your file handle>
@atoms = <DATA>;
foreach my $row (@atoms){
    my @temp = split(/ /,$row);
    my $n = $temp[1];
    $x[$n] = $temp[6];
    $y[$n] = $temp[7];
    $z[$n] = $temp[8];
}
print "Distance between atom 1 and atom 2 is ", calcdist(1, 2);
sub calcdist {
    my ($i, $j) = @_;
    my $dist = sqrt(($x[$i] - $x[$j])**2 + ($y[$i] - $y[$j])**2 + ($z[$i] - $z[$j])**2);
    }

__DATA__
ATOM 1 N LYS A 257 -5.036 -29.330 -27.709 1.0041.51 N
ATOM 2 CA LYS A 257 -3.873 -29.331 -26.757 1.00 41.55 C

Hello!! ... Thanks a ton for all the help!! Well, i ve done this initial code..

use strict;
 
use warnings;
 
my @pdb;
 
open(IN, "/home/subs/vindytest/PDB.pl") or die "Could not open file!";
 
while (my $line = <IN>) {
 
 my @array = (split (/\s+/, $line))[0..12];
 
  print ("@array\n");
 
  push (@pdb,@array) ;
 
}
 
close(IN);

When i run this program, there is an error :
Use of uninitialized value in join or string at distcal3.pl line 13, <IN> line 1.
ATOM 6 CG LYS A 257 -3.149 -31.509 -27.908 1.00 44.85 C
Use of uninitialized value in join or string at distcal3.pl line 13, <IN> line 2.
ATOM 7 CD LYS A 257 -2.221 -32.646 -27.517 1.00 47.26 C
Use of uninitialized value in join or string at distcal3.pl line 13, <IN> line 3.

How do i get rid of this error?

~

To get rid of the warning about using uninitialized values in array elements change line 13 as follows:

#my @array = (split (/\s+/, $line))[0..12]; But there are not always 13 values!
my @array = (split (/\s+/, $line));

The [0..12] after the split function attempts to define an array having 13 elements (including element [0]) by taking a slice of the array resulting from the split. But in the data you have shown us there are not always 13 space-delimited values on each line, so some of the array elements in the slice specified by [0..12] have no values, which causes a warning, but not a fatal error.

Hey... Thank you... the script is working fine now...by d way how do you code wrap before posting using (code)... i broke the forum rule...it doesnt work...do u just put your code withing square brackets...

The easiest way is to select the code to be wrapped, then click the

[/b] icon at the top of your message box. This places an opening code tag at the beginning of the selection and a closing code tag  (square brackets containing a forward slash followed by the word [b]code[/b] ).

You can modify the opening code tag to specify what kind of code you are wrapping. The default for the Perl forum is [code=perl]. When posting an example of text data I like to wrap it in code tags opening with [code=text] so it will preserve the original spacing without coloring what it supposes to be Perl commands.

For example, [CODE=text]She said, "The name is '!&#', which is pronounced 'Bang Pound'."
ATOM 2 CA LYS A 257 -3.873 -29.331 -26.757 1.00 41.55 C

In case my explanation is confusing, try the Help with Code Tags link.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.