forked from owensgl/reformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSNPtable2merged.pl
49 lines (47 loc) · 1.02 KB
/
SNPtable2merged.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/perl
use warnings;
use strict;
#This combines multiple tab delimited files
#This version is not memory safe
my $Nvalue = "NN";
my %genotypehash;
my %positionhash;
my %totalsamplehash;
foreach my $n (0..$#ARGV){
open IN, $ARGV[$n];
my %samplehash;
while (<IN>){
chomp;
my @a = split(/\t/,$_);
if ($. == 1){
foreach my $i (2..$#a){
$samplehash{$i} = $a[$i];
$totalsamplehash{$a[$i]}++;
}
}else{
my $chrom = $a[0];
my $pos = $a[1];
$positionhash{$chrom}{$pos}++;
foreach my $i (2..$#a){
$genotypehash{$chrom}{$pos}{$samplehash{$i}} = $a[$i];
}
}
}
close IN;
}
print "CHROM\tPOS";
foreach my $sample (sort keys %totalsamplehash){
print "\t$sample";
}
foreach my $chrom (sort keys %positionhash){
foreach my $pos (sort {$a<=>$b} keys %{ $positionhash{$chrom} } ) {
print "\n$chrom\t$pos";
foreach my $sample (sort keys %totalsamplehash){
if ($genotypehash{$chrom}{$pos}{$sample}){
print "\t$genotypehash{$chrom}{$pos}{$sample}";
}else{
print "\t$Nvalue";
}
}
}
}