This file is indexed.

/usr/share/perl5/Bio/Tradis/CombinePlots.pm is in bio-tradis 1.3.3+dfsg-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
package Bio::Tradis::CombinePlots;

# ABSTRACT: Combine multiple plotfiles and generate updated statistics for the combined files

=head1 SYNOPSIS

Takes a tab-delimited file with an ID as the first column followed by 
a list of plotfiles to combine per row. The ID will be used to name the new
plotfile and as an identifier in the stats file, so ensure these are unique.

For example, an input file named plots_to_combine.txt:

    tradis1 plot1.1.gz  plot1.2.gz plot1.3.gz
    tradis2 plot2.1.gz  plot2.2.gz
    tradis3 plot3.1.gz  plot3.2.gz plot3.3.gz   plot3.4.gz

will produce 

=over

1. a directory named combined with 3 files - tradis1.insertion_site_plot.gz,
tradis2.insertion_site_plot.gz, tradis3.insertion_site_plot.gz
2. a stats file named plots_to_combine.stats

=back

=head1 USAGE

   use Bio::Tradis::CombinePlots;
   
   my $pipeline = Bio::Tradis::CombinePlots->new(plotfile => 'abc');
   $pipeline->combine;

=cut

use Moose;
use strict;
use warnings;
use File::Temp;
use File::Path qw( remove_tree );
use Data::Dumper;
use Cwd;
use Bio::Tradis::Analysis::Exceptions;

has 'plotfile'     => ( is => 'rw', isa => 'Str', required => 1 );
has 'combined_dir' => ( is => 'rw', isa => 'Str', default  => 'combined' );
has '_plot_handle' => (
    is       => 'ro',
    isa      => 'FileHandle',
    required => 0,
    lazy     => 1,
    builder  => '_build__plot_handle'
);
has '_stats_handle' => (
    is       => 'ro',
    isa      => 'FileHandle',
    required => 0,
    lazy     => 1,
    builder  => '_build__stats_handle'
);
has '_ordered_plot_ids' => (
    is       => 'rw',
    isa      => 'ArrayRef',
    required => 0,
    lazy     => 1,
    builder  => '_build__ordered_plot_ids'
);
has '_destination' => (
    is       => 'rw',
    isa      => 'Str',
    required => 0,
    lazy     => 1,
    builder  => '_build__destination'
);

sub _build__destination {
    my $tmp_dir = File::Temp->newdir( DIR=> getcwd, CLEANUP => 0 );
    return $tmp_dir->dirname;
}

sub _build__stats_handle {
    my ($self) = @_;
    my $filelist = $self->plotfile;
    $filelist =~ s/([^\/]+$)//;
    my $filename = $1;
    $filename =~ s/[^\.]+$/stats/;
    open( my $stats, ">", $filename );
    return $stats;
}

sub _build__plot_handle {
    my ($self) = @_;
    my $plot = $self->plotfile;
    open( my $plot_h, "<", $plot );
    return $plot_h;
}

sub _build__ordered_plot_ids {
    my ($self) = @_;
    my $filelist = $self->plotfile;

    my @id_order = `awk '{print \$1}' $filelist`;
    foreach my $id (@id_order) {
        chomp($id);
    }
    return \@id_order;
}

sub combine {
    my ($self)       = @_;
    my $ordered_keys = $self->_ordered_plot_ids;
    my $plot_handle  = $self->_plot_handle;
    my $combined_dir = $self->combined_dir;

    $self->_write_stats_header;


    system("mkdir $combined_dir") unless ( -d "$combined_dir" );
    my @tabix_plot;

    while ( my $line = <$plot_handle> ) {
        #parse line into hash. keys = id, len, files. unzips files if needed.
        my %plothash = $self->_parse_line($line);
        my $id       = $plothash{'id'};

        #create output plot file
        my $comb_plot_name = "$combined_dir/$id.insert_site_plot";
        my $filelen = $plothash{'len'};
        my ( @currentlines, $this_line );

        my @full_plot;
        foreach my $i ( 0 .. $filelen ) {
            @currentlines = ();

            foreach my $curr_fh ( @{ $plothash{'files'} } ) {
                $this_line = <$curr_fh>;
                push( @currentlines, $this_line ) if( defined $line && $line ne "");
            }

            my $comb_line = $self->_combine_lines( \@currentlines );

	    my $plot_values_tabix = $comb_line;
	    $plot_values_tabix =~ s/\s/\t/ if(defined $plot_values_tabix && $plot_values_tabix ne "");

	    my $tabix_line;
	    if ($id !~ m/^zip_combined/) {
	      my $tabix_line = "$id\t$i\t" . $plot_values_tabix if( defined $plot_values_tabix && $plot_values_tabix ne "");
	      push( @tabix_plot, $tabix_line ) if( $comb_line ne "");
	    }

            push(@full_plot, $comb_line) if ( $comb_line ne '' );
        }

        open( CPLOT, '>', $comb_plot_name );
        print CPLOT join("\n", @full_plot);
        close(CPLOT);

        $self->_write_stats($id, $filelen);
        system("gzip -f $comb_plot_name");
    }


    if (@tabix_plot) {
      $self->_prepare_and_create_tabix_for_combined_plots(\@tabix_plot);
    }

	File::Temp::cleanup();
    # double check tmp dir is deleted. cleanup not working properly
    remove_tree($self->_destination);
    return 1;
}

sub _prepare_and_create_tabix_for_combined_plots {

  my ($self, $tabix_plot) = @_;

  my $tabix_plot_name = "combined/tabix.insert_site_plot.gz";
  my $sorted_tabix_plot_name = "combined/tabix_sorted.insert_site_plot.gz";

  open(my $tabix_plot_fh , '|-', " gzip >". $tabix_plot_name) or warn "Couldn't create the initial plot file for tabix";
  print $tabix_plot_fh join( "\n", @{ $tabix_plot } );
  close($tabix_plot_fh);

  `cat $tabix_plot_name | gunzip - | sort -k1,1 -k2,2n | bgzip > $sorted_tabix_plot_name && tabix -b 2 -e 2 $sorted_tabix_plot_name`;
  unlink($tabix_plot_name);

}


sub _parse_line {
    my ( $self, $line ) = @_;
    chomp $line;
    my @fields = split( /\s+/, $line );
    my $id     = shift @fields;
    my @files = @{ $self->_unzip_plots(\@fields) };
    my $len    = $self->_get_file_len( \@files );
    if ( $len == 0 ){
        die "\nPlots with ID $id not of equal length.\n";
    }
    #build file handles for each file
    my @file_hs;
    foreach my $f (@files){
        open(my $fh, "<", $f);
        push(@file_hs, $fh);
    }
    return ( id => $id, len => $len, files => \@file_hs );
}

sub _get_file_len {
    my ( $self, $files ) = @_;

    #check all files are of equal lens and return len if true
    #wc misses last line - return $l++
    my @lens;
    for my $f ( @{$files} ) {
        my $wc = `wc $f | awk '{print \$1}'`;
        chomp $wc;
        push( @lens, $wc );
    }

    my $l = shift @lens;
    for my $x (@lens) {
        return 0 if ( $x != $l );
    }
    return $l+1;
}

sub _combine_lines {
    my ( $self, $lines ) = @_;

    my @totals = ( 0, 0 );
    foreach my $l ( @{$lines} ) {
        if(!defined($l)){
            return "";
            next;
        }
        my @cols = split( /\s+/, $l );
        $totals[0] += $cols[0];
        $totals[1] += $cols[1];
    }
    return join( " ", @totals );
}

sub _write_stats_header {
    my ($self) = @_;
    my @fields =
      ( "ID", "Sequence Length", "Unique Insertion Sites", "Seq Len/UIS" );
    print { $self->_stats_handle } join( ",", @fields ) . "\n";
    return 1;
}

sub _write_stats {
    my ( $self, $id, $seq_len ) = @_;
    my $combined_dir = $self->combined_dir;
    my $comb_plot = "$combined_dir/$id.insert_site_plot";

    #my $seq_len = `wc $comb_plot | awk '{print \$1}'`;
    #chomp($seq_len);
    my $uis = `grep -c -v "0 0" $comb_plot`;
    chomp($uis);
    my $sl_per_uis = "NaN";
    $sl_per_uis = $seq_len / $uis if($uis > 0);

    my $stats = "$id,$seq_len,$uis,$sl_per_uis\n";
    print { $self->_stats_handle } $stats;

    return 1;
}

sub _abs_path_list {
    my ( $self, $files ) = @_;
    my $plot_path = $self->_get_plotfile_path;

    my @pathlist;
    foreach my $f ( @{$files} ) {
        if   ( $f =~ /^\// ) { push( @pathlist, $f ); }
        else                 { push( @pathlist, $plot_path . $f ); }
    }
    return \@pathlist;
}

sub _get_plotfile_path {
    my ($self) = @_;
    my $plotfile = $self->plotfile;

    my @dirs = split( '/', $plotfile );
    pop(@dirs);
    my $path2plot = join( '/', @dirs );
    return "$path2plot/";
}

sub _is_gz {
    my ( $self, $plotname ) = @_;

    if ( $plotname =~ /\.gz$/ ) {
        return 1;
    }
    else {
        return 0;
    }
}

sub _unzip_plots {
    my ( $self, $files ) = @_;
    my $destination_directory = $self->_destination;

    my @filelist = @{ $self->_abs_path_list($files) };
    my @unz_plots;
    foreach my $plotname ( @filelist ) {
        Bio::Tradis::Analysis::Exceptions::FileNotFound->throw("Cannot find $plotname\n") unless ( -e $plotname );
        if ( $self->_is_gz($plotname) ) {
            $plotname =~ /([^\/]+$)/;
            my $unz = $1;
            $unz =~ s/\.gz//;
            my $unzip_cmd = "gunzip -c $plotname > $destination_directory/$unz";
            system($unzip_cmd);
            push(@unz_plots, "$destination_directory/$unz");
        }
        else {
            push(@unz_plots, $plotname);
        }
    }
    return \@unz_plots;
}

__PACKAGE__->meta->make_immutable;
no Moose;
1;