-1

I ran a molecular dynamics simulations which gave me 2,000 trajectory files. These files have the file name format au25-c2-benzalacetone.001 up to 2000. I wrote a general perl program but it isn't reading through the 2,000 files. How do I modify my code so it reads every file and extracts the coordinates I want for each file individually? I need 2,000 coordinates_of_interest.dat files. The tinker file here is the au25-c2-benzalacetone file. Here is the code I wrote:

#!/usr/bin/env perl
use Math::VectorReal;
use Math::Trig qw/acos/;
use strict;
use warnings;


   my $file1 = $ARGV[0];
   my $n1 = $ARGV[1];
   my $n2 = $ARGV[2];
   my $tinker_file = sprintf "%s.%03d"

   my( $file1, $n1, $n2 ) = @ARGV; 

  foreach $tinker_file (glob "$tinker_file.*") {
     print "Filename: $tinker_file\n";
  }

   my $file2 = "coordinates_of_interest.dat";
   my %lines_of_interest = map { $_ => 1 } 18, 25, 26;


{
open(FILE2, '>', $file2) or die "couldn't open the file!";

for(my $i=$n1;$i<=$n2;$i++){
{
open(FILE1, '<', $tinker_file) or die "couldn't open the file!";
{

my $num_lines = keys %lines_of_interest;

while (<FILE1>) {
   if ($lines_of_interest{$.}) {
      print FILE2;
      last unless --$num_lines;
              }
            }
         }
       }
    }
 }                                                                                                                                     

~
~
~

Heh
  • 3
  • 3
  • 1
    You need to fix several syntax errors (`my` on `$n1` and `$n2`), `$i` is not declared with `my`, your `for` is not closed with `}`, your first `open` is not terminated properly (missing `)` ...). You should probably also implicity `close()` your file handles. – Drav Sloan Aug 23 '16 at 14:44
  • True. I'm about to edit this here. I ran the code and it's giving me problems with FILE1 and FILE2. – Heh Aug 23 '16 at 14:48

3 Answers3

0

If the file format is au25-c2-benzalacetone.001, your first problem appears to be that $i may not be three digits. You probably want something like my $tinker_file = sprintf "%s.%03d", $file1, $i;

The other problem I'm noticing is that you keep opening "coordinates_of_interest.dat" for write, not append, so only the last file will have its three lines there. I would suggest that if you're writing to a single file for all inputs, open that file outside (before) of your for loop, and close it after the loop is completed.

Tanktalus
  • 21,664
  • 5
  • 41
  • 68
0

These files have the file name format au25-c2-benzalacetone.001 up to 2000.

my $tinker_file = sprintf "%s.%03d"

Aside from the fact that this line is incomplete, you cannot fit the numbers from 1 to 2000 in a three-digit field. This is probably why your script is not reading all of the files.

hymie
  • 1,982
  • 1
  • 13
  • 18
-1

glob is your friend here either use it with a wildcard and search the directory:

my ( $filespec, $n1, $n2 ) = @ARGV; 

foreach my $file ( glob "$filespec.*" ) { 
   print "Filename: $file\n";
}

Or you can use glob to expand a pattern:

my $expr = join ",", 0..9;

foreach my $entry ( glob ( "test.{$expr}{$expr}{$expr}" ) ) {
   print $entry,"\n";
}

But I'd probably stick with the first one, as that'll make sure files actually exist.

Sobrique
  • 52,974
  • 7
  • 60
  • 101
  • When I add this to my code it gives me the following errors: Global symbol "$tinker_file" requires explicit package name at ./extract_coordinates.pl line 15. syntax error at ./extract_coordinates.pl line 15, near "$tinker_file (" – Heh Aug 23 '16 at 15:48
  • Maybe I'm implementing the file naming incorrectly. – Heh Aug 23 '16 at 15:49
  • I changed my code to reflect the changes you gave me – Heh Aug 23 '16 at 15:50
  • Well, my code doesn't include `$tinker_file` so it's not someting in that snippet... – Sobrique Aug 23 '16 at 15:52
  • I changed the name filespec to tinker_file since I thought that was what you were referring to. – Heh Aug 23 '16 at 15:56
  • Ugh, that's because your previous line (the `sprintf`) is missing a semicolon. So it's treating the `my` part as part of `sprintf`. – Sobrique Aug 23 '16 at 15:57