« lib/BoutrosLab/TSVStream/IO/Role/Base/Fixed.pm
↑ Summary
lib/BoutrosLab/TSVStream/IO/Role/Fixed.pm »
| 1 | # safe Perl | ||
| 2 | use warnings; | ||
| 3 | use strict; | ||
| 4 | use Carp; | ||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | BoutrosLab::TSVStream::Format::VCF::Types | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | Collection of types used in VCF format fields. Used internally in | ||
| 13 | BoutrosLab::TSVStream::Format::VCF::Role. | ||
| 14 | |||
| 15 | =cut | ||
| 16 | |||
| 17 | package BoutrosLab::TSVStream::Format::VCF::Types; | ||
| 18 | |||
| 19 | use MooseX::Types -declare => [ | ||
| 20 | qw( | ||
| 21 | Str_No_Whitespace | ||
| 22 | VCF_Chrom | ||
| 23 | VCF_Ref | ||
| 24 | VCF_Ref_Full | ||
| 25 | VCF_Alt | ||
| 26 | VCF_Alt_Full | ||
| 27 | VCF_KV_Str | ||
| 28 | ) | ||
| 29 | ]; | ||
| 30 | |||
| 31 | use MooseX::Types::Moose qw( Int Str ArrayRef HashRef ); | ||
| 32 | |||
| 33 | subtype Str_No_Whitespace, | ||
| 34 | as Str, | ||
| 35 | where { /^\S+$/ }, | ||
| 36 | message {"may not contain whitespace characters"}; | ||
| 37 | |||
| 38 | subtype VCF_Chrom, as Str_No_Whitespace; | ||
| 39 | |||
| 40 | subtype VCF_Ref, | ||
| 41 | as Str, | ||
| 42 | where { /^-$/ || /^[CGAT]+$/i }, | ||
| 43 | message {"VCF_Ref must be '-' (dash), or a series of 'CGAT' characters"}; | ||
| 44 | |||
| 45 | subtype VCF_Ref_Full, | ||
| 46 | as Str, | ||
| 47 | # where { /^-$/ || /^[CGAT]+$/i }, | ||
| 48 | # message {"VCF_Ref must be '-' (dash), or a series of 'CGAT' characters"} | ||
| 49 | ; | ||
| 50 | |||
| 51 | subtype VCF_Alt, | ||
| 52 | as Str, | ||
| 53 | where { /^-$/ || /^[CGAT]+(?:,[CGAT]+)*$/i }, | ||
| 54 | message {"VCF_Alt must be '-' (dash), or one or more comma-separated series of 'CGAT' characters"}; | ||
| 55 | |||
| 56 | subtype VCF_Alt_Full, | ||
| 57 | as Str, | ||
| 58 | # where { /^-$/ || /^[CGAT]+(?:,[CGAT]+)*$/i }, | ||
| 59 | # message {"VCF_Alt must be '-' (dash), or one or more comma-separated series of 'CGAT' characters"} | ||
| 60 | ; | ||
| 61 | |||
| 62 | subtype VCF_KV_Str, | ||
| 63 | as 'BoutrosLab::TSVStream::Format::VCF::Types::KeyValueString'; | ||
| 64 | |||
| 65 | coerce VCF_KV_Str, | ||
| 66 | from Str, | ||
| 67 | via { BoutrosLab::TSVStream::Format::VCF::Types::KeyValueString->new($_) }; | ||
| 68 | |||
| 69 | |||
| 70 | package BoutrosLab::TSVStream::Format::VCF::Types::KeyValueString; | ||
| 71 | |||
| 72 | use overload | ||
| 73 | '""' => 'stringify'; | ||
| 74 | |||
| 75 | sub new { | ||
| 76 | my ($class, $data) = @_; | ||
| 77 | $class = ref($class) || $class; | ||
| 78 | my $data_hash = {}; | ||
| 79 | if (ref($data)) { # copy if it is already a HashRef (or KeyValueString object) | ||
| 80 | while (my ($key, $value) = each %$data) { | ||
| 81 | $data_hash->{$key} = $value; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | else { # split up a Str | ||
| 85 | my @split = split(';', $data); | ||
| 86 | foreach my $kv_pair (@split) { | ||
| 87 | my ($key, $value) = split('=', $kv_pair); | ||
| 88 | $data_hash->{$key} = $value; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | return bless $data_hash, $class; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub clone { | ||
| 95 | my $self = shift; | ||
| 96 | return $self->new($self); | ||
| 97 | } | ||
| 98 | |||
| 99 | sub stringify { | ||
| 100 | my ($self) = @_; | ||
| 101 | my $str = ''; | ||
| 102 | foreach my $key (sort keys %{$self}) { | ||
| 103 | my $val = $self->{$key}; | ||
| 104 | $str .= $key; | ||
| 105 | $str .= "=$val" if defined $val; | ||
| 106 | $str .= ";"; | ||
| 107 | } | ||
| 108 | $str =~ s/;$//; | ||
| 109 | return $str; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | =head1 AUTHOR | ||
| 114 | |||
| 115 | John Macdonald - Boutros Lab | ||
| 116 | |||
| 117 | =head1 ACKNOWLEDGEMENTS | ||
| 118 | |||
| 119 | Paul Boutros, Phd, PI - Boutros Lab | ||
| 120 | |||
| 121 | The Ontario Institute for Cancer Research | ||
| 122 | |||
| 123 | =cut | ||
| 124 | |||
| 125 | 1; | ||
| 126 |