-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathreorderheader.pl
More file actions
executable file
·69 lines (58 loc) · 1.93 KB
/
reorderheader.pl
File metadata and controls
executable file
·69 lines (58 loc) · 1.93 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl -w
use strict;
my $usage = "reorderheader.pl version 0.0.1
Author: Liron Ganel
Reorders header lines of a VCF file based on the header of a reference file. Header lines not in the reference are placed at the end of the header (before the #CHROM line).
usage: ./reorderheader.pl file.vcf reference.vcf
file.vcf: VCF whose header is to be reordered
reference.vcf: VCF with desired order of header lines;
substituting \"stdin\" for either file results in reading that file from standard input\n";
die $usage unless scalar @ARGV == 1 || scalar @ARGV == 2;
my ($file, $reffile) = @ARGV;
die "Standard input can only be used for one input file" if $reffile eq "stdin" && $file eq "stdin";
my ($reffilehandle, $filehandle);
if($reffile eq "stdin") {
$reffilehandle=*STDIN;
} elsif ($reffile =~ /\.gz$/) {
open($reffilehandle,"zcat $reffile |") || die "Could not open $reffile: $!";
} else {
open($reffilehandle,"<$reffile") || die "Could not open $reffile: $!";
}
my (%reforder, $line) = ();
my $count = 0;
until(!defined($line = <$reffilehandle>) || $line =~ /^#CHROM/) {
$reforder{$line} = $count;
$count++;
}
close $reffilehandle;
die "Malformed VCF" unless defined $line;
if($file eq "stdin") {
$filehandle=*STDIN;
} elsif ($file =~ /\.gz$/) {
open($filehandle,"zcat $file |") || die "Could not open $file: $!";
} else {
open($filehandle,"<$file") || die "Could not open $file: $!";
}
my @header = ();
while (my $line = <$filehandle>) {
push @header, $line;
last if $line =~ /^#CHROM/;
}
my @sortedhead = sort mysort @header;
print foreach (@sortedhead);
while(<$filehandle>) {print};
sub mysort {
if (defined($reforder{$a}) && defined($reforder{$b})) {
return $reforder{$a} <=> $reforder{$b};
} elsif ($a =~ /^#CHROM/) {
return 1;
} elsif ($b =~ /^#CHROM/) {
return -1;
} elsif (defined $reforder{$a}) {
return -1;
} elsif (defined $reforder{$b}) {
return 1;
} else {
return $a cmp $b;
}
}