7 my $standard_output = "&STDOUT";
9 my $out_file = $standard_output;
13 GetOptions("o=s" => \$out_file,
14 "T|temporary-directory=s" => \@tmp_dirs,
15 "h|help" => \$help) or pd2usage(-exitval => 2, -verbose => 2);
18 pod2usage(-verbose => 2) if ($help == 1);
19 pod2usage(-msg => "Invalid number of arguments!", -exitval => 2, -verbose => 2) if (scalar(@ARGV) != 2);
22 my (@fields, @header) = ();
29 open(INPUT, "$ARGV[0].ti");
35 ($M, $type) = split(/ /, $lines[0]);
36 for (my $i = 0; $i < $M; $i++) {
37 push(@fields, "SN:$lines[$i * 6 + 1]");
44 open(INPUT, $ARGV[1]);
46 while (($line = <INPUT>) && substr($line, 0, 1) eq '@') {
52 my $n = scalar(@header);
59 for (my $i = 0; $i < $n; $i++) {
60 my @arr = split(/\t/, $header[$i]);
61 if ($arr[0] ne "\@SQ") { push(@ktable, ""); next; }
63 foreach my $key (@arr) {
64 if (substr($key, 0, 3) eq "SN:") {
70 if (!$hasSN) { print STDERR "\"$header[$i]\" does not have a SN tag!\n"; exit(-1); }
71 push(@ktable, $fields[$tid++]);
74 if ($tid != $M) { print STDERR "Number of \@SQ lines is not correct!\n"; exit(-1); }
76 open(OUTPUT, ">$out_file");
77 for (my $i = 0; $i < $n; $i++) {
78 if ($ktable[$i] eq "") { print OUTPUT $header[$i]; }
79 else { print OUTPUT $header[$hash{$ktable[$i]}]; }
86 # extract alignment section
87 $command = "grep ^[^@] $ARGV[1] > $ARGV[1].__temp";
88 &runCommand($command);
90 # sort and output the alignment section
91 $command = "sort -k 1,1 -s";
92 if (scalar(@tmp_dirs) > 0) { $" = " -T "; $command .= " -T @tmp_dirs"; }
93 $command .= " $ARGV[1].__temp";
94 if ($out_file ne $standard_output) { $command .= " >> $out_file"; }
95 &runCommand($command);
97 # delete temporary files
98 $command = "rm -f $ARGV[1].__temp";
99 &runCommand($command);
102 print STDERR "Conversion is completed successfully!\n";
106 print STDERR $_[0]."\n";
107 my $status = system($_[0]);
110 if (scalar(@_) > 1) { $errmsg = $_[1]; }
111 else { $errmsg = "\"$command\" failed! Plase check if you provide correct parameters/options for the script!"; }
112 print STDERR $errmsg."\n";
128 convert-sam-for-rsem [options] reference_name input_sam
136 =item B<reference_name>
138 The name of the reference used. This should be the same name used by 'rsem-prepare-reference'.
142 The SAM file (*.sam) generated by user's aligner. If the aligner produces a BAM file, please use samtools to convert it to a SAM file (with header information).
152 Output the converted SAM file into <file>. (Default: STDOUT)
154 =item B<-T/--temporary-directory> <directory>
156 'convert-sam-for-rsem' will call 'sort' command and this is the '-T/--temporary-directory' option of 'sort' command. The following is the description from 'sort' : "use DIR for temporaries, not $TMPDIR or /tmp; multiple options specify multiple directories".
160 Show help information.
166 This program converts the SAM file generated by user's aligner into a SAM file which RSEM can process. However, users should make sure their aligners use 'reference_name.idx.fa' generated by 'rsem-prepare-reference' as their references. In addition, their aligners should output header information and make two mates of the same alignment adjacent to each other for paired-end data. This program will output the converted file into standard output by default for the purpose of piping. By setting '-o' option, users can make the converted file written into disk.
168 Note: You do not need to run this script if Bowtie (not Bowtie 2) is used, or the order of @SQ lines is the same as 'reference_name.idx.fa' and the alignment lines of a same read group together and the mates of the same alignment are adjacent each other for paired-end reads.
170 Note: This program can only recognize SAM files. See ARGUMENTS section.
174 Suppose reference_name and input_sam are set to '/ref/mouse_125' and 'input.sam'.
176 1) Output to standard output and gzip the output to 'input_for_rsem.sam.gz':
178 convert-sam-for-rsem /ref/mouse_125 input.sam | gzip > input_for_rsem.sam.gz
180 2) Output to 'input_for_rsem.sam' directly:
182 convert-sam-for-rsem /ref/mouse_125 input.sam -o input_for_rsem.sam