« lib/BoutrosLab/TSVStream/IO/Role/Fixed.pm
↑ Summary
lib/BoutrosLab/TSVStream/Format/VCF/Types.pm »
| 1 | # safe Perl | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | BoutrosLab::TSVStream::Format::VCF::Role | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | Collection of roles that implement VCF format capable objects. | ||
| 13 | This role provides the common attributes for VCF format. | ||
| 14 | It also supplies methods that allow a VCF object act as a AnnovarInput | ||
| 15 | object. | ||
| 16 | |||
| 17 | =head1 DESCRIPTION | ||
| 18 | |||
| 19 | These roles are combined with an IO/Base role to provide one of the | ||
| 20 | two standard variants (Fixed and Dyn - depending upon whether you wish | ||
| 21 | to allow only the default set of fields or to allow additional trailing | ||
| 22 | fields following the default ones.) | ||
| 23 | |||
| 24 | See BoutrosLab::TSVStream::Format::Human::Fixed for an example. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | package BoutrosLab::TSVStream::Format::VCF::Role::Base; | ||
| 29 | |||
| 30 | use Moose::Role; | ||
| 31 | use BoutrosLab::TSVStream::Format::VCF::Types qw( VCF_Chrom VCF_Ref Str_No_Whitespace VCF_KV_Str ); | ||
| 32 | use MooseX::Types::Moose qw( ArrayRef HashRef Int ); | ||
| 33 | use MooseX::ClassAttribute; | ||
| 34 | use namespace::autoclean; | ||
| 35 | |||
| 36 | =head1 Class Attributes | ||
| 37 | |||
| 38 | =head2 _fields | ||
| 39 | |||
| 40 | The B<_fields> attribute is required by the IO roles to determine | ||
| 41 | which fields are to be read or written. In this case, the fields | ||
| 42 | are C<chrom>, C<start>, C<end>, C<ref>, and C<alt>, which are described | ||
| 43 | as attributes below. | ||
| 44 | |||
| 45 | =cut | ||
| 46 | |||
| 47 | class_has '_fields' => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => 'ArrayRef', | ||
| 50 | default => sub { [qw(chrom pos id ref alt qual filter info)] } | ||
| 51 | ); | ||
| 52 | |||
| 53 | =head1 Attributes | ||
| 54 | |||
| 55 | =head2 chrom | ||
| 56 | |||
| 57 | The B<chrom> attribute provides the name of the chromosome that is | ||
| 58 | being specified. There are different names used for different | ||
| 59 | species of organism; and for different ways of processing the same | ||
| 60 | organism; so this attribute is provided in a separate role. | ||
| 61 | |||
| 62 | =cut | ||
| 63 | |||
| 64 | has 'chrom' => ( is => 'rw', isa => VCF_Chrom ); | ||
| 65 | |||
| 66 | =head2 pos | ||
| 67 | |||
| 68 | The B<pos> attribute is an integer that provide the start | ||
| 69 | position within the chromosome that is being described. | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | has 'pos' => ( is => 'rw', isa => 'Int' ); | ||
| 74 | |||
| 75 | =head2 id | ||
| 76 | |||
| 77 | The B<id> attribute is a string, not yet supported (in this module} for any specific use. | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | has 'id' => ( is => 'rw', isa => Str_No_Whitespace ); | ||
| 82 | |||
| 83 | =head2 ref | ||
| 84 | |||
| 85 | =head2 alt | ||
| 86 | |||
| 87 | The B<ref> and B<alt> attributes describe amino acid sequences. | ||
| 88 | They can either contain the string I<'-'>, or a sequence of acids | ||
| 89 | (C<'A'>, C<'C'>, C<'G'>, and C<'T'> of at most 500 acids. (E.G.: | ||
| 90 | CGATCGAT) | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | =head2 qual | ||
| 95 | |||
| 96 | =head2 filter | ||
| 97 | |||
| 98 | =head2 info | ||
| 99 | |||
| 100 | The B<qual>, B<filter>, and B<ifno> attributes are strings, not yet supported (in this module} for any specific use. | ||
| 101 | |||
| 102 | =cut | ||
| 103 | |||
| 104 | has [qw(qual filter)] => ( is => 'rw', isa => Str_No_Whitespace ); | ||
| 105 | |||
| 106 | has 'info' => ( | ||
| 107 | is => 'rw', | ||
| 108 | isa => VCF_KV_Str, | ||
| 109 | coerce => 1 | ||
| 110 | ); | ||
| 111 | |||
| 112 | =head2 _reader_args | ||
| 113 | |||
| 114 | =head2 _writer_args | ||
| 115 | |||
| 116 | =head2 info | ||
| 117 | |||
| 118 | The B<_reader_args> and B<_writer_args> attributes are internal settings. | ||
| 119 | They cause the reader and writer be given special args required for handling | ||
| 120 | VCF format files properly. | ||
| 121 | |||
| 122 | =cut | ||
| 123 | |||
| 124 | class_has '_reader_args' => ( | ||
| 125 | is => 'ro', | ||
| 126 | isa => 'HashRef', | ||
| 127 | init_arg => undef, | ||
| 128 | default => sub { | ||
| 129 | return { | ||
| 130 | pre_header => 1, | ||
| 131 | pre_comment => 0, | ||
| 132 | comment => 0, | ||
| 133 | pre_header_pattern => qr/^##/, | ||
| 134 | header_fix => sub { | ||
| 135 | my $line = shift; | ||
| 136 | my %newline; | ||
| 137 | ($newline{line} = $line->{line}) =~ s/^#\s*//; | ||
| 138 | $newline{fields} = [ @{ $line->{fields} } ]; | ||
| 139 | $newline{fields}[0] =~ s/^#\s*//; | ||
| 140 | return \%newline; | ||
| 141 | }, | ||
| 142 | }; | ||
| 143 | } | ||
| 144 | ); | ||
| 145 | |||
| 146 | class_has '_writer_args' => ( | ||
| 147 | is => 'ro', | ||
| 148 | isa => 'HashRef', | ||
| 149 | init_arg => undef, | ||
| 150 | default => sub { | ||
| 151 | return { | ||
| 152 | pre_header => 1, | ||
| 153 | pre_comment => 0, | ||
| 154 | comment => 0, | ||
| 155 | header_fix => sub { | ||
| 156 | my $headers = [ @{(shift)} ]; | ||
| 157 | $headers->[0] =~ s/^/#/; | ||
| 158 | my @uc_headers = map { uc } @{$headers}; | ||
| 159 | return \@uc_headers; | ||
| 160 | }, | ||
| 161 | }; | ||
| 162 | } | ||
| 163 | ); | ||
| 164 | |||
| 165 | #### Methods | ||
| 166 | |||
| 167 | =head1 WRAPPER ROLES | ||
| 168 | |||
| 169 | =head2 BoutrosLab::TSVStream::Format::VCF::Role::AsAnnovarInputChr | ||
| 170 | |||
| 171 | =head2 BoutrosLab::TSVStream::Format::VCF::Role::AsAnnovarInputNoChr | ||
| 172 | |||
| 173 | These wrapper roles provide a wrapper around the basic VCF role that | ||
| 174 | allows a vcf format record to be used as if it were an AnnovarInput | ||
| 175 | (or AnnovarInput...NoChr) format record. This is done by providing | ||
| 176 | B<start>, B<end>, and B<chr> methods that match the attributes that | ||
| 177 | are normally present in an AnnovarInput record. | ||
| 178 | |||
| 179 | =head3 start | ||
| 180 | |||
| 181 | =head3 end | ||
| 182 | |||
| 183 | The B<start> and B<end> attributes are integers that provide the start | ||
| 184 | and end position within the chromosome that is being described. The | ||
| 185 | B<start> value is just a synonym for the B<pos> attribute. The B<end> | ||
| 186 | attribute is computed from the B<pos> value and the length of the | ||
| 187 | B<ref> attribute. | ||
| 188 | |||
| 189 | =head3 chr | ||
| 190 | |||
| 191 | The B<chr> attribute is the B<chrom> attribute with a leading 'chr' | ||
| 192 | string either forced or removed. | ||
| 193 | |||
| 194 | =cut | ||
| 195 | |||
| 196 | package BoutrosLab::TSVStream::Format::VCF::Role::Full; | ||
| 197 | |||
| 198 | use Moose::Role; | ||
| 199 | use namespace::autoclean; | ||
| 200 | use BoutrosLab::TSVStream::Format::VCF::Types qw( VCF_Ref_Full VCF_Alt_Full ); | ||
| 201 | |||
| 202 | has 'ref' => ( is => 'rw', isa => VCF_Ref_Full ); | ||
| 203 | has 'alt' => ( is => 'rw', isa => VCF_Alt_Full ); | ||
| 204 | |||
| 205 | package BoutrosLab::TSVStream::Format::VCF::Role::RecSNV; | ||
| 206 | |||
| 207 | use Moose::Role; | ||
| 208 | use namespace::autoclean; | ||
| 209 | use BoutrosLab::TSVStream::Format::VCF::Types qw( VCF_Ref VCF_Alt ); | ||
| 210 | |||
| 211 | has 'ref' => ( is => 'rw', isa => VCF_Ref ); | ||
| 212 | has 'alt' => ( is => 'rw', isa => VCF_Alt ); | ||
| 213 | |||
| 214 | package BoutrosLab::TSVStream::Format::VCF::Role::WithChr; | ||
| 215 | |||
| 216 | use Carp qw(croak); | ||
| 217 | |||
| 218 | use Moose::Role; | ||
| 219 | use namespace::autoclean; | ||
| 220 | use BoutrosLab::TSVStream::Format::AnnovarInput::Types qw( AI_ChrHumanTagWithChr ); | ||
| 221 | |||
| 222 | sub chr { | ||
| 223 | my $self = shift; | ||
| 224 | my $chrom = my $chr = $self->chrom; | ||
| 225 | if ($chr =~ /^chr/i) { | ||
| 226 | $chr =~ s/^chr/chr/i; | ||
| 227 | } | ||
| 228 | else { | ||
| 229 | $chr = "chr$chr"; | ||
| 230 | } | ||
| 231 | croak( "chrom ($chrom) failed to convert to AnnovarInput chr" ) | ||
| 232 | unless is_AI_ChrHumanTagWithChr($chr); | ||
| 233 | return to_AI_ChrHumanTagWithChr($chr); | ||
| 234 | } | ||
| 235 | |||
| 236 | package BoutrosLab::TSVStream::Format::VCF::Role::WithNoChr; | ||
| 237 | |||
| 238 | use Carp qw(croak); | ||
| 239 | |||
| 240 | use Moose::Role; | ||
| 241 | use namespace::autoclean; | ||
| 242 | use BoutrosLab::TSVStream::Format::AnnovarInput::Types qw( AI_ChrHumanTagNoChr ); | ||
| 243 | |||
| 244 | sub chr { | ||
| 245 | my $self = shift; | ||
| 246 | my $chrom = my $chr = $self->chrom; | ||
| 247 | $chr =~ s/^chr//i; | ||
| 248 | croak( "chrom ($chrom) failed to convert to AnnovarInput chr" ) | ||
| 249 | unless is_AI_ChrHumanTagNoChr($chr); | ||
| 250 | return to_AI_ChrHumanTagNoChr($chr); | ||
| 251 | } | ||
| 252 | |||
| 253 | package BoutrosLab::TSVStream::Format::VCF::Role::FullChr; | ||
| 254 | |||
| 255 | use Moose::Role; | ||
| 256 | use namespace::autoclean; | ||
| 257 | |||
| 258 | with 'BoutrosLab::TSVStream::Format::VCF::Role::Base', | ||
| 259 | 'BoutrosLab::TSVStream::Format::VCF::Role::Full', | ||
| 260 | 'BoutrosLab::TSVStream::Format::VCF::Role::WithChr'; | ||
| 261 | |||
| 262 | package BoutrosLab::TSVStream::Format::VCF::Role::FullNoChr; | ||
| 263 | |||
| 264 | use Moose::Role; | ||
| 265 | use namespace::autoclean; | ||
| 266 | |||
| 267 | with 'BoutrosLab::TSVStream::Format::VCF::Role::Base', | ||
| 268 | 'BoutrosLab::TSVStream::Format::VCF::Role::Full', | ||
| 269 | 'BoutrosLab::TSVStream::Format::VCF::Role::WithNoChr'; | ||
| 270 | |||
| 271 | package BoutrosLab::TSVStream::Format::VCF::Role; | ||
| 272 | |||
| 273 | use Moose::Role; | ||
| 274 | use namespace::autoclean; | ||
| 275 | |||
| 276 | with 'BoutrosLab::TSVStream::Format::VCF::Role::Base', | ||
| 277 | 'BoutrosLab::TSVStream::Format::VCF::Role::RecSNV'; | ||
| 278 | |||
| 279 | package BoutrosLab::TSVStream::Format::VCF::Role::AsAnnovarInput; | ||
| 280 | |||
| 281 | use Moose::Role; | ||
| 282 | use namespace::autoclean; | ||
| 283 | |||
| 284 | with 'BoutrosLab::TSVStream::Format::VCF::Role'; | ||
| 285 | |||
| 286 | sub start { | ||
| 287 | my $self = shift; | ||
| 288 | return $self->pos; | ||
| 289 | } | ||
| 290 | |||
| 291 | sub end { | ||
| 292 | my $self = shift; | ||
| 293 | return $self->pos + length($self->ref) - 1; | ||
| 294 | } | ||
| 295 | |||
| 296 | package BoutrosLab::TSVStream::Format::VCF::Role::AsAnnovarInputChr; | ||
| 297 | |||
| 298 | use Moose::Role; | ||
| 299 | use namespace::autoclean; | ||
| 300 | |||
| 301 | with 'BoutrosLab::TSVStream::Format::VCF::Role::AsAnnovarInput', | ||
| 302 | 'BoutrosLab::TSVStream::Format::VCF::Role::WithChr'; | ||
| 303 | |||
| 304 | package BoutrosLab::TSVStream::Format::VCF::Role::AsAnnovarInputNoChr; | ||
| 305 | |||
| 306 | use Moose::Role; | ||
| 307 | use namespace::autoclean; | ||
| 308 | |||
| 309 | with 'BoutrosLab::TSVStream::Format::VCF::Role::AsAnnovarInput', | ||
| 310 | 'BoutrosLab::TSVStream::Format::VCF::Role::WithNoChr'; | ||
| 311 | |||
| 312 | =head1 SEE ALSO | ||
| 313 | |||
| 314 | =over | ||
| 315 | |||
| 316 | =item BoutrosLab::TSVStream::Format | ||
| 317 | |||
| 318 | This describes how IO-capable objects (such as ones created using | ||
| 319 | a subrole of this role) are defined. | ||
| 320 | |||
| 321 | =item BoutrosLab::TSVStream::IO | ||
| 322 | |||
| 323 | This describes of how readers and writers convert objects to or | ||
| 324 | from a text stream. | ||
| 325 | |||
| 326 | =item - BoutrosLab::TSVStream::Format::VCF::Fixed | ||
| 327 | |||
| 328 | =item - BoutrosLab::TSVStream::Format::VCF::Dyn | ||
| 329 | |||
| 330 | These are the two variants for the object specification for VCF | ||
| 331 | objects. Those are the modules that you will typically B<use> | ||
| 332 | and refer to in your program. | ||
| 333 | |||
| 334 | Fixed/Dyn specifies whether only the standard fields are expected, | ||
| 335 | or if a dynamic list of additional fields may occur. | ||
| 336 | |||
| 337 | =back | ||
| 338 | |||
| 339 | =head1 AUTHOR | ||
| 340 | |||
| 341 | John Macdonald - Boutros Lab | ||
| 342 | |||
| 343 | =head1 ACKNOWLEDGEMENTS | ||
| 344 | |||
| 345 | Paul Boutros, Phd, PI - Boutros Lab | ||
| 346 | |||
| 347 | The Ontario Institute for Cancer Research | ||
| 348 | |||
| 349 | =cut | ||
| 350 | |||
| 351 | 1; | ||
| 352 |