0

I am trying to take one set of data and subtract each value in that data by another set of data.

For example:

Data set one (1, 2, 3)
Data set two (1, 2, 3, 4, 5)

So I should get something like (1 - (1 .. 5)) then (2 - (1..5)) and so on.

I currently have:

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

my $inputfile = $ARGV[0];

open( INPUTFILE, "<", $inputfile ) or die $!;

my @array = <INPUTFILE>;

my $protein = 'PROT';
my $chain   = 'P';
my $protein_coords;

for ( my $line = 0; $line <= $#array; ++$line ) {
    if ( $array[$line] =~ m/\s+$protein\s+/ ) {
        chomp $array[$line];
        my @splitline = ( split /\s+/, $array[$line] );
        my %coordinates = (
            x => $splitline[5],
            y => $splitline[6],
            z => $splitline[7],
        );
        push @{ $protein_coords->[0] }, \%coordinates;
    }
}

print "$protein_coords->[0]->[0]->{'z'} \n";

my $lipid1 = 'MEM1';
my $lipid2 = 'MEM2';
my $lipid_coords;

for ( my $line = 0; $line <= $#array; ++$line ) {
    if ( $array[$line] =~ m/\s+$lipid1\s+/ || $array[$line] =~ m/\s+$lipid2\s+/ ) {
        chomp $array[$line];
        my @splitline = ( split /\s+/, $array[$line] );
        my %coordinates = (
            x => $splitline[5],
            y => $splitline[6],
            z => $splitline[7],
        );
        push @{ $lipid_coords->[1] }, \%coordinates;
    }
}

print "$lipid_coords->[1]->[0]->{'z'} \n";

I am trying to take every value in $protein_coords->[0]->[$ticker]->{'z'} minus each value in $lipid_coords->[1]->[$ticker]->{'z'}.

My overall objective is to find (z2-z1)^2 in the equation d = sqrt((x2-x1)^2+(y2-y1)^2-(z2-z1)^2). I think that if I can do this once then I can do it for X and Y also. Technically I am trying to find the distance between every atom in a PDB file against every lipid atom in the same PDB and print the ResID for distance less than 5A.

Miller
  • 34,962
  • 4
  • 39
  • 60

3 Answers3

3

To iterate on all combinations of two arrays, just embed two for loops:

use strict;
use warnings;

my @dataone = (1, 2, 3);
my @datatwo = (1, 2, 3, 4, 5);

for my $one (@dataone) {
    for my $two (@datatwo) {
        print "$one - $two\n";
    }
}

Outputs:

1 - 1
1 - 2
1 - 3
1 - 4
1 - 5
2 - 1
2 - 2
2 - 3
2 - 4
2 - 5
3 - 1
3 - 2
3 - 3
3 - 4
3 - 5
Miller
  • 34,962
  • 4
  • 39
  • 60
1

This will give you the result of subtracting each element of set 2 from each element of set 1 in what I believe is the manner you were asking.

#!/usr/bin/perl

use strict;
use warnings;

my @set1 = (1, 2, 3);
my @set2 = (1, 2, 3, 4, 5);

my @set3 = ();
for my $val (@set1) {
    push @set3, map { $val - $_ } @set2;
}

local $" = ', ';
print "@set3\n";

system 'pause';

The result will be an array containing (1 - (1..5), 2 - (1..5), 3 - (1..5)).

Contents of @set3 after script runs:

0, -1, -2, -3, -4, 1, 0, -1, -2, -3, 2, 1, 0, -1, -2

All the other protein and lipid stuff is way over my head, but I hope this at least helps a little. You should now have an array containing the subtracted elements that you can work with to get the rest of your results!


Edit:

Can replace the loop with this one liner :)

my @set3 = map { my $v = $_; map { $v - $_ } @set2 } @set1;

map is a pretty nifty function!

tjwrona1992
  • 8,614
  • 8
  • 35
  • 98
0

The easiest way to do this is to do your calculations while you're going through file two:

for (my $line = 0; $line <= $#array; ++$line) {
    if (($array[$line] =~ m/\s+$lipid1\s+/) | ($array[$line] =~ m/\s+$lipid2\s+/)) {  
        chomp $array[$line];
        my @splitline = (split /\s+/, $array[$line]);
        my %coordinates = (x => $splitline[5],
                           y => $splitline[6],
                           z => $splitline[7],
                          );
        push @{$lipid_coords->[1]}, \%coordinates;

        # go through each of the sets of protein coors in your array...
        for my $p (@{$protein_coords->[0]}) {
            # you can store this value however you want...
            my $difference = $protein_coords->[0][$p]{z} - $coordinates{z};
        }
    }
}

If I were you, I would use some form of unique identifier to allow me to access the data on each combination -- e.g. build a hash of the form $difference->{<protein_id>}{<lipid_id>} = <difference>.

i alarmed alien
  • 9,412
  • 3
  • 27
  • 40