| 1 | package App::Sandy::Seq; | ||
| 2 | # ABSTRACT: Base class to simulate seq entries | ||
| 3 | |||
| 4 | 1 | ▶ | use App::Sandy::Base 'class'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | use App::Sandy::Quality; | ||
| 6 | |||
| 7 | use constant NUM_TRIES => 1000; | ||
| 8 | |||
| 9 | with qw{ | ||
| 10 | App::Sandy::Role::RunTimeTemplate | ||
| 11 | App::Sandy::Role::Template::Fastq | ||
| 12 | App::Sandy::Role::Template::Sam | ||
| 13 | }; | ||
| 14 | |||
| 15 | our $VERSION = '0.25'; # VERSION | ||
| 16 | |||
| 17 | has 'format' => ( | ||
| 18 | is => 'ro', | ||
| 19 | isa => 'My:Format', | ||
| 20 | required => 1 | ||
| 21 | ); | ||
| 22 | |||
| 23 | has 'template_id' => ( | ||
| 24 | is => 'ro', | ||
| 25 | isa => 'Str', | ||
| 26 | required => 1 | ||
| 27 | ); | ||
| 28 | |||
| 29 | has 'sequencing_error' => ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'My:NumHS', | ||
| 32 | required => 1 | ||
| 33 | ); | ||
| 34 | |||
| 35 | has 'quality_profile' => ( | ||
| 36 | is => 'ro', | ||
| 37 | isa => 'My:QualityP', | ||
| 38 | required => 1, | ||
| 39 | coerce => 1 | ||
| 40 | ); | ||
| 41 | |||
| 42 | has 'read_mean' => ( | ||
| 43 | is => 'ro', | ||
| 44 | isa => 'My:IntGt0', | ||
| 45 | required => 1 | ||
| 46 | ); | ||
| 47 | |||
| 48 | has 'read_stdd' => ( | ||
| 49 | is => 'ro', | ||
| 50 | isa => 'My:IntGe0', | ||
| 51 | required => 1 | ||
| 52 | ); | ||
| 53 | |||
| 54 | has '_template_id' => ( | ||
| 55 | traits => ['Code'], | ||
| 56 | is => 'ro', | ||
| 57 | isa => 'CodeRef', | ||
| 58 | builder => '_build_template_id', | ||
| 59 | lazy_build => 1, | ||
| 60 | handles => { | ||
| 61 | _gen_id => 'execute' | ||
| 62 | } | ||
| 63 | ); | ||
| 64 | |||
| 65 | has '_template_seq' => ( | ||
| 66 | traits => ['Code'], | ||
| 67 | is => 'ro', | ||
| 68 | isa => 'CodeRef', | ||
| 69 | builder => '_build_template_seq', | ||
| 70 | lazy_build => 1, | ||
| 71 | handles => { | ||
| 72 | _gen_seq => 'execute' | ||
| 73 | } | ||
| 74 | ); | ||
| 75 | |||
| 76 | has '_info' => ( | ||
| 77 | traits => ['Hash'], | ||
| 78 | is => 'ro', | ||
| 79 | isa => 'HashRef[Str]', | ||
| 80 | builder => '_build_info', | ||
| 81 | lazy_build => 1, | ||
| 82 | handles => { | ||
| 83 | _set_info => 'set', | ||
| 84 | _get_info => 'get' | ||
| 85 | } | ||
| 86 | ); | ||
| 87 | |||
| 88 | has '_read_size' => ( | ||
| 89 | traits => ['Code'], | ||
| 90 | is => 'ro', | ||
| 91 | isa => 'CodeRef', | ||
| 92 | builder => '_build_read_size', | ||
| 93 | lazy_build => 1, | ||
| 94 | handles => { | ||
| 95 | _get_read_size => 'execute' | ||
| 96 | } | ||
| 97 | ); | ||
| 98 | |||
| 99 | has '_quality' => ( | ||
| 100 | is => 'ro', | ||
| 101 | isa => 'App::Sandy::Quality', | ||
| 102 | builder => '_build_quality', | ||
| 103 | lazy_build => 1, | ||
| 104 | handles => ['gen_quality'] | ||
| 105 | ); | ||
| 106 | |||
| 107 | has '_sym_table' => ( | ||
| 108 | is => 'ro', | ||
| 109 | isa => 'HashRef[Str]', | ||
| 110 | builder => '_build_sym_table', | ||
| 111 | lazy_build => 1 | ||
| 112 | ); | ||
| 113 | |||
| 114 | sub BUILD { | ||
| 115 | my $self = shift; | ||
| 116 | ## Just to ensure that the lazy attributes are built before &new returns | ||
| 117 | $self->_quality; | ||
| 118 | } | ||
| 119 | |||
| 120 | sub _build_sym_table { | ||
| 121 | my $sym_table = { | ||
| 122 | '%q' => '$info->{quality_profile}', | ||
| 123 | '%m' => '$info->{read_mean}', | ||
| 124 | '%d' => '$info->{read_stdd}', | ||
| 125 | '%r' => '$info->{read_size}', | ||
| 126 | '%e' => '$info->{sequencing_error}', | ||
| 127 | '%c' => '$info->{seq_id}', | ||
| 128 | '%C' => '$info->{seq_id_type}', | ||
| 129 | '%a' => '$info->{start_ref}', | ||
| 130 | '%b' => '$info->{end_ref}', | ||
| 131 | '%t' => '$info->{start}', | ||
| 132 | '%n' => '$info->{end}', | ||
| 133 | '%i' => '$info->{instrument}', | ||
| 134 | '%I' => '$info->{id}', | ||
| 135 | '%R' => '$info->{read}', | ||
| 136 | '%U' => '$info->{num}', | ||
| 137 | '%s' => '$info->{strand}', | ||
| 138 | '%x' => '$info->{error}', | ||
| 139 | '%v' => '$info->{var}' | ||
| 140 | }; | ||
| 141 | |||
| 142 | return $sym_table; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub _build_template_id { | ||
| 146 | my $self = shift; | ||
| 147 | my $sym_table = $self->_sym_table; | ||
| 148 | return $self->with_compile_template($self->template_id, 'info', $sym_table); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub _build_template_seq { | ||
| 152 | my $self = shift; | ||
| 153 | |||
| 154 | my $format = $self->format; | ||
| 155 | my $gen_seq; | ||
| 156 | |||
| 157 | if ($format =~ /fastq/) { | ||
| 158 | $gen_seq = sub { $self->with_fastq_template(@_) }; | ||
| 159 | } elsif ($format eq 'sam') { | ||
| 160 | $gen_seq = sub { $self->with_sam_align_template(@_) }; | ||
| 161 | } elsif ($format eq 'bam') { | ||
| 162 | $gen_seq = sub { $self->with_bam_align_template(@_) }; | ||
| 163 | } else { | ||
| 164 | croak "No valid format: '$format'"; | ||
| 165 | } | ||
| 166 | |||
| 167 | return $gen_seq; | ||
| 168 | } | ||
| 169 | |||
| 170 | sub _build_info { | ||
| 171 | my $self = shift; | ||
| 172 | |||
| 173 | my $info = { | ||
| 174 | instrument => 'SR', | ||
| 175 | quality_profile => $self->quality_profile, | ||
| 176 | sequencing_error => $self->sequencing_error, | ||
| 177 | read_mean => $self->read_mean, | ||
| 178 | read_stdd => $self->read_stdd | ||
| 179 | }; | ||
| 180 | |||
| 181 | return $info; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub _build_read_size { | ||
| 185 | my $self = shift; | ||
| 186 | my $fun; | ||
| 187 | |||
| 188 | if ($self->read_stdd == 0) { | ||
| 189 | $fun = sub { $self->read_mean }; | ||
| 190 | } else { | ||
| 191 | $fun = sub { | ||
| 192 | my $rng = shift; | ||
| 193 | my $size = 0; | ||
| 194 | my $random_tries = 0; | ||
| 195 | until ($size > 0) { | ||
| 196 | if (++$random_tries > NUM_TRIES) { | ||
| 197 | croak "So many tries to calculate a seq size greater than zero ..."; | ||
| 198 | } | ||
| 199 | $size = $rng->get_norm($self->read_mean, $self->read_stdd); | ||
| 200 | } | ||
| 201 | return $size; | ||
| 202 | }; | ||
| 203 | } | ||
| 204 | |||
| 205 | return $fun; | ||
| 206 | } | ||
| 207 | |||
| 208 | sub _build_quality { | ||
| 209 | my $self = shift; | ||
| 210 | App::Sandy::Quality->new( | ||
| 211 | quality_profile => $self->quality_profile | ||
| 212 | ); | ||
| 213 | } | ||
| 214 | |||
| 215 | sub gen_sam_header { | ||
| 216 | my ($self, $argv) = @_; | ||
| 217 | my $format = $self->format; | ||
| 218 | my $header; | ||
| 219 | |||
| 220 | if ($format eq 'sam') { | ||
| 221 | $header = $self->with_sam_header_template($argv); | ||
| 222 | } elsif ($format eq 'bam') { | ||
| 223 | $header = $self->with_bam_header_template($argv); | ||
| 224 | } else { | ||
| 225 | croak "No valid format: '$format'"; | ||
| 226 | } | ||
| 227 | |||
| 228 | return $header; | ||
| 229 | } | ||
| 230 | |||
| 231 | sub gen_eof_marker { | ||
| 232 | my ($self, $file) = @_; | ||
| 233 | |||
| 234 | open my $fh, ">>" => $file | ||
| 235 | or croak "Cannot open '$file': $!"; | ||
| 236 | |||
| 237 | binmode $fh; | ||
| 238 | |||
| 239 | my $eof_ref = $self->with_eof_marker; | ||
| 240 | print {$fh} $$eof_ref; | ||
| 241 | |||
| 242 | close $fh | ||
| 243 | or croak "Cannot write to '$file': $!"; | ||
| 244 | } | ||
| 245 | |||
| 246 | __END__ | ||