Add tests for memory leaks and weaken for Issue #81
[bioperl-live.git] / examples / contributed / nmrpdb_parse.pl
blob47b22162f133a630eeebc8a7ec2aaeff4916b5a5
1 #!/usr/bin/perl
2 use strict;
4 # This program will read in an NMR derived PDB file containing
5 # multiple conformers, and will allow the user to extract either
6 # one or all of the models to separate files.
8 # Although the program will run interactively, the command line
9 # usage is "nmrsplit [input.file] [number of model to extract]"
13 # Written 13/12/00 by Simon Andrews (simon.andrews@bbsrc.ac.uk)
15 # Submitted to bioperl script project 2001/08/06
17 # Description: Script which reads an NMR-derived multiple structure
18 # PDB file, and will either extract a single structure from it, or
19 # will extract all of the structures into single files. This is
20 # useful when you want to work with a single representative structure
21 # from an NMR ensemble - especially in conjunction with the OLDERADO
22 # database (http://neon.chem.le.ac.uk/olderado/) which finds the
23 # most representative structure from an ensemble.
26 my $Input = $ARGV[0]; # File to be read
27 my $Pullout = $ARGV[1]; # Specific model to extract
28 my @Pdbfile; # Array for whole PDB file
29 my $Header = ""; # String to hold the PDB header
30 my $Model = ""; # String to hold individual models
31 my $Output; # Prefix for output files
32 my $Modno = 1; # Number of the model being processed
34 while () {
35 if ($Input) {
37 if (-r $Input) {
38 last;
39 }else{
40 print "\"$Input\" does not exist, or could not be read\n";
44 print "\nEnter name of multiple PDB file: ";
45 $Input = <STDIN>;
46 chomp $Input;
47 $Input =~ s/^\s*//;
48 $Input =~ s/\s*$//;
50 next;
53 while () {
54 if ($Pullout) {
56 if ($Pullout =~ /^\d+$/){
57 if ($Pullout == int $Pullout) {
58 last;
59 }else {
60 print "\"$Pullout\" should be an integer\n";
62 }else {
63 print "\"$Pullout\" should be a number\n";
67 print "\nEnter number of specific model to extract (Return for none): ";
68 $Pullout = <STDIN>;
69 chomp $Pullout;
70 $Pullout =~ s/^\s*//;
71 $Pullout =~ s/\s*$//;
73 last unless ($Pullout);
74 next;
78 ($Output = $Input) =~ s/\.\w*$//; # Take off everything after the last . to use as prefix
81 open (PDB,$Input) || die "Can't open $Input because $!";
85 ########## Read the header information ####################
88 while (<PDB>) {
89 if (/^MODEL\b/){last;}
90 $Header = $Header . $_;
94 ######### Read the separate models #######################
97 while () {
99 model();
100 if ($Model) { # Check if we're past the last model
101 if ($Pullout) { # Check if we're writing one or all
102 last if ($Modno > $Pullout);# No point continuing if we've got the one we want
103 readout();
104 }else {
105 writeout();
107 $Model = "";
108 ++$Modno;
109 }else {
110 last;
113 --$Modno; # Correct last increment which didn't find a model
115 if (($Pullout) & ($Modno < $Pullout)) {
116 print "\nCannot find model $Pullout : Only $Modno models in this file\n";
119 #################### subroutines start here ##########################
122 sub model {
124 while (<PDB>) {
125 if (/^(MODEL\b|END\b|MASTER\b)/){next;}
126 # Stops you getting MODEL... at the top of the output
127 # and makes sure there isn't a file containing just END or MASTER
129 if (/^ENDMDL\b/){last;} # Check for the end of the model
130 $Model = $Model . $_; # Append the line to $Model
134 sub writeout { # Used when all files are being written out
136 if (-e "$Output\_$Modno.pdb"){ # Check whether we're overwriting anything
138 print "\n$Output\_$Modno.pdb already exists. Overwrite (y/n)? ";
139 my $Question = <STDIN>;
140 unless ($Question =~ /^y/i) {
141 print "\nSkipping $Output\_$Modno.pdb";
142 return;
146 open (OUT,">$Output\_$Modno.pdb") || die "Can't open $Output\_$Modno.pdb because $!";
147 print "\nWriting $Output\_$Modno.pdb ...";
148 print OUT $Header;
149 print OUT $Model;
150 print OUT "END\n"; # Adds and END statement to the PDB file
152 close OUT || die "Couldn't close $Output\_$Modno.pdb because $!";
156 sub readout {
158 if ($Modno == $Pullout) {
160 if (-e "$Output\_$Modno.pdb") { # Check whether we're overwriting anything
162 print "\n$Output\_$Modno.pdb already exists. Overwrite (y/n)? ";
163 my $Question = <STDIN>;
164 unless ($Question =~ /^y/i) {
165 print "\nModel not extracted\n";
166 $Model = "";
167 return;
171 open (OUT,">$Output\_$Modno.pdb") || die "Can't open $Output\_$Modno.pdb because $!";
172 print "\nWriting $Output\_$Modno.pdb ...\n";
173 print OUT $Header;
174 print OUT $Model;
175 print OUT "END\n"; # Adds and END statement to the PDB file
177 close OUT || die "Couldn't close $Output\_$Modno.pdb because $!";
179 $Model = ""; # Stops the reading after this model
180 }else {
181 print "\nReading Model $Modno ...";