vcf_serializer.t 6.57 KB
Newer Older
1 2 3
=head1 LICENSE

Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
Tiago Grego's avatar
Tiago Grego committed
4
Copyright [2016-2019] EMBL-European Bioinformatics Institute
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

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

=cut

use strict;
use warnings;

use Test::More;
use Test::Differences;

use Bio::EnsEMBL::CoordSystem;
use Bio::EnsEMBL::Slice;
use Bio::EnsEMBL::Variation::Variation;
use Bio::EnsEMBL::Variation::VariationFeature;
use IO::String;

use Bio::EnsEMBL::IO::Translator::SampleGenotypeFeature;
use Bio::EnsEMBL::IO::Writer::VCF4;
use Bio::EnsEMBL::IO::Object::VCF4Metadata;

## Create some sample objects (we don't want unit tests to depend on a db connection
my $chr = 1;
## Create some sample objects (we don't want unit tests to depend on a db connection
my $cs = Bio::EnsEMBL::CoordSystem->new(-NAME    => 'chromosome',
                                        -VERSION => 'GRCh38',
                                        -RANK    => 1,
                                        ); 
43
my $slice = Bio::EnsEMBL::Slice->new( -coord_system     => $cs,
44
                                      -seq_region_name  => $chr,
45 46
                                      -start            => 1,
                                      -end              => 248956422,
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
                                      -strand           => 1,
                                      );


my %var_data = ( 'rs754176245' => { 'start'   => 230710045, 
                                    'end'     => 230710045,
                                    'alleles' => 'C/T',
                                    'aa'      => 'C'
                                  },  
                 'rs11568053'  => { 'start'   => 230710047, 
                                    'end'     => 230710047,
                                    'alleles' => 'C/T',
                                    'aa'      => 'C'
                                  },
                 'rs699'       => { 'start'   => 230710048, 
                                    'end'     => 230710048,
                                    'alleles' => 'A/G',
                                    'aa'      => 'G',
                                    'ma'      => 'A',
                                    'maf'     => 0.3,
                                    'mac'     => 1477
                                  }
              );

#my %sample_data = ( 'NA18594' => { 'genotype' => {'rs699' => '1/1'}, 'phased' => 0},
#                    'NA18573' => { 'genotype' => {'rs699' => '1/1'}, 'phased' => 0},
#                    'NA12875' => { 'genotype' => {'rs699' => '1/1'}, 'phased' => 0},
#                    'NA12874' => { 'genotype' => {'rs699' => '1/1'}, 'phased' => 0},
#                    'HG00096' => { 'genotype' => {'rs699' => '1/1'}, 'phased' => 0},
#                  );


my @vars = map { $_ } sort { $var_data{$a}{'start'} <=> $var_data{$b}{'start'} } keys(%var_data);
my @samples_list; #my @samples_list = sort(keys(%sample_data));

my @info    = ('AA','MA','MAF','MAC'); #('AA','MA','MAF','MAC', 'NS');
my @formats = (); #('GT');


my @features = ();
foreach my $var (@vars) {

  my $var_d = $var_data{$var};

  my $var_obj = Bio::EnsEMBL::Variation::Variation->new(
Tiago Grego's avatar
Tiago Grego committed
92
     -NAME   => $var
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  );
  
  #my %sample_genotypes = ();
  #foreach my $sample (@samples_list) {
  #  if ($sample_data{$sample}{'genotype'} && $sample_data{$sample}{'genotype'}{$var}) {
  #    $sample_genotypes{$sample}{'genotype'} = $sample_data{$sample}{'genotype'}{$var};
  #    $sample_genotypes{$sample}{'phased'} = $sample_data{$sample}{'phased'};
  #  }
  #}
  #$var_obj->{'sample_genotypes'} = \%sample_genotypes if (scalar(keys(%sample_genotypes)) > 0);
  
  push @features, Bio::EnsEMBL::Variation::VariationFeature->new(
    -START => $var_d->{'start'},
    -END   => $var_d->{'end'},
    -STRAND        => 1,
    -SLICE => $slice,
    -ALLELE_STRING => $var_d->{'alleles'},
    -VARIATION     => $var_obj,
    -VARIATION_NAME => $var,
    -MINOR_ALLELE => $var_d->{'ma'},
    -MINOR_ALLELE_FREQUENCY => $var_d->{'maf'},
Tiago Grego's avatar
Tiago Grego committed
114 115
    -MINOR_ALLELE_COUNT => $var_d->{'mac'},
    -ANCESTRAL_ALLELE => $var_d->{'aa'}
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
  );
  
  
}

#my $vcf_string = <<VCF;
# ##fileformat=VCFv4.2
# ##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral Allele">
# ##INFO=<ID=MA,Number=1,Type=String,Description="Minor Allele">
# ##INFO=<ID=MAF,Number=1,Type=Float,Description="Minor Allele Frequency">
# ##INFO=<ID=MAC,Number=1,Type=Integer,Description="Minor Allele Count">
# ##INFO=<ID=NS,Number=1,Type=Integer,Description="Number of Samples With Data">
# ##FORMAT=<ID=GT,Number=1,Type=String,Description="Genotype">
# #CHROM	POS	ID	REF	ALT	QUAL	FILTER	INFO	FORMAT	HG00096	NA12874	NA12875	NA18573	NA18594
# 1	230710045	rs754176245	C	T	.	.	AA=C;NS=0	GT	.	.	.	.	.	.	.
# 1	230710047	rs11568053	C	T	.	.	AA=C;NS=0	GT	.	.	.	.	.	.	.
# 1	230710048	rs699	A	G	.	.	AA=G;MA=A;MAF=0.3;MAC=1477;NS=4	GT	1/1	1/1	1/1	1/1	.
# VCF

my $vcf_string = <<VCF;
##fileformat=VCFv4.2
##INFO=<ID=AA,Number=1,Type=String,Description="Ancestral Allele">
##INFO=<ID=MA,Number=1,Type=String,Description="Minor Allele">
##INFO=<ID=MAF,Number=1,Type=Float,Description="Minor Allele Frequency">
##INFO=<ID=MAC,Number=1,Type=Integer,Description="Minor Allele Count">
#CHROM	POS	ID	REF	ALT	QUAL	FILTER	INFO
1	230710045	rs754176245	C	T	.	.	AA=C
1	230710047	rs11568053	C	T	.	.	AA=C
1	230710048	rs699	A	G	.	.	AA=G;MA=A;MAF=0.3;MAC=1477
VCF

# Create a string file handle to write to
my $fh = IO::String->new();

my $translator = Bio::EnsEMBL::IO::Translator::SampleGenotypeFeature->new(\@info,\@formats,\@samples_list);
my $serializer = Bio::EnsEMBL::IO::Writer::VCF4->new($translator);
$serializer->open($fh);

# Write headers
$serializer->write(Bio::EnsEMBL::IO::Object::VCF4Metadata->directive('fileformat', 'VCFv4.2'));
foreach my $i (@info) {
  $serializer->write(Bio::EnsEMBL::IO::Object::VCF4Metadata->info($i));
}
foreach my $f (@formats) {
  $serializer->write(Bio::EnsEMBL::IO::Object::VCF4Metadata->format($f));
}
$serializer->write(Bio::EnsEMBL::IO::Object::VCF4Metadata->header(\@samples_list));

foreach my $feature (@features) {
    $serializer->write($feature);
}

eq_or_diff(${$fh->string_ref()}, $vcf_string, "Checking is serializer matches expected VCF");

done_testing();