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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
|
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
use lib ("$FindBin::RealBin/../../../PerlLib");
use SingleLinkageClusterer;
use PSL_parser;
use Getopt::Long qw(:config no_ignore_case bundling);
use File::Basename;
my $usage = <<__EOUSAGE__;
####################################################################################
#
# --target <string>
# --query <string>
#
# --min_per_id <int> default(95)
# --max_per_gap <int> default(5)
# --min_per_length <float> default(100)
#
# --forward_orient
#
# --allow_non_unique_mappings a single query transcript counts towards all
# transcripts encapsulated
#
# --reuse don't prompt for reusing blat if the output already exists.
# --no_reuse dont prompt and do NOT reuse existing outputs
#
# --out_prefix <string> output prefix
#
#####################################################################################
__EOUSAGE__
;
my $target;
my $query;
my $min_per_id = 95;
my $max_per_gap = 5;
my $min_per_length = 100;
my $forward_orient = 0;
my $help_flag;
my $allow_non_unique_mappings = 0;
my $reuse_flag = 0;
my $no_reuse_flag = 0;
my $out_prefix = "";
&GetOptions ( 'h' => \$help_flag,
'target=s' => \$target,
'query=s' => \$query,
'min_per_id=i' => \$min_per_id,
'forward_orient' => \$forward_orient,
'max_per_gap=i' => \$max_per_gap,
'allow_non_unique_mappings' => \$allow_non_unique_mappings,
'min_per_length=f' => \$min_per_length,
'reuse' => \$reuse_flag,
'no_reuse' => \$no_reuse_flag,
'out_prefix=s' => \$out_prefix,
);
unless ($target && $query) {
die $usage;
}
if ($help_flag) {
die $usage;
}
my %mapping_info;
main: {
unless ($out_prefix) {
$out_prefix = basename($query);
}
my $blat_output = "$out_prefix.pslx";
my $blat_prog = `which blat`;
chomp $blat_prog;
unless ($blat_prog) {
die "Error, cannot find a blat program in your path.\n";
}
my $cmd = "$blat_prog -t=dna -q=dna -out=pslx $target $query $blat_output > /dev/null";
my $run_blat = 0;
if (-s $blat_output) {
if ($no_reuse_flag) {
$run_blat = 1;
}
elsif (! $reuse_flag) {
print STDERR "Regenerate blat output file?";
my $response = <STDIN>;
if ($response =~ /^y/i) {
$run_blat = 1;
}
}
}
else {
$run_blat = 1;
}
if ($run_blat) {
my $ret = system($cmd);
if ($ret) {
die "Error, cmd: $cmd died with ret $ret";
}
else {
print STDERR "Blat done. Parsing output...\n";
}
}
my %gene_to_encapsulating_assemblies = &examine_blat_mappings($blat_output);
my @pairs;
foreach my $gene (keys %gene_to_encapsulating_assemblies) {
my @assemblies = @{$gene_to_encapsulating_assemblies{$gene}};
if ($allow_non_unique_mappings) {
$gene =~ s/GENE://;
foreach my $asm (@assemblies) {
$asm =~ s/ASM://;
}
print "$gene\t" . join(",", @assemblies) . "\n";
next;
}
foreach my $assembly (@assemblies) {
push (@pairs, [$gene, $assembly]);
}
}
if ($allow_non_unique_mappings) {
## done.
exit(0);
}
## Find best transcript mapping to gene for those that are full-length
#print "Input as pairs: @pairs\n";
my @clusters = &SingleLinkageClusterer::build_clusters(@pairs);
#print "Clusters: @clusters\n";
#die;
my $selected_blat_file = "$blat_output.FL_selected";
open (my $ofh, ">$selected_blat_file") or die $!;
foreach my $cluster (@clusters) {
my @genes = grep { /GENE:/ } @$cluster;
my @asms = grep { /ASM:/ } @$cluster;
foreach my $gene (@genes) {
$gene =~ s/GENE://;
}
foreach my $asm (@asms) {
$asm =~ s/ASM://;
}
## find best matching, resolve overlaps
&resolve_overlaps(\@genes, \@asms, $ofh);
}
exit(0);
}
####
sub resolve_overlaps {
my ($genes_aref, $asms_aref, $ofh) = @_;
my @genes = @$genes_aref;
my @asms = @$asms_aref;
my @mappings;
foreach my $asm (@asms) {
foreach my $gene (@genes) {
my $key = join("$;", $gene, $asm);
if (exists $mapping_info{$key}) {
my $struct = $mapping_info{$key};
push (@mappings, $struct);
}
}
}
@mappings = reverse sort {$a->{score}<=>$b->{score}} @mappings;
my @report;
foreach my $mapping (@mappings) {
# ensure no overlap among assembly and gene coordinates
if (! &overlaps_asm($mapping, \@report)) {
push (@report, $mapping);
}
}
## regroup annotations and assemblies
my %asm_links;
foreach my $entry (@report) {
my ($gene_id, $trans_id) = ($entry->{gene_id}, $entry->{trans_id});
push (@{$asm_links{$trans_id}}, $gene_id);
my $key = join("$;", $gene_id, $trans_id);
my $struct = $mapping_info{$key} or die "Error, cannot find alignment for $key";
print $ofh $struct->{psl}->get_line();
}
foreach my $asm (keys %asm_links) {
my @genes = @{$asm_links{$asm}};
print join(",", @genes) . "\t$asm\n";
}
return;
}
####
sub overlaps_asm {
my ($mapping, $report_aref) = @_;
my ($map_lend, $map_rend) = ($mapping->{trans_lend}, $mapping->{trans_rend});
my $gene_id = $mapping->{gene_id};
my $trans_id = $mapping->{trans_id};
foreach my $entry (@$report_aref) {
my $entry_trans_id = $entry->{trans_id};
my $entry_gene_id = $entry->{gene_id};
if ($gene_id eq $entry_gene_id) {
# already used gene
return(1);
}
elsif ($entry_trans_id eq $trans_id) {
my ($lend, $rend) = ($entry->{trans_lend}, $entry->{trans_rend});
if ($lend < $map_rend && $rend > $map_lend) {
return(1);
}
}
}
return(0); # no meaningful overlap
}
####
sub examine_blat_mappings {
my ($blat_output) = @_;
my %mappings;
my $psl_parser = new PSL_parser($blat_output);
while (my $psl_entry = $psl_parser->get_next()) {
#print $psl_entry->toString();
my $trans_assembly = $psl_entry->get_Q_name();
my $gene_id = $psl_entry->get_T_name();
my $per_id = $psl_entry->get_per_id();
my $num_gap_opens = $psl_entry->get_T_gap_count() + $psl_entry->get_Q_gap_count();
my $num_gap_bases = $psl_entry->get_T_gap_bases() + $psl_entry->get_Q_gap_bases();
my $num_matches = $psl_entry->get_match_count();
my $num_mismatches = $psl_entry->get_mismatch_count();
my ($trans_end5, $trans_end3) = $psl_entry->get_Q_span();
my ($gene_end5, $gene_end3) = $psl_entry->get_T_span();
my $orient = $psl_entry->get_strand();
if ($forward_orient && $orient eq '-') { next; }
if ($per_id < $min_per_id) { next; }
my $gene_seq_len = $psl_entry->get_T_size();
my $percent_gapped = ($num_gap_bases) / ($num_matches + $num_mismatches) * 100;
if ($percent_gapped > $max_per_gap ) {
#print STDERR "\%gapped = $percent_gapped, so skipping.\n";
next; # too gappy
}
my ($gene_lend, $gene_rend) = sort {$a<=>$b} ($gene_end5, $gene_end3);
my ($trans_lend, $trans_rend) = sort {$a<=>$b} ($trans_end5, $trans_end3);
my $delta = ($gene_lend - 1) + ($gene_seq_len - $gene_rend);
my $pct_len = 100 - ($delta/$gene_seq_len * 100);
#print STDERR "PERCENT_LEN: $pct_len (vs. $min_per_length)\n";
if ($pct_len >= $min_per_length) {
#print STDERR "OK!!!\n";
my $pair = join("$;", $gene_id, $trans_assembly);
## score the alignment:
my $score = (5 * $num_matches) - (4 * $num_mismatches) - (20 * $num_gap_opens) - log($num_gap_bases + 1);
my $struct = $mapping_info{$pair};
if ( (! $struct) || $score > $struct->{score}) {
push (@{$mappings{"GENE:$gene_id"}}, "ASM:$trans_assembly");
$mapping_info{$pair} = { "per_id" => $per_id,
gene_id => $gene_id,
trans_id => $trans_assembly,
gene_lend => $gene_lend,
gene_rend => $gene_rend,
trans_lend => $trans_lend,
trans_rend => $trans_rend,
length => $gene_rend - $gene_lend + 1,
per_id => $per_id,
score => $score,
strand => $orient,
psl => $psl_entry,
};
}
}
}
return(%mappings);
}
####
sub parse_sequence_lengths {
my ($seqlen_file) = @_;
my %acc_to_len;
open (my $fh, $seqlen_file) or die "Error, cannot open file $seqlen_file";
while (<$fh>) {
chomp;
my @x = split(/\t/);
my $acc = $x[1];
my $len = $x[0];
$acc_to_len{$acc} = $len;
}
close $fh;
return(%acc_to_len);
}
|