Reputation: 133
I am not sure what any of this called, I got it from a lab friend. I am trying to make Keys and Values for each X, Y, and Z Coordinates for specific particles but am having problems printing what I need or calling upon the assigned coordinates.
This is my exact script:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
#my $inputFile = $ARGV[0];
my $inputFile = '8ns_emb_alt_101.pdb';
open (INPUTFILE, "<", $inputFile) or die $!;
my @array = <INPUTFILE>;
#check
#print "@array \n";
######
#### CoM of MET ####
my $met_Sulfur = 'SD';
my $met_CarbonB = 'CB';
my $met_CarbonG = 'CG';
my $met_CarbonE = 'CE';
my $met_number = '34';
my $COM_MET_Coords;
for (my $line = 0; $line <= $#array; ++$line){
if (($array[$line] =~ m/\s+$met_Sulfur\s+/)&&($array[$line] =~ m/\s+$met_number\s+/)){
# print "$array[$line]";
my @splitLine = (split /\s+/, $array[$line]);
my %coordinates = (x => $splitLine[6],
y => $splitLine[7],
z => $splitLine[8],
);
push @{$COM_MET_Coords->[0]}, \%coordinates;
}
}
foreach my $line (@{$COM_MET_Coords->[0]}){
print "$line->{'x'}\n";
I have also tried to print by using:
print $COM_MET_Coords->[0]->[0]->{'x'};
My main problem is that I don't totally understand how and where the information I want is being stored and I am not sure what this is called. I am using perl v5.10.1. If anyone can tell me how to print X Y and Z as well as why that would be Perfect!
This is a Sample of the data:
ATOM 527 N MET 34 -9.849 -0.893 17.835 0.00 0.00 PROT
ATOM 528 HN MET 34 -9.231 -0.252 17.386 0.00 0.00 PROT
ATOM 529 CA MET 34 -10.593 -1.664 16.897 0.00 0.00 PROT
ATOM 530 HA MET 34 -11.657 -1.526 17.023 0.00 0.00 PROT
ATOM 531 CB MET 34 -10.159 -1.300 15.525 0.00 0.00 PROT
ATOM 532 HB1 MET 34 -10.188 -2.148 14.808 0.00 0.00 PROT
ATOM 533 HB2 MET 34 -9.082 -1.050 15.637 0.00 0.00 PROT
ATOM 534 CG MET 34 -10.806 0.010 15.045 0.00 0.00 PROT
ATOM 535 HG1 MET 34 -10.226 0.384 14.174 0.00 0.00 PROT
ATOM 536 HG2 MET 34 -10.755 0.660 15.945 0.00 0.00 PROT
ATOM 537 SD MET 34 -12.551 -0.153 14.748 0.00 0.00 PROT
ATOM 538 CE MET 34 -12.486 -0.712 13.101 0.00 0.00 PROT
Upvotes: 0
Views: 266
Reputation: 126722
This is what I think you want to do.
The code you have shown just builds a hash of 3D coordinates from the input file, and then prints the x coordinates from each entry. This is how I would write something to do just that.
Since you don't define $COM_MET_Coords
I have used a simple array variable @coords
. I also use while
to read the file line by line as it is wasteful to read it into an array in its entirety and then just process the array line by line.
The output isn't very interesting as there is only one line in your data that contains both SD
and 34
.
By the way, please don't use capital letters in local identifiers. Numbers, lower-case letters and underscore _
are fine, but capitals are reserved for global names like packages.
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
my ($input_file) = @ARGV;
$input_file = '8ns_emb_alt_101.pdb';
#### CoM of MET ####
my $met_sulfer = 'SD';
my $met_carbon_b = 'CB';
my $met_carbon_g = 'CG';
my $met_carbon_e = 'CE';
my $met_number = '34';
open my $fh, '<', $input_file;
my @coords;
while (<$fh>) {
next unless /\s$met_sulfer\s/ and /\s$met_number\s/;
my %coordinates;
@coordinates{qw/ x y z /} = (split)[5, 6, 7];
push @coords, \%coordinates;
}
for my $item (@coords) {
print $item->{x}, "\n";
}
output
-12.551
Upvotes: 1
Reputation: 239
When you don't understand your data formats in perl, Data::Dumper is often a good starting point.
use Data::Dumper;
# ... populate data structures
print Dumper($COM_MET_Coords);
If all you need is your data, then this will be the end of the solution. If you need to massage the data, then this will at least let you understand how it is laid out.
Upvotes: 0