PERL Script

Document Sample
PERL Script Powered By Docstoc
					                                                                                          1


Supporting information for Clifford et al. (2002) Proc. Natl. Acad. Sci. USA 99 (8),
4830–4836. (10.1073/pnas.062639799)

PERL Script

Remember to change the first line of the script to reflect the path for the PERL executable.
The script works only on the JCAMP format output from XWIN-NMR (tojdx in output mode
“1D Real spectrum” and compression mode “Fix”). This script was run on PERL5.005_04
on an Indy SGI workstation (IRIX 6.2).
#!/usr/sbin/perl
##################################################################
#perl script for converting BRUKER JCAMP to XY column data               #
#Author: Thomas Clifford, KBJ Group, Fri Mar 19 23:37:13 CST 1999#
##################################################################
##################################################################
#Usage: change $jcampPath to the directory you want to do the #
#batch conversion, fill the directory with Jcamps (XXX.dx)        #
# you want converting and run this script                 #
#10###############################################################
# change this path to that in which your data files for conversion are placed.

$dirname = "/usr/people/tc/titrations/tc114iib/";

########################
sub dirBatch #this opens every file in a directory and then does
             #something to that file.
#call as (dirname,old file extension, newfile extension)
   {
my($dirname)=$_[0];
my($oldExtn)=$_[1];
my($newExtn)=$_[2];
opendir(DIR, $dirname) or die "sub dirBatch: cant open directory $dirname: $!";
while (defined($file = readdir(DIR))) { #read in all files in directory.
chomp($file);
next if $file !~ /.$oldExtn/;
next if FileType($file) ne "bruker"; #only change process bruker files
$file=$dirname.$file;
$newfile = $file;
$newfile =~ s/.$oldExtn/.$newExtn/; #newfiles are given the new extension
open( NEWFILE, ">$newfile" );               #open the file for writing
select(NEWFILE);                      #redirect output to newfile
parsFunc($file) > $newfile;            #parse the old file and copy
close( NEWFILE );                     #to newfile...close file
}
closedir(DIR);                   #close directory
   }
                                                                                      2


##End dirBatch
#######################


#######################tests to see what sort of file and returns the filetype name
## subroutein FileType
# Usage call FileType([file]) returns file type bruker for bruker nmr
# JCAMP format, unknown for everything else.
sub FileType {
my($datafile)=$_[0];
my($bruker)="";
$fullpath = $dirname.$datafile;
open( IN, "<$fullpath" ) || die "could not open $fullpath.(Sub is FileType)...\n";
                          #read in a data file
while ($l = <IN>) {
     if ($l !~ /Bruker/){        #see if it looks like a bruker
     next;
                  }

       else {
     $bruker="bruker";
          }
              }
close( IN );
return $bruker;
             }
#end FileType
######################


######################
sub parsFunc
    {
@X = ("X"); #set up arrays for the column data
@Y = ("Y");
my($offset)=1;
my($xfactor)=1;
my($frequency)=400;
my($firstx)=1;
my($constant)=$offset-($firstx/$frequency);
my($factor)=$xfactor/$frequency;

my($jcampFile)=$_[0];
print "at line 77, $constant\n";

open(JCAMP, "< $jcampFile") or die "cant open file: $!";
                                                                                  3



while (<JCAMP>){


if (/\#\#\$OFFSET/) {
         split(/=\s+/, $_);
         $offset = $_[1];
         next;
         }                    # Find the values to change X
                              # to ppm
elsif (/\#\#XFACTOR/){
         split(/=\s+/, $_);
         $xfactor = $_[1];
         next;
         }
elsif (/\#\#.OBSERVE FREQUENCY/){
         split(/=\s+/, $_);
         $frequency = $_[1];
         next;
         }
elsif (/\#\#\FIRSTX/){
         split(/=\s+/, $_);
         $firstx = $_[1];
         next;
         }

elsif(/\#\#XYDATA/.../\#\#END/){        #grab only the XYDATA

                     #read in datafile by line and
$fileLine = $_;            #store as an array of data
@line = split(/\s+/,$fileLine); # note wee prob if no white sp befor 1st col

for ($i=0; $i < $#line-1; $i++)
     {                #first element is the first X

   $nextX = (($line[1]-$i)*$xfactor/$frequency)+($offset-($firstx/$frequency));

                   #value of that group so fill in
    push (@X, $nextX);      #rest of X values throug a loop to
                   #end of array.

   if ($i < $#line) {push (@Y, $line[$i+2])};
     }                #Y values are also collected and placed
  next;                #in its own array starting from the 2nd
                      #word in the line
                   }
                                                         4


#ed
      }

END {close(STDOUT) or die "cant close STDOUT: $!"}

close(JCAMP);

for ($n=0; $n <= $#X; $n++) #data output as XY columns
   {
    print $X[$n]," ", $Y[$n], " \n";
   }

   }
##End parsfile
#####################

# main starts here

dirBatch($dirname, DX, dat);

# End of Main
# END SCRIPT