An example input file is :#!/usr/bin/perl -w # Split a tab-delimited text files into n files, repeating # first column and shuffling columns. # Julien Textoris use strict; use warnings; open(IN, "$ARGV[0]"); my $n = $ARGV[1]; #Thanks to Q&A for this ! sub melange_fy { my $tab = shift; my $i; for($i = @$tab; --$i;) { my $j = int rand($i+1); next if ($i == $j); @$tableau[$i,$j] = @$tableau[$j,$i]; } } my $perm =0 ; my (@ind); while ( my $ligne = <IN>) { $ligne =~ s/[\r\n]//g; my ($id,@elmt) = split(/\t/, $ligne); #make the permutation of tab indices only the first time unless($perm++) { @ind = (0..@elmt-1); melange_fy(\@ind); } my $w = int(scalar(@elmt)/$n+0.5); for(my $i = 0; $i < $n; $i++) { open (OUT, ">>$ARGV[0].$i"); my $d = $i*$w; my($f); if($i == $n-1) { $f = scalar(@elmt)-1; } else { $f = $d+$w-1; } print OUT join("\t",$id,@elmt[@ind[$d..$f]])."\n"; close(OUT); } }
code is to be launched like :Genes setA setB setC setD setE setF setG g1 1 2 3 4 5 6 7 g2 1 2 3 4 5 6 7 g3 1 2 3 4 5 6 7
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: A vertical (+/- random) split of a file
by Hue-Bond (Priest) on Jul 21, 2006 at 20:26 UTC | |
by Marsel (Sexton) on Jul 21, 2006 at 20:32 UTC | |
|
Re: A vertical (+/- random) split of a file
by jwkrahn (Abbot) on Jul 22, 2006 at 01:09 UTC | |
by Marsel (Sexton) on Jul 22, 2006 at 05:33 UTC | |
by jwkrahn (Abbot) on Jul 22, 2006 at 06:56 UTC | |
|
Re: A vertical (+/- random) split of a file
by ysth (Canon) on Jul 21, 2006 at 20:59 UTC |