]> git.donarmstrong.com Git - rsem.git/blobdiff - rsem-calculate-expression
Fixed a typo in rsem-calculate-expression's document; Added multi-thread and memory...
[rsem.git] / rsem-calculate-expression
index 4d1b70a8a4dca169ccb0702caf32d2bb30caaa2f..b523eddd932313ce70da5b7d349f59f60cb7f3b1 100755 (executable)
@@ -2,9 +2,12 @@
 
 use Getopt::Long;
 use Pod::Usage;
-use File::Basename;
+use FindBin;
+use lib $FindBin::Bin;
 use strict;
 
+use rsem_perl_utils qw(runCommand collectResults showVersionInfo);
+
 #const
 my $BURNIN = 200;
 my $NCV = 1000;
@@ -13,15 +16,12 @@ my $CONFIDENCE = 0.95;
 my $NSPC = 50;
 
 my $NMB = 1024; # default
+my $SortMem = "1G"; # default as 1G per thread
 
 my $status = 0;
 
 my $read_type = 1; # default, single end with qual
 
-my @transcript_title = ("transcript_id", "gene_id", "length", "effective_length", "expected_count", "TPM", "FPKM", "IsoPct", "pme_expected_count", "pme_TPM", "pme_FPKM", "IsoPct_from_pme_TPM", "TPM_ci_lower_bound", "TPM_ci_upper_bound", "FPKM_ci_lower_bound", "FPKM_ci_upper_bound");
-
-my @gene_title = ("gene_id", "transcript_id(s)", "length", "effective_length", "expected_count", "TPM", "FPKM", "pme_expected_count", "pme_TPM", "pme_FPKM", "TPM_ci_lower_bound", "TPM_ci_upper_bound", "FPKM_ci_lower_bound", "FPKM_ci_upper_bound");
-
 my $bowtie_path = "";
 my $C = 2;
 my $E = 99999999;
@@ -62,6 +62,12 @@ my $keep_intermediate_files = 0;
 
 my $strand_specific = 0;
 
+my $bowtie2 = 0;
+my $bowtie2_path = "";
+my $bowtie2_mismatch_rate = 0.1;
+my $bowtie2_k = 200;
+my $bowtie2_sensitivity_level = "sensitive"; # must be one of "very_fast", "fast", "sensitive", "very_sensitive"
+
 my $version = 0;
 
 my $mTime = 0;
@@ -92,6 +98,11 @@ GetOptions("keep-intermediate-files" => \$keep_intermediate_files,
           "phred33-quals" => \$phred33,
           "phred64-quals" => \$phred64, #solexa1.3-quals" => \$phred64,
           "solexa-quals" => \$solexa,
+          "bowtie2" => \$bowtie2,
+          "bowtie2-path=s" => \$bowtie2_path,
+          "bowtie2-mismatch-rate=f" => \$bowtie2_mismatch_rate,
+          "bowtie2-k=i" => \$bowtie2_k,
+          "bowtie2-sensitivity-level=s" => \$bowtie2_sensitivity_level,
           "forward-prob=f" => \$probF,
           "fragment-length-min=i" => \$minL,
           "fragment-length-max=i" => \$maxL,
@@ -106,12 +117,13 @@ GetOptions("keep-intermediate-files" => \$keep_intermediate_files,
           "var" => \$var_opt,
           "calc-ci" => \$calcCI,
           "ci-memory=i" => \$NMB,
+          "samtools-sort-mem=s" => \$SortMem,
           "time" => \$mTime,
           "version" => \$version,
           "q|quiet" => \$quiet,
           "h|help" => \$help) or pod2usage(-exitval => 2, -verbose => 2);
 
-my ($fn, $dir, $suf) = fileparse($0);
+my $dir = "$FindBin::Bin/";
 
 pod2usage(-verbose => 2) if ($help == 1);
 &showVersionInfo($dir) if ($version == 1);
@@ -121,12 +133,17 @@ pod2usage(-verbose => 2) if ($help == 1);
 if ($is_sam || $is_bam) {
     pod2usage(-msg => "Invalid number of arguments!", -exitval => 2, -verbose => 2) if (scalar(@ARGV) != 3);
     pod2usage(-msg => "--sam and --bam cannot be active at the same time!", -exitval => 2, -verbose => 2) if ($is_sam == 1&& $is_bam == 1);
-    pod2usage(-msg => "--bowtie-path, --bowtie-n, --bowtie-e, --bowtie-m, --phred33-quals, --phred64-quals or --solexa-quals cannot be set if input is SAM/BAM format!", -exitval => 2, -verbose => 2) if ($bowtie_path ne "" || $C != 2 || $E != 99999999 || $maxHits != 200 || $phred33 || $phred64 || $solexa);
+    pod2usage(-msg => "--bowtie-path, --bowtie-n, --bowtie-e, --bowtie-m, --phred33-quals, --phred64-quals, --solexa-quals, --bowtie2, --bowtie2-path, --bowtie2-mismatch-rate, --bowtie2-k and --bowtie2-sensitivity-level cannot be set if input is SAM/BAM format!", -exitval => 2, -verbose => 2) if ($bowtie_path ne "" || $C != 2 || $E != 99999999 || $maxHits != 200 || $phred33 || $phred64 || $solexa || $bowtie2 || $bowtie2_path ne "" || $bowtie2_mismatch_rate != 0.1 || $bowtie2_k != 200 || $bowtie2_sensitivity_level ne "sensitive");
 }
 else {
     pod2usage(-msg => "Invalid number of arguments!", -exitval => 2, -verbose => 2) if (!$paired_end && scalar(@ARGV) != 3 || $paired_end && scalar(@ARGV) != 4);    
-    pod2usage(-msg => "Only one of --phred33-quals --phred64-quals/--solexa1.3-quals --solexa-suqls can be active!", -exitval => 2, -verbose => 2) if ($phred33 + $phred64 + $solexa > 1);    
-    podwusage(-msg => "--sam , --bam or --sam-header-info cannot be set if use bowtie aligner to produce alignments!", -exitval => 2, -verbose => 2) if ($is_sam || $is_bam || $fn_list ne "");
+    pod2usage(-msg => "If --no-qualities is set, neither --phred33-quals, --phred64-quals or --solexa-quals can be active!", -exitval => 2, -verbose => 2) if ($no_qual && ($phred33 + $phred64 + $solexa > 0));
+    pod2usage(-msg => "Only one of --phred33-quals, --phred64-quals, and --solexa-quals can be active!", -exitval => 2, -verbose => 2) if ($phred33 + $phred64 + $solexa > 1);    
+    pod2usage(-msg => "--sam , --bam or --sam-header-info cannot be set if use bowtie/bowtie2 aligner to produce alignments!", -exitval => 2, -verbose => 2) if ($is_sam || $is_bam || $fn_list ne "");
+    pod2usage(-msg => "--bowtie2-path, --bowtie2-mismatch-rate, --bowtie2-k and --bowtie2-sensitivity-level cannot be set if bowtie aligner is used!", -exitval => 2, -verbose => 2) if (!$bowtie2 && ($bowtie2_path ne "" || $bowtie2_mismatch_rate != 0.1 || $bowtie2_k != 200 || $bowtie2_sensitivity_level ne "sensitive"));
+    pod2usage(-msg => "--bowtie-path, --bowtie-n, --bowtie-e, --bowtie-m cannot be set if bowtie2 aligner is used!", -exitval => 2, -verbose => 2) if ($bowtie2 && ($bowtie_path ne "" || $C != 2 || $E != 99999999 || $maxHits != 200));
+    pod2usage(-msg => "Mismatch rate must be within [0, 1]!", -exitval => 2, -verbose => 2) if ($bowtie2 && ($bowtie2_mismatch_rate < 0.0 || $bowtie2_mismatch_rate > 1.0));
+    pod2usage(-msg => "Sensitivity level must be one of \"very_fast\", \"fast\", \"sensitive\", and \"very_sensitive\"!", -exitval => 2, -verbose => 2) if ($bowtie2 && (($bowtie2_sensitivity_level ne "very_fast") && ($bowtie2_sensitivity_level ne "fast") && ($bowtie2_sensitivity_level ne "sensitive") && ($bowtie2_sensitivity_level ne "very_sensitive"))); 
 }
 
 pod2usage(-msg => "Forward probability should be in [0, 1]!", -exitval => 2, -verbose => 2) if ($probF < 0 || $probF > 1);
@@ -190,39 +207,77 @@ if (!$is_sam && !$is_bam && !$no_qual && ($phred33 + $phred64 + $solexa == 0)) {
 my ($mate_minL, $mate_maxL) = (1, $maxL);
 
 if ($bowtie_path ne "") { $bowtie_path .= "/"; }
+if ($bowtie2_path ne "") { $bowtie2_path .= "/"; }
 
 my $command = "";
 
 if (!$is_sam && !$is_bam) {
-    $command = $bowtie_path."bowtie";
-    if ($no_qual) { $command .= " -f"; }
-    else { $command .= " -q"; }
-    
-    if ($phred33) { $command .= " --phred33-quals"; }
-    elsif ($phred64) { $command .= " --phred64-quals"; }
-    elsif ($solexa) { $command .= " --solexa-quals"; }
+    if (!$bowtie2) {
+       $command = $bowtie_path."bowtie";
+       if ($no_qual) { $command .= " -f"; }
+       else { $command .= " -q"; }
     
-    $command .= " -n $C -e $E -l $L";
-    if ($read_type == 2 || $read_type == 3) { $command .= " -I $minL -X $maxL"; }
-    if ($chunkMbs > 0) { $command .= " --chunkmbs $chunkMbs"; }
+       if ($phred33) { $command .= " --phred33-quals"; }
+       elsif ($phred64) { $command .= " --phred64-quals"; }
+       elsif ($solexa) { $command .= " --solexa-quals"; }
     
-    if ($strand_specific || $probF == 1.0) { $command .= " --norc"; }
-    elsif ($probF == 0.0) { $command .= " --nofw"; }
-
-    $command .= " -p $nThreads -a -m $maxHits -S";
-    if ($quiet) { $command .= " --quiet"; }    
-
-    $command .= " $refName";
-    if ($read_type == 0 || $read_type == 1) {
-       $command .= " $mate1_list"; 
+       $command .= " -n $C -e $E -l $L";
+       if ($read_type == 2 || $read_type == 3) { $command .= " -I $minL -X $maxL"; }
+       if ($chunkMbs > 0) { $command .= " --chunkmbs $chunkMbs"; }
+       
+       if ($strand_specific || $probF == 1.0) { $command .= " --norc"; }
+       elsif ($probF == 0.0) { $command .= " --nofw"; }
+       
+       $command .= " -p $nThreads -a -m $maxHits -S";
+       if ($quiet) { $command .= " --quiet"; }    
+
+       $command .= " $refName";
+       if ($read_type == 0 || $read_type == 1) {
+           $command .= " $mate1_list"; 
+       }
+       else {
+           $command .= " -1 $mate1_list -2 $mate2_list";
+       }
+
+       # pipe to samtools to generate a BAM file
+       $command .= " | $dir\sam/samtools view -S -b -o $imdName.bam -";
     }
     else {
-       $command .= " -1 $mate1_list -2 $mate2_list";
+       $command = $bowtie2_path."bowtie2";
+       if ($no_qual) { $command .= " -f"; }
+       else { $command .= " -q"; }
+
+       if ($phred33) { $command .= " --phred33"; }
+       elsif ($phred64) { $command .= " --phred64"; }
+       elsif ($solexa) { $command .= " --solexa-quals"; }
+
+       if ($bowtie2_sensitivity_level eq "very_fast") { $command .= " --very-fast"; }
+       elsif ($bowtie2_sensitivity_level eq "fast") { $command .= " --fast"; }
+       elsif ($bowtie2_sensitivity_level eq "sensitive") { $command .= " --sensitive"; }
+       else { $command .= " --very-sensitive"; }
+
+       $command .= " --dpad 0 --gbar 99999999 --mp 1,1 --np 1 --score-min L,0,-$bowtie2_mismatch_rate";  
+
+       if ($read_type == 2 || $read_type == 3) { $command .= " -I $minL -X $maxL --no-mixed --no-discordant"; }
+
+       if ($strand_specific || $probF == 1.0) { $command .= " --norc"; }
+       elsif ($probF == 0.0) { $command .= " --nofw"; }
+       
+       $command .= " -p $nThreads -k $bowtie2_k";
+       if ($quiet) { $command .= " --quiet"; }    
+
+       $command .= " -x $refName";
+       if ($read_type == 0 || $read_type == 1) {
+           $command .= " -U $mate1_list"; 
+       }
+       else {
+           $command .= " -1 $mate1_list -2 $mate2_list";
+       }
+
+       # pipe to samtools to generate a BAM file
+       $command .= " | $dir\sam/samtools view -S -b -o $imdName.bam -";
     }
 
-    # pipe to samtools to generate a BAM file
-    $command .= " | $dir\sam/samtools view -S -b -o $imdName.bam -";
-
     if ($mTime) { $time_start = time(); }
 
     &runCommand($command);
@@ -283,7 +338,7 @@ if ($quiet) { $command .= " -q"; }
 &collectResults("gene", "$imdName.gene_res", "$sampleName.genes.results"); # gene level
 
 if ($genBamF) {
-    $command = $dir."sam/samtools sort $sampleName.transcript.bam $sampleName.transcript.sorted";
+    $command = $dir."sam/samtools sort -@ $nThreads -m $SortMem $sampleName.transcript.bam $sampleName.transcript.sorted";
     &runCommand($command);
     $command = $dir."sam/samtools index $sampleName.transcript.sorted.bam";
     &runCommand($command);
@@ -291,7 +346,7 @@ if ($genBamF) {
     if ($genGenomeBamF) {
        $command = $dir."rsem-tbam2gbam $refName $sampleName.transcript.bam $sampleName.genome.bam";
        &runCommand($command);
-       $command = $dir."sam/samtools sort $sampleName.genome.bam $sampleName.genome.sorted";
+       $command = $dir."sam/samtools sort -@ $nThreads -m $SortMem $sampleName.genome.bam $sampleName.genome.sorted";
        &runCommand($command);
        $command = $dir."sam/samtools index $sampleName.genome.sorted.bam";
        &runCommand($command);
@@ -347,78 +402,6 @@ if ($mTime) {
     close(OUTPUT);
 }
 
-# command, {err_msg}
-sub runCommand {
-    print $_[0]."\n";
-    my $status = system($_[0]);
-    if ($status != 0) {
-        my $errmsg = "";
-        if (scalar(@_) > 1) { $errmsg .= $_[1]."\n"; }
-        $errmsg .= "\"$_[0]\" failed! Please check if you provide correct parameters/options for the pipeline!\n";
-        print $errmsg;
-        exit(-1);
-    }
-    print "\n";
-}
-# inpF, outF
-sub collectResults {
-    my $local_status;
-    my ($inpF, $outF);
-    my @results = ();
-    my $line;
-
-    $inpF = $_[1];
-    $outF = $_[2];
-
-    $local_status = open(INPUT, $inpF);
-    if ($local_status == 0) { print "Fail to open file $inpF!\n"; exit(-1); }
-    
-    @results = ();
-    
-    while ($line = <INPUT>) {
-       chomp($line);
-       my @local_arr = split(/\t/, $line);
-       push(@results, \@local_arr); 
-    }
-
-    close(INPUT);
-
-    $local_status = open(OUTPUT, ">$outF");
-    if ($local_status == 0) { print "Fail to create file $outF!\n"; exit(-1); }
-
-    my $n = scalar(@results);
-    my $m = scalar(@{$results[0]});
-
-    $" = "\t";
-
-    my @out_arr = ();
-    for (my $i = 0; $i < $n; $i++) {
-       if ($_[0] eq "isoform") { push(@out_arr, $transcript_title[$i]); }
-       elsif ($_[0] eq "gene") { push(@out_arr, $gene_title[$i]); }
-       else { print "A bug on 'collectResults' is detected!\n"; exit(-1); }
-    }
-    print OUTPUT "@out_arr\n";
-
-    for (my $i = 0; $i < $m; $i++) {
-       @out_arr = ();
-       for (my $j = 0; $j < $n; $j++) { push(@out_arr, $results[$j][$i]); }
-       print OUTPUT "@out_arr\n"; 
-    }
-
-    close(OUTPUT);
-}
-
-# dir
-sub showVersionInfo {
-    open(INPUT, "$_[0]\WHAT_IS_NEW");
-    my $line = <INPUT>;
-    chomp($line);
-    close(INPUT);
-    print "$line\n";
-    exit(0);
-}
-
 __END__
 
 =head1 NAME
@@ -427,14 +410,10 @@ rsem-calculate-expression
 
 =head1 SYNOPSIS
 
-=over
-
- rsem-calculate-expression [options] upstream_read_file(s) reference_name sample_name
- rsem-calculate-expression [options] --paired-end upstream_read_file(s) downstream_read_file(s) reference_name sample_name
+ rsem-calculate-expression [options] upstream_read_file(s) reference_name sample_name 
+ rsem-calculate-expression [options] --paired-end upstream_read_file(s) downstream_read_file(s) reference_name sample_name 
  rsem-calculate-expression [options] --sam/--bam [--paired-end] input reference_name sample_name
 
-=back
-
 =head1 ARGUMENTS
 
 =over
@@ -475,7 +454,7 @@ Input reads do not contain quality scores. (Default: off)
 
 =item B<--strand-specific>
 
-The RNA-Seq protocol used to generate the reads is strand specific, i.e., all (upstream) reads are derived from the forward strand.  This option is equivalent to --forward-prob=1.0.  With this option set, if RSEM runs the Bowtie aligner, the '--norc' Bowtie option will be used, which disables alignment to the reverse strand of transcripts.  (Default: off)
+The RNA-Seq protocol used to generate the reads is strand specific, i.e., all (upstream) reads are derived from the forward strand.  This option is equivalent to --forward-prob=1.0.  With this option set, if RSEM runs the Bowtie/Bowtie 2 aligner, the '--norc' Bowtie/Bowtie 2 option will be used, which disables alignment to the reverse strand of transcripts.  (Default: off)
 
 =item B<--sam>
 
@@ -491,7 +470,7 @@ RSEM reads header information from input by default. If this option is on, heade
 
 =item B<-p/--num-threads> <int>
 
-Number of threads to use. Both Bowtie and expression estimation will use this many threads. (Default: 1)
+Number of threads to use. Both Bowtie/Bowtie2, expression estimation and 'samtools sort' will use this many threads. (Default: 1)
 
 =item B<--no-bam-output>
 
@@ -519,7 +498,7 @@ The name of the optional field used in the SAM input for identifying a read with
 
 =item B<--bowtie-path> <path>
 
-The path to the bowtie executables. (Default: the path to the bowtie executables is assumed to be in the user's PATH environment variable)
+The path to the Bowtie executables. (Default: the path to the Bowtie executables is assumed to be in the user's PATH environment variable)
 
 =item B<--bowtie-n> <int>
 
@@ -535,7 +514,7 @@ The path to the bowtie executables. (Default: the path to the bowtie executables
 
 =item B<--bowtie-chunkmbs> <int>
 
-(Bowtie parameter) memory allocated for best first alignment calculation (Default: 0 - use bowtie's default)
+(Bowtie parameter) memory allocated for best first alignment calculation (Default: 0 - use Bowtie's default)
 
 =item B<--phred33-quals>
 
@@ -549,17 +528,37 @@ Input quality scores are encoded as Phred+64 (default for GA Pipeline ver. >= 1.
 
 Input quality scores are solexa encoded (from GA Pipeline ver. < 1.3). (Default: off)
 
+=item B<--bowtie2>
+
+Use Bowtie 2 instead of Bowtie to align reads. Since currently RSEM does not handle indel, local and discordant alignments, the Bowtie2 parameters are set in a way to avoid those alignments. In particular, we use options '--sensitive --dpad 0 --gbar 99999999 --mp 1,1 --np 1 --score-min L,0,-0.1' by default. "-0.1", the last parameter of '--score-min' is the negative value of the maximum mismatch rate allowed. This rate can be set by option '--bowtie2-mismatch-rate'. If reads are paired-end, we additionally use options '--no-mixed' and '--no-discordant'. (Default: off)
+
+=item B<--bowtie2-path> <path>
+
+(Bowtie 2 parameter) The path to the Bowtie 2 executables. (Default: the path to the Bowtie 2 executables is assumed to be in the user's PATH environment variable)
+
+=item B<--bowtie2-mismatch-rate> <double>
+
+(Bowtie 2 parameter) The maximum mismatch rate allowed. (Default: 0.1)
+
+=item B<--bowtie2-k> <int>
+
+(Bowtie 2 parameter) Find up to <int> alignments per read. (Default: 200)
+
+=item B<--bowtie2-sensitivity-level> <string>
+
+(Bowtie 2 parameter) Set Bowtie 2's preset options in --end-to-end mode. This option controls how hard Bowtie 2 tries to find alignments. <string> must be one of "very_fast", "fast", "sensitive" and "very_sensitive". The four candidates correspond to Bowtie 2's "--very-fast", "--fast", "--sensitive" and "--very-sensitive" options. (Default: "sensitive" - use Bowtie 2's default)
+
 =item B<--forward-prob> <double>
 
 Probability of generating a read from the forward strand of a transcript. Set to 1 for a strand-specific protocol where all (upstream) reads are derived from the forward strand, 0 for a strand-specific protocol where all (upstream) read are derived from the reverse strand, or 0.5 for a non-strand-specific protocol. (Default: 0.5)
 
 =item B<--fragment-length-min> <int>
 
-Minimum read/insert length allowed. This is also the value for the bowtie -I option. (Default: 1)
+Minimum read/insert length allowed. This is also the value for the Bowtie/Bowtie2 -I option. (Default: 1)
 
 =item B<--fragment-length-max> <int>
 
-Maximum read/insert length allowed. This is also the value for the bowtie -X option. (Default: 1000)
+Maximum read/insert length allowed. This is also the value for the Bowtie/Bowtie 2 -X option. (Default: 1000)
 
 =item B<--fragment-length-mean> <double>
 
@@ -581,6 +580,10 @@ Number of bins in the RSPD. Only relevant when '--estimate-rspd' is specified.
 
 Maximum size (in memory, MB) of the auxiliary buffer used for computing credibility intervals (CI). Set it larger for a faster CI calculation. However, leaving 2 GB memory free for other usage is recommended. (Default: 1024)
 
+=item B<--samtools-sort-mem> <string>
+
+Set the maximum memory per thread that can be used by 'samtools sort'. <string> represents the memory and accepts suffices 'K/M/G'. RSEM will pass <string> to the '-m' option of 'samtools sort'.  Please note that the default used here is different from the default used by samtools. (Default: 1G)
+
 =item B<--keep-intermediate-files>
 
 Keep temporary files generated by RSEM.  RSEM creates a temporary directory, 'sample_name.temp', into which it puts all intermediate output files. If this directory already exists, RSEM overwrites all files generated by previous RSEM runs inside of it. By default, after RSEM finishes, the temporary directory is deleted.  Set this option to prevent the deletion of this directory and the intermediate files inside of it. (Default: off)
@@ -601,6 +604,10 @@ Suppress the output of logging information. (Default: off)
 
 Show help information.
 
+=item B<--version>
+
+Show version information.
+
 =back
 
 =head1 DESCRIPTION
@@ -791,7 +798,7 @@ Assume the path to the bowtie executables is in the user's PATH environment vari
                            /ref/mouse_125 \
                            mmliver_single_without_quals
 
-4) Data are the same as 1). We want to take a fragment length distribution into consideration. We set the fragment length mean to 150 and the standard deviation to 35. In addition to a BAM file, we also want to generate credibility intervals.  We allow RSEM to use 1GB of memory for CI calculation:
+4) Data are the same as 1). This time we assume the bowtie executables are under '/sw/bowtie'. We want to take a fragment length distribution into consideration. We set the fragment length mean to 150 and the standard deviation to 35. In addition to a BAM file, we also want to generate credibility intervals. We allow RSEM to use 1GB of memory for CI calculation:
 
  rsem-calculate-expression --bowtie-path /sw/bowtie \
                            --phred64-quals \
@@ -805,7 +812,7 @@ Assume the path to the bowtie executables is in the user's PATH environment vari
                            /ref/mouse_125 \
                            mmliver_single_quals
 
-5) '/data/mmliver_paired_end_quals.bam', paired-end reads with quality scores.  We want to use 8 threads:
+5) '/data/mmliver_paired_end_quals.bam', paired-end reads with quality scores. We want to use 8 threads:
 
  rsem-calculate-expression --paired-end \
                            --bam \