#!/usr/bin/perl use strict; use warnings; my @files = glob("file*.tab"); my %output = (); foreach my $file ( @files ){ open(my $fh,'<',$file) or die $!; while(<$fh>){ chomp; my @s = split /\t/; die("_ERROR_ not 9 columns [ $_ ]\n") if (@s != 9); $output{$s[0]."\t".$s[1]."\t".$s[2]."\t".$s[3]} = []; } close $fh; } foreach my $file ( @files ){ foreach my $k(keys %output){ push @{$output{$k}},0; } open(my $fh,'<',$file) or die $!; while(<$fh>){ chomp; my @s = split /\t/; die("_ERROR_ not 9 columns [ $_ ]\n") if (@s != 9); my @array = @{$output{$s[0]."\t".$s[1]."\t".$s[2]."\t".$s[3]}}; $array[-1] = $s[6]; $output{$s[0]."\t".$s[1]."\t".$s[2]."\t".$s[3]} = \@array; } close $fh; } print join("\t",'chr','fivep','threep','strand',@files),"\n"; foreach my $k(keys %output){ my @s = split /\t/,$k; my $chr = $s[0]; my $fivep = $s[1]; my $threep = $s[2]; my $strand = $s[3]; next if($strand =~ /^0$/); if($strand =~ /^0$/){ print join("\t",$chr,$fivep,$threep,"+",@{$output{$chr."\t".$fivep."\t".$threep."\t".$strand}} ),"\n"; }else{ print join("\t",$chr,$fivep,$threep,"-",@{$output{$chr."\t".$fivep."\t".$threep."\t".$strand}} ),"\n"; } }