File: make-csq-test

package info (click to toggle)
bcftools 1.22-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,792 kB
  • sloc: ansic: 72,978; perl: 7,583; sh: 694; python: 595; makefile: 301
file content (312 lines) | stat: -rwxr-xr-x 11,205 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl
#
# Author: petr.danecek@sanger
#

use strict;
use warnings;
use Carp;
use FindBin;
$ENV{PATH} = $FindBin::RealBin."/../../:$ENV{PATH}";

my $opts = parse_params();
parse_gff($opts);
create_ref($opts);
create_vcf($opts,0);
create_vcf($opts,$$opts{offset});
create_gff($opts,0);
create_gff($opts,$$opts{offset});
run_vep($opts);
run_bcsq($opts,$$opts{offset});
run_bcsq($opts,0);

exit;

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

sub error
{
    my (@msg) = @_;
    if ( scalar @msg ) { confess @msg; }
    print 
        "Usage: make-csq-test [OPTIONS]\n",
        "Options:\n",
        "   -f, --fasta-ref <file>          \n",
        "   -g, --gff <file>                \n",
        "   -i, --indels <int>              introduce indels\n",
        "   -o, --output <string>           \n",
        "   -p, --position <pos>            \n",
        "       --pad <int>                 [20]\n",
        "   -t, --transcript <id>           \n",
        "   -h, -?, --help                  This help message.\n",
        "\n",
        "Example:\n",
        "   ./make-csq-test -f \$ref -g \$gff -o rmme -p 3566625-AGCAGGCTG-A -t ENST00000378322\n",
        "\n";
    exit -1;
}
sub parse_params
{
    my $opts = { indels=>0, pad=>20 };
    while (defined(my $arg=shift(@ARGV)))
    {
        if (                 $arg eq '--pad' ) { $$opts{pad}=shift(@ARGV); next; }
        if ( $arg eq '-p' || $arg eq '--position' ) { push @{$$opts{pos}},shift(@ARGV); next; }
        if ( $arg eq '-i' || $arg eq '--indels' ) { $$opts{indels}=shift(@ARGV); next; }
        if ( $arg eq '-f' || $arg eq '--fasta-ref' ) { $$opts{ref}=shift(@ARGV); next; }
        if ( $arg eq '-t' || $arg eq '--transcript' ) { push @{$$opts{tscript}},shift(@ARGV); next; }
        if ( $arg eq '-g' || $arg eq '--gff' ) { $$opts{gff}=shift(@ARGV); next; }
        if ( $arg eq '-o' || $arg eq '--output' ) { $$opts{out}=shift(@ARGV); next; }
        if ( $arg eq '-?' || $arg eq '-h' || $arg eq '--help' ) { error(); }
        error("Unknown parameter \"$arg\". Run -h for help.\n");
    }
    if ( !exists($$opts{ref}) ) { error("Missing the -f option.\n") }
    if ( !exists($$opts{gff}) ) { error("Missing the -g option.\n") }
    if ( !exists($$opts{out}) ) { error("Missing the -o option.\n") }
    if ( !exists($$opts{tscript}) ) { error("Missing the -t option.\n") }
    for my $tscript (@{$$opts{tscript}}) { $$opts{tscripts}{$tscript} = 0; }
    return $opts;
}
sub parse_gff
{
    my ($opts) = @_;
    my $ntscript = 0;
    my @buf = ();
    my $cmd = ($$opts{gff} =~ /\.gz$/i) ? "gunzip -c $$opts{gff}" : "cat $$opts{gff}";
    open(my $fh,"$cmd |") or error("$cmd: $!");
    while (my $line=<$fh>)
    {
        if ( $line=~/^###/ ) { @buf = (); next; }
        if ( $line=~/^#/ ) { next; }
        my @vals = split(/\t/,$line);
        push @buf,\@vals;
        if ( !($vals[8]=~/ID=transcript:([^;]+);/) ) { next; }
        my $tscript = $1;
        if ( !exists($$opts{tscripts}{$tscript}) ) { next; }
        $$opts{tscripts}{$tscript} = ++$ntscript;
        if ( $ntscript==scalar keys %{$$opts{tscripts}} ) { last; }
    }
    if ( $ntscript!=scalar keys %{$$opts{tscripts}} )
    { 
        my @missing = ();
        for my $tscript (keys %{$$opts{tscripts}}) { if (!$$opts{tscripts}{$tscript}) { push @missing,$tscript; } }
        error("Transcript(s) not found in $$opts{gff}: ".join(",",@missing)."\n"); 
    }
    if ( !($buf[-1][8]=~/Parent=gene:([^;\s+]+)/) ) { error("Gene ID not present: $buf[-1][8]\n"); }
    my $gene_id = $1;
    while (my $line=<$fh>)
    {
        if ( $line=~/^###/ ) { last; }
        my @vals = split(/\t/,$line);
        push @buf,\@vals;
    }
    my $tline;
    my @dat = ();
    my ($chr,$beg_min,$end_max);
    for my $line (@buf)
    {
        if ( $$line[8]=~/ID=gene:$gene_id/ ) { push @dat,$line; next; }
        if ( $$line[8]=~/ID=transcript:([^;]+)/ )
        {
            my $tscript = $1;
            if ( exists($$opts{tscripts}{$tscript}) )
            {
                push @dat,$line;
                if ( !defined $chr ) { $chr = $$line[0]; }
                if ( !defined $beg_min or $beg_min > $$line[3] ) { $beg_min = $$line[3]; }
                if ( !defined $end_max or $end_max < $$line[4] ) { $end_max = $$line[4]; }
                $tline = $line;
            }
            next;
        }
        if ( $$line[8]=~/Parent=transcript:([^;]+)/ )
        {
            my $tscript = $1;
            if ( exists($$opts{tscripts}{$tscript}) ) { push @dat,$line; }
            next;
        }
        if ( $$line[8]=~/Parent=transcript:$$opts{tscript}/ ) { push @dat,$line; next; }
    }
    close($fh);
    $$opts{dat} = \@dat;
    $$opts{offset} = $beg_min - $$opts{pad} - 1;  # pos_new = pos_ori - offset
    $$opts{tchr} = $chr;
    $$opts{tbeg} = $beg_min;
    $$opts{tend} = $end_max;
}
sub get_type
{
    my ($rec) = @_;
    if ( $$rec[2] eq 'gene' ) { return $$rec[2]; }
    if ( $$rec[2] eq 'transcript' ) { return $$rec[2]; }
    if ( $$rec[2] eq 'exon' ) { return $$rec[2]; }
    if ( $$rec[2] eq 'CDS' ) { return $$rec[2]; }
    if ( $$rec[2] eq 'three_prime_UTR' ) { return $$rec[2]; }
    if ( $$rec[2] eq 'five_prime_UTR' ) { return $$rec[2]; }
    error("Unknown type: $$rec[2] .. ".join("\t",@$rec));
}
sub get_strand
{
    my ($rec) = @_;
    return $$rec[6];
}
sub create_ref
{
    my ($opts) = @_;
    my $chr = $$opts{tchr};
    my $beg = $$opts{tbeg} - $$opts{pad};
    my $end = $$opts{tend} + $$opts{pad};
    my $ref = '';
    open(my $out,'>',"$$opts{out}.fa") or error("$$opts{out}.fa: $!");
    open(my $fh,"samtools faidx $$opts{ref} $chr:$beg-$end|") or error("samtools faidx $$opts{ref} $chr:$beg-$end: $!");
    <$fh>;
    print $out ">$chr\t$chr:$beg-$end\n";
    while (my $line=<$fh>)
    {
        print $out $line;
        chomp($line);
        $ref .= $line;
    }
    close($fh);
    close($out);
    $$opts{refseq} = $ref;
    `rm -f $$opts{out}.fai; samtools faidx $$opts{out}.fa`;
}
sub create_vcf
{
    my ($opts,$offset) = @_;
    my $strand = get_strand($$opts{dat}[0]);
    my $chr  = $$opts{tchr};
    my $beg0 = $$opts{tbeg} - $$opts{pad};
    my $end0 = $$opts{tend} + $$opts{pad};
    my %dat;
    if ( exists($$opts{pos}) )
    {
        for my $pos_ref_alt (@{$$opts{pos}})
        {
            my ($pos,$ref,$alt) = split(/-/,$pos_ref_alt);
            $dat{$pos}{ref} = $ref;
            $dat{$pos}{alt} = $alt;
            push @{$dat{$pos}{type}}, join('-',@{$$opts{tscript}}).":$pos_ref_alt";
        }
    }
    else
    {
        # unused. If used, get_chr_beg_end() needs fixing
        # for my $dat (@{$$opts{dat}})
        # {
        #     my ($chr,$beg,$end) = get_chr_beg_end($dat);
        #     my $type   = get_type($dat);
        #     for my $off (-9,-8,-4,-3,-2,0,2,3,4,8,9)
        #     {
        #         my $pos = $beg + $off;
        #         if ( $pos<$beg0 or $pos>$end0 ) { next; }
        #         if ( $$opts{indels} == 0  )
        #         {
        #             $dat{$pos}{ref} = substr($$opts{refseq},$pos-$beg0,1);
        #             $dat{$pos}{alt} = $dat{$pos}{ref} eq 'A' ? 'C' : 'A';
        #         }
        #         elsif ( $$opts{indels} > 0 )
        #         {
        #             $dat{$pos}{ref} = substr($$opts{refseq},$pos-$beg0,1);
        #             $dat{$pos}{alt} = $dat{$pos}{ref}. ('A' x $$opts{indels});
        #         }
        #         else
        #         {
        #             if ( $pos-$beg0-$$opts{indels} > length($$opts{refseq}) ) { next; }
        #             $dat{$pos}{ref} = substr($$opts{refseq},$pos-$beg0,-$$opts{indels});
        #             $dat{$pos}{alt} = substr($$opts{refseq},$pos-$beg0,1);
        #         }
        #         push @{$dat{$pos}{type}}, "$type:$off";
        #     }
        # }
    }
    my $fname = $offset ? "$$opts{out}.vcf" : "$$opts{out}.vcf.ori";
    open(my $out,'>',"$fname") or error("$fname: $!");
    print $out qq[##fileformat=VCFv4.2\n];
    print $out qq[##contig=<ID=$chr,length=249250621>\n];
    print $out qq[##INFO=<ID=type,Number=.,Type=String,Description="">\n];
    print $out qq[##INFO=<ID=EXP,Number=1,Type=String,Description="Expected consequence">\n];
    print $out qq[##INFO=<ID=EXPL,Number=1,Type=String,Description="Expected consequence with bt/csq -l">\n];
    print $out "#".join("\t", qw(CHROM POS ID REF ALT QUAL FILTER INFO))."\n";
    for my $pos (sort {$a<=>$b} keys %dat)
    {
        my $ref = $dat{$pos}{ref};
        my $alt = $dat{$pos}{alt};
        my $type = join(',',@{$dat{$pos}{type}});
printf STDERR "offset=$offset  .. $pos  %d\n",$pos-$offset;
        print $out join("\t",($chr,$pos-$offset,'.',$ref,$alt,'.','.',"type=$type"))."\n";
    }
    close($out);
}
sub create_gff
{
    my ($opts,$offset) = @_;
    my $fname = $offset ? "$$opts{out}.gff" : "$$opts{out}.gff.ori";
    open(my $out,'>',"$fname") or error("$fname: $!");
    for my $dat (@{$$opts{dat}})
    {
        my (@x) = (@$dat);
        $x[3] -= $offset;
        $x[4] -= $offset;
        print $out join("\t",@x);
    }
    close($out);
}
sub run_vep
{
    my ($opts) = @_;

    my $vep = "perl -I /software/vertres/installs/ensembl/82/ /software/vertres/bin-external/variant_effect_predictor_v82.pl --db_version 82 -t SO --format vcf --force_overwrite --cache --dir /lustre/scratch116/vr/ref/ensembl/vep_cache/ --species human --offline --symbol --biotype --vcf --no_stats --assembly GRCh37 --no_progress --quiet -o -";
    my $cmd = "cat $$opts{out}.vcf.ori | $vep | bcftools query -f'%POS\\t%REF\\t%ALT\\t%CSQ\\n'";

    open(my $fh,"$cmd |") or error("$cmd: $!");
    open(my $out,'>',"$$opts{out}.vep.txt") or error("$$opts{out}.vep.txt: $!");
    while (my $line=<$fh>)
    {
        my @out = ();
        my ($pos,$ref,$alt,$csq) = split(/\t/,$line);
        my @csq = split(/,/,$csq);
        chomp($csq[-1]);
        for my $csq (@csq)
        {
            if ( $csq=~/Transcript\|$$opts{tscript}/ ) { push @out,$csq; }
        }
        if ( !scalar @out ) { @out = ('.'); }
        $csq = join(',',@out);
        print $out join("\t",($pos,$ref,$alt,$csq))."\n";
    }
    close($fh);
    close($out);
}
sub run_bcsq
{
    my ($opts,$offset) = @_;
    my $fname = $offset ? "$$opts{out}.bcsq.txt" : "$$opts{out}.bcsq.txt.ori";
    my $vcf   = $offset ? "$$opts{out}.vcf" : "$$opts{out}.vcf.ori";
    my $gff   = $offset ? "$$opts{out}.gff" : $$opts{gff};
    my $ref   = $offset ? "$$opts{out}.fa"  : $$opts{ref};

    my $cmd = "bcftools csq -f $ref -g $gff $vcf | bcftools query -f'%POS\\t%REF\\t%ALT\\t%BCSQ\\n'";

    print STDERR "$cmd\n";
    open(my $fh,"$cmd |") or error("$cmd: $!");
    open(my $out,'>',$fname) or error("$fname: $!");
    while (my $line=<$fh>)
    {
        my @out = ();
        my ($pos,$ref,$alt,$csq) = split(/\t/,$line);
        my @csq = split(/,/,$csq);
        chomp($csq[-1]);
        for my $csq (@csq)
        {
            if ( !($csq=~/Transcript/) or $csq=~/Transcript\|$$opts{tscript}/ ) { push @out,$csq; }
        }
        if ( !scalar @out ) { @out = ('.'); }
        $csq = join(',',@out);
        print $out join("\t",($pos,$ref,$alt,$csq))."\n";
    }
    close($fh);
    close($out);
}