File: export2sam.pl

package info (click to toggle)
samtools 0.1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,168 kB
  • ctags: 2,085
  • sloc: ansic: 12,759; perl: 1,669; makefile: 156; python: 141
file content (461 lines) | stat: -rwxr-xr-x 15,255 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/env perl
#
#
# Script to convert GERALD export files to SAM format.
#
#
#
########## License:
#
# The MIT License
#
# Original SAMtools version 0.1.2 copyright (c) 2008-2009 Genome Research Ltd.
# Modifications from version 0.1.2 to 2.0.0 copyright (c) 2010 Illumina, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
#
#
########## ChangeLog:
#
# Version: 2.0.0 (15FEB2010)
#   Script updated by Illumina in conjunction with CASAVA 1.7.0 release.
#   Major changes are as follows:
#   - The CIGAR string has been updated to include all gaps from ELANDv2 alignments.
#   - The ELAND single read alignment score is always stored in the optional "SM" field
#       and the ELAND paired read alignment score is stored in the optional "AS" field
#       when it exists.
#   - The MAPQ value is set to the higher of the two alignment scores, but no greater
#       than 254,  i.e. min(254,max(SM,AS))
#   - The SAM "proper pair" bit (0x0002) is now set for read pairs meeting ELAND's
#       expected orientation and insert size criteria.
#   - The default quality score translation is set for export files which contain
#       Phread+64 quality values. An option, "--qlogodds", has been added to
#       translate quality values from the Solexa+64 format used in export files prior
#       to Pipeline 1.3
#   - The export match descriptor is now reverse-complemented when necessary such that
#       it always corresponds to the forward strand of the reference, to be consistent
#       with other information in the SAM record. It is now written to the optional
#       'XD' field (rather than 'MD') to acknowledge its minor differences from the 
#       samtools match descriptor (see additional detail below).
#   - An option, "--nofilter", has been added to include reads which have failed
#       primary analysis quality filtration. Such reads will have the corresponding
#       SAM flag bit (0x0200) set.
#   - Labels in the export 'contig' field are preserved by setting RNAME to
#       "$export_chromosome/$export_contig" when then contig label exists.
#
#
# Contact: lh3
# Version: 0.1.2 (03JAN2009)
#
#
#
########## Known Conversion Limitations:
#
# - Export records for reads that map to a position < 1 (allowed in export format), are converted
#     to unmapped reads in the SAM record.
# - Export records contain the reserved chromosome names: "NM" and "QC". "NM" indicates that the
#     aligner could not map the read to the reference sequence set, and "QC" means that the 
#     aligner did not attempt to map the read due to some technical limitation. Both of these 
#     alignment types are collapsed to the single unmapped alignment state in the SAM record.
# - The export match descriptor is slightly different than the samtools match descriptor. For
#     this reason it is stored in the optional SAM field 'XD' (and not 'MD'). Note that the
#     export match descriptor differs from the samtools version in two respects: (1) indels 
#     are explicitly closed with the '$' character and (2) insertions must be enumerated in
#     the match descriptor. For example a 35-base read with a two-base insertion is described
#     as: 20^2$14
#
#
#

my $version = "2.0.0";

use strict;
use warnings;

use File::Spec qw(splitpath);
use Getopt::Long;
use List::Util qw(min max);


use constant {
  EXPORT_INDEX => 6,
  EXPORT_READNO => 7,
  EXPORT_READ => 8,
  EXPORT_QUAL => 9,
  EXPORT_CHROM => 10,
  EXPORT_CONTIG => 11,
  EXPORT_POS => 12,
  EXPORT_STRAND => 13, 
  EXPORT_MD => 14,
  EXPORT_SEMAP => 15,
  EXPORT_PEMAP => 16,
  EXPORT_PASSFILT => 21,
};


use constant {
  SAM_QNAME => 0,
  SAM_FLAG => 1,
  SAM_RNAME => 2,
  SAM_POS => 3,
  SAM_MAPQ => 4,
  SAM_CIGAR => 5,
  SAM_MRNM => 6,
  SAM_MPOS => 7,
  SAM_ISIZE => 8,
  SAM_SEQ => 9,
  SAM_QUAL => 10,
};


# function prototypes for Richard's code
sub match_desc_to_cigar($);
sub match_desc_frag_length($);
sub reverse_compl_match_descriptor($);
sub write_header($;$;$);


&export2sam;
exit;




sub export2sam {

  my $cmdline = $0 . " " . join(" ",@ARGV);
  my $arg_count = scalar @ARGV;
  my @spval = File::Spec->splitpath($0);
  my $progname = $spval[2];

  my $is_logodds_qvals = 0; # if true, assume files contain logodds (i.e. "solexa") quality values
  my $is_nofilter = 0;
  my $read1file;
  my $read2file;
  my $print_version = 0;
  my $help = 0;

  my $result = GetOptions( "qlogodds" => \$is_logodds_qvals, 
                           "nofilter" => \$is_nofilter,
                           "read1=s"  => \$read1file,
                           "read2=s"  => \$read2file,
                           "version"  => \$print_version,
                           "help"     => \$help );

  my $usage = <<END;

$progname converts GERALD export files to SAM format.

Usage: $progname --read1=FILENAME [ options ] | --version | --help

  --read1=FILENAME  read1 export file (mandatory)
  --read2=FILENAME  read2 export file
  --nofilter        include reads that failed the pipeline/RTA purity filter
  --qlogodds        assume export file(s) use logodds quality values as reported
                      by pipeline prior to v1.3 (default: phred quality values)

END

  my $version_msg = <<END;

$progname version: $version

END

  if((not $result) or $help or ($arg_count==0)) {
    die($usage);
  }  

  if(@ARGV) {
    print STDERR "\nERROR: Unrecognized arguments: " . join(" ",@ARGV) . "\n\n";
    die($usage);
  }

  if($print_version) {
    die($version_msg);
  }

  if(not defined($read1file)) {
    print STDERR "\nERROR: read1 export file must be specified\n\n";
    die($usage);
  }

  my ($fh1, $fh2, $is_paired);

  open($fh1, $read1file) or die("\nERROR: Can't open read1 export file: $read1file\n\n");
  $is_paired = defined $read2file;
  if ($is_paired) {
    open($fh2, $read2file) or die("\nERROR: Can't open read2 export file: $read2file\n\n");
  }
  # quality value conversion table
  my @conv_table;
  if($is_logodds_qvals){ # convert from solexa+64 quality values (pipeline pre-v1.3):
    for (-64..64) {
      $conv_table[$_+64] = int(33 + 10*log(1+10**($_/10.0))/log(10)+.499);
    }
  } else {               # convert from phred+64 quality values (pipeline v1.3+):
    for (-64..-1) {
      $conv_table[$_+64] = undef;
    }
    for (0..64) {
      $conv_table[$_+64] = int(33 + $_);
    }
  }
  # write the header
  print write_header( $progname, $version, $cmdline );
  # core loop
  my $export_line_count = 0;
  while (<$fh1>) {
    $export_line_count++;
    my (@s1, @s2);
    &export2sam_aux($_, $export_line_count, \@s1, \@conv_table, $is_paired, 1, $is_nofilter);
    if ($is_paired) {
      my $read2line = <$fh2>;
      if(not $read2line){
        die("\nERROR: read1 and read2 export files do not contain the same number of reads.\n  Extra reads observed in read1 file at line no: $export_line_count.\n\n");
      }
      &export2sam_aux($read2line, $export_line_count, \@s2, \@conv_table, $is_paired, 2, $is_nofilter);
       
      if (@s1 && @s2) { # then set mate coordinate
        if($s1[SAM_QNAME] ne $s2[SAM_QNAME]){
          die("\nERROR: Non-paired reads in export files on line: $export_line_count.\n  Read1: $_  Read2: $read2line\n");
        }

        my $isize = 0;
        if ($s1[SAM_RNAME] ne '*' && $s1[SAM_RNAME] eq $s2[SAM_RNAME]) { # then calculate $isize
          my $x1 = ($s1[SAM_FLAG] & 0x10)? $s1[SAM_POS] + length($s1[SAM_SEQ]) : $s1[SAM_POS];
          my $x2 = ($s2[SAM_FLAG] & 0x10)? $s2[SAM_POS] + length($s2[SAM_SEQ]) : $s2[SAM_POS];
          $isize = $x2 - $x1;
        }

        foreach ([\@s1,\@s2,$isize],[\@s2,\@s1,-$isize]){ 
          my ($sa,$sb,$is) = @{$_};
          if ($sb->[SAM_RNAME] ne '*') {
            $sa->[SAM_MRNM] = ($sb->[SAM_RNAME] eq $sa->[SAM_RNAME]) ? "=" : $sb->[SAM_RNAME];
            $sa->[SAM_MPOS] = $sb->[SAM_POS];
            $sa->[SAM_ISIZE] = $is;
            $sa->[SAM_FLAG] |= 0x20 if ($sb->[SAM_FLAG] & 0x10);
          } else {
            $sa->[SAM_FLAG] |= 0x8;
          }
        } 
      }
    }
    print join("\t", @s1), "\n" if (@s1);
    print join("\t", @s2), "\n" if (@s2 && $is_paired);
  }
  close($fh1);
  if($is_paired) {
    while(my $read2line = <$fh2>){
      $export_line_count++;
      die("\nERROR: read1 and read2 export files do not contain the same number of reads.\n  Extra reads observed in read2 file at line no: $export_line_count.\n\n");
    }
    close($fh2);
  }
}

sub export2sam_aux {
  my ($line, $line_no, $s, $ct, $is_paired, $read_no, $is_nofilter) = @_;
  chomp($line);
  my @t = split("\t", $line);
  @$s = ();
  my $isPassFilt = ($t[EXPORT_PASSFILT] eq 'Y');
  return if(not ($isPassFilt or $is_nofilter));
  # read name
  $s->[SAM_QNAME] = $t[1]? "$t[0]_$t[1]:$t[2]:$t[3]:$t[4]:$t[5]" : "$t[0]:$t[2]:$t[3]:$t[4]:$t[5]";
  # initial flag (will be updated later)
  $s->[SAM_FLAG] = 0;
  if($is_paired) {
    if($t[EXPORT_READNO] != $read_no){
      die("\nERROR: read$read_no export file contains record with read number: " .$t[EXPORT_READNO] . " on line: $line_no\n\n");
    }
    $s->[SAM_FLAG] |= 1 | 1<<(5 + $read_no);
  }
  $s->[SAM_FLAG] |= 0x200 if (not $isPassFilt);

  # read & quality
  my $is_export_rev = ($t[EXPORT_STRAND] eq 'R');
  if ($is_export_rev) { # then reverse the sequence and quality
    $s->[SAM_SEQ] = reverse($t[EXPORT_READ]);
    $s->[SAM_SEQ] =~ tr/ACGTacgt/TGCAtgca/;
    $s->[SAM_QUAL] = reverse($t[EXPORT_QUAL]);
  } else {
    $s->[SAM_SEQ] = $t[EXPORT_READ];
    $s->[SAM_QUAL] = $t[EXPORT_QUAL];
  }
  my @convqual = ();
  foreach (unpack('C*', $s->[SAM_QUAL])){
    my $val=$ct->[$_];
    if(not defined $val){
      my $msg="\nERROR: can't interpret export quality value: " . $_ . " in read$read_no export file, line: $line_no\n";
      if( $_ < 64 ) { $msg .= "  Use --qlogodds flag to translate logodds (solexa) quality values.\n"; }
      die($msg . "\n");
    }
    push @convqual,$val;
  }

  $s->[SAM_QUAL] = pack('C*',@convqual); # change coding


  # coor
  my $has_coor = 0;
  $s->[SAM_RNAME] = "*";
  if ($t[EXPORT_CHROM] eq 'NM' or $t[EXPORT_CHROM] eq 'QC') {
    $s->[SAM_FLAG] |= 0x4; # unmapped
  } elsif ($t[EXPORT_CHROM] =~ /(\d+):(\d+):(\d+)/) {
    $s->[SAM_FLAG] |= 0x4; # TODO: should I set BAM_FUNMAP in this case?
    push(@$s, "H0:i:$1", "H1:i:$2", "H2:i:$3")
  } elsif ($t[EXPORT_POS] < 1) {
    $s->[SAM_FLAG] |= 0x4; # unmapped
  } else {
    $s->[SAM_RNAME] = $t[EXPORT_CHROM];
    $s->[SAM_RNAME] .= "/" . $t[EXPORT_CONTIG] if($t[EXPORT_CONTIG] ne '');
    $has_coor = 1;
  }
  $s->[SAM_POS] = $has_coor? $t[EXPORT_POS] : 0;

#  print STDERR "t[14] = " . $t[14] . "\n";
  my $matchDesc = '';
  $s->[SAM_CIGAR] = "*";
  if($has_coor){
    $matchDesc = ($is_export_rev) ? reverse_compl_match_descriptor($t[EXPORT_MD]) : $t[EXPORT_MD];

    if($matchDesc =~ /\^/){
      # construct CIGAR string using Richard's function
      $s->[SAM_CIGAR] = match_desc_to_cigar($matchDesc); # indel processing
    } else {
      $s->[SAM_CIGAR] = length($s->[SAM_SEQ]) . "M";
    }
  }

#  print STDERR "cigar_string = $cigar_string\n";

  $s->[SAM_FLAG] |= 0x10 if ($has_coor && $is_export_rev);
  if($has_coor){
    my $semap = ($t[EXPORT_SEMAP] ne '') ? $t[EXPORT_SEMAP] : 0;
    my $pemap = 0;
    if($is_paired) {
      $pemap = ($t[EXPORT_PEMAP] ne '') ? $t[EXPORT_PEMAP] : 0;

      # set `proper pair' bit if non-blank, non-zero PE alignment score:
      $s->[SAM_FLAG] |= 0x02 if ($pemap > 0);
    }
    $s->[SAM_MAPQ] = min(254,max($semap,$pemap));
  } else {
    $s->[SAM_MAPQ] = 0;
  }
  # mate coordinate
  $s->[SAM_MRNM] = '*';
  $s->[SAM_MPOS] = 0;
  $s->[SAM_ISIZE] = 0;
  # aux
  push(@$s, "BC:Z:$t[EXPORT_INDEX]") if ($t[EXPORT_INDEX]);
  if($has_coor){
    # The export match descriptor differs slightly from the samtools match descriptor.
    # In order for the converted SAM files to be as compliant as possible,
    # we put the export match descriptor in optional field 'XD' rather than 'MD':
    push(@$s, "XD:Z:$matchDesc"); 
    push(@$s, "SM:i:$t[EXPORT_SEMAP]") if ($t[EXPORT_SEMAP] ne '');
    push(@$s, "AS:i:$t[EXPORT_PEMAP]") if ($is_paired and ($t[EXPORT_PEMAP] ne ''));
  }
}



# 
# the following code is taken from Richard Shaw's sorted2sam.pl file
#
sub reverse_compl_match_descriptor($)
{
#    print "\nREVERSING THE MATCH DESCRIPTOR!\n";
    my ($match_desc) = @_;
    my $rev_compl_match_desc = reverse($match_desc);
    $rev_compl_match_desc =~ tr/ACGT\^\$/TGCA\$\^/;

    # Unreverse the digits of numbers.
    $rev_compl_match_desc = join('',
                                 map {($_ =~ /\d+/)
                                      ? join('', reverse(split('', $_)))
                                      : $_} split(/(\d+)/,
                                                  $rev_compl_match_desc));

    return $rev_compl_match_desc;
}



sub match_desc_to_cigar($)
{
    my ($match_desc) = @_;

    my @match_desc_parts = split(/(\^.*?\$)/, $match_desc);
    my $cigar_str = '';
    my $cigar_del_ch = 'D';
    my $cigar_ins_ch = 'I';
    my $cigar_match_ch = 'M';

    foreach my $match_desc_part (@match_desc_parts) {
        next if (!$match_desc_part);

        if ($match_desc_part =~ /^\^([ACGTN]+)\$$/) {
            # Deletion
            $cigar_str .= (length($1) . $cigar_del_ch);
        } elsif ($match_desc_part =~ /^\^(\d+)\$$/) {
            # Insertion
            $cigar_str .= ($1 . $cigar_ins_ch);
        } else {
            $cigar_str .= (match_desc_frag_length($match_desc_part)
                           . $cigar_match_ch);
        }
    }

    return $cigar_str;
}


#------------------------------------------------------------------------------

sub match_desc_frag_length($)
                           {
    my ($match_desc_str) = @_;
    my $len = 0;

    my @match_desc_fields = split(/([ACGTN]+)/, $match_desc_str);

    foreach my $match_desc_field (@match_desc_fields) {
        next if ($match_desc_field eq '');

        $len += (($match_desc_field =~ /(\d+)/)
                 ? $1 : length($match_desc_field));
    }

    return $len;
}


# argument holds the command line
sub write_header($;$;$) 
{
	my ($progname,$version,$cl) = @_;
	my $complete_header = "";
	$complete_header .= "\@PG\tID:$progname\tVN:$version\tCL:$cl\n";

	return $complete_header;
}