annotate PsiCLASS-1.0.2/samtools-0.1.19/misc/export2sam.pl @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 #!/usr/bin/env perl
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 # export2sam.pl converts GERALD export files to SAM format.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 ########## License:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 # The MIT License
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 # Original SAMtools work copyright (c) 2008-2009 Genome Research Ltd.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13 # Modified SAMtools work copyright (c) 2010 Illumina, Inc.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 # Permission is hereby granted, free of charge, to any person obtaining a copy
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16 # of this software and associated documentation files (the "Software"), to deal
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 # in the Software without restriction, including without limitation the rights
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 # copies of the Software, and to permit persons to whom the Software is
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 # furnished to do so, subject to the following conditions:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 # The above copyright notice and this permission notice shall be included in
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 # all copies or substantial portions of the Software.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 # THE SOFTWARE.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 ########## ChangeLog:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 # Version: 2.3.1 (18MAR2011)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 # - Restore file '-' as stdin input.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 # Version: 2.3.0 (24JAN2011)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 # - Add support for export reserved chromosome name "CONTROL",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45 # which is translated to optional field "XC:Z:CONTROL".
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 # - Check for ".gz" file extension on export files and open
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47 # these as gzip pipes when the extension is found.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 # Version: 2.2.0 (16NOV2010)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 # - Remove any leading zeros in export fields: RUNNO,LANE,TILE,X,Y
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 # - For export records with reserved chromosome name identifiers
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 # "QC" and "RM", add the optional field "XC:Z:QC" or "XC:Z:RM"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 # to the SAM record, so that these cases can be distinguished
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 # from other unmatched reads.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57 # Version: 2.1.0 (21SEP2010)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 # - Additional export record error checking.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 # - Convert export records with chromomsome value of "RM" to unmapped
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 # SAM records.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 # Version: 2.0.0 (15FEB2010)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 # Script updated by Illumina in conjunction with CASAVA 1.7.0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 # release.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 # Major changes are as follows:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 # - The CIGAR string has been updated to include all gaps from
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 # ELANDv2 alignments.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 # - The ELAND single read alignment score is always stored in the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 # optional "SM" field and the ELAND paired read alignment score
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 # is stored in the optional "AS" field when it exists.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 # - The MAPQ value is set to the higher of the two alignment scores,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 # but no greater than 254, i.e. min(254,max(SM,AS))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 # - The SAM "proper pair" bit (0x0002) is now set for read pairs
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 # meeting ELAND's expected orientation and insert size criteria.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 # - The default quality score translation is set for export files
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 # which contain Phread+64 quality values. An option,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 # "--qlogodds", has been added to translate quality values from
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81 # the Solexa+64 format used in export files prior to Pipeline
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 # 1.3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 # - The export match descriptor is now reverse-complemented when
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 # necessary such that it always corresponds to the forward
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 # strand of the reference, to be consistent with other
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 # information in the SAM record. It is now written to the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 # optional 'XD' field (rather than 'MD') to acknowledge its
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 # minor differences from the samtools match descriptor (see
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89 # additional detail below).
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 # - An option, "--nofilter", has been added to include reads which
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 # have failed primary analysis quality filtration. Such reads
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 # will have the corresponding SAM flag bit (0x0200) set.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 # - Labels in the export 'contig' field are preserved by setting
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 # RNAME to "$export_chromosome/$export_contig" when the contig
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 # label exists.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 # Contact: lh3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 # Version: 0.1.2 (03JAN2009)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 ########## Known Conversion Limitations:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 # - Export records for reads that map to a position < 1 (allowed
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 # in export format), are converted to unmapped reads in the SAM
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 # record.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 # - Export records contain the reserved chromosome names: "NM",
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 # "QC","RM" and "CONTROL". "NM" indicates that the aligner could
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 # not map the read to the reference sequence set. "QC" means that
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 # the aligner did not attempt to map the read due to some
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 # technical limitation. "RM" means that the read mapped to a set
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 # of 'contaminant' sequences specified in GERALD's RNA-seq
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 # workflow. "CONTROL" means that the read is a control. All of
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 # these alignment types are collapsed to the single unmapped
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 # alignment state in the SAM record, but the optional SAM "XC"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 # field is used to record the original reserved chromosome name of
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 # the read for all but the "NM" case.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 # - The export match descriptor is slightly different than the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 # samtools match descriptor. For this reason it is stored in the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 # optional SAM field 'XD' (and not 'MD'). Note that the export
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 # match descriptor differs from the samtools version in two
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 # respects: (1) indels are explicitly closed with the '$'
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 # character and (2) insertions must be enumerated in the match
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125 # descriptor. For example a 35-base read with a two-base insertion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 # is described as: 20^2$14
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 my $version = "2.3.1";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 use strict;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 use warnings;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 use Getopt::Long;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 use File::Spec;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 use List::Util qw(min max);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 use constant {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 EXPORT_MACHINE => 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 EXPORT_RUNNO => 1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 EXPORT_LANE => 2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 EXPORT_TILE => 3,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 EXPORT_X => 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 EXPORT_Y => 5,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 EXPORT_INDEX => 6,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 EXPORT_READNO => 7,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 EXPORT_READ => 8,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 EXPORT_QUAL => 9,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 EXPORT_CHROM => 10,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 EXPORT_CONTIG => 11,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154 EXPORT_POS => 12,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 EXPORT_STRAND => 13,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 EXPORT_MD => 14,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 EXPORT_SEMAP => 15,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 EXPORT_PEMAP => 16,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 EXPORT_PASSFILT => 21,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 EXPORT_SIZE => 22,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 use constant {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 SAM_QNAME => 0,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 SAM_FLAG => 1,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 SAM_RNAME => 2,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 SAM_POS => 3,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 SAM_MAPQ => 4,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 SAM_CIGAR => 5,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 SAM_MRNM => 6,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 SAM_MPOS => 7,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 SAM_ISIZE => 8,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 SAM_SEQ => 9,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 SAM_QUAL => 10,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 # function prototypes for Richard's code
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 sub match_desc_to_cigar($);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181 sub match_desc_frag_length($);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 sub reverse_compl_match_descriptor($);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 sub write_header($;$;$);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 &export2sam;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 exit;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 sub export2sam {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 my $cmdline = $0 . " " . join(" ",@ARGV);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 my $arg_count = scalar @ARGV;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 my $progname = (File::Spec->splitpath($0))[2];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 my $is_logodds_qvals = 0; # if true, assume files contain logodds (i.e. "solexa") quality values
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199 my $is_nofilter = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 my $read1file;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 my $read2file;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 my $print_version = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203 my $help = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 my $result = GetOptions( "qlogodds" => \$is_logodds_qvals,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 "nofilter" => \$is_nofilter,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 "read1=s" => \$read1file,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 "read2=s" => \$read2file,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 "version" => \$print_version,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 "help" => \$help );
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 my $usage = <<END;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 $progname converts GERALD export files to SAM format.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 Usage: $progname --read1=FILENAME [ options ] | --version | --help
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 --read1=FILENAME read1 export file or '-' for stdin (mandatory)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 (file may be gzipped with ".gz" extension)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 --read2=FILENAME read2 export file or '-' for stdin
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 (file may be gzipped with ".gz" extension)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 --nofilter include reads that failed the basecaller
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 purity filter
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 --qlogodds assume export file(s) use logodds quality values
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 as reported by OLB (Pipeline) prior to v1.3
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 (default: phred quality values)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 END
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 my $version_msg = <<END;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232 $progname version: $version
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 END
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 if((not $result) or $help or ($arg_count==0)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 die($usage);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 if(@ARGV) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 print STDERR "\nERROR: Unrecognized arguments: " . join(" ",@ARGV) . "\n\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 die($usage);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 if($print_version) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 die($version_msg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 if(not defined($read1file)) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 print STDERR "\nERROR: read1 export file must be specified\n\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 die($usage);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254 unless((-f $read1file) or ($read1file eq '-')) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 die("\nERROR: Can't find read1 export file: '$read1file'\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 if (defined $read2file) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 unless((-f $read2file) or ($read2file eq '-')) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 die("\nERROR: Can't find read2 export file: '$read2file'\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 if($read1file eq $read2file) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 die("\nERROR: read1 and read2 export filenames are the same: '$read1file'\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 my ($fh1, $fh2, $is_paired);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 my $read1cmd="$read1file";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 $read1cmd = "gzip -dc $read1file |" if($read1file =~ /\.gz$/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 open($fh1, $read1cmd)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 or die("\nERROR: Can't open read1 process: '$read1cmd'\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 $is_paired = defined $read2file;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 if ($is_paired) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 my $read2cmd="$read2file";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 $read2cmd = "gzip -dc $read2file |" if($read2file =~ /\.gz$/);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 open($fh2, $read2cmd)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 or die("\nERROR: Can't open read2 process: '$read2cmd'\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 # quality value conversion table
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 my @conv_table;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 if($is_logodds_qvals){ # convert from solexa+64 quality values (pipeline pre-v1.3):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 for (-64..64) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 $conv_table[$_+64] = int(33 + 10*log(1+10**($_/10.0))/log(10)+.499);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 } else { # convert from phred+64 quality values (pipeline v1.3+):
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 for (-64..-1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 $conv_table[$_+64] = undef;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290 for (0..64) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 $conv_table[$_+64] = int(33 + $_);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 # write the header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 print write_header( $progname, $version, $cmdline );
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 # core loop
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 my $export_line_count = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 while (<$fh1>) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 $export_line_count++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 my (@s1, @s2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 &export2sam_aux($_, $export_line_count, \@s1, \@conv_table, $is_paired, 1, $is_nofilter);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 if ($is_paired) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 my $read2line = <$fh2>;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 if(not $read2line){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 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");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 &export2sam_aux($read2line, $export_line_count, \@s2, \@conv_table, $is_paired, 2, $is_nofilter);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 if (@s1 && @s2) { # then set mate coordinate
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 if($s1[SAM_QNAME] ne $s2[SAM_QNAME]){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 die("\nERROR: Non-paired reads in export files on line: $export_line_count.\n Read1: $_ Read2: $read2line\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 my $isize = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 if ($s1[SAM_RNAME] ne '*' && $s1[SAM_RNAME] eq $s2[SAM_RNAME]) { # then calculate $isize
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 my $x1 = ($s1[SAM_FLAG] & 0x10)? $s1[SAM_POS] + length($s1[SAM_SEQ]) : $s1[SAM_POS];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 my $x2 = ($s2[SAM_FLAG] & 0x10)? $s2[SAM_POS] + length($s2[SAM_SEQ]) : $s2[SAM_POS];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 $isize = $x2 - $x1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 foreach ([\@s1,\@s2,$isize],[\@s2,\@s1,-$isize]){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 my ($sa,$sb,$is) = @{$_};
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 if ($sb->[SAM_RNAME] ne '*') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 $sa->[SAM_MRNM] = ($sb->[SAM_RNAME] eq $sa->[SAM_RNAME]) ? "=" : $sb->[SAM_RNAME];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 $sa->[SAM_MPOS] = $sb->[SAM_POS];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 $sa->[SAM_ISIZE] = $is;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 $sa->[SAM_FLAG] |= 0x20 if ($sb->[SAM_FLAG] & 0x10);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 $sa->[SAM_FLAG] |= 0x8;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 print join("\t", @s1), "\n" if (@s1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 print join("\t", @s2), "\n" if (@s2 && $is_paired);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 close($fh1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 if($is_paired) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 while(my $read2line = <$fh2>){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 $export_line_count++;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 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");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 close($fh2);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 sub export2sam_aux {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 my ($line, $line_no, $s, $ct, $is_paired, $read_no, $is_nofilter) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 chomp($line);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 my @t = split("\t", $line);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 if(scalar(@t) < EXPORT_SIZE) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 my $msg="\nERROR: Unexpected number of fields in export record on line $line_no of read$read_no export file. Found " . scalar(@t) . " fields but expected " . EXPORT_SIZE . ".\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 $msg.="\t...erroneous export record:\n" . $line . "\n\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 die($msg);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 @$s = ();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 my $isPassFilt = ($t[EXPORT_PASSFILT] eq 'Y');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 return if(not ($isPassFilt or $is_nofilter));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 # read name
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 my $samQnamePrefix = $t[EXPORT_MACHINE] . (($t[EXPORT_RUNNO] ne "") ? "_" . int($t[EXPORT_RUNNO]) : "");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 $s->[SAM_QNAME] = join(':', $samQnamePrefix, int($t[EXPORT_LANE]), int($t[EXPORT_TILE]),
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 int($t[EXPORT_X]), int($t[EXPORT_Y]));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 # initial flag (will be updated later)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364 $s->[SAM_FLAG] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365 if($is_paired) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 if($t[EXPORT_READNO] != $read_no){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 die("\nERROR: read$read_no export file contains record with read number: " .$t[EXPORT_READNO] . " on line: $line_no\n\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369 $s->[SAM_FLAG] |= 1 | 1<<(5 + $read_no);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 $s->[SAM_FLAG] |= 0x200 if (not $isPassFilt);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 # read & quality
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 my $is_export_rev = ($t[EXPORT_STRAND] eq 'R');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 if ($is_export_rev) { # then reverse the sequence and quality
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 $s->[SAM_SEQ] = reverse($t[EXPORT_READ]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 $s->[SAM_SEQ] =~ tr/ACGTacgt/TGCAtgca/;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 $s->[SAM_QUAL] = reverse($t[EXPORT_QUAL]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 $s->[SAM_SEQ] = $t[EXPORT_READ];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 $s->[SAM_QUAL] = $t[EXPORT_QUAL];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 my @convqual = ();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 foreach (unpack('C*', $s->[SAM_QUAL])){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 my $val=$ct->[$_];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 if(not defined $val){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 my $msg="\nERROR: can't interpret export quality value: " . $_ . " in read$read_no export file, line: $line_no\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 if( $_ < 64 ) { $msg .= " Use --qlogodds flag to translate logodds (solexa) quality values.\n"; }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 die($msg . "\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 push @convqual,$val;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 $s->[SAM_QUAL] = pack('C*',@convqual); # change coding
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 # coor
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 my $has_coor = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 $s->[SAM_RNAME] = "*";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 if (($t[EXPORT_CHROM] eq 'NM') or
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 ($t[EXPORT_CHROM] eq 'QC') or
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 ($t[EXPORT_CHROM] eq 'RM') or
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 ($t[EXPORT_CHROM] eq 'CONTROL')) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404 $s->[SAM_FLAG] |= 0x4; # unmapped
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 push(@$s,"XC:Z:".$t[EXPORT_CHROM]) if($t[EXPORT_CHROM] ne 'NM');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 } elsif ($t[EXPORT_CHROM] =~ /(\d+):(\d+):(\d+)/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 $s->[SAM_FLAG] |= 0x4; # TODO: should I set BAM_FUNMAP in this case?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 push(@$s, "H0:i:$1", "H1:i:$2", "H2:i:$3")
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 } elsif ($t[EXPORT_POS] < 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 $s->[SAM_FLAG] |= 0x4; # unmapped
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 $s->[SAM_RNAME] = $t[EXPORT_CHROM];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 $s->[SAM_RNAME] .= "/" . $t[EXPORT_CONTIG] if($t[EXPORT_CONTIG] ne '');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 $has_coor = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 $s->[SAM_POS] = $has_coor? $t[EXPORT_POS] : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 # print STDERR "t[14] = " . $t[14] . "\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 my $matchDesc = '';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 $s->[SAM_CIGAR] = "*";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 if($has_coor){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 $matchDesc = ($is_export_rev) ? reverse_compl_match_descriptor($t[EXPORT_MD]) : $t[EXPORT_MD];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 if($matchDesc =~ /\^/){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 # construct CIGAR string using Richard's function
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 $s->[SAM_CIGAR] = match_desc_to_cigar($matchDesc); # indel processing
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 $s->[SAM_CIGAR] = length($s->[SAM_SEQ]) . "M";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 # print STDERR "cigar_string = $cigar_string\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 $s->[SAM_FLAG] |= 0x10 if ($has_coor && $is_export_rev);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 if($has_coor){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 my $semap = ($t[EXPORT_SEMAP] ne '') ? $t[EXPORT_SEMAP] : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 my $pemap = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 if($is_paired) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 $pemap = ($t[EXPORT_PEMAP] ne '') ? $t[EXPORT_PEMAP] : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 # set `proper pair' bit if non-blank, non-zero PE alignment score:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 $s->[SAM_FLAG] |= 0x02 if ($pemap > 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 $s->[SAM_MAPQ] = min(254,max($semap,$pemap));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446 $s->[SAM_MAPQ] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 # mate coordinate
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 $s->[SAM_MRNM] = '*';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450 $s->[SAM_MPOS] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 $s->[SAM_ISIZE] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 # aux
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 push(@$s, "BC:Z:$t[EXPORT_INDEX]") if ($t[EXPORT_INDEX]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 if($has_coor){
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 # The export match descriptor differs slightly from the samtools match descriptor.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 # In order for the converted SAM files to be as compliant as possible,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 # we put the export match descriptor in optional field 'XD' rather than 'MD':
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 push(@$s, "XD:Z:$matchDesc");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 push(@$s, "SM:i:$t[EXPORT_SEMAP]") if ($t[EXPORT_SEMAP] ne '');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 push(@$s, "AS:i:$t[EXPORT_PEMAP]") if ($is_paired and ($t[EXPORT_PEMAP] ne ''));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 # the following code is taken from Richard Shaw's sorted2sam.pl file
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 #
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 sub reverse_compl_match_descriptor($)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 # print "\nREVERSING THE MATCH DESCRIPTOR!\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 my ($match_desc) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 my $rev_compl_match_desc = reverse($match_desc);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 $rev_compl_match_desc =~ tr/ACGT\^\$/TGCA\$\^/;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 # Unreverse the digits of numbers.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 $rev_compl_match_desc = join('',
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 map {($_ =~ /\d+/)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 ? join('', reverse(split('', $_)))
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 : $_} split(/(\d+)/,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 $rev_compl_match_desc));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 return $rev_compl_match_desc;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 sub match_desc_to_cigar($)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 my ($match_desc) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 my @match_desc_parts = split(/(\^.*?\$)/, $match_desc);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 my $cigar_str = '';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494 my $cigar_del_ch = 'D';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 my $cigar_ins_ch = 'I';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 my $cigar_match_ch = 'M';
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 foreach my $match_desc_part (@match_desc_parts) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 next if (!$match_desc_part);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 if ($match_desc_part =~ /^\^([ACGTN]+)\$$/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502 # Deletion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503 $cigar_str .= (length($1) . $cigar_del_ch);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 } elsif ($match_desc_part =~ /^\^(\d+)\$$/) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 # Insertion
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 $cigar_str .= ($1 . $cigar_ins_ch);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 $cigar_str .= (match_desc_frag_length($match_desc_part)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 . $cigar_match_ch);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513 return $cigar_str;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517 #------------------------------------------------------------------------------
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519 sub match_desc_frag_length($)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 my ($match_desc_str) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 my $len = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524 my @match_desc_fields = split(/([ACGTN]+)/, $match_desc_str);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 foreach my $match_desc_field (@match_desc_fields) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527 next if ($match_desc_field eq '');
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
529 $len += (($match_desc_field =~ /(\d+)/)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
530 ? $1 : length($match_desc_field));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
531 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
532
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
533 return $len;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
534 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
535
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
536
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
537 # argument holds the command line
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
538 sub write_header($;$;$)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
539 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
540 my ($progname,$version,$cl) = @_;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
541 my $complete_header = "";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
542 $complete_header .= "\@PG\tID:$progname\tVN:$version\tCL:$cl\n";
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
543
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
544 return $complete_header;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
545 }