8 use File::Path 'rmtree';
13 my ($in_file, $out_file) = ();
18 GetOptions("T|temporary-directory=s" => \@tmp_dirs,
19 "h|help" => \$help) or pd2usage(-exitval => 2, -verbose => 2);
22 pod2usage(-verbose => 2) if ($help == 1);
23 pod2usage(-msg => "Invalid number of arguments!", -exitval => 2, -verbose => 2) if (scalar(@ARGV) != 2);
28 $out_file = "$ARGV[1].bam";
30 my ($fn, $dir, $suf) = fileparse($in_file, qr/\.[^.]*/);
32 $suf = lc(substr($suf, 1));
33 pod2usage(-msg => "Input file's suffix is neither sam nor bam!", -exitval => 2, -verbose => 2) if (($suf ne "sam") && ($suf ne "bam"));
34 my $isSam = ($suf eq "sam");
36 $dir = "$FindBin::Bin/";
38 my $temp_dir = "$out_file.temp";
39 if (-d $temp_dir) { print "Warning: $temp_dir exists, convert-sam-for-rsem will write temporary files into this folder and delete it after it finishes!\n"; }
41 if (!mkdir($temp_dir)) { print "Fail to create folder $temp_dir.\n"; exit(-1); }
44 # Convert to SAM format if input is a BAM file
49 $sam_file = "$temp_dir/input.sam";
50 $command = $dir."sam/samtools view -h -o $sam_file $in_file";
51 &runCommand($command);
57 # Phase I, sort entries so that all entries of a same read groups together
59 my $tmp_sam = "$temp_dir/tmp.sam";
62 $command = "grep ^@ $sam_file > $tmp_sam";
63 &runCommand($command);
65 # sort alignment section
66 $command = "grep ^[^@] $sam_file | sort -k 1,1 -s";
67 if (scalar(@tmp_dirs) > 0) { $" = " -T "; $command .= " -T @tmp_dirs"; }
68 $command .= " >> $tmp_sam";
69 &runCommand($command);
71 # Phase II, parse the temporary SAM file to make paired-end alignments' two mates adjacent to each other
73 $command = $dir."rsem-scan-for-paired-end-reads $tmp_sam $out_file";
74 &runCommand($command);
76 # delete temporary directory
79 print STDERR "Conversion is completed. $out_file will be checked by 'rsem-sam-validator'.\n";
81 # Phase III, validate if the resulting bam file is correct
83 $command = $dir."rsem-sam-validator $out_file";
84 &runCommand($command);
94 convert-sam-for-rsem [options] <input.sam/input.bam> output_file_name
100 =item B<input.sam/input.bam>
102 The SAM or BAM file generated by user's aligner. We require this file contains the header section. If input is a SAM file, it must end with suffix 'sam' (case insensitive). If input is a BAM file, it must end with suffix 'bam' (case insensitive).
104 =item B<output_file_name>
106 The output name for the converted file. 'convert-sam-for-rsem' will output a BAM with the name 'output_file_name.bam'.
114 =item B<-T/--temporary-directory> <directory>
116 '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".
120 Show help information.
126 This program converts the SAM/BAM file generated by user's aligner into a BAM 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 and output header sections. This program will create a temporary directory called 'output_file_name.bam.temp' to store the intermediate files. The directory will be deleted automatically after the conversion. After the conversion, this program will call 'rsem-sam-validator' to validate the resulting BAM file.
128 Note: You do not need to run this script if `rsem-sam-validator' reports that your SAM/BAM file is valid.
130 Note: This program does not check the correctness of input file. You should make sure the input is a valid SAM/BAM format file.
134 Suppose input is set to 'input.sam' and output file name is "output"
136 convert-sam-for-rsem input.sam output
138 We will get a file called 'output.bam' as output.