forked from eberdahl/SaM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSAM-preprocessor
executable file
·100 lines (63 loc) · 2.28 KB
/
SAM-preprocessor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/perl
#
# Edgar Berdahl, Jan 21 2012
# Updated February 2012
#
# First argument is input filename for MDL file
# Second argument is output filename for MDX file
#
# For example: ./SAM-preprocessor examplelinks.mdl examplelinks.mdx
#
# ALL NUMBERS IN MDL FILES SHOULD BE FLOATING-POINT NUMBERS (NOT INTEGERS)
# Here we include the regular expression definitions necessary for parsing model specification files.
require 'SAM-reg-ex';
use v5.10;
use warnings;
$filename = $ARGV[0]; # e.g. "examplelinks.mdl";
$filenameout = $ARGV[1]; # e.g. "examplelinks.mdx";
my $indx = 0; # index variable used over and over again
#say "Starting SAM-preprocessor...";
# First remove all of the comments and write the result into $tmp_filename
$tmp_filename = "tempSAM3981.tmp";
open my $fh_tmp_out, '>', $tmp_filename
or die "Cannot write '$tmp_filename'";
open $filehandle, '<', $filename
or die "Cannot read '$filename': $!\n";
#my $beforeComment = qr/(\A[^\n\r#]*)/; # Contains anything except newlines, returns, and pound symbols (#)
#my $afterComment = qr/([^\n\r]*)/; # Contains anything except newlines and returns
my $beforeComment = qr/([^#]*)/; # Contains anything except pound symbols (#)
my $afterComment = qr/(.*)/; # Contains anything
my $filterOutComments = qr/\A${beforeComment}#${afterComment}$/;
while (<$filehandle>)
{
chomp;
$theline = $_;
# Each line out is simply the line in up until the point where a pound symbol (#) is reached
if ($theline =~ qr/$filterOutComments/) {
#say "1=" . $1 . ", 2=" . $2;
say $fh_tmp_out $1;
} else {
say $fh_tmp_out $theline; # If there are no comments in the line
}
}
close $fh_tmp_out;
$filename = $tmp_filename; # Starting now we read out of the temporary file
# Open MDX file for output and prepare to write into it
open my $fh_out, '>', $filenameout
or die "Cannot write '$filenameout': $!\n";
# For now just copy over all lines
open $filehandle, '<', $filename
or die "Cannot read '$filename': $!\n";
while (<$filehandle>)
{
chomp;
$theline = $_;
say $fh_out $theline;
}
# Delete the temporary file if it still exists
close $filehandle;
if (-e $tmp_filename)
{
unlink($tmp_filename);
}
#say "SAM-preprocessor finished.";