File: partitioned_trinity_aggregator.pl

package info (click to toggle)
trinityrnaseq 2.11.0%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 417,528 kB
  • sloc: perl: 48,420; cpp: 17,749; java: 12,695; python: 3,124; sh: 1,030; ansic: 983; makefile: 688; xml: 62
file content (155 lines) | stat: -rwxr-xr-x 3,884 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
149
150
151
152
153
154
155
#!/usr/bin/env perl

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

my $help_flag;


my $usage = <<__EOUSAGE;

#################################################
#
# usage:  $0 [opts] < trinity_fasta_files_listing
#
#################################################
#
# Required
#
#  --token_prefix <string>       eg.  TRINITY_DN or TRINITY_GG
#
#  --output_prefix <string>      eg.  Trinity  or Trinity_GG
#
# Optional:
#
#  --include_supertranscripts    flag, captures the supertranscripts fasta and gtf
#
##################################################  




__EOUSAGE

    ;
    

my $token_prefix;
my $output_prefix;
my $include_supertranscripts_flag;

&GetOptions ( 'h' => \$help_flag,
              'token_prefix=s' => \$token_prefix,
              'output_prefix=s' => \$output_prefix,
              'include_supertranscripts' => \$include_supertranscripts_flag,
    );


if ($help_flag) {
    die $usage;
}

unless ($token_prefix && $output_prefix) {
    die $usage;
}

my $ofh_trinity_fasta;
open($ofh_trinity_fasta, ">$output_prefix.fasta") or die "Error, cannot write to $output_prefix.fasta";

my $ofh_supertrans_fasta;
my $ofh_supertrans_gtf;
if ($include_supertranscripts_flag) {
    open($ofh_supertrans_fasta, ">$output_prefix.SuperTrans.fasta") or die $!;
    open($ofh_supertrans_gtf, ">$output_prefix.SuperTrans.gtf") or die $!;
}


main: {

    while (<STDIN>) {
        my $filename = $_;
        chomp $filename;
        unless (-e $filename) {
            print STDERR "ERROR, filename: $filename is indicated to not exist.\n";
            next;
        }
        if (-s $filename) {
            
            # ie. read_partitions/Fb_0/CBin_161/c16167.trinity.reads.fa
            
            $filename =~ m|c(\d+)\.trinity\.reads| or die "Error, cannot parse Trinity component value from filename: $filename";
            my $component = $1;

            &process_files($filename, $component);
            
        }
    }

    exit(0);
}
            

####
sub process_files {
    my ($filename, $component) = @_;

    {
        # write fasta
        open (my $fh, $filename) or die "Error, cannot open file $filename";
        while (<$fh>) {
            if (/>/) {
                s/>/>${token_prefix}${component}\_/;
            }
            print $ofh_trinity_fasta $_;
        }
        close $fh;
    }

    if ($include_supertranscripts_flag) {
        # write supertranscripts
        
        my $core_filename = $filename;
        $core_filename =~ s/\.fasta$//;

        { # supertranscripts fasta:
            my $supertranscripts_fasta = "$core_filename.SuperTrans.fasta";
            if (! -s $supertranscripts_fasta) {
                die "Error, missing file: $supertranscripts_fasta";
            }
            open(my $fh, $supertranscripts_fasta) or die "Error, cannot open file: $supertranscripts_fasta";
            while (<$fh>) {
                if (/>/) {
                    s/>/>${token_prefix}${component}\_/;
                }
                print $ofh_supertrans_fasta $_;
            }
            close $fh;
        }

        {
            # supertranscripts gtf
            my $supertranscripts_gtf = "$core_filename.SuperTrans.gtf";
            if (! -s $supertranscripts_gtf) {
                die "Error, missing file: $supertranscripts_gtf";
            }
            open(my $fh, $supertranscripts_gtf) or die "Error, cannot open file $supertranscripts_gtf";
            while (<$fh>) {
                my $line = $_;
                if (/\w/) {

                    my @x = split(/\t/, $line);
                    my $id = $x[0];
                    $line =~ s/$id/${token_prefix}${component}_${id}/g;

                }
                print $ofh_supertrans_gtf $line;
            }
            close $fh;
        }
    }
    
    return;
}