]> git.donarmstrong.com Git - rsem.git/blob - convert-sam-for-rsem
Updated README.md and WHAT_IS_NEW
[rsem.git] / convert-sam-for-rsem
1 #!/usr/bin/env perl
2
3 use Getopt::Long;
4 use Pod::Usage;
5 use File::Basename;
6 use File::Path 'rmtree';
7
8 use FindBin;
9 use lib $FindBin::RealBin;
10 use rsem_perl_utils;
11
12 use Env qw(@PATH);
13 @PATH = ($FindBin::RealBin, "$FindBin::RealBin/sam", @PATH);
14
15 use strict;
16
17 my ($in_file, $out_file) = ();
18
19 my @tmp_dirs = ();
20 my $help = 0;
21
22 GetOptions("T|temporary-directory=s" => \@tmp_dirs,
23            "h|help" => \$help) or pd2usage(-exitval => 2, -verbose => 2);
24
25            
26 pod2usage(-verbose => 2) if ($help == 1);
27 pod2usage(-msg => "Invalid number of arguments!", -exitval => 2, -verbose => 2) if (scalar(@ARGV) != 2);
28
29 my $command;
30
31 $in_file = $ARGV[0]; 
32 $out_file = "$ARGV[1].bam";
33
34 my ($fn, $dir, $suf) = fileparse($in_file, qr/\.[^.]*/);
35
36 $suf = lc(substr($suf, 1));
37 pod2usage(-msg => "Input file's suffix is neither sam nor bam!", -exitval => 2, -verbose => 2) if (($suf ne "sam") && ($suf ne "bam"));
38 my $isSam = ($suf eq "sam");
39
40 my $temp_dir = "$out_file.temp";
41 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"; }
42 else {
43     if (!mkdir($temp_dir)) { print "Fail to create folder $temp_dir.\n"; exit(-1); }
44
45
46 # Convert to SAM format if input is a BAM file
47
48 my $sam_file;
49
50 if (!$isSam) {
51     $sam_file = "$temp_dir/input.sam";
52     $command = "samtools view -h -o $sam_file $in_file";
53     &runCommand($command); 
54 }
55 else {
56     $sam_file = $in_file;
57 }
58
59 # Phase I, sort entries so that all entries of a same read groups together
60
61 my $tmp_sam = "$temp_dir/tmp.sam";
62
63 # grep header section
64 $command = "grep ^@ $sam_file > $tmp_sam";
65 &runCommand($command);
66
67 # sort alignment section
68 $command = "grep ^[^@] $sam_file | sort -k 1,1 -s";
69 if (scalar(@tmp_dirs) > 0) { $" = " -T "; $command .= " -T @tmp_dirs"; }
70 $command .= " >> $tmp_sam";
71 &runCommand($command);
72
73 # Phase II, parse the temporary SAM file to make paired-end alignments' two mates adjacent to each other
74
75 $command = "rsem-scan-for-paired-end-reads $tmp_sam $out_file";
76 &runCommand($command);
77
78 # delete temporary directory
79 rmtree($temp_dir);
80
81 print STDERR "Conversion is completed. $out_file will be checked by 'rsem-sam-validator'.\n";
82
83 # Phase III, validate if the resulting bam file is correct
84
85 $command = "rsem-sam-validator $out_file";
86 &runCommand($command);
87
88 __END__
89
90 =head1 NAME
91
92 convert-sam-for-rsem
93
94 =head1 SYNOPSIS
95
96 convert-sam-for-rsem [options] <input.sam/input.bam> output_file_name
97
98 =head1 ARGUMENTS
99
100 =over
101
102 =item B<input.sam/input.bam>
103
104 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). 
105
106 =item B<output_file_name>
107
108 The output name for the converted file. 'convert-sam-for-rsem' will output a BAM with the name 'output_file_name.bam'.
109
110 =back
111
112 =head1 OPTIONS
113
114 =over
115
116 =item B<-T/--temporary-directory> <directory>
117
118 '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". 
119
120 =item B<-h/--help>
121
122 Show help information.
123
124 =back
125
126 =head1 DESCRIPTION
127
128 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. 
129
130 Note: You do not need to run this script if `rsem-sam-validator' reports that your SAM/BAM file is valid.
131
132 Note: This program does not check the correctness of input file. You should make sure the input is a valid SAM/BAM format file.
133
134 =head1 EXAMPLES
135
136 Suppose input is set to 'input.sam' and output file name is "output" 
137
138  convert-sam-for-rsem input.sam output
139
140 We will get a file called 'output.bam' as output.
141
142 =cut