-1

This is the code that's giving me problems. The error is showing up as a syntactical one, but I can't figure out where or what it is? It says the error is in the 'while open (FILE , $array[$i])' statement , but I can't see what's wrong .

    #! /usr/bin/perl -w
    $splitpdb_version = '0.01' ;
    use File::Basename;


    $true = 1 ;
    $false = 0 ;

    use Getopt::Std ;

    $opt_h = 0 ;
    $opt_s = 0 ;
    $i = 0;
    getopts ('hs') ;

    $help = $opt_h ;
    $strip = $opt_s ;

    @array = <top8000_fullProteins_meso/*.pdb>

    while open (FILE, $array[$i])
    {

    $pdbfile = $array[$i] ;
    $string = "$array[$i]" ;
    $filename = fileparse($string);
    if (! $pdbfile || $help) {
      print STDERR "\n";
     print STDERR "Usage: \"splitpdb [-hs] pdbfile.pdb\"\n\n";
     if (! $help) {
      exit ;
      } 
     }

     if (! $pdbfile) {
     print "-h  flag gets this help message\n" ;
     print "-s  flag strips header lines\n" ;
     exit ;
     }

     open (PDBFILE, $pdbfile) or die "Could not open file \"$pdbfile\"" ;

     %chains = () ;
    @header = () ;
    $header_done = $false ;
    $lastchain = $false ;
    $atoms = [] ;

    while () {

    $atom = <PDBFILE> ;

    if (! $atom) {
    if ($lastchain) {
    $chains{$chain} = $atoms ;
    last;
    }
    }

    if (($atom =~ /^ATOM/i) | ($atom =~ /^HETATM/i)) {

    $header_done = $true ;

    $chain = substr $atom, 21, 1 ;

    if (! $lastchain) {
    $lastchain = $chain ;
    }

    if (lc($chain) eq lc($lastchain)) {
    push @{$atoms}, $atom ;
    } else {
    $chains{$lastchain} = $atoms ;
    $atoms = [] ;
    $lastchain = $chain ;
    push @{$atoms}, $atom ;
    }

    } else {
    if (! $header_done) {
    push @header, $atom ;
    }
    }

    } 

    close (PDBFILE);

    $chainno = 0 ;

    foreach $chain (keys %chains) {

    $chainno++ ;

    open (PDBOUT, ">$filename$chain$chainno.pdb") ;

    @atoms = @{$chains{$chain}} ;

    $lineno = 0 ;
    $atomno = 0 ;

    if (! $strip) {
    foreach $line (@header) {
    print PDBOUT $line ;
    }
     }


    while ($atoms[$lineno]) {
    if (($atoms[$lineno] =~ /^ATOM/i) | ($atoms[$lineno] =~ /^HETATM/i)) {
    $atomno++;
    $atomnostr = sprintf "%4d", $atomno;
    $atoms[$lineno] =~ s/^(.{7}).{4}/$1$atomnostr/ ;
    }
    print PDBOUT $atoms[$lineno] ;
    $lineno++;
    }

    close PDBOUT ;
    }

    printf ("There were %d chains.\n\n", (scalar keys %chains));
    $i++;
    };
jparthj
  • 1,606
  • 3
  • 20
  • 44
user2474041
  • 11
  • 1
  • 5
  • We don't know what kind of error you are talking about. Have you tried using `strict`? It's good that you have `warnings` (the `-w` flag), but you should add `strict` as well. After you have fixed all the stuff it complains about, maybe your problem will go away on its own. :) – simbabque Jun 28 '13 at 08:51
  • 1
    Let me make that more clear: Please post the exact error message. – simbabque Jun 28 '13 at 08:52
  • Tried it. Now it says "global symbol 'true' requires explicit package name" ... I have no idea what that means .. – user2474041 Jun 28 '13 at 08:54
  • this was the exact error message before I used strict "syntax error at split_pdb.pl line 22 near "){" "syntax error at split_pdb.pl line 124 near "}" – user2474041 Jun 28 '13 at 09:00
  • Some general hints, please format your code using proper indention to make it more readable. `use strict;` and `use warnings;` should also be applied as already mentioned. And use the three argument version of `open` with lexical filehandles `open my $filehandle , '<' , $filename or die "Cannot read $filename: $!";`. Limit the scope of your variables, so don't use global variables. – dgw Jun 28 '13 at 09:28

1 Answers1

2

Line 19 is missing a semi-colon at the end. As mentioned above, use strict may find other errors, but the missing semi-colon is probably the reason for the Syntax error you received.

dms
  • 797
  • 1
  • 5
  • 15