[Bio] / Sprout / FixRolesInFile.pl Repository:
ViewVC logotype

View of /Sprout/FixRolesInFile.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (download) (as text) (annotate)
Wed Sep 21 21:05:15 2011 UTC (7 years, 11 months ago) by parrello
Branch: MAIN
CVS Tags: rast_rel_2014_0912, rast_rel_2014_0729, mgrast_release_3_1_2, rast_rel_2011_0928, mgrast_version_3_2, mgrast_dev_12152011, mgrast_dev_10262011, HEAD
Changes to support complexes.

#!/usr/bin/perl -w

# -*- perl -*-
#
# Copyright (c) 2003-2011 University of Chicago and Fellowship
# for Interpretations of Genomes. All Rights Reserved.
#
# This file is part of the SEED Toolkit.
#
# The SEED Toolkit is free software. You can redistribute
# it and/or modify it under the terms of the SEED Toolkit
# Public License.
#
# You should have received a copy of the SEED Toolkit Public License
# along with this program; if not write to the University of Chicago
# at info@ci.uchicago.edu or the Fellowship for Interpretation of
# Genomes at veronika@thefig.info or download a copy from
# http://www.theseed.org/LICENSE.TXT.
#

=head1 FixRolesInFile

This script applies role changes to a single column of a flat file. The two
positional parameters are the name of the role-change file and the name of the
file to update.

The role-change file is a tab-delimited file containing four columns. The old
role name is in column 3 and the new role name is in column 4. (This is the
same format used by the role-change command in L<fig.pl>).

The file being changed is a tab-delimited file with the role names in the
first column. A command-line option can be used to specify a different column.
The updated file will be written to the standard output. If no file name is
specified, the standard input will be used.

The currently-supported command-line options are as follows.

=over 4

=item col

Index (1-based) of the column containing the role names to be changed. If
C<0> is specified, the last column will be used. The default is C<1>.

=item user

Name suffix to be used for log files. If omitted, the PID is used.

=item trace

Numeric trace level. A higher trace level causes more messages to appear. The
default trace level is 2. Tracing will be directly to the standard output
as well as to a C<trace>I<User>C<.log> file in the FIG temporary directory,
where I<User> is the value of the B<user> option above.

=item sql

If specified, turns on tracing of SQL activity.

=item background

Save the standard and error output to files. The files will be created
in the FIG temporary directory and will be named C<err>I<User>C<.log> and
C<out>I<User>C<.log>, respectively, where I<User> is the value of the
B<user> option above.

=item h

Display this command's parameters and options.

=back

=cut

use strict;
use Tracer;
use Stats;

# Parse the command line.
my ($options, @parameters) = StandardSetup([qw(ERDB Sapling)],
    {col =>   [1, "column containing role names to change"],
     trace => ["2-", "tracing level"]
    },
    "<changesFile> <inputFile>", @ARGV);

# Create the statistics object.
my $stats = Stats->new();
eval {
    # We'll put the role change map in here.
    my %roleMap;
    # Get the role-change file.
    my $changeFile = $parameters[0];
    if (! $changeFile) {
        Confess("No role-change file specified.");
    } elsif (! -f $changeFile) {
        Confess("Role-change file $changeFile not found.");
    } else {
        # Read in the role changes.
        Trace("Reading role changes from $changeFile.") if T(2);
        my $ch = Open(undef, "<$changeFile");
        while (! eof $ch) {
            my (undef, undef, $oldRole, $newRole) = Tracer::GetLine($ch);
            $stats->Add(changeLinesRead => 1);
            if (! $oldRole || ! $newRole) {
                Confess("Invalid line in role-change file.");
            } elsif (exists $roleMap{$oldRole}) {
                Confess("Duplicate change order for role \"$oldRole\".");
            } else {
                $roleMap{$oldRole} = $newRole;
            }
        }
        Trace(scalar(keys %roleMap) . " role changes found.") if T(2);
    }
    # Find the input file.
    my $inputFile = $parameters[1];
    if (! $inputFile) {
        Trace("Standard input will be used.") if T(2);
        $inputFile = "-";
    } elsif (! -f $inputFile) {
        Confess("Input file $inputFile not found.");
    } else {
        Trace("Input will be taken from $inputFile.") if T(2);
    }
    my $ih = Open(undef, "<$inputFile");
    # Determine the column number which contains the roles to change.
    my $col = $options->{col};
    # Loop through the input.
    while (! eof $ih) {
        # Get the next input line.
        my @cols = Tracer::GetLine($ih);
        $stats->Add(inputLines => 1);
        # Locate the role column.
        my $colIndex = ($col ? $col - 1 : $#cols);
        # Get the role.
        my $currentRole = $cols[$colIndex];
        # Check to see if it needs to be changed.
        my $newRole = $roleMap{$currentRole};
        if ($newRole) {
            $stats->Add(changedRoles => 1);
            $cols[$colIndex] = $newRole;
        }
        # Write the columns to the output.
        print join("\t", @cols) . "\n";
        $stats->Add(linesOut => 1);
    }
};
if ($@) {
    Trace("ERROR: $@") if T(0);
}
Trace("Statistics for run:\n" . $stats->Show());

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3