#!/usr/bin/perl use strict; use warnings; use NEURO4 qw(load_project print_help); use Data::Dump qw(dump); use Text::CSV qw( csv ); use Excel::Writer::XLSX; sub inplace{ my $place = shift; my $thing = shift; $thing =~ s/\/$//; if( $thing =~ /\// ){ $thing =~ s/.*\/(.+?)$/$1/; } return $place.'/'.$thing; } my $idir; my $guide; my $ofile; my $info_page; @ARGV = ("-h") unless @ARGV; while (@ARGV and $ARGV[0] =~ /^-/) { $_ = shift; last if /^--$/; if (/^-i/) { $idir = shift; chomp($idir);} if (/^-g/) { $guide = shift; chomp($guide);} if (/^-o/) { $ofile = shift; chomp($ofile);} if (/^-s/) { $info_page = shift; chomp($info_page);} if (/^-h/) { print_help $ENV{'PIPEDIR'}.'/doc/metrics2xls.hlp'; exit;} } my $study = shift; my %std = load_project($study); die "Should supply project name\n" unless $study; die "Should supply results directory\n" unless $idir; die "Should supply guidance file\n" unless $guide; $ofile = $idir.'.xlsx' unless $ofile; $info_page = 'info_page.csv' unless $info_page; $idir = inplace $std{'DATA'}, $idir; $info_page = inplace $std{'DATA'}, $info_page; $guide = inplace $std{'DATA'}, $guide; $ofile = inplace $std{'DATA'}, $ofile; my $info = csv (in => $info_page); $ofile =~ s/\.(\w*)?$/.xlsx/; my $workbook = Excel::Writer::XLSX->new($ofile); my $worksheet = $workbook->add_worksheet('Info'); for my $i (0 .. $#{$info}) { my $row = $info->[$i]; for my $j (0 .. $#{$row}){ $worksheet->write( $i, $j, $row->[$j]); } } opendir (DIR, $idir); my @ifiles = grep(/\.csv/, readdir(DIR)); close DIR; foreach my $ifile (@ifiles){ my $tmpf = 'tmp_'.$ifile; my $order = 'join -t, -1 2 -2 1 '.$guide.' '.$idir.'/'.$ifile.' > '.$tmpf; system($order); my $idata = csv (in => $tmpf); # as array of array (my $shname = $ifile) =~ s/\.csv$//; $worksheet = $workbook->add_worksheet($shname); for my $i (0 .. $#{$idata}) { my $row = $idata->[$i]; for my $j (0 .. $#{$row}){ $worksheet->write( $i, $j, $row->[$j]); } } unlink $tmpf; }