| 1 | #!/usr/local/bin/perl | ||
| 2 | |||
| 3 | # documentation at end of file | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use English qw(-no_match_vars); | ||
| 8 | use Getopt::Long qw(:config no_ignore_case bundling); | ||
| 9 | use Pod::Usage; | ||
| 10 | use IO::Prompt::Tiny qw(prompt); | ||
| 11 | use Bio::ToolBox::Data; | ||
| 12 | use Bio::ToolBox::db_helper qw( | ||
| 13 | open_db_connection | ||
| 14 | verify_or_request_feature_types | ||
| 15 | ); | ||
| 16 | use Bio::ToolBox::Parser; | ||
| 17 | use Bio::ToolBox::GeneTools qw(:all); | ||
| 18 | use Bio::ToolBox::utility qw(parse_list format_with_commas); | ||
| 19 | |||
| 20 | our $VERSION = '2.03'; | ||
| 21 | |||
| 22 | print "\n This program will get specific regions from features\n\n"; | ||
| 23 | |||
| 24 | ### Quick help | ||
| 25 | unless (@ARGV) { | ||
| 26 | |||
| 27 | # when no command line options are present | ||
| 28 | # print SYNOPSIS | ||
| 29 | pod2usage( | ||
| 30 | { | ||
| 31 | '-verbose' => 0, | ||
| 32 | '-exitval' => 1, | ||
| 33 | } | ||
| 34 | ); | ||
| 35 | } | ||
| 36 | |||
| 37 | ### Get command line options and initialize values | ||
| 38 | my ( | ||
| 39 | $infile, $outfile, $database, $request, | ||
| 40 | $transcript_type, $tsl, $gencode, $tbiotype, | ||
| 41 | $start_adj, $stop_adj, $unique, $slop, | ||
| 42 | $chromosome_exclude, $bed, $gz, $bed_name, | ||
| 43 | $help, $print_version, | ||
| 44 | ); | ||
| 45 | my @feature_types; | ||
| 46 | |||
| 47 | # Command line options | ||
| 48 | GetOptions( | ||
| 49 | 'i|in=s' => \$infile, # the input data file | ||
| 50 | 'o|out=s' => \$outfile, # name of output file | ||
| 51 | 'd|db=s' => \$database, # source annotation database | ||
| 52 | 'f|feature=s' => \@feature_types, # the features requested from database | ||
| 53 | 'r|region=s' => \$request, # the region requested | ||
| 54 | 't|transcript=s' => \$transcript_type, # which transcripts to take | ||
| 55 | 'tsl=s' => \$tsl, # filter on transcript support level | ||
| 56 | 'gencode!' => \$gencode, # filter on gencode basic tag | ||
| 57 | 'biotype=s' => \$tbiotype, # filter on transcript biotype | ||
| 58 | 'b|begin|start=i' => \$start_adj, # start coordinate adjustment | ||
| 59 | 'e|end|stop=i' => \$stop_adj, # stop coordinate adjustment | ||
| 60 | 'u|unique!' => \$unique, # boolean to ensure uniqueness | ||
| 61 | 'l|slop=i' => \$slop, # slop factor in bp to identify uniqueness | ||
| 62 | 'K|chrskip=s' => \$chromosome_exclude, # skip chromosomes | ||
| 63 | 'bed!' => \$bed, # convert the output to bed format | ||
| 64 | 'bedname=s' => \$bed_name, # what to use for bed name | ||
| 65 | 'z|gz!' => \$gz, # compress output | ||
| 66 | 'h|help' => \$help, # request help | ||
| 67 | 'v|version' => \$print_version, # print the version | ||
| 68 | ) or die " unrecognized option(s)!! please refer to the help documentation\n\n"; | ||
| 69 | |||
| 70 | # Print help | ||
| 71 | if ($help) { | ||
| 72 | |||
| 73 | # print entire POD | ||
| 74 | pod2usage( | ||
| 75 | { | ||
| 76 | '-verbose' => 2, | ||
| 77 | '-exitval' => 1, | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | } | ||
| 81 | |||
| 82 | # Print version | ||
| 83 | if ($print_version) { | ||
| 84 | print " Biotoolbox script get_gene_regions.pl, version $VERSION\n\n"; | ||
| 85 | eval { | ||
| 86 | require Bio::ToolBox; | ||
| 87 | my $v = Bio::ToolBox->VERSION; | ||
| 88 | print " Biotoolbox package version $v\n"; | ||
| 89 | }; | ||
| 90 | exit; | ||
| 91 | } | ||
| 92 | |||
| 93 | ### Check for requirements and set defaults | ||
| 94 | unless ( $infile or $database ) { | ||
| 95 | print STDERR | ||
| 96 | " FATAL: must define a database or input gene table file! use --help for more information\n"; | ||
| 97 | exit 1; | ||
| 98 | } | ||
| 99 | if ( $database | ||
| 100 | and $database =~ | ||
| 101 | /\. (?: gtf | gff3? | txt | refflat | genepred | ucsc ) (?:\.gz)? $/xi ) | ||
| 102 | { | ||
| 103 | |||
| 104 | # looks like a gene table file was specified as the database | ||
| 105 | # intercept and assign to input file name | ||
| 106 | $infile = $database; | ||
| 107 | $database = undef; | ||
| 108 | } | ||
| 109 | |||
| 110 | unless ( defined $outfile ) { | ||
| 111 | print STDERR | ||
| 112 | " FATAL: must define an output file name! use --help for more information\n"; | ||
| 113 | exit 1; | ||
| 114 | } | ||
| 115 | |||
| 116 | unless ( defined $slop ) { | ||
| 117 | $slop = 0; | ||
| 118 | } | ||
| 119 | |||
| 120 | unless ( defined $gz ) { | ||
| 121 | $gz = 0; | ||
| 122 | } | ||
| 123 | |||
| 124 | # one or more feature types may have been provided | ||
| 125 | # check if it is a comma delimited list | ||
| 126 | if ( scalar @feature_types == 1 and $feature_types[0] =~ /,/ ) { | ||
| 127 | @feature_types = split /,/, shift @feature_types; | ||
| 128 | } | ||
| 129 | |||
| 130 | # boolean values for different transcript types to take | ||
| 131 | my ( | ||
| 132 | $do_mrna, $do_mirna, $do_ncrna, $do_snrna, $do_snorna, | ||
| 133 | $do_trna, $do_rrna, $do_miscrna, $do_lincrna, $do_all_rna | ||
| 134 | ); | ||
| 135 | |||
| 136 | ### Determine methods and transcript types | ||
| 137 | # Determine request_method and put into global variable | ||
| 138 | my $method; | ||
| 139 | determine_method(); | ||
| 140 | |||
| 141 | ### Collect feature regions | ||
| 142 | # collect the regions based on provided database or input file | ||
| 143 | # using the method determined above | ||
| 144 | printf " Collecting %s$request regions...\n", $unique ? 'unique ' : q(); | ||
| 145 | my $outdata; | ||
| 146 | if ($database) { | ||
| 147 | $outdata = collect_from_database(); | ||
| 148 | } | ||
| 149 | elsif ($infile) { | ||
| 150 | $outdata = collect_from_file(); | ||
| 151 | } | ||
| 152 | |||
| 153 | printf " Collected %s regions\n", format_with_commas( $outdata->last_row ); | ||
| 154 | print " Sorting...\n"; | ||
| 155 | $outdata->gsort_data; | ||
| 156 | |||
| 157 | ### Write the file | ||
| 158 | my $success; | ||
| 159 | if ($bed) { | ||
| 160 | |||
| 161 | # output stream | ||
| 162 | my $Stream = Bio::ToolBox::Data->new( | ||
| 163 | stream => 1, | ||
| 164 | out => $outfile, | ||
| 165 | bed => 6, | ||
| 166 | ) or die "unable to write output file '$outfile'!"; | ||
| 167 | |||
| 168 | # determine what to use for bed name column | ||
| 169 | my $name_i; | ||
| 170 | if ($bed_name) { | ||
| 171 | if ( $bed_name =~ /geneid/i ) { | ||
| 172 | $name_i = 1; | ||
| 173 | } | ||
| 174 | elsif ( $bed_name =~ /genename/i ) { | ||
| 175 | $name_i = 2; | ||
| 176 | } | ||
| 177 | elsif ( $bed_name =~ /transcriptid/i ) { | ||
| 178 | $name_i = 3; | ||
| 179 | } | ||
| 180 | elsif ( $bed_name =~ /transcriptname/i ) { | ||
| 181 | $name_i = 4; | ||
| 182 | } | ||
| 183 | elsif ( $bed_name =~ /featurename/i ) { | ||
| 184 | $name_i = 5; | ||
| 185 | } | ||
| 186 | else { | ||
| 187 | printf " ERROR: unrecognized bed name '%s'! Using default feature name\n"; | ||
| 188 | $name_i = 5; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | else { | ||
| 192 | $name_i = 5; | ||
| 193 | } | ||
| 194 | |||
| 195 | # generate bed | ||
| 196 | $outdata->iterate( | ||
| 197 | sub { | ||
| 198 | my $row = shift; | ||
| 199 | my $bedstring = | ||
| 200 | $row->bed_string( bed => 6, name => $row->value($name_i) ); | ||
| 201 | $Stream->write_row($bedstring); | ||
| 202 | } | ||
| 203 | ); | ||
| 204 | $success = $Stream->filename; | ||
| 205 | } | ||
| 206 | else { | ||
| 207 | $success = $outdata->write_file( | ||
| 208 | 'filename' => $outfile, | ||
| 209 | 'gz' => $gz, | ||
| 210 | ); | ||
| 211 | } | ||
| 212 | if ($success) { | ||
| 213 | print " wrote file '$success'\n"; | ||
| 214 | } | ||
| 215 | else { | ||
| 216 | # failure! the subroutine will have printed error messages | ||
| 217 | print " unable to write file!\n"; | ||
| 218 | } | ||
| 219 | |||
| 220 | ######################## Subroutines ################################### | ||
| 221 | |||
| 222 | sub determine_method { | ||
| 223 | |||
| 224 | # determine the region request from user if necessary | ||
| 225 | unless ($request) { | ||
| 226 | $request = collect_method_from_user(); | ||
| 227 | } | ||
| 228 | $request = lc $request; | ||
| 229 | |||
| 230 | # determine the method | ||
| 231 | # also change the name of the request from short to long form | ||
| 232 | if ( $request eq 'tss' ) { | ||
| 233 | $request = 'transcription start site'; | ||
| 234 | $method = \&collect_tss; | ||
| 235 | } | ||
| 236 | elsif ( $request eq 'transcription start site' ) { | ||
| 237 | $method = \&collect_tss; | ||
| 238 | } | ||
| 239 | elsif ( $request eq 'tts' ) { | ||
| 240 | $request = 'transcription stop site'; | ||
| 241 | $method = \&collect_tts; | ||
| 242 | } | ||
| 243 | elsif ( $request eq 'transcription stop site' ) { | ||
| 244 | $method = \&collect_tts; | ||
| 245 | } | ||
| 246 | elsif ( $request =~ /^splices?$/x ) { | ||
| 247 | $request = 'splice sites'; | ||
| 248 | $method = \&collect_splice_sites; | ||
| 249 | } | ||
| 250 | elsif ( $request =~ /^exons?$/x ) { | ||
| 251 | $request = 'exon'; | ||
| 252 | $method = \&collect_exons; | ||
| 253 | } | ||
| 254 | elsif ( $request =~ /^collapsed .? exon s?$/x ) { | ||
| 255 | $request = 'collapsed exon'; | ||
| 256 | $method = \&collect_collapsed_exons; | ||
| 257 | } | ||
| 258 | elsif ( $request =~ /^first.?exon$/x ) { | ||
| 259 | $request = 'first exon'; | ||
| 260 | $method = \&collect_first_exon; | ||
| 261 | } | ||
| 262 | elsif ( $request =~ /^last.?exon$/x ) { | ||
| 263 | $request = 'last exon'; | ||
| 264 | $method = \&collect_last_exon; | ||
| 265 | } | ||
| 266 | elsif ( $request =~ /^alt .* exons?$/x ) { | ||
| 267 | $request = 'alternate exon'; | ||
| 268 | $method = \&collect_alt_exons; | ||
| 269 | } | ||
| 270 | elsif ( $request =~ /^common .? exons?$/x ) { | ||
| 271 | $request = 'common exon'; | ||
| 272 | $method = \&collect_common_exons; | ||
| 273 | } | ||
| 274 | elsif ( $request =~ /^uncommon .? exons?$/x ) { | ||
| 275 | $request = 'uncommon exon'; | ||
| 276 | $method = \&collect_uncommon_exons; | ||
| 277 | } | ||
| 278 | elsif ( $request =~ /^introns?$/ ) { | ||
| 279 | $method = \&collect_introns; | ||
| 280 | } | ||
| 281 | elsif ( $request =~ /^collapsed .? introns?$/x ) { | ||
| 282 | $request = 'collapsed intron'; | ||
| 283 | $method = \&collect_collapsed_introns; | ||
| 284 | } | ||
| 285 | elsif ( $request =~ /^first .? intron$/x ) { | ||
| 286 | $request = 'first intron'; | ||
| 287 | $method = \&collect_first_intron; | ||
| 288 | } | ||
| 289 | elsif ( $request =~ /^last .? intron$/x ) { | ||
| 290 | $request = 'last intron'; | ||
| 291 | $method = \&collect_last_intron; | ||
| 292 | } | ||
| 293 | elsif ( $request =~ /^alt .* introns?$/x ) { | ||
| 294 | $request = 'alternate intron'; | ||
| 295 | $method = \&collect_alt_introns; | ||
| 296 | } | ||
| 297 | elsif ( $request =~ /^common .? intron s?$/x ) { | ||
| 298 | $request = 'common intron'; | ||
| 299 | $method = \&collect_common_introns; | ||
| 300 | } | ||
| 301 | elsif ( $request =~ /^uncommon .? intron s?$/x ) { | ||
| 302 | $request = 'uncommon intron'; | ||
| 303 | $method = \&collect_uncommon_introns; | ||
| 304 | } | ||
| 305 | elsif ( $request eq 'utr' ) { | ||
| 306 | $request = 'UTRs'; | ||
| 307 | $method = \&collect_utrs; | ||
| 308 | } | ||
| 309 | elsif ( $request =~ /^cds .? start$/x ) { | ||
| 310 | $request = 'CDS start'; | ||
| 311 | $method = \&collect_cds_start; | ||
| 312 | } | ||
| 313 | elsif ( $request =~ /^cds .? stop$/x ) { | ||
| 314 | $request = 'CDS stop'; | ||
| 315 | $method = \&collect_cds_stop; | ||
| 316 | } | ||
| 317 | else { | ||
| 318 | print STDERR " FATAL: unknown region request!\n"; | ||
| 319 | exit 1; | ||
| 320 | } | ||
| 321 | |||
| 322 | return $method; | ||
| 323 | } | ||
| 324 | |||
| 325 | sub collect_method_from_user { | ||
| 326 | |||
| 327 | my %list = ( | ||
| 328 | 1 => 'transcription start site', | ||
| 329 | 2 => 'transcription stop site', | ||
| 330 | 3 => 'exons', | ||
| 331 | 4 => 'collapsed exons', | ||
| 332 | 5 => 'first exon', | ||
| 333 | 6 => 'last exon', | ||
| 334 | 7 => 'alternate exons', | ||
| 335 | 8 => 'uncommon exons', | ||
| 336 | 9 => 'common exons', | ||
| 337 | 10 => 'introns', | ||
| 338 | 11 => 'collapsed introns', | ||
| 339 | 12 => 'first intron', | ||
| 340 | 13 => 'last intron', | ||
| 341 | 14 => 'alternate introns', | ||
| 342 | 15 => 'uncommon introns', | ||
| 343 | 16 => 'common introns', | ||
| 344 | 17 => 'splice sites', | ||
| 345 | 18 => 'UTRs', | ||
| 346 | 19 => 'CDS start', | ||
| 347 | 20 => 'CDS stop', | ||
| 348 | ); | ||
| 349 | |||
| 350 | # request feature from the user | ||
| 351 | print " These are the available feature types in the database:\n"; | ||
| 352 | foreach my $i ( sort { $a <=> $b } keys %list ) { | ||
| 353 | print " $i\t$list{$i}\n"; | ||
| 354 | } | ||
| 355 | my $p = " Enter the type of region to collect: "; | ||
| 356 | my $answer = prompt($p); | ||
| 357 | |||
| 358 | # verify and return answer | ||
| 359 | if ( exists $list{$answer} ) { | ||
| 360 | return $list{$answer}; | ||
| 361 | } | ||
| 362 | else { | ||
| 363 | print STDERR " unknown request!\n"; | ||
| 364 | exit 1; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | sub determine_transcript_types { | ||
| 369 | |||
| 370 | # if we are collecting from a database, the user may have already selected | ||
| 371 | # an RNA feature type, which makes this selection redundant. | ||
| 372 | my @features = @_; | ||
| 373 | |||
| 374 | # collect all the transcript types requested | ||
| 375 | my @types; | ||
| 376 | if ($transcript_type) { | ||
| 377 | |||
| 378 | # provided by the user from the command line | ||
| 379 | if ( $transcript_type =~ /,/ ) { | ||
| 380 | @types = split /,/, $transcript_type; | ||
| 381 | } | ||
| 382 | else { | ||
| 383 | push @types, $transcript_type; | ||
| 384 | } | ||
| 385 | } | ||
| 386 | elsif (@features) { | ||
| 387 | |||
| 388 | # user selected types from a database | ||
| 389 | foreach (@features) { | ||
| 390 | my ( $p, $s ) = split /:/; # take only the primary tag if both present | ||
| 391 | push @types, $p if $p =~ /rna | transcript/xi; | ||
| 392 | } | ||
| 393 | } | ||
| 394 | |||
| 395 | # request from the user if not otherwise provided | ||
| 396 | unless (@types) { | ||
| 397 | |||
| 398 | print " Genes may generate different types of RNA transcripts.\n"; | ||
| 399 | my $i = 1; | ||
| 400 | my %i2tag; | ||
| 401 | foreach (qw(all mRNA ncRNA snRNA snoRNA tRNA rRNA miRNA lincRNA misc_RNA)) { | ||
| 402 | print " $i\t$_\n"; | ||
| 403 | $i2tag{$i} = $_; | ||
| 404 | $i++; | ||
| 405 | } | ||
| 406 | my $p = ' Select one or more RNA types to include: '; | ||
| 407 | my $response = prompt($p); | ||
| 408 | @types = map { $i2tag{$_} || undef } parse_list($response); | ||
| 409 | } | ||
| 410 | |||
| 411 | # string for visual output | ||
| 412 | my $string = " Collecting transcript types:"; | ||
| 413 | |||
| 414 | foreach (@types) { | ||
| 415 | if (m/^all$/i) { | ||
| 416 | $do_all_rna = 1; | ||
| 417 | $string .= ' all RNAs'; | ||
| 418 | last; | ||
| 419 | } | ||
| 420 | if (m/^mRNA$/i) { | ||
| 421 | $do_mrna = 1; | ||
| 422 | $string .= ' mRNA'; | ||
| 423 | } | ||
| 424 | if (m/^miRNA$/i) { | ||
| 425 | $do_mirna = 1; | ||
| 426 | $string .= ' miRNA'; | ||
| 427 | } | ||
| 428 | if (m/^ncRNA$/i) { | ||
| 429 | $do_ncrna = 1; | ||
| 430 | $string .= ' ncRNA'; | ||
| 431 | } | ||
| 432 | if (m/^snRNA$/i) { | ||
| 433 | $do_snrna = 1; | ||
| 434 | $string .= ' snRNA'; | ||
| 435 | } | ||
| 436 | if (m/^snoRNA$/i) { | ||
| 437 | $do_snorna = 1; | ||
| 438 | $string .= ' snoRNA'; | ||
| 439 | } | ||
| 440 | if (m/^tRNA$/i) { | ||
| 441 | $do_trna = 1; | ||
| 442 | $string .= ' tRNA'; | ||
| 443 | } | ||
| 444 | if (m/^rRNA$/i) { | ||
| 445 | $do_rrna = 1; | ||
| 446 | $string .= ' rRNA'; | ||
| 447 | } | ||
| 448 | if (m/^misc_RNA$/i) { | ||
| 449 | $do_miscrna = 1; | ||
| 450 | $string .= ' misc_RNA'; | ||
| 451 | } | ||
| 452 | if (m/^lincRNA$/i) { | ||
| 453 | $do_lincrna = 1; | ||
| 454 | $string .= ' lincRNA'; | ||
| 455 | } | ||
| 456 | } | ||
| 457 | print "$string\n"; | ||
| 458 | } | ||
| 459 | |||
| 460 | sub collect_from_database { | ||
| 461 | |||
| 462 | # open database connection | ||
| 463 | my $db = open_db_connection($database) | ||
| 464 | or die " unable to open database connection!\n"; | ||
| 465 | |||
| 466 | # get feature type if necessary | ||
| 467 | my $prompt = <<PROMPT; | ||
| 468 | Select one or more database features (typically genes) from which to collect regions. | ||
| 469 | PROMPT | ||
| 470 | my @features = verify_or_request_feature_types( | ||
| 471 | 'db' => $db, | ||
| 472 | 'feature' => \@feature_types, | ||
| 473 | 'prompt' => $prompt, | ||
| 474 | 'single' => 0, | ||
| 475 | 'limit' => 'gene|rna', | ||
| 476 | ) or die "No valid gene feature type was provided! see help\n"; | ||
| 477 | |||
| 478 | # get transcript_type | ||
| 479 | determine_transcript_types(@features); | ||
| 480 | |||
| 481 | # generate output data | ||
| 482 | my $Data = generate_output_structure(); | ||
| 483 | $Data->database($database); | ||
| 484 | |||
| 485 | # generate a seqfeature stream | ||
| 486 | my $iterator = $db->features( | ||
| 487 | -type => \@features, | ||
| 488 | -iterator => 1, | ||
| 489 | ); | ||
| 490 | |||
| 491 | # process the features | ||
| 492 | while ( my $seqfeat = $iterator->next_seq ) { | ||
| 493 | |||
| 494 | # skip unwanted chromosomes | ||
| 495 | if ( $chromosome_exclude and $seqfeat->seq_id =~ $chromosome_exclude ) { | ||
| 496 | next; | ||
| 497 | } | ||
| 498 | |||
| 499 | # collect the regions based on the primary tag and the method re | ||
| 500 | if ( $seqfeat->primary_tag eq 'gene' ) { | ||
| 501 | |||
| 502 | # gene | ||
| 503 | my @regions = process_gene($seqfeat); | ||
| 504 | foreach (@regions) { | ||
| 505 | |||
| 506 | # each element is an anon array of found feature info | ||
| 507 | $Data->add_row($_); | ||
| 508 | } | ||
| 509 | } | ||
| 510 | elsif ( $seqfeat->primary_tag =~ /rna | transcript/xi ) { | ||
| 511 | |||
| 512 | # transcript | ||
| 513 | my @regions = process_transcript($seqfeat); | ||
| 514 | |||
| 515 | # remove duplicates if requested | ||
| 516 | if ($unique) { | ||
| 517 | remove_duplicates( \@regions ); | ||
| 518 | } | ||
| 519 | |||
| 520 | # add regions to collection | ||
| 521 | foreach (@regions) { | ||
| 522 | $Data->add_row($_); | ||
| 523 | } | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | # finished | ||
| 528 | return $Data; | ||
| 529 | } | ||
| 530 | |||
| 531 | sub collect_from_file { | ||
| 532 | |||
| 533 | # get transcript_type | ||
| 534 | unless ( defined $transcript_type ) { | ||
| 535 | |||
| 536 | # user providing a file, so we'll just take everything in here | ||
| 537 | $transcript_type = 'all'; | ||
| 538 | } | ||
| 539 | determine_transcript_types(); | ||
| 540 | |||
| 541 | # Collect the top features for each sequence group. | ||
| 542 | # Rather than going feature by feature through the gff, | ||
| 543 | # we'll load the top features, which will collect all the features | ||
| 544 | # and assemble appropriate feature -> subfeatures according to the | ||
| 545 | # parent - child attributes. | ||
| 546 | |||
| 547 | # generate output data | ||
| 548 | my $Data = generate_output_structure(); | ||
| 549 | $Data->add_comment("Source data file $infile"); | ||
| 550 | |||
| 551 | # open appropriate parser object | ||
| 552 | my $Parser = Bio::ToolBox::Parser->new( file => $infile, ); | ||
| 553 | unless ($Parser) { | ||
| 554 | die " unable to open and parse '$infile'!\n"; | ||
| 555 | } | ||
| 556 | |||
| 557 | # set parser attributes - most of these are probably redundant or not necessary | ||
| 558 | my $type_string = $Parser->typelist; | ||
| 559 | $Parser->do_gene(1); # assume we always want genes? | ||
| 560 | $Parser->do_exon(1); | ||
| 561 | $Parser->do_cds(1); | ||
| 562 | if ( $request =~ /utr/i ) { | ||
| 563 | if ( $type_string =~ /utr/i ) { | ||
| 564 | $Parser->do_utr(1); | ||
| 565 | } | ||
| 566 | } | ||
| 567 | elsif ( $request =~ /cds st/i ) { | ||
| 568 | if ( $type_string =~ /codon/ ) { | ||
| 569 | $Parser->do_codon(1); | ||
| 570 | } | ||
| 571 | } | ||
| 572 | if ( $tsl or $type_string !~ /rna/i ) { | ||
| 573 | |||
| 574 | # we need the extra attributes for transcript_support_level and biotype | ||
| 575 | $Parser->simplify(0); | ||
| 576 | } | ||
| 577 | else { | ||
| 578 | $Parser->simplify(1); | ||
| 579 | } | ||
| 580 | |||
| 581 | # process the features | ||
| 582 | my @bad_features; | ||
| 583 | while ( my $seqfeat = $Parser->next_top_feature ) { | ||
| 584 | |||
| 585 | # skip unwanted chromosomes | ||
| 586 | if ( $chromosome_exclude and $seqfeat->seq_id =~ $chromosome_exclude ) { | ||
| 587 | next; | ||
| 588 | } | ||
| 589 | |||
| 590 | # collect the regions based on the primary tag | ||
| 591 | my $type = $seqfeat->primary_tag; | ||
| 592 | if ( $type =~ /gene$/i ) { | ||
| 593 | |||
| 594 | # gene, including things like gene, miRNA_gene, pseudogene, etc | ||
| 595 | my @regions = process_gene($seqfeat); | ||
| 596 | foreach (@regions) { | ||
| 597 | $Data->add_row($_); | ||
| 598 | } | ||
| 599 | } | ||
| 600 | elsif ( $type =~ /rna | transcript/xi ) { | ||
| 601 | |||
| 602 | # any sort of RNA transcript | ||
| 603 | my @regions = process_transcript($seqfeat); | ||
| 604 | |||
| 605 | # remove duplicates if requested | ||
| 606 | if ($unique) { | ||
| 607 | remove_duplicates( \@regions ); | ||
| 608 | } | ||
| 609 | foreach (@regions) { | ||
| 610 | $Data->add_row($_); | ||
| 611 | } | ||
| 612 | } | ||
| 613 | elsif ( $type =~ /chromosome | contig | scaffold | sequence/xi ) { | ||
| 614 | next; # safely ignore these | ||
| 615 | } | ||
| 616 | else { | ||
| 617 | push @bad_features, $seqfeat; | ||
| 618 | } | ||
| 619 | } | ||
| 620 | |||
| 621 | # finished | ||
| 622 | if (@bad_features) { | ||
| 623 | my %bad_types; | ||
| 624 | foreach (@bad_features) { | ||
| 625 | $bad_types{ $_->primary_tag } += 1; | ||
| 626 | } | ||
| 627 | printf " skipped %d unrecognized top feature types:\n%s\n", | ||
| 628 | scalar(@bad_features), | ||
| 629 | join( "\n", map {" $bad_types{$_} $_"} sort { $a cmp $b } keys %bad_types ); | ||
| 630 | } | ||
| 631 | return $Data; | ||
| 632 | } | ||
| 633 | |||
| 634 | sub generate_output_structure { | ||
| 635 | my $Data = Bio::ToolBox::Data->new( | ||
| 636 | feature => "region", | ||
| 637 | columns => [ | ||
| 638 | qw(GeneID GeneName TranscriptID TranscriptName Name Chromosome Start | ||
| 639 | Stop Strand) | ||
| 640 | ], | ||
| 641 | ); | ||
| 642 | $Data->program("$PROGRAM_NAME, v $VERSION"); | ||
| 643 | my $r = $request; | ||
| 644 | $r =~ s/\s/_/g; # remove spaces | ||
| 645 | $Data->metadata( 3, 'type', $transcript_type ); | ||
| 646 | $Data->metadata( 5, 'type', $r ); | ||
| 647 | if ($start_adj) { | ||
| 648 | $Data->metadata( 7, 'start_adjusted', $start_adj ); | ||
| 649 | } | ||
| 650 | if ($stop_adj) { | ||
| 651 | $Data->metadata( 8, 'stop_adjusted', $stop_adj ); | ||
| 652 | } | ||
| 653 | if ($unique) { | ||
| 654 | $Data->metadata( 5, 'unique', 1 ); | ||
| 655 | $Data->metadata( 5, 'slop', $slop ); | ||
| 656 | } | ||
| 657 | |||
| 658 | return $Data; | ||
| 659 | } | ||
| 660 | |||
| 661 | sub process_gene { | ||
| 662 | |||
| 663 | # passed gene object | ||
| 664 | my $gene = shift; | ||
| 665 | my @regions; | ||
| 666 | |||
| 667 | # need to pull out the appropriate transcript types from the gene | ||
| 668 | my @transcripts; | ||
| 669 | foreach my $t ( get_transcripts($gene) ) { | ||
| 670 | next unless acceptable_transcript($t); | ||
| 671 | push @transcripts, $t; | ||
| 672 | } | ||
| 673 | return unless @transcripts; | ||
| 674 | |||
| 675 | # filter for gencode transcripts if requested | ||
| 676 | if ($gencode) { | ||
| 677 | my $new_transcripts = filter_transcript_gencode_basic( \@transcripts ); | ||
| 678 | return unless scalar @{$new_transcripts}; | ||
| 679 | @transcripts = @{$new_transcripts}; | ||
| 680 | } | ||
| 681 | |||
| 682 | # filter for transcript support level if requested | ||
| 683 | if ($tsl) { | ||
| 684 | my $new_transcripts = filter_transcript_support_level( \@transcripts, $tsl ); | ||
| 685 | return unless scalar @{$new_transcripts}; | ||
| 686 | @transcripts = @{$new_transcripts}; | ||
| 687 | } | ||
| 688 | |||
| 689 | # filter for biotype if requested | ||
| 690 | if ($tbiotype) { | ||
| 691 | my $new_transcripts = filter_transcript_biotype( \@transcripts, $tbiotype ); | ||
| 692 | return unless scalar @{$new_transcripts}; | ||
| 693 | @transcripts = @{$new_transcripts}; | ||
| 694 | } | ||
| 695 | |||
| 696 | # alternate or common exons require working with multiple transcripts | ||
| 697 | if ( $request =~ /alternate | common | collapsed/xi ) { | ||
| 698 | |||
| 699 | # pass all the transcripts together | ||
| 700 | @regions = &{$method}(@transcripts); | ||
| 701 | } | ||
| 702 | else { | ||
| 703 | # do each transcript one at a time | ||
| 704 | foreach my $t (@transcripts) { | ||
| 705 | push @regions, &{$method}($t); | ||
| 706 | } | ||
| 707 | } | ||
| 708 | return unless @regions; | ||
| 709 | |||
| 710 | # finally add gene name | ||
| 711 | foreach my $region (@regions) { | ||
| 712 | unshift @{$region}, $gene->primary_id, $gene->display_name; | ||
| 713 | } | ||
| 714 | |||
| 715 | # remove duplicates if requested | ||
| 716 | if ($unique) { | ||
| 717 | remove_duplicates( \@regions ); | ||
| 718 | } | ||
| 719 | |||
| 720 | # return the regions | ||
| 721 | return @regions; | ||
| 722 | } | ||
| 723 | |||
| 724 | sub process_transcript { | ||
| 725 | |||
| 726 | # passed objects | ||
| 727 | my $transcript = shift; | ||
| 728 | |||
| 729 | # filter transcripts | ||
| 730 | return unless acceptable_transcript($transcript); | ||
| 731 | |||
| 732 | # filter for gencode transcripts if requested | ||
| 733 | if ($gencode) { | ||
| 734 | $transcript = filter_transcript_gencode_basic($transcript); | ||
| 735 | return unless scalar @{$transcript}; | ||
| 736 | } | ||
| 737 | |||
| 738 | # filter for transcript support level if requested | ||
| 739 | if ($tsl) { | ||
| 740 | $transcript = filter_transcript_support_level( $transcript, $tsl ); | ||
| 741 | return unless scalar @{$transcript}; | ||
| 742 | } | ||
| 743 | |||
| 744 | # filter for biotype if requested | ||
| 745 | if ($tbiotype) { | ||
| 746 | $transcript = filter_transcript_biotype( $transcript, $tbiotype ); | ||
| 747 | return unless scalar @{$transcript}; | ||
| 748 | } | ||
| 749 | |||
| 750 | # call appropriate method | ||
| 751 | my @regions = &{$method}($transcript); | ||
| 752 | |||
| 753 | # add non-existent gene id and name | ||
| 754 | foreach my $region (@regions) { | ||
| 755 | unshift @{$region}, '.', '.'; | ||
| 756 | } | ||
| 757 | return @regions; | ||
| 758 | } | ||
| 759 | |||
| 760 | sub collect_tss { | ||
| 761 | |||
| 762 | # get seqfeature objects | ||
| 763 | my $transcript = shift; | ||
| 764 | |||
| 765 | # get coordinates | ||
| 766 | my $chromo = $transcript->seq_id; | ||
| 767 | my ( $start, $stop, $strand ); | ||
| 768 | if ( $transcript->strand == 1 ) { | ||
| 769 | |||
| 770 | # forward strand | ||
| 771 | |||
| 772 | $strand = 1; | ||
| 773 | $start = $transcript->start; | ||
| 774 | $stop = $transcript->start; | ||
| 775 | } | ||
| 776 | elsif ( $transcript->strand == -1 ) { | ||
| 777 | |||
| 778 | # reverse strand | ||
| 779 | |||
| 780 | $strand = -1; | ||
| 781 | $start = $transcript->end; | ||
| 782 | $stop = $transcript->end; | ||
| 783 | } | ||
| 784 | else { | ||
| 785 | die " poorly formatted transcript seqfeature object with strand 0!\n"; | ||
| 786 | } | ||
| 787 | |||
| 788 | # get name | ||
| 789 | my $name = $transcript->display_name . '_TSS'; | ||
| 790 | |||
| 791 | return _adjust_positions( | ||
| 792 | [ | ||
| 793 | $transcript->primary_id, $transcript->display_name, $name, $chromo, | ||
| 794 | $start, $stop, $strand | ||
| 795 | ] | ||
| 796 | ); | ||
| 797 | } | ||
| 798 | |||
| 799 | sub collect_tts { | ||
| 800 | |||
| 801 | # get seqfeature objects | ||
| 802 | my $transcript = shift; | ||
| 803 | |||
| 804 | # get coordinates | ||
| 805 | my $chromo = $transcript->seq_id; | ||
| 806 | my ( $start, $stop, $strand ); | ||
| 807 | if ( $transcript->strand == 1 ) { | ||
| 808 | |||
| 809 | # forward strand | ||
| 810 | |||
| 811 | $strand = 1; | ||
| 812 | $start = $transcript->end; | ||
| 813 | $stop = $transcript->end; | ||
| 814 | } | ||
| 815 | elsif ( $transcript->strand == -1 ) { | ||
| 816 | |||
| 817 | # reverse strand | ||
| 818 | |||
| 819 | $strand = -1; | ||
| 820 | $start = $transcript->start; | ||
| 821 | $stop = $transcript->start; | ||
| 822 | } | ||
| 823 | else { | ||
| 824 | die " poorly formatted transcript seqfeature object with strand 0!\n"; | ||
| 825 | } | ||
| 826 | |||
| 827 | # get name | ||
| 828 | my $name = $transcript->display_name . '_TTS'; | ||
| 829 | |||
| 830 | return _adjust_positions( | ||
| 831 | [ | ||
| 832 | $transcript->primary_id, $transcript->display_name, $name, $chromo, | ||
| 833 | $start, $stop, $strand | ||
| 834 | ] | ||
| 835 | ); | ||
| 836 | } | ||
| 837 | |||
| 838 | sub collect_exons { | ||
| 839 | my $transcript = shift; | ||
| 840 | |||
| 841 | # process and adjust the exons | ||
| 842 | my @exons; | ||
| 843 | foreach my $e ( get_exons($transcript) ) { | ||
| 844 | push @exons, | ||
| 845 | _adjust_positions( | ||
| 846 | [ | ||
| 847 | $transcript->primary_id, $transcript->display_name, | ||
| 848 | $e->display_name, $e->seq_id, | ||
| 849 | $e->start, $e->end, | ||
| 850 | $e->strand | ||
| 851 | ] | ||
| 852 | ); | ||
| 853 | } | ||
| 854 | |||
| 855 | return @exons; | ||
| 856 | } | ||
| 857 | |||
| 858 | sub collect_collapsed_exons { | ||
| 859 | my $transcript = collapse_transcripts(@_); | ||
| 860 | return collect_exons($transcript); | ||
| 861 | } | ||
| 862 | |||
| 863 | sub collect_first_exon { | ||
| 864 | my $transcript = shift; | ||
| 865 | |||
| 866 | # find the exons and/or CDSs | ||
| 867 | my @list = get_exons($transcript); | ||
| 868 | return unless @list; | ||
| 869 | |||
| 870 | # the first exon | ||
| 871 | my $first = $transcript->strand >= 0 ? shift @list : pop @list; | ||
| 872 | |||
| 873 | # identify the exon name if it has one | ||
| 874 | my $name = $first->display_name | ||
| 875 | || $transcript->display_name . "_firstExon"; | ||
| 876 | |||
| 877 | # finished | ||
| 878 | return _adjust_positions( | ||
| 879 | [ | ||
| 880 | $transcript->primary_id, $transcript->display_name, | ||
| 881 | $name, $first->seq_id, | ||
| 882 | $first->start, $first->end, | ||
| 883 | $first->strand | ||
| 884 | ] | ||
| 885 | ); | ||
| 886 | } | ||
| 887 | |||
| 888 | sub collect_last_exon { | ||
| 889 | my $transcript = shift; | ||
| 890 | |||
| 891 | # find the exons and/or CDSs | ||
| 892 | my @list = get_exons($transcript); | ||
| 893 | return unless @list; | ||
| 894 | |||
| 895 | # the last exon | ||
| 896 | my $last = $transcript->strand >= 0 ? pop @list : shift @list; | ||
| 897 | |||
| 898 | # identify the exon name if it has one | ||
| 899 | my $name = $last->display_name | ||
| 900 | || $transcript->display_name . "_lastExon"; | ||
| 901 | |||
| 902 | # finished | ||
| 903 | return _adjust_positions( | ||
| 904 | [ | ||
| 905 | $transcript->primary_id, $transcript->display_name, | ||
| 906 | $name, $last->seq_id, | ||
| 907 | $last->start, $last->end, | ||
| 908 | $last->strand | ||
| 909 | ] | ||
| 910 | ); | ||
| 911 | } | ||
| 912 | |||
| 913 | sub collect_alt_exons { | ||
| 914 | my $ac_exons = get_alt_common_exons(@_); | ||
| 915 | |||
| 916 | # we need the transcript name, so can't use the simpler get_alt_exons() | ||
| 917 | my @exons; | ||
| 918 | foreach my $transcript ( keys %{$ac_exons} ) { | ||
| 919 | next if $transcript eq 'common'; | ||
| 920 | next if $transcript eq 'uncommon'; | ||
| 921 | foreach my $e ( @{ $ac_exons->{$transcript} } ) { | ||
| 922 | push @exons, | ||
| 923 | _adjust_positions( | ||
| 924 | [ | ||
| 925 | $transcript->primary_id, $transcript, $e->display_name, | ||
| 926 | $e->seq_id, $e->start, $e->end, | ||
| 927 | $e->strand | ||
| 928 | ] | ||
| 929 | ); | ||
| 930 | } | ||
| 931 | } | ||
| 932 | return @exons; | ||
| 933 | } | ||
| 934 | |||
| 935 | sub collect_uncommon_exons { | ||
| 936 | my @exons; | ||
| 937 | foreach my $e ( get_uncommon_exons(@_) ) { | ||
| 938 | push @exons, _adjust_positions( | ||
| 939 | [ | ||
| 940 | 'uncommon', # more than one transcript, so put generic identifier | ||
| 941 | 'uncommon', | ||
| 942 | $e->display_name, | ||
| 943 | $e->seq_id, | ||
| 944 | $e->start, | ||
| 945 | $e->end, | ||
| 946 | $e->strand, | ||
| 947 | ] | ||
| 948 | ); | ||
| 949 | } | ||
| 950 | return @exons; | ||
| 951 | } | ||
| 952 | |||
| 953 | sub collect_common_exons { | ||
| 954 | my @exons; | ||
| 955 | foreach my $e ( get_common_exons(@_) ) { | ||
| 956 | push @exons, _adjust_positions( | ||
| 957 | [ | ||
| 958 | 'common', # more than one transcript, so put generic identifier | ||
| 959 | 'common', | ||
| 960 | $e->display_name, | ||
| 961 | $e->seq_id, | ||
| 962 | $e->start, | ||
| 963 | $e->end, | ||
| 964 | $e->strand, | ||
| 965 | ] | ||
| 966 | ); | ||
| 967 | } | ||
| 968 | return @exons; | ||
| 969 | } | ||
| 970 | |||
| 971 | sub collect_splice_sites { | ||
| 972 | |||
| 973 | # seqfeature object | ||
| 974 | my $transcript = shift; | ||
| 975 | |||
| 976 | # find the exons and/or CDSs | ||
| 977 | my $list = get_exons($transcript); | ||
| 978 | return unless $list; | ||
| 979 | return if ( scalar @{$list} == 1 ); | ||
| 980 | |||
| 981 | # identify the last exon index position | ||
| 982 | my $last = scalar @{$list} - 1; | ||
| 983 | |||
| 984 | # collect the splice sites | ||
| 985 | my @splices; | ||
| 986 | |||
| 987 | # forward strand | ||
| 988 | if ( $transcript->strand == 1 ) { | ||
| 989 | |||
| 990 | # walk through each exon | ||
| 991 | for ( my $i = 0; $i <= $last; $i++ ) { | ||
| 992 | |||
| 993 | # get the exon name | ||
| 994 | my $exon = $list->[$i]; | ||
| 995 | my $name = $exon->display_name | ||
| 996 | || $transcript->display_name . ".exon$i"; | ||
| 997 | |||
| 998 | # first exon | ||
| 999 | if ( $i == 0 ) { | ||
| 1000 | push @splices, | ||
| 1001 | _adjust_positions( | ||
| 1002 | [ | ||
| 1003 | $transcript->primary_id, $transcript->display_name, | ||
| 1004 | $name . '_3p', $exon->seq_id, | ||
| 1005 | $exon->end + 1, $exon->end + 1 | ||
| 1006 | ] | ||
| 1007 | ); | ||
| 1008 | } | ||
| 1009 | |||
| 1010 | # last exon | ||
| 1011 | elsif ( $i == $last ) { | ||
| 1012 | push @splices, | ||
| 1013 | _adjust_positions( | ||
| 1014 | [ | ||
| 1015 | $transcript->primary_id, $transcript->display_name, | ||
| 1016 | $name . '_5p', $exon->seq_id, | ||
| 1017 | $exon->start - 1, $exon->start - 1, | ||
| 1018 | $exon->strand | ||
| 1019 | ] | ||
| 1020 | ); | ||
| 1021 | |||
| 1022 | } | ||
| 1023 | |||
| 1024 | # middle exons | ||
| 1025 | else { | ||
| 1026 | |||
| 1027 | # 5' splice | ||
| 1028 | push @splices, | ||
| 1029 | _adjust_positions( | ||
| 1030 | [ | ||
| 1031 | $transcript->primary_id, $transcript->display_name, | ||
| 1032 | $name . '_5p', $exon->seq_id, | ||
| 1033 | $exon->start - 1, $exon->start - 1, | ||
| 1034 | $exon->strand | ||
| 1035 | ] | ||
| 1036 | ); | ||
| 1037 | |||
| 1038 | # 3' splice | ||
| 1039 | push @splices, | ||
| 1040 | _adjust_positions( | ||
| 1041 | [ | ||
| 1042 | $transcript->primary_id, $transcript->display_name, | ||
| 1043 | $name . '_3p', $exon->seq_id, | ||
| 1044 | $exon->end + 1, $exon->end + 1, | ||
| 1045 | $exon->strand | ||
| 1046 | ] | ||
| 1047 | ); | ||
| 1048 | } | ||
| 1049 | } | ||
| 1050 | } | ||
| 1051 | |||
| 1052 | # reverse strand | ||
| 1053 | else { | ||
| 1054 | |||
| 1055 | # walk through each exon | ||
| 1056 | for ( my $i = 0; $i <= $last; $i++ ) { | ||
| 1057 | |||
| 1058 | # get the exon name | ||
| 1059 | my $exon = $list->[$i]; | ||
| 1060 | my $name = $exon->display_name | ||
| 1061 | || $transcript->display_name . ".exon$i"; | ||
| 1062 | |||
| 1063 | # first exon | ||
| 1064 | if ( $i == 0 ) { | ||
| 1065 | push @splices, | ||
| 1066 | _adjust_positions( | ||
| 1067 | [ | ||
| 1068 | $transcript->primary_id, $transcript->display_name, | ||
| 1069 | $name . '_3p', $exon->seq_id, | ||
| 1070 | $exon->start - 1, $exon->start - 1, | ||
| 1071 | $exon->strand | ||
| 1072 | ] | ||
| 1073 | ); | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | # last exon | ||
| 1077 | elsif ( $i == $last ) { | ||
| 1078 | push @splices, | ||
| 1079 | _adjust_positions( | ||
| 1080 | [ | ||
| 1081 | $transcript->primary_id, $transcript->display_name, | ||
| 1082 | $name . '_5p', $exon->seq_id, | ||
| 1083 | $exon->end + 1, $exon->end + 1, | ||
| 1084 | $exon->strand | ||
| 1085 | ] | ||
| 1086 | ); | ||
| 1087 | |||
| 1088 | } | ||
| 1089 | |||
| 1090 | # middle exons | ||
| 1091 | else { | ||
| 1092 | |||
| 1093 | # 5' splice | ||
| 1094 | push @splices, | ||
| 1095 | _adjust_positions( | ||
| 1096 | [ | ||
| 1097 | $transcript->primary_id, $transcript->display_name, | ||
| 1098 | $name . '_5p', $exon->seq_id, | ||
| 1099 | $exon->end + 1, $exon->end + 1, | ||
| 1100 | $exon->strand | ||
| 1101 | ] | ||
| 1102 | ); | ||
| 1103 | |||
| 1104 | # 3' splice | ||
| 1105 | push @splices, | ||
| 1106 | _adjust_positions( | ||
| 1107 | [ | ||
| 1108 | $transcript->primary_id, $transcript->display_name, | ||
| 1109 | $name . '_3p', $exon->seq_id, | ||
| 1110 | $exon->start - 1, $exon->start - 1, | ||
| 1111 | $exon->strand | ||
| 1112 | ] | ||
| 1113 | ); | ||
| 1114 | } | ||
| 1115 | } | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | # finished | ||
| 1119 | return @splices; | ||
| 1120 | } | ||
| 1121 | |||
| 1122 | sub collect_introns { | ||
| 1123 | my $transcript = shift; | ||
| 1124 | |||
| 1125 | # collect the introns | ||
| 1126 | my @introns; | ||
| 1127 | foreach my $int ( get_introns($transcript) ) { | ||
| 1128 | push @introns, | ||
| 1129 | _adjust_positions( | ||
| 1130 | [ | ||
| 1131 | $transcript->primary_id, $transcript->display_name, | ||
| 1132 | $int->display_name, $int->seq_id, | ||
| 1133 | $int->start, $int->end, | ||
| 1134 | $int->strand | ||
| 1135 | ] | ||
| 1136 | ); | ||
| 1137 | } | ||
| 1138 | |||
| 1139 | # finished | ||
| 1140 | return @introns; | ||
| 1141 | } | ||
| 1142 | |||
| 1143 | sub collect_collapsed_introns { | ||
| 1144 | my $transcript = collapse_transcripts(@_); | ||
| 1145 | return collect_introns($transcript); | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | sub collect_first_intron { | ||
| 1149 | my $transcript = shift; | ||
| 1150 | |||
| 1151 | # find the introns | ||
| 1152 | my @list = get_introns($transcript); | ||
| 1153 | return unless @list; | ||
| 1154 | |||
| 1155 | # the first intron | ||
| 1156 | my $first = $transcript->strand >= 0 ? shift @list : pop @list; | ||
| 1157 | return _adjust_positions( | ||
| 1158 | [ | ||
| 1159 | $transcript->primary_id, $transcript->display_name, | ||
| 1160 | $first->display_name, $first->seq_id, | ||
| 1161 | $first->start, $first->end, | ||
| 1162 | $first->strand | ||
| 1163 | ] | ||
| 1164 | ); | ||
| 1165 | } | ||
| 1166 | |||
| 1167 | sub collect_last_intron { | ||
| 1168 | my $transcript = shift; | ||
| 1169 | |||
| 1170 | # get the introns | ||
| 1171 | my @list = get_introns($transcript); | ||
| 1172 | return unless @list; | ||
| 1173 | |||
| 1174 | # the last intron | ||
| 1175 | my $last = $transcript->strand >= 0 ? pop @list : shift @list; | ||
| 1176 | return _adjust_positions( | ||
| 1177 | [ | ||
| 1178 | $transcript->primary_id, $transcript->display_name, | ||
| 1179 | $last->display_name, $last->seq_id, | ||
| 1180 | $last->start, $last->end, | ||
| 1181 | $last->strand | ||
| 1182 | ] | ||
| 1183 | ); | ||
| 1184 | } | ||
| 1185 | |||
| 1186 | sub collect_alt_introns { | ||
| 1187 | my $ac_introns = get_alt_common_introns(@_); | ||
| 1188 | my @introns; | ||
| 1189 | foreach my $transcript ( keys %{$ac_introns} ) { | ||
| 1190 | next if $transcript eq 'common'; | ||
| 1191 | next if $transcript eq 'uncommon'; | ||
| 1192 | foreach my $i ( @{ $ac_introns->{$transcript} } ) { | ||
| 1193 | push @introns, | ||
| 1194 | _adjust_positions( | ||
| 1195 | [ | ||
| 1196 | $transcript, $transcript, $i->display_name, $i->seq_id, | ||
| 1197 | $i->start, $i->end, $i->strand | ||
| 1198 | ] | ||
| 1199 | ); | ||
| 1200 | } | ||
| 1201 | } | ||
| 1202 | return @introns; | ||
| 1203 | } | ||
| 1204 | |||
| 1205 | sub collect_uncommon_introns { | ||
| 1206 | my @introns; | ||
| 1207 | foreach my $i ( get_uncommon_introns(@_) ) { | ||
| 1208 | push @introns, _adjust_positions( | ||
| 1209 | [ | ||
| 1210 | 'uncommon', # more than one transcript, so put generic identifier | ||
| 1211 | 'uncommon', | ||
| 1212 | $i->display_name, | ||
| 1213 | $i->seq_id, | ||
| 1214 | $i->start, | ||
| 1215 | $i->end, | ||
| 1216 | $i->strand, | ||
| 1217 | ] | ||
| 1218 | ); | ||
| 1219 | } | ||
| 1220 | return @introns; | ||
| 1221 | } | ||
| 1222 | |||
| 1223 | sub collect_common_introns { | ||
| 1224 | my @introns; | ||
| 1225 | foreach my $i ( get_common_introns(@_) ) { | ||
| 1226 | push @introns, _adjust_positions( | ||
| 1227 | [ | ||
| 1228 | 'common', # more than one transcript, so put generic identifier | ||
| 1229 | 'common', | ||
| 1230 | $i->display_name, | ||
| 1231 | $i->seq_id, | ||
| 1232 | $i->start, | ||
| 1233 | $i->end, | ||
| 1234 | $i->strand, | ||
| 1235 | ] | ||
| 1236 | ); | ||
| 1237 | } | ||
| 1238 | return @introns; | ||
| 1239 | } | ||
| 1240 | |||
| 1241 | sub collect_utrs { | ||
| 1242 | my $transcript = shift; | ||
| 1243 | |||
| 1244 | # process and adjust the UTRs | ||
| 1245 | my @utrs; | ||
| 1246 | foreach my $u ( get_utrs($transcript) ) { | ||
| 1247 | push @utrs, | ||
| 1248 | _adjust_positions( | ||
| 1249 | [ | ||
| 1250 | $transcript->primary_id, $transcript->display_name, | ||
| 1251 | $u->display_name, $u->seq_id, | ||
| 1252 | $u->start, $u->end, | ||
| 1253 | $u->strand | ||
| 1254 | ] | ||
| 1255 | ); | ||
| 1256 | } | ||
| 1257 | |||
| 1258 | return @utrs; | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | sub collect_cds_start { | ||
| 1262 | |||
| 1263 | # get seqfeature objects | ||
| 1264 | my $transcript = shift; | ||
| 1265 | |||
| 1266 | # get the cds start | ||
| 1267 | my $pos = get_cdsStart($transcript); | ||
| 1268 | return unless $pos; | ||
| 1269 | |||
| 1270 | # get other things | ||
| 1271 | my $chromo = $transcript->seq_id; | ||
| 1272 | my $strand = $transcript->strand; | ||
| 1273 | my $name = $transcript->display_name . '_cdsStart'; | ||
| 1274 | |||
| 1275 | return _adjust_positions( | ||
| 1276 | [ | ||
| 1277 | $transcript->primary_id, $transcript->display_name, $name, $chromo, $pos, | ||
| 1278 | $pos, $strand | ||
| 1279 | ] | ||
| 1280 | ); | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | sub collect_cds_stop { | ||
| 1284 | |||
| 1285 | # get seqfeature objects | ||
| 1286 | my $transcript = shift; | ||
| 1287 | |||
| 1288 | # get the cds start | ||
| 1289 | my $pos = get_cdsEnd($transcript); | ||
| 1290 | return unless $pos; | ||
| 1291 | |||
| 1292 | # get other things | ||
| 1293 | my $chromo = $transcript->seq_id; | ||
| 1294 | my $strand = $transcript->strand; | ||
| 1295 | my $name = $transcript->display_name . '_cdsStart'; | ||
| 1296 | |||
| 1297 | return _adjust_positions( | ||
| 1298 | [ | ||
| 1299 | $transcript->primary_id, $transcript->display_name, $name, $chromo, $pos, | ||
| 1300 | $pos, $strand | ||
| 1301 | ] | ||
| 1302 | ); | ||
| 1303 | } | ||
| 1304 | |||
| 1305 | sub acceptable_transcript { | ||
| 1306 | my $transcript = shift; | ||
| 1307 | my $t = $transcript->primary_tag; | ||
| 1308 | return 1 | ||
| 1309 | if ( $t =~ m/rna | transcript | retained_intron | antisense | nonsense/xi | ||
| 1310 | and $do_all_rna ); | ||
| 1311 | return 1 if ( is_coding($transcript) and $do_mrna ); | ||
| 1312 | return 1 if ( $t =~ /mirna/i and $do_mirna ); | ||
| 1313 | return 1 if ( $t =~ /ncrna/i and $do_ncrna ); | ||
| 1314 | return 1 if ( $t =~ /snrna/i and $do_snrna ); | ||
| 1315 | return 1 if ( $t =~ /snorna/i and $do_snorna ); | ||
| 1316 | return 1 if ( $t =~ /trna/i and $do_rrna ); | ||
| 1317 | return 1 if ( $t =~ /rrna/i and $do_rrna ); | ||
| 1318 | return 1 | ||
| 1319 | if ( $t =~ m/misc_rna | transcript | retained_intron | antisense | nonsense/xi | ||
| 1320 | and $do_miscrna ); | ||
| 1321 | return 1 if ( $t =~ /lincrna/i and $do_lincrna ); | ||
| 1322 | return 0; | ||
| 1323 | } | ||
| 1324 | |||
| 1325 | sub _adjust_positions { | ||
| 1326 | |||
| 1327 | my $region = shift; | ||
| 1328 | |||
| 1329 | # region is an anonymous array of 7 elements | ||
| 1330 | # [$transcript_id, $transcript_name, $name, $chromo, $start, $stop, $strand] | ||
| 1331 | |||
| 1332 | # adjust the start and end positions according to strand | ||
| 1333 | if ( $region->[6] == 1 ) { | ||
| 1334 | |||
| 1335 | # forward strand | ||
| 1336 | |||
| 1337 | if ($start_adj) { | ||
| 1338 | $region->[4] += $start_adj; | ||
| 1339 | } | ||
| 1340 | if ($stop_adj) { | ||
| 1341 | $region->[5] += $stop_adj; | ||
| 1342 | } | ||
| 1343 | } | ||
| 1344 | elsif ( $region->[6] == -1 ) { | ||
| 1345 | |||
| 1346 | # reverse strand | ||
| 1347 | |||
| 1348 | if ($start_adj) { | ||
| 1349 | $region->[5] -= $start_adj; | ||
| 1350 | } | ||
| 1351 | |||
| 1352 | # stop | ||
| 1353 | if ($stop_adj) { | ||
| 1354 | $region->[4] -= $stop_adj; | ||
| 1355 | } | ||
| 1356 | } | ||
| 1357 | |||
| 1358 | # return adjusted region coordinates | ||
| 1359 | return $region; | ||
| 1360 | } | ||
| 1361 | |||
| 1362 | sub remove_duplicates { | ||
| 1363 | |||
| 1364 | my $regions = shift; | ||
| 1365 | |||
| 1366 | # regions are an anonymous arrays of 9 elements | ||
| 1367 | # start/stop are indexes 6,7 | ||
| 1368 | |||
| 1369 | # look for duplicates using a quick hash of seen positions | ||
| 1370 | my %seenit; | ||
| 1371 | my @to_remove; | ||
| 1372 | |||
| 1373 | if ($slop) { | ||
| 1374 | |||
| 1375 | # to simplify, we only use the start position when using slop, since | ||
| 1376 | # the end position is going to be too variable and flapping in the | ||
| 1377 | # breeze, so to speak | ||
| 1378 | # not entirely accurate, but that's slop for you | ||
| 1379 | for my $i ( 0 .. $#{$regions} ) { | ||
| 1380 | |||
| 1381 | # we will be using the start position as the unique identifier | ||
| 1382 | # to account for the slop factor, we'll be adding/subtracting the | ||
| 1383 | # slop value to/from the start position | ||
| 1384 | # if this position matches anything else, we'll assume it's a duplicate | ||
| 1385 | |||
| 1386 | foreach my $pos ( | ||
| 1387 | |||
| 1388 | # generate an array of possible start positions | ||
| 1389 | # with a default slop of 0, this will only be 1 position | ||
| 1390 | ( $regions->[$i]->[6] - $slop ) .. ( $regions->[$i]->[6] + $slop ) | ||
| 1391 | ) | ||
| 1392 | { | ||
| 1393 | if ( exists $seenit{$pos} ) { | ||
| 1394 | push @to_remove, $i; | ||
| 1395 | } | ||
| 1396 | else { | ||
| 1397 | $seenit{$pos} = 1; | ||
| 1398 | } | ||
| 1399 | } | ||
| 1400 | } | ||
| 1401 | } | ||
| 1402 | else { | ||
| 1403 | # without slop, we look for precise matches based on both start and end | ||
| 1404 | for my $i ( 0 .. $#{$regions} ) { | ||
| 1405 | my ( $s, $e ) = ( $regions->[$i]->[6], $regions->[$i]->[7] ); | ||
| 1406 | if ( exists $seenit{$s}{$e} ) { | ||
| 1407 | push @to_remove, $i; | ||
| 1408 | } | ||
| 1409 | else { | ||
| 1410 | $seenit{$s}{$e} = 1; | ||
| 1411 | } | ||
| 1412 | } | ||
| 1413 | } | ||
| 1414 | |||
| 1415 | # remove the duplicates | ||
| 1416 | while (@to_remove) { | ||
| 1417 | my $i = pop @to_remove; | ||
| 1418 | |||
| 1419 | # take from end to avoid shifting regions array | ||
| 1420 | splice( @{$regions}, $i, 1 ); | ||
| 1421 | } | ||
| 1422 | } | ||
| 1423 | |||
| 1424 | __END__ |