Search code examples
bashshellperlpdb-files

How to take out certain elements from a pdb file


I am trying to take out certain columns from a pdb file. I already have taken out all lines that start out with ATOM in my code. For some reason my sub functions are not working and I do not know where or how to call them. My code is:

open (FILE, $ARGV[0])
    or die "Could not open file\n";

my @newlines;
 while ( my $line = <FILE> ) {
    if ($line =~ m/^ATOM.*/) {
    push @newlines, $line;
    }
}

my $atomcount = @newlines;
#print "@newlines\n";
#print "$atomcount\n";

##############################################################
#This function will take out the element from each line
#The element is from column 77 and contains one or two letters

sub atomfreq {
    foreach my $record1(@newlines) {
      my $element = substr($record1, 76, 2);
      print "$element\n";
      return;
    }
}

################################################################
#This function will take out the residue name from each line
#The element is from column 18 and contains 3 letters

sub resfreq {
    foreach my $record2(@newlines) {
      my $residue = substr($record2, 17, 3);
      print "$residue\n";
      return;
    }
}

Solution

  • As @Ossip already said in this answer you simply need to call your functions:

    sub atomfreq {
        ...
    }
    
    sub resfreq {
        ...
    }
    
    atomfreq();
    resfreq();
    

    But I'm not sure whether these functions do what you intended because the comments imply that they should print every $residue and $element from the @newlines array. You've put a return statement inside the for loop which will immediately return from the whole function (and its for loop) so it will print only the first $residue or $element. Because the functions aren't supposed to return anything you can just drop that statement:

    sub atomfreq {
        foreach my $record1(@newlines) {
            my $element = substr($record1, 76, 2);
            print "$element\n";
        }
    }
    
    sub resfreq {
        foreach my $record2(@newlines) {
            my $residue = substr($record2, 17, 3);
            print "$residue\n";
        }
    }
    
    atomfreq();
    resfreq();