[Bio] / FigMetagenomeTools / split_files_by_column.pl Repository:
ViewVC logotype

View of /FigMetagenomeTools/split_files_by_column.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (download) (as text) (annotate) (vendor branch)
Mon Feb 19 17:15:26 2007 UTC (12 years, 9 months ago) by olson
Branch: x, MAIN
CVS Tags: mgrast_dev_08112011, mgrast_dev_08022011, mgrast_dev_05262011, mgrast_dev_04082011, mgrast_version_3_2, mgrast_dev_12152011, mgrast_dev_06072011, mgrast_rel_2008_0806, mgrast_dev_10262011, mgrast_dev_02212011, mgrast_rel_2008_0923, mgrast_release_3_0, mgrast_dev_03252011, mgrast_rel_2008_0924, mgrast_rel_2008_1110_v2, mgrast_rel_2008_0625, mgrast_release_3_0_4, mgrast_release_3_0_2, mgrast_release_3_0_3, mgrast_release_3_0_1, mgrast_dev_03312011, mgrast_release_3_1_2, mgrast_release_3_1_1, mgrast_release_3_1_0, mgrast_dev_04132011, mgrast_dev_04012011, mgrast_rel_2008_0919, mgrast_rel_2008_1110, myrast_33, mgrast_rel_2008_0917, mgrast_dev_04052011, mgrast_dev_02222011, y, HEAD
Changes since 1.1: +0 -0 lines
Initial import

#!/usr/bin/perl -w

=pod

=head1 split_files_by_column.pl

split a file into seperate files each with a different numbmer of columns. This allows you to take a big file and open it in excel where you are only allowed 255 columns.

=cut

# split into different numbers of columns

use strict;
my $usage=<<EOF;
$0

-c number of columns to split the files into
-f file to split
-k number of columns to keep and put at the beginning of each file. To keep just the first column (default) use -k 1. To keep the first two columns use -k 2

EOF

my ($file, $cols, $keep)=(undef, undef, 1);
while (@ARGV)
{
    my $t=shift;
    if ($t eq "-c") {$cols=shift}
    if ($t eq "-f") {$file=shift}
    if ($t eq "-k") {$keep=shift}
}


die $usage unless ($cols && $file);

my $filehandles;
open(IN, $file) || die "Can't open $file";
while (<IN>)
{
    chomp;
    my @a=split /\t/;
    my $posn=$keep;
    my $thisfile=0;
    my $end=$#a-$cols;
    while ($posn < $end)
    {
        $thisfile++;
        my $fh;
        if ($filehandles->{$thisfile}) {$fh=$filehandles->{$thisfile}}
        else
        {
            my $out=$file;
            ($out =~ /.txt$/) ? ($out =~ s/.txt$/.$thisfile.txt/) : ($out .= ".$thisfile");
            open($fh, ">$out") || die "Can't write to $out";
            $filehandles->{$thisfile}=$fh;
        }
        print $fh join("\t", @a[0..($keep-1)], @a[$posn .. $posn+$cols-1]), "\n";
        $posn+=$cols;
    }
    unless ($posn == $#a)
    {
        $thisfile++;my $fh;
        if ($filehandles->{$thisfile}) {$fh=$filehandles->{$thisfile}}
        else
        {
            my $out = $file;
            ($out =~ /.txt$/) ? ($out =~ s/.txt$/.$thisfile.txt/) : ($out .= ".$thisfile");
            open($fh, ">$out") || die "Can't write to $out";
            $filehandles->{$thisfile}=$fh;
        }
        print $fh join("\t", @a[0..($keep-1)], @a[$posn .. $#a]), "\n";
    }
}

MCS Webmaster
ViewVC Help
Powered by ViewVC 1.0.3