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