File: run_Stringtie_via_bam_file_list.pl

package info (click to toggle)
trinityrnaseq 2.15.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 468,004 kB
  • sloc: perl: 49,905; cpp: 17,993; java: 12,489; python: 3,282; sh: 1,989; ansic: 985; makefile: 717; xml: 62
file content (148 lines) | stat: -rwxr-xr-x 3,205 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env perl

use strict;
use warnings;

use FindBin;
use lib("$FindBin::RealBin/../../PerlLib");
use Pipeliner;
use File::Basename;
use Cwd;
use List::Util qw(min);

use Carp;
use Getopt::Long qw(:config no_ignore_case bundling pass_through);


my $usage = <<__EOUSAGE__;

######################################################################
#
#  Required:
#  --bam_file_list  <string>   bam_file_list (format: sample_id(tab)/path/to/file.bam)
#  
#  Optional
#  --gtf <string>              annotations in gtf format
#  --CPU <int>                 number of threads (default: 2)
#  --SS_lib_type <string>      
# 
#
#######################################################################


__EOUSAGE__

    ;


my $bam_file_list;

my $CPU = 2;

my $help_flag;
my $gtf_file;

my $SS_lib_type;

&GetOptions( 'h' => \$help_flag,

             'bam_file_list=s' => \$bam_file_list,
             'CPU=i' => \$CPU,
             'gtf=s' => \$gtf_file,

             'SS_lib_type=s' => \$SS_lib_type,
    );


unless ($bam_file_list) {
    die $usage;
}

if ($help_flag) {
    die $usage;
}

if (@ARGV) {
    die "Error, cannot recognize opts: @ARGV";
}



main: {
	
    ## ensure all full paths
    $gtf_file = &Pipeliner::ensure_full_path($gtf_file) if $gtf_file;
    
    my @entries;
    open(my $fh, $bam_file_list) or die $!;
    while(<$fh>) {
        chomp;
        my ($sample_id, $bam_filename) = split(/\t/);
        unless ($sample_id && $bam_filename) {
            die "Error, bam file list doesn't have expected tab-delimited formatting at $_";
        }
        unless (-e $bam_filename) {
            die "Error, cannot locate file: $bam_filename";
        }
        
        push (@entries, { sample_id => $sample_id,
                          bam => $bam_filename } );

    }

    my $pipeliner = new Pipeliner(-verbose => 1);
      
    
    my @indiv_stringtie_outputs;
    
    foreach my $entry (@entries) {
        my $cmd = "stringtie " . $entry->{bam} . 
            " -l STRG." . $entry->{sample_id} . " ";
        
        if ($SS_lib_type) {
            if ($SS_lib_type =~ /^R/i) {
                $cmd .= " --rf ";
            }
            elsif ($SS_lib_type =~ /^F/i) {
                $cmd .= " --fr ";
            }
            else {
                die "Error, cannot determine strand-specificity type from $SS_lib_type";
            }
        }

        $cmd .= " -o " . $entry->{sample_id} . ".strg.gtf";
        
        push (@indiv_stringtie_outputs, $entry->{sample_id} . ".strg.gtf");

        if ($gtf_file) {
            $cmd .= " -G $gtf_file ";
        }
        my $checkpoint = $entry->{sample_id} . ".strg.ok";
        
        $pipeliner->add_commands( new Command($cmd, $checkpoint));

    }
    
    $pipeliner->run();


    # merge transcripts
    if (scalar(@indiv_stringtie_outputs) > 1) {
        my $cmd = "stringtie --merge -o stringtie.merged.gtf -g 1 ";
        if ($gtf_file) {
            $cmd .= " -G $gtf_file ";
        }
        $cmd .= join(" ", @indiv_stringtie_outputs);
    
        $pipeliner->add_commands( new Command($cmd, "stringtie_merge.ok"));
        
        $pipeliner->run();
        
    }
    
        
	exit(0);
}