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
|
#!/usr/bin/env perl
use strict;
use warnings;
use Carp;
use Getopt::Long qw(:config no_ignore_case bundling pass_through);
use lib ("/usr/lib/trinityrnaseq/PerlLib");
use WigParser;
my $jaccard_file;
my $trough_win = 200;
my $min_jaccard_delta = 0.35;
my $max_jaccard_trough_val = 0.05;
my $coverage_wig;
my $usage = <<__EOUSAGE__;
##############################################################################################################################
#
# Required;
#
# --jaccard_wig <string> :jaccard wig file
#
# Optional:
#
# --trough_win <int> :default($trough_win)
# --min_jaccard_delta <float> :default($min_jaccard_delta)
# --max_jaccard_trough_val <float> :default($max_jaccard_trough_val)
#
# --coverage_wig <string> :fragment coverage wig. Smallest coverage value in trough_win of putative clip is used.
#
#
##############################################################################################################################
__EOUSAGE__
;
my $VERBOSE = 0;
my $help_flag;
&GetOptions ( 'h' => \$help_flag,
'jaccard_wig=s' => \$jaccard_file,
'trough_win=i' => \$trough_win,
'min_jaccard_delta=f' => \$min_jaccard_delta,
'max_jaccard_trough_val=f' => \$max_jaccard_trough_val,
'coverage_wig=s' => \$coverage_wig,
);
unless ($jaccard_file) {
die $usage;
}
if ($help_flag) {
die $usage;
}
if (@ARGV) {
die "Error, couldn't parse params: @ARGV";
}
main: {
my $scaff_to_jaccard_parser = new WigParser($jaccard_file);
my $scaff_to_frag_coverage_parser;
if ($coverage_wig) {
$scaff_to_frag_coverage_parser = new WigParser($coverage_wig);
}
foreach my $scaff (sort $scaff_to_jaccard_parser->get_contig_list()) {
my @jaccard_vals = $scaff_to_jaccard_parser->get_wig_array($scaff);
my $vals_aref = \@jaccard_vals; # retaining old usage after module refactoring
my @trough_positions;
for (my $i = $trough_win; $i <= $#$vals_aref; $i++) {
my $left_win_pos = $i - $trough_win;
my $right_win_pos = $i;
my $center_win = int( ($left_win_pos + $right_win_pos)/2);
my $jaccard_val_left = $vals_aref->[$left_win_pos];
my $jaccard_val_right = $vals_aref->[$right_win_pos];
my $jaccard_val_center = $vals_aref->[$center_win];
if ($jaccard_val_center <= $max_jaccard_trough_val) {
## found a trough
push (@trough_positions, { pos => $center_win,
jaccard => $jaccard_val_center,
avg_delta => ( ($jaccard_val_left - $jaccard_val_center ) + ($jaccard_val_right - $jaccard_val_center) ) / 2,
} );
}
}
if (@trough_positions) {
@trough_positions = &group_trough_positions_within_window_select_best_clip(@trough_positions);
## restrict clips to those that are in troughs of required depth from neighboring hills
my @clips = &require_neighboring_hills(\@trough_positions, $vals_aref);
if ($coverage_wig) {
my @coverage_array = $scaff_to_frag_coverage_parser->get_wig_array($scaff);
&reposition_clips_by_min_coverage(\@clips, \@coverage_array);
}
print "variableStep chrom=$scaff\n";
foreach my $clip (@clips) {
my $pos = $clip->{pos};
print join("\t", $pos, 1) . "\n";
}
}
}
exit(0);
}
####
sub group_trough_positions_within_window_select_best_clip {
my @trough_positions = @_;
my @trough_groups;
my $trough_pos = shift @trough_positions;
push (@trough_groups, [$trough_pos]);
while (@trough_positions) {
my $curr_trough_entry = shift @trough_positions;
my $curr_trough_pos = $curr_trough_entry->{pos};
my $prev_trough_group_aref = $trough_groups[$#trough_groups];
my $last_pos_val = $prev_trough_group_aref->[$#$prev_trough_group_aref]->{pos};
if ($curr_trough_pos - $last_pos_val <= $trough_win) {
push (@$prev_trough_group_aref, $curr_trough_entry);
}
else {
## start a new group
push (@trough_groups, [ $curr_trough_entry ] );
}
}
## take the best one as a clip point
## best defined here as the one with the least jaccard support and greatest delta value
my @clips;
foreach my $group (@trough_groups) {
my @eles = @$group;
@eles = sort {$a->{jaccard}<=>$b->{jaccard}
||
$b->{avg_delta} <=> $a->{avg_delta} ## order should be low jaccard, high avg_delta
} @eles;
my $clip_ele = shift @eles;
push (@clips, $clip_ele);
}
return(@clips);
}
####
sub require_neighboring_hills {
my ($clips_aref, $jaccard_aref) = @_;
my @validated_clips;
foreach my $clip (@$clips_aref) {
my $pos = $clip->{pos};
my $jaccard = $clip->{jaccard};
my $hill_jaccard_min = $jaccard + $min_jaccard_delta;
my $left_hill_stop = $pos - $trough_win;
if ($left_hill_stop < 1) {
$left_hill_stop = 1;
}
my $right_hill_stop = $pos + $trough_win;
if ($right_hill_stop > $#$jaccard_aref) {
$right_hill_stop = $#$jaccard_aref;
}
if (&search_for_hill($jaccard_aref, $left_hill_stop, $pos-1, $hill_jaccard_min)
&&
&search_for_hill($jaccard_aref, $pos + 1, $right_hill_stop, $hill_jaccard_min) ) {
push (@validated_clips, $clip);
}
}
return(@validated_clips);
}
####
sub search_for_hill {
my ($jaccard_aref, $lend, $rend, $min_jaccard_val) = @_;
for (my $i = $lend; $i <= $rend; $i++) {
if ($jaccard_aref->[$i] >= $min_jaccard_val) {
return(1);
}
}
return(0);
}
####
sub reposition_clips_by_min_coverage {
my ($clips_aref, $coverage_aref) = @_;
foreach my $clip (@$clips_aref) {
my $clip_pos = $clip->{pos};
my $win_left = $clip_pos - int($trough_win/2);
if ($win_left < 1) {
$win_left = 1;
}
my $win_right = $clip_pos + int($trough_win/2);
if ($win_right > $#$coverage_aref) {
$win_right = $#$coverage_aref;
}
my $min_cov_val = $coverage_aref->[$clip_pos];
my $min_cov_pos = $clip_pos;
for (my $i = $win_left; $i <= $win_right; $i++) {
my $cov = $coverage_aref->[$i];
if ($cov < $min_cov_val) {
$min_cov_val = $cov;
$min_cov_pos = $i;
}
}
$clip->{pos} = $min_cov_pos;
}
return;
}
|