| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PreprocessApp::cleanup_alignment - Keep a single record for multimappers, sort and collapse similar STAR alignments. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools-preprocess cleanup_alignment [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Clean up STAR aligner SAM output file. Will keep only a single record for | ||
| 12 | multimappers. Will sort STAR alignments and collapse alignments with same | ||
| 13 | sequence and position. Will add an XC:i tag for each alignment with the | ||
| 14 | copy number - the number of collapsed alignments. | ||
| 15 | |||
| 16 | =head1 OPTIONS | ||
| 17 | |||
| 18 | Input. | ||
| 19 | --sam <Str> SAM file with STAR alignments. | ||
| 20 | |||
| 21 | Output | ||
| 22 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 23 | extension to path. [Default: ./] | ||
| 24 | |||
| 25 | Other options. | ||
| 26 | -v --verbose print progress lines and extra information. | ||
| 27 | -h -? --usage --help print help message | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | package CLIPSeqTools::PreprocessApp::cleanup_alignment; | ||
| 32 | $CLIPSeqTools::PreprocessApp::cleanup_alignment::VERSION = '1.0.0'; | ||
| 33 | |||
| 34 | # Make it an app command | ||
| 35 | use MooseX::App::Command; | ||
| 36 | extends 'CLIPSeqTools::PreprocessApp'; | ||
| 37 | |||
| 38 | |||
| 39 | ####################################################################### | ||
| 40 | ####################### Load External modules ##################### | ||
| 41 | ####################################################################### | ||
| 42 | use Modern::Perl; | ||
| 43 | use autodie; | ||
| 44 | use namespace::autoclean; | ||
| 45 | |||
| 46 | |||
| 47 | ####################################################################### | ||
| 48 | ####################### Command line options ###################### | ||
| 49 | ####################################################################### | ||
| 50 | option 'sam' => ( | ||
| 51 | is => 'rw', | ||
| 52 | isa => 'Str', | ||
| 53 | required => 1, | ||
| 54 | documentation => 'SAM file with STAR alignments.', | ||
| 55 | ); | ||
| 56 | |||
| 57 | |||
| 58 | ####################################################################### | ||
| 59 | ########################## Consume Roles ########################## | ||
| 60 | ####################################################################### | ||
| 61 | with | ||
| 62 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 63 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 64 | -excludes => 'validate_args', | ||
| 65 | }; | ||
| 66 | |||
| 67 | |||
| 68 | ####################################################################### | ||
| 69 | ######################## Interface Methods ######################## | ||
| 70 | ####################################################################### | ||
| 71 | sub validate_args { | ||
| 72 | my ($self) = @_; | ||
| 73 | |||
| 74 | $self->_validate_args_for_output_prefix; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub run { | ||
| 78 | my ($self) = @_; | ||
| 79 | |||
| 80 | warn "Starting job: cleanup_alignment\n"; | ||
| 81 | |||
| 82 | warn "Validating arguments\n" if $self->verbose; | ||
| 83 | $self->validate_args(); | ||
| 84 | |||
| 85 | warn "Creating output path\n" if $self->verbose; | ||
| 86 | $self->make_path_for_output_prefix(); | ||
| 87 | |||
| 88 | warn "Keeping a single copy for multimappers\n" if $self->verbose; | ||
| 89 | $self->keep_single_copy_for_multimappers($self->sam, $self->o_prefix.'single.sam'); | ||
| 90 | |||
| 91 | warn "Sorting SAM file\n" if $self->verbose; | ||
| 92 | $self->sort_sam($self->o_prefix.'single.sam', $self->o_prefix.'single.sorted.sam'); | ||
| 93 | |||
| 94 | warn "Collapsing alignments with same sequence and position. Adding XC:i tag with copy number\n" if $self->verbose; | ||
| 95 | $self->collapse_sorted_sam($self->o_prefix.'single.sorted.sam', $self->o_prefix.'single.sorted.collapsed.sam'); | ||
| 96 | } | ||
| 97 | |||
| 98 | sub keep_single_copy_for_multimappers { | ||
| 99 | my ($self, $in_sam, $out_sam) = @_; | ||
| 100 | |||
| 101 | open (my $IN, '<', $in_sam); | ||
| 102 | open (my $OUT, '>', $out_sam); | ||
| 103 | while (my $line = <$IN>) { | ||
| 104 | if ($line !~ /^@/) { | ||
| 105 | my $flag = (split(/\t/, $line))[1]; | ||
| 106 | next if $flag & 256; | ||
| 107 | } | ||
| 108 | print $OUT $line; | ||
| 109 | } | ||
| 110 | close $IN; | ||
| 111 | close $OUT; | ||
| 112 | } | ||
| 113 | |||
| 114 | sub sort_sam { | ||
| 115 | my ($self, $in_sam, $out_sam) = @_; | ||
| 116 | |||
| 117 | my $cmd = "(grep \"^@\" $in_sam && grep -v \"^@\" $in_sam | sort -k 3,3 -k 4,4n) > $out_sam"; | ||
| 118 | system "$cmd"; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub collapse_sorted_sam { | ||
| 122 | my ($self, $in_sam, $out_sam) = @_; | ||
| 123 | |||
| 124 | my %coords_counts; | ||
| 125 | my ($current_pos, $current_rname); | ||
| 126 | |||
| 127 | open (my $IN_SAM, '<', $in_sam); | ||
| 128 | open (my $OUT_SAM, '>', $out_sam); | ||
| 129 | while (my $line = <$IN_SAM>) { | ||
| 130 | chomp $line; | ||
| 131 | |||
| 132 | # Skip the header | ||
| 133 | if ($line =~ /^@/) { | ||
| 134 | say $OUT_SAM $line; | ||
| 135 | next; | ||
| 136 | }; | ||
| 137 | |||
| 138 | # Split the SAM line and get required fields | ||
| 139 | my @fields = split(/\t/, $line); | ||
| 140 | my ($flag, $rname, $pos, $seq) = @fields[1, 2, 3, 9]; | ||
| 141 | $fields[10] = '*'; # delete quality | ||
| 142 | |||
| 143 | # Skip unmapped reads | ||
| 144 | next if $flag & 4; #unmapped | ||
| 145 | |||
| 146 | # Get the strand | ||
| 147 | my $strand = $flag & 16 ? -1 : 1; | ||
| 148 | |||
| 149 | # Check if a similar record has been read | ||
| 150 | if (defined $current_pos and defined $current_rname and ($rname eq $current_rname) and ($pos == $current_pos)) { | ||
| 151 | if (!exists $coords_counts{$strand}{$seq}) { | ||
| 152 | $coords_counts{$strand}{$seq} = [@fields, 0]; # Q: Why use hash? | ||
| 153 | # A: SAM is sorted by rname and pos. | ||
| 154 | # Different sequences that map at exactly the same position could come in a non consecutive manner. | ||
| 155 | # Obviously we don't want to collapse these into one. | ||
| 156 | } | ||
| 157 | |||
| 158 | $coords_counts{$strand}{$seq}->[-1]++; | ||
| 159 | } | ||
| 160 | else { | ||
| 161 | if (defined $current_pos and defined $current_rname) { | ||
| 162 | _print_records_found_on_previous_pos(\%coords_counts, $OUT_SAM); | ||
| 163 | } | ||
| 164 | %coords_counts = (); | ||
| 165 | $current_rname = $rname; | ||
| 166 | $current_pos = $pos; | ||
| 167 | |||
| 168 | $coords_counts{$strand}{$seq} = [@fields, 1]; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | _print_records_found_on_previous_pos(\%coords_counts, $OUT_SAM); | ||
| 172 | close $IN_SAM; | ||
| 173 | close $OUT_SAM; | ||
| 174 | } | ||
| 175 | |||
| 176 | |||
| 177 | ####################################################################### | ||
| 178 | ######################## Private Functions ######################## | ||
| 179 | ####################################################################### | ||
| 180 | sub _print_records_found_on_previous_pos { | ||
| 181 | my ($stored_records_hashref, $OUT) = @_; | ||
| 182 | |||
| 183 | foreach my $strand (keys %{$stored_records_hashref}) { | ||
| 184 | foreach my $seq (keys %{$stored_records_hashref->{$strand}}) { | ||
| 185 | my $fields = $stored_records_hashref->{$strand}->{$seq}; | ||
| 186 | $fields->[-1] = join(':', 'XC', 'i', $fields->[-1]); | ||
| 187 | say $OUT join("\t", @$fields); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | 1; |