This file is indexed.

/usr/bin/vcf-concat is in vcftools 0.1.14+dfsg-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#!/usr/bin/env perl
#
# Author: petr.danecek@sanger
#

use strict;
use warnings;
use Carp;
use Vcf;

my $opts = parse_params();
if ( $$opts{check_columns} )
{
    check_columns($opts);
}
elsif ( !exists($$opts{sort}) )
{
    concat($opts);
}
else
{
    concat_merge($opts);
}

exit;

#--------------------------------

sub error
{
    my (@msg) = @_;
    if ( scalar @msg )
    {
        croak @msg;
    }
    die
        "About: Convenience tool for concatenating VCF files (e.g. VCFs split by chromosome).\n",
        "   In the basic mode it does not do anything fancy except for a sanity check that all\n",
        "   files have the same columns.  When run with the -s option, it will perform a partial\n",
        "   merge sort, looking at limited number of open files simultaneously.\n",
        "Usage: vcf-concat [OPTIONS] A.vcf.gz B.vcf.gz C.vcf.gz > out.vcf\n",
        "Options:\n",
        "   -c, --check-columns              Do not concatenate, only check if the columns agree.\n",
        "   -f, --files <file>               Read the list of files from a file.\n",
        "   -p, --pad-missing                Write '.' in place of missing columns. Useful for joining chrY with the rest.\n",
        "   -s, --merge-sort <int>           Allow small overlaps in N consecutive files.\n",
        "   -h, -?, --help                   This help message.\n",
        "\n";
}

sub parse_params
{
    my $opts = { files=>[] };
    while (my $arg=shift(@ARGV))
    {
        if ( $arg eq '-p' || $arg eq '--pad-missing' ) { $$opts{pad_missing}=1; next; } 
        if ( $arg eq '-s' || $arg eq '--merge-sort' ) { $$opts{sort}=shift(@ARGV); next; } 
        if ( $arg eq '-c' || $arg eq '--check-columns' ) { $$opts{check_columns}=1; next;  }
        if ( $arg eq '-f' || $arg eq '--files' ) 
        {
            my $files = shift(@ARGV);
            open(my $fh,'<',$files) or error("$files: $!");
            while (my $line=<$fh>)
            {
                chomp($line);
                push @{$$opts{files}},$line;
            }
            close($fh);
            next;
        }
        if ( $arg eq '-?' || $arg eq '-h' || $arg eq '--help' ) { error(); }
        if ( -e $arg ) { push @{$$opts{files}},$arg; next }
        error("Unknown parameter \"$arg\". Run -h for help.\n");
    }
    if ( ! @{$$opts{files}} ) { error("No files to concat?\n") }
    return $opts;
}

sub can_be_padded
{
    my ($opts,$cols1,$cols2) = @_;
    if ( @$cols1<@$cols2 ) { error(sprintf "Not ready for this, sorry, expected fewer columns (%d!<%d)", @$cols1,@$cols2); }
    my $has1 = {};
    my $has2 = {};
    for (my $i=0; $i<@$cols1; $i++) { $$has1{$$cols1[$i]} = $i; }
    for (my $i=0; $i<@$cols2; $i++) 
    {
        if ( !exists($$has1{$$cols2[$i]}) ) { error("The column [$$cols2[$i]] not seen previously."); }
        $$has2{$$cols2[$i]} = $i; 
    }
    my @map;
    for (my $i=0; $i<@$cols1; $i++)
    {
        my $cname = $$cols1[$i];
        push @map, exists($$has2{$cname}) ? $$has2{$cname} : -1;
    }
    return \@map;
}

sub check_columns
{
    my ($opts) = @_;
    my @columns;
    for my $file (@{$$opts{files}})
    {
        my $vcf = Vcf->new(file=>$file);
        $vcf->parse_header();

        if ( @columns )
        {
            my $different_order;
            my $different_columns;
            if ( @columns != @{$$vcf{columns}} ) { warn("Different number of columns in [$file].\n"); }
            if ( $$opts{pad_missing} && can_be_padded($opts,\@columns,$$vcf{columns}) ) { next; }
            for (my $i=0; $i<@columns; $i++)
            {
                if ( $$vcf{columns}[$i] ne $columns[$i] ) 
                { 
                    if ( !exists($$vcf{has_column}{$columns[$i]}) )
                    {
                        warn("The column names do not match; the column \"$columns[$i]\" no present in [$file].\n"); 
                        $different_columns = $columns[$i];
                    }
                    elsif ( !defined $different_order )
                    {
                        $different_order = $columns[$i];
                    }
                }
            }
            if ( defined $different_order && !defined $different_columns )
            {
                warn("The columns ordered differently in [$file]. Use vcf-shuffle-cols to reorder.\n");
            }
        }
        else
        {
            @columns = @{$$vcf{columns}};
        }
        $vcf->close();
    }
}

sub concat
{
    my ($opts) = @_;
    my @columns;
    for my $file (@{$$opts{files}})
    {
        my $vcf = Vcf->new(file=>$file);
        $vcf->parse_header();

        my $map;
        if ( @columns )
        {
            if ( @columns != @{$$vcf{columns}} ) 
            { 
                if ( !$$opts{pad_missing} )
                {
                    error(sprintf "Different number of columns in [%s], expected %d, found %d\n", $file,scalar @columns,scalar @{$$vcf{columns}}); 
                }
                $map = can_be_padded($opts,\@columns,$$vcf{columns});
            }
            else
            {
                my $different_order;
                for (my $i=0; $i<@columns; $i++)
                {
                    if ( $$vcf{columns}[$i] ne $columns[$i] )
                    {
                        if ( !exists($$vcf{has_column}{$columns[$i]}) )
                        {
                            error("The column names do not match; the column \"$columns[$i]\" no present in [$file].\n");
                        }
                        elsif ( !defined $different_order )
                        {
                            $different_order = $columns[$i];
                        }
                    }
                }
                if ( defined $different_order )
                {
                    error("The columns ordered differently in [$file]. Use vcf-shuffle-cols to reorder.\n");
                }
            }
        }
        else
        {
            @columns = @{$$vcf{columns}};
            print $vcf->format_header();
        }
        while (my $line=$vcf->next_line())
        {
            if ( defined $map )
            {
                my @line = split(/\t/,$line);
                chomp($line[-1]);
                my @out;
                for my $idx (@$map)
                {
                    if ( $idx==-1 ) { push @out,'.'; }
                    else { push @out,$line[$$map[$idx]] }
                }
                print join("\t",@out),"\n";
            }
            else
            {
                print $line;
            }
        }
    }
}

sub get_chromosomes
{
    my ($files) = @_;
    my @out;
    my %has_chrm;
    for my $file (@$files)
    {
        my $vcf = Vcf->new(file=>$file);
        my $chrms = $vcf->get_chromosomes();
        for my $chr (@$chrms)
        {
            if ( exists($has_chrm{$chr}) ) { next; }
            $has_chrm{$chr} = 1;
            push @out,$chr;
        }
    }
    return \@out;
}

sub concat_merge
{
    my ($opts) = @_;

    my $header_printed = 0;
    my $chroms = get_chromosomes($$opts{files});
    for my $chr (@$chroms)
    {
        my $reader = Reader->new(files=>$$opts{files},nsort=>$$opts{sort},seq=>$chr,header_printed=>$header_printed);
        $header_printed = 1;
        $reader->open_next();
        while (1)
        {
            my $line = $reader->next_line();
            if ( !defined $line )
            {
                if ( !$reader->open_next() ) { last; }
                next;
            }
            print $line;
        }
    }
    if ( !$header_printed )
    {
        my $vcf = Vcf->new(file=>$$opts{files}[0]);
        $vcf->parse_header();
        print $vcf->format_header();
    }
}

#---------------------------------

package Reader;

use strict;
use warnings;
use Carp;
use Vcf;

sub new
{
    my ($class,@args) = @_;
    my $self = @args ? {@args} : {};
    bless $self, ref($class) || $class;
    if ( !$$self{files} ) { $self->throw("Expected the files option.\n"); }
    if ( !$$self{nsort} ) { $$self{nsort} = 2; }
    if ( $$self{nsort}>@{$$self{files}} ) { $$self{nsort} = scalar @{$$self{files}}; } 
    $$self{idxs} = undef;
    $$self{vcfs} = undef;
    return $self;
}

sub throw
{
    my ($self,@msg) = @_;
    confess @msg;
}

sub print_header
{
    my ($self,$vcf) = @_;
    if ( $$self{header_printed} ) { return; }
    print $vcf->format_header();
    $$self{header_printed} = 1;
}


# Open VCF, parse header, check column names and when callled for the first time, output the VCF header.
sub open_vcf
{
    my ($self,$file) = @_;
    my $vcf = Vcf->new(file=>$file,region=>$$self{seq},print_header=>1);
    $vcf->parse_header();
    if ( !exists($$self{columns}) )
    {
        $$self{columns} = [ @{$$vcf{columns}} ];
    }
    else
    {
        if ( @{$$self{columns}} != @{$$vcf{columns}} ) { $self->throw("Different number of columns in [$file].\n"); }
        for (my $i=0; $i<@{$$self{columns}}; $i++)
        {
            if ( $$vcf{columns}[$i] ne $$self{columns}[$i] ) { $self->throw("The column names do not agree in [$file].\n"); }
        }
    }
    $self->print_header($vcf);
    return $vcf;
}

sub open_next
{
    my ($self) = @_;

    if ( !defined $$self{idxs} )
    {
        for (my $i=0; $i<$$self{nsort}; $i++)
        {
            $$self{idxs}[$i] = $i;
        }
    }
    else
    {
        my $prev = $$self{idxs}[-1];

        shift(@{$$self{idxs}});
        shift(@{$$self{vcfs}});

        if ( $prev+1 < @{$$self{files}} ) 
        {
            # New file to be opened
            push @{$$self{idxs}}, $prev+1;
        }
    }
    for (my $i=0; $i<@{$$self{idxs}}; $i++)
    {
        if ( exists($$self{vcfs}[$i]) ) { next; }
        my $idx = $$self{idxs}[$i];
        $$self{vcfs}[$i] = $self->open_vcf($$self{files}[$idx]);
    }
    if ( !@{$$self{idxs}} ) { return 0; }
    return 1;
}

sub next_line
{
    my ($self) = @_;

    my $min = $$self{vcfs}[0]->next_line();
    if ( !defined $min ) { return undef; }

    if ( !($min=~/^(\S+)\t(\d+)/) ) { $self->throw("Could not parse the line: $min\n"); }
    my $min_chr = $1;
    my $min_pos = $2;
    my $min_vcf = $$self{vcfs}[0];

    for (my $i=1; $i<@{$$self{vcfs}}; $i++)
    {
        if ( !exists($$self{vcfs}[$i]) ) { next; }
        my $line = $$self{vcfs}[$i]->next_line();
        if ( !defined $line ) { next; }
        if ( !($line=~/^(\S+)\t(\d+)/) ) { $self->throw("Could not parse the line: $line\n"); }
        my $chr = $1;
        my $pos = $2;

        if ( $chr ne $min_chr ) { $self->throw("FIXME: When run with the -s option, only one chromosome can be present.\n"); }
        if ( $min_pos > $pos )
        {
            $min_pos = $pos;
            $min_vcf->_unread_line($min);
            $min_vcf = $$self{vcfs}[$i];
            $min = $line;
        }
        else
        {
            $$self{vcfs}[$i]->_unread_line($line);
        }
    }
    return $min;
}