Here's the complete code:
#!/usr/bin/perl -w
use strict;
#put this into a directory in which all of the files that end in .txt are .RAW to .txt files that you want converted to the perl processed 4 column format
my $line;
my @array;
my $scan;
my $acquisition_mode;
my $MS_mode;
my $mz;
my $intensity;
my $time;
my $path;
my $file;
my @files;
my $output_file;
opendir (DIR, "\.");
@files = readdir DIR;
closedir DIR;
foreach $file (@files) {
if ($file =~ /\.txt$/ and $file !~ /^perl_processed/) {
open (INPUT, "<$file") or die "can't find $file\n";
$output_file = $file;
$output_file = "perl_processed_profile_$output_file";
open (OUTPUT1, ">$output_file");
while ($line = <INPUT>) {
chomp $line;
$line =~ s/\r$//;
$line =~ s/^\s+//;
$line =~ s/\s+$//;
if ($line =~ /\S/) {
if ($line =~ /^ScanHeader/) {
$scan = "";
$time = "";
$mz = "";
$intensity = "";
$MS_mode = "";
$acquisition_mode = "";
if ($line =~ /^ScanHeader # (\d+)$/) {
$scan = $1;
}
else {
die "unexpected scan header line: $line\n";
}
}
if ($line =~ /^start_time/) {
if ($line =~ /^start_time = (\d+\.\d+), end_time = 0\.000000, packet_type = 0$/) {
$time = $1;
}
else {
die "didn't expect this time line:\n$line\n";
}
}
if ($line =~ /^Polarity positive/) {
if ($line eq "Polarity positive, Profile Data, Full Scan Type, MS Scan") {
$MS_mode = 1;
$acquisition_mode = "profile";
}
elsif ($line eq "Polarity positive, Cenrtoid Data, Full Scan Type, MS Scan") {
$MS_mode = 1;
$acquisition_mode = "centroid";
}
elsif ($line =~ /^Polarity positive, \w+ Data, Full Scan Type, MS2 Scan$/) {
$MS_mode = 2;
$acquisition_mode = "MS2";
}
else {
die "this line was unexpected:\n$line\n";
}
}
if ($line =~ /^Packet \#/) {
if ($line =~ /^Packet \# (\d+), intensity = (\d+\.\d+), mass\/position = (\d+\.\d+)$/) {
if ($MS_mode == 1) {
$intensity = $2;
$mz = $3;
if ($acquisition_mode eq "profile") {
die "this one was supposed to have been collected in centroid mode\n";
}
elsif ($acquisition_mode eq "centroid") {
print OUTPUT1 "$scan\t$time\t$mz\t$intensity\n";
}
elsif ($acquisition_mode ne "MS2") {
die "unexpected acquisition mode:\n$acquisition_mode\n";
}
}
}
else {
die "an unexpected line here:\n$line\n";
}
}
}
}
close OUTPUT1;
close INPUT;
}
}
print "done!\n";