Hi,

today, i had to split a file vertically. I didn't find it in Q&A section, so i made something myself, and post it here because i think it could be usefull for others ? My apologies if it already exists on the site.

I have tab-delimited text files where first column is identifiers and following columns contain data. The aim was to split the file vertically into n files. The bonus is i wanted to shuffle the data columns (ie not conserve the order of the columns in the destination files). For my purpose, i had to repeat the original first column into all destination files, but this can easily be adapted for your purpose.

This is a first try, but i'm quite new to perl, so comments and improvements are welcomed.
#!/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); } }
An example input file is :
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
code is to be launched like :
./vSplit <fileToVSplit> <integer>

<integer> is the number of part you want to split the file into.

Hopes it will help

Marsel

In reply to A vertical (+/- random) split of a file by Marsel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.