| 1 | package App::Sandy::Role::Template::Sam; | ||
| 2 | # ABSTRACT: Sam template role | ||
| 3 | |||
| 4 | 1 | ▶ | use App::Sandy::Base 'role'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | use constant { | ||
| 7 | PAIRED => 0x1, | ||
| 8 | PROPER_PAIR => 0x2, | ||
| 9 | UNMAP => 0x4, | ||
| 10 | MUNMAP => 0x8, | ||
| 11 | REVERSE => 0x10, | ||
| 12 | MREVERSE => 0x20, | ||
| 13 | READ1 => 0x40, | ||
| 14 | READ2 => 0x80, | ||
| 15 | SECONDARY => 0x100, | ||
| 16 | QCFAIL => 0x200, | ||
| 17 | DUP => 0x400, | ||
| 18 | SUPPLEMENTARY => 0x800, | ||
| 19 | SAM_VERSION => '1.0' | ||
| 20 | }; | ||
| 21 | |||
| 22 | our $VERSION = '0.25'; # VERSION | ||
| 23 | |||
| 24 | has '_flags' => ( | ||
| 25 | traits => ['Array'], | ||
| 26 | is => 'ro', | ||
| 27 | isa => 'ArrayRef', | ||
| 28 | builder => '_build_flags', | ||
| 29 | lazy_build => 1, | ||
| 30 | handles => { | ||
| 31 | _get_flag => 'get' | ||
| 32 | } | ||
| 33 | ); | ||
| 34 | |||
| 35 | sub _build_flags { | ||
| 36 | my $single_end = UNMAP; | ||
| 37 | my $read1 = PAIRED|UNMAP|MUNMAP|READ1; | ||
| 38 | my $read2 = PAIRED|UNMAP|MUNMAP|READ2; | ||
| 39 | return [$single_end, $read1, $read2]; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub with_eof_marker { | ||
| 43 | my $eof = pack("C28", | ||
| 44 | 0x1f, 0x8b, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00, | ||
| 45 | 0x00, 0xff, 0x06, 0x00, 0x42, 0x43, 0x02, 0x00, | ||
| 46 | 0x1b, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
| 47 | 0x00, 0x00, 0x00, 0x00 | ||
| 48 | ); | ||
| 49 | return \$eof; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub with_sam_header_template { | ||
| 53 | my ($self, $argv) = @_; | ||
| 54 | my $cl = "sandy @$argv"; | ||
| 55 | |||
| 56 | ## no critic | ||
| 57 | my $vn = do { no strict 'vars'; $VERSION || "dev" }; | ||
| 58 | ## use critic | ||
| 59 | |||
| 60 | my $header = sprintf "\@HD\tVN:%s\tSO:queryname\n\@PG\tID:%s\tPN:%s\tVN:%s\tCL:%s\n" | ||
| 61 | => SAM_VERSION, 'sandy', 'sandy', $vn, $cl; | ||
| 62 | |||
| 63 | return \$header; | ||
| 64 | } | ||
| 65 | |||
| 66 | sub with_sam_align_template { | ||
| 67 | my ($self, $seqid_ref, $read_ref, $quality_ref, $read_flag) = @_; | ||
| 68 | |||
| 69 | my $sam = sprintf "%s\t%d\t*\t0\t0\t*\t*\t0\t0\t%s\t%s\n" | ||
| 70 | => $$seqid_ref, $self->_get_flag($read_flag), $$read_ref, $$quality_ref; | ||
| 71 | |||
| 72 | return \$sam; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub with_bam_header_template { | ||
| 76 | my ($self, $argv) = @_; | ||
| 77 | my $header_ref = $self->with_sam_header_template($argv); | ||
| 78 | |||
| 79 | my $l_text = length($$header_ref); | ||
| 80 | my $block = pack("c4l<", 66, 65, 77, 1, $l_text) . $$header_ref . pack("l<", 0); | ||
| 81 | |||
| 82 | return \$block; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub with_bam_align_template { | ||
| 86 | my ($self, $seqid_ref, $read_ref, $quality_ref, $read_flag, $read_size) = @_; | ||
| 87 | |||
| 88 | # Catch the seqid size nul padded | ||
| 89 | my $seqid_size = length($$seqid_ref) + 1; | ||
| 90 | |||
| 91 | # Encode all bases in 4 bits 0-15 (hex 0x0-0xf) and | ||
| 92 | # pack the codes to high level nibbles | ||
| 93 | my $packed_read = pack("H*", | ||
| 94 | $$read_ref =~ tr [=ACMGRSVTWYHKDBNacmgrsvtwyhkdbn\x00-\xff] [\x00-\x0f\x01-\x0f]r); | ||
| 95 | |||
| 96 | # quality to ascii and calculate the phred score [!-~] | ||
| 97 | my $packed_quality = $$quality_ref =~ tr [\x21-\x7e\x00-\xff] [\x00-\x5d\x00]r; | ||
| 98 | |||
| 99 | # Calculate the total block size in bytes | ||
| 100 | my $block_size = $seqid_size + $read_size + int(($read_size + 1) / 2) + 32; | ||
| 101 | |||
| 102 | # Lets set this align block | ||
| 103 | my $block = pack("l<3C2S<3l<4", | ||
| 104 | $block_size, | ||
| 105 | -1, | ||
| 106 | -1, | ||
| 107 | $seqid_size, | ||
| 108 | 0, | ||
| 109 | 4680, | ||
| 110 | 0, | ||
| 111 | $self->_get_flag($read_flag), | ||
| 112 | $read_size, | ||
| 113 | -1, | ||
| 114 | -1, | ||
| 115 | 0 | ||
| 116 | ); | ||
| 117 | |||
| 118 | # Cat the seqid, read and quality packs to block | ||
| 119 | $block .= $$seqid_ref . pack("x") . $packed_read . $packed_quality; | ||
| 120 | return \$block; | ||
| 121 | } | ||
| 122 | |||
| 123 | __END__ | ||