]> git.donarmstrong.com Git - rsem.git/blob - convert-sam-for-rsem
Added user-friendly error messages if users forget to compile the source codes
[rsem.git] / convert-sam-for-rsem
1 #!/usr/bin/perl
2
3 use Getopt::Long;
4 use Pod::Usage;
5 use File::Basename;
6 use File::Path 'rmtree';
7 use strict;
8
9 use rsem_perl_utils;
10
11 my ($in_file, $out_file) = ();
12
13 my @tmp_dirs = ();
14 my $help = 0;
15
16 GetOptions("T|temporary-directory=s" => \@tmp_dirs,
17            "h|help" => \$help) or pd2usage(-exitval => 2, -verbose => 2);
18
19            
20 pod2usage(-verbose => 2) if ($help == 1);
21 pod2usage(-msg => "Invalid number of arguments!", -exitval => 2, -verbose => 2) if (scalar(@ARGV) != 2);
22
23 my $command;
24
25 $in_file = $ARGV[0]; 
26 $out_file = "$ARGV[1].bam";
27
28 my ($fn, $dir, $suf) = fileparse($in_file, qr/\.[^.]*/);
29
30 $suf = lc(substr($suf, 1));
31 pod2usage(-msg => "Input file's suffix is neither sam nor bam!", -exitval => 2, -verbose => 2) if (($suf ne "sam") && ($suf ne "bam"));
32 my $isSam = ($suf eq "sam");
33
34 ($fn, $dir, $suf) = fileparse($0);
35
36 my $temp_dir = "$out_file.temp";
37 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"; }
38 else {
39     if (!mkdir($temp_dir)) { print "Fail to create folder $temp_dir.\n"; exit(-1); }
40
41
42 # Convert to SAM format if input is a BAM file
43
44 my $sam_file;
45
46 if (!$isSam) {
47     $sam_file = "$temp_dir/input.sam";
48     $command = $dir."sam/samtools view -h -o $sam_file $in_file";
49     &runCommand($command); 
50 }
51 else {
52     $sam_file = $in_file;
53 }
54
55 # Phase I, sort entries so that all entries of a same read groups together
56
57 my $tmp_sam = "$temp_dir/tmp.sam";
58
59 # grep header section
60 $command = "grep ^@ $sam_file > $tmp_sam";
61 &runCommand($command);
62
63 # sort alignment section
64 $command = "grep ^[^@] $sam_file | sort -k 1,1 -s";
65 if (scalar(@tmp_dirs) > 0) { $" = " -T "; $command .= " -T @tmp_dirs"; }
66 $command .= " >> $tmp_sam";
67 &runCommand($command);
68
69 # Phase II, parse the temporary SAM file to make paired-end alignments' two mates adjacent to each other
70
71 $command = $dir."rsem-scan-for-paired-end-reads $tmp_sam $out_file";
72 &runCommand($command);
73
74 # delete temporary directory
75 rmtree($temp_dir);
76
77 print STDERR "Conversion is completed. $out_file will be checked by 'rsem-sam-validator'.\n";
78
79 # Phase III, validate if the resulting bam file is correct
80
81 $command = $dir."rsem-sam-validator $out_file";
82 &runCommand($command);
83
84 __END__
85
86 =head1 NAME
87
88 convert-sam-for-rsem
89
90 =head1 SYNOPSIS
91
92 =over
93
94  convert-sam-for-rsem [options] <input.sam/input.bam> output_file_name
95
96 =back
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