1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
#!/usr/bin/env perl
use strict;
use warnings;
use lib("/usr/lib/trinityrnaseq/PerlLib");
use Pipeliner;
use File::Basename;
use Cwd;
use Carp;
use Getopt::Long qw(:config no_ignore_case bundling pass_through);
my $usage = <<__EOUSAGE__;
######################################################################
#
# Required:
# --genome <string> target genome to align to
# --gtf|G <string> GTF file for incorporating reference splice site info. (recommended)
#
# --reads <string> fastq files. If pairs, indicate both in quotes, ie. "left.fq right.fq"
#
# Optional:
# --CPU <int> number of threads (default: 2)
# --out_prefix <string> output prefix (default: star)
# --out_dir <string> output directory (default: current working directory)
# --star_path <string> full path to the STAR program to use.
# --patch <string> genomic targets to patch the genome fasta with.
# --chim_search include Chimeric.junction outputs
#
#######################################################################
__EOUSAGE__
;
my ($genome, $reads);
my $CPU = 2;
my $help_flag;
my $out_prefix = "star";
my $gtf_file;
my $out_dir;
my $ADV = 0;
my $star_path = "STAR";
my $patch;
my $chim_search;
&GetOptions( 'h' => \$help_flag,
'genome=s' => \$genome,
'reads=s' => \$reads,
'CPU=i' => \$CPU,
'out_prefix=s' => \$out_prefix,
'gtf|G=s' => \$gtf_file,
'out_dir=s' => \$out_dir,
'ADV' => \$ADV,
'star_path=s' => \$star_path,
'patch=s' => \$patch,
'chim_search' => \$chim_search,
);
unless ($genome && $reads) {
die $usage;
}
if ($help_flag) {
die $usage;
}
if (@ARGV) {
die "Error, cannot recognize opts: @ARGV";
}
my $star_prog = `which $star_path`;
chomp $star_prog;
unless ($star_prog =~ /\w/) {
die "Error, cannot locate STAR program. Be sure it's in your PATH setting. ";
}
main: {
## ensure all full paths
$genome = &Pipeliner::ensure_full_path($genome);
$gtf_file = &Pipeliner::ensure_full_path($gtf_file) if $gtf_file;
my @read_files = split(/\s+/, $reads);
foreach my $read_file (@read_files) {
if ($read_file) {
$read_file = &Pipeliner::ensure_full_path($read_file);
}
}
$reads = join(" ", @read_files);
if ($out_dir) {
unless (-d $out_dir) {
mkdir $out_dir or die "Error, cannot mkdir $out_dir";
}
chdir $out_dir or die "Error, cannot cd to $out_dir";
}
my $star_index = "$genome.star.idx";
if (! -e "$star_index/build.ok") {
## build star index
unless (-d $star_index) {
mkdir($star_index) or die "Error, cannot mkdir $star_index";
}
my $cmd = "$star_prog --runThreadN $CPU --runMode genomeGenerate --genomeDir $star_index "
. " --genomeFastaFiles $genome "
. " --limitGenomeGenerateRAM 40419136213 ";
if ($gtf_file) {
$cmd .= " --sjdbGTFfile $gtf_file "
. " --sjdbOverhang 100 ";
}
&process_cmd($cmd);
&process_cmd("touch $star_index/build.ok");
}
## run STAR
my @tmpfiles;
my $pipeliner = new Pipeliner(-verbose => 1);
my $cmd = "$star_prog "
. " --runThreadN $CPU "
. " --genomeDir $star_index "
. " --outSAMtype BAM SortedByCoordinate "
. " --runMode alignReads "
. " --readFilesIn $reads "
. " --twopassMode Basic "
. " --alignSJDBoverhangMin 10 "
. " --outSAMstrandField intronMotif "
. " --outSAMunmapped Within "
. " --limitBAMsortRAM 20000000000";
if ($chim_search) {
$cmd .= " --chimJunctionOverhangMin 12 "
. " --chimSegmentMin 12 "
. " --chimSegmentReadGapMax parameter 3 "
}
if ($patch) {
$cmd .= " --genomeFastaFiles $patch ";
}
$cmd .= " --alignSJstitchMismatchNmax 5 -1 5 5 "; #which allows for up to 5 mismatches for non-canonical GC/AG, and AT/AC junctions, and any number of mismatches for canonical junctions (the default values 0 -1 0 0 replicate the old behavior (from AlexD)
if ($reads =~ /\.gz$/) {
$cmd .= " --readFilesCommand 'gunzip -c' ";
}
$pipeliner->add_commands( new Command($cmd, "star_align.ok") );
my $bam_outfile = "Aligned.sortedByCoord.out.bam";
my $renamed_bam_outfile = "$out_prefix.sortedByCoord.out.bam";
$pipeliner->add_commands( new Command("mv $bam_outfile $renamed_bam_outfile", "$renamed_bam_outfile.ok") );
$pipeliner->add_commands( new Command("samtools index $renamed_bam_outfile", "$renamed_bam_outfile.bai.ok") );
$pipeliner->run();
exit(0);
}
####
sub process_cmd {
my ($cmd) = @_;
print STDERR "CMD: $cmd\n";
#return;
my $ret = system($cmd);
if ($ret) {
die "Error, cmd: $cmd died with ret ($ret)";
}
return;
}
|