#!usr/bin/perl # Programmer: Rachel Bingham # Date: 29 April 2009 # Usage: 'perl swap --f=[filename] # Documentation: Swaps input and output files of training data records # to have reversed input/output neural nets. Creates an # output file renamed by replacing 'single' with 'reverse'. # Tested on file: # /training_data/dunbrack-30pc-1763-1.single-str2 # Notes: Script not optimized. It's a quick one to just do the job. use strict; use warnings; use Getopt::Long; my ($infile, $outfile) = undef; my ($to_replace, $replace_with) = ("single", "reverse"); my $options = GetOptions("f=s"=>\$infile); open(IN_FILE, $infile) or die("Cannot open file: $infile\n"); $outfile=$infile; $outfile =~ s/$to_replace/$replace_with/g; open(OUT_FILE, "+>$outfile") or die("Cannot open file for writing: $outfile\n"); # Temporary array to hold the input and output file strings my @swap_array = ("","",""); # Run through the file line by line parsing by whitespace while(my $line = ) { if($line =~ m/(^($|InfilePrefix[\s+]$))/i) {print(OUT_FILE $line); next;} chomp($line); @swap_array = split(/ /, $line); print(OUT_FILE $swap_array[0]," ",$swap_array[2]," ",$swap_array[1],"\n"); } close(OUT_FILE) or die("Cannot close file: $outfile\n"); close(IN_FILE) or die("Cannot close file: $infile\n");