« CLIPSeqTools/PreprocessApp/annotate_with_file.pm
↑ Summary
CLIPSeqTools/App/genomic_distribution.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PreprocessApp::annotate_with_genic_elements - Annotate alignments in a database table with genic information. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools-preprocess annotate_with_genic_elements [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Annotate alignments in a database table with genic information | ||
| 12 | Adds columns named "transcript", "exon", "coding_transcript", "utr5", "cds", "utr3". | ||
| 13 | Column values will be NOT NULL if an alignment is contained in the corresponding region and NULL otherwise. | ||
| 14 | |||
| 15 | =head1 OPTIONS | ||
| 16 | |||
| 17 | Input options for library. | ||
| 18 | --driver <Str> driver for database connection (eg. mysql, | ||
| 19 | SQLite). | ||
| 20 | --database <Str> database name or path to database file for file | ||
| 21 | based databases (eg. SQLite). | ||
| 22 | --table <Str> database table. | ||
| 23 | --host <Str> hostname for database connection. | ||
| 24 | --user <Str> username for database connection. | ||
| 25 | --password <Str> password for database connection. | ||
| 26 | --records_class <Str> type of records stored in database. | ||
| 27 | --filter <Filter> filter library. May be used multiple times. | ||
| 28 | Syntax: column_name="pattern" | ||
| 29 | e.g. keep reads with deletions AND not repeat | ||
| 30 | masked AND longer than 31 | ||
| 31 | --filter deletion="def" | ||
| 32 | --filter rmsk="undef" . | ||
| 33 | --filter query_length=">31". | ||
| 34 | Operators: >, >=, <, <=, =, !=, def, undef | ||
| 35 | |||
| 36 | Other input | ||
| 37 | --gtf <Str> GTF file with genes/transcripts. | ||
| 38 | |||
| 39 | Database options. | ||
| 40 | --drop drop columns if they already exist (not | ||
| 41 | supported in SQlite). | ||
| 42 | |||
| 43 | Other options. | ||
| 44 | -v --verbose print progress lines and extra information. | ||
| 45 | -h -? --usage --help print help message | ||
| 46 | |||
| 47 | =cut | ||
| 48 | |||
| 49 | package CLIPSeqTools::PreprocessApp::annotate_with_genic_elements; | ||
| 50 | $CLIPSeqTools::PreprocessApp::annotate_with_genic_elements::VERSION = '1.0.0'; | ||
| 51 | |||
| 52 | # Make it an app command | ||
| 53 | use MooseX::App::Command; | ||
| 54 | extends 'CLIPSeqTools::PreprocessApp'; | ||
| 55 | |||
| 56 | |||
| 57 | ####################################################################### | ||
| 58 | ####################### Load External modules ##################### | ||
| 59 | ####################################################################### | ||
| 60 | use Modern::Perl; | ||
| 61 | use autodie; | ||
| 62 | use namespace::autoclean; | ||
| 63 | use Try::Tiny; | ||
| 64 | |||
| 65 | |||
| 66 | ####################################################################### | ||
| 67 | ####################### Command line options ###################### | ||
| 68 | ####################################################################### | ||
| 69 | option 'drop' => ( | ||
| 70 | is => 'rw', | ||
| 71 | isa => 'Bool', | ||
| 72 | documentation => 'drop columns if they already exist (not supported in SQlite).', | ||
| 73 | ); | ||
| 74 | |||
| 75 | |||
| 76 | ####################################################################### | ||
| 77 | ########################## Consume Roles ########################## | ||
| 78 | ####################################################################### | ||
| 79 | with | ||
| 80 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 81 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 82 | -excludes => 'validate_args', | ||
| 83 | }, | ||
| 84 | "CLIPSeqTools::Role::Option::Transcripts" => { | ||
| 85 | -alias => { validate_args => '_validate_args_for_transcripts' }, | ||
| 86 | -excludes => 'validate_args', | ||
| 87 | }; | ||
| 88 | |||
| 89 | |||
| 90 | ####################################################################### | ||
| 91 | ######################## Interface Methods ######################## | ||
| 92 | ####################################################################### | ||
| 93 | sub validate_args { | ||
| 94 | my ($self) = @_; | ||
| 95 | |||
| 96 | $self->_validate_args_for_library; | ||
| 97 | $self->_validate_args_for_transcripts; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub run { | ||
| 101 | my ($self) = @_; | ||
| 102 | |||
| 103 | warn "Starting: annotate_with_genic_elements\n"; | ||
| 104 | |||
| 105 | warn "Validating arguments\n" if $self->verbose; | ||
| 106 | $self->validate_args(); | ||
| 107 | |||
| 108 | warn "Creating transcript collection\n" if $self->verbose; | ||
| 109 | my $transcript_collection = $self->transcript_collection; | ||
| 110 | |||
| 111 | warn "Creating reads collection\n" if $self->verbose; | ||
| 112 | my $reads_collection = $self->reads_collection; | ||
| 113 | my $reads_rs = $reads_collection->resultset; | ||
| 114 | |||
| 115 | |||
| 116 | my $table = $self->table; | ||
| 117 | if ($self->drop) { | ||
| 118 | warn "Droping columns transcript, exon, coding_transcript, utr5, cds, utr3\n" if $self->verbose; | ||
| 119 | try { | ||
| 120 | $reads_collection->schema->storage->dbh_do( sub { | ||
| 121 | my ($storage, $dbh, @cols) = @_; | ||
| 122 | $dbh->do( "ALTER TABLE $table DROP COLUMN transcript" ); | ||
| 123 | $dbh->do( "ALTER TABLE $table DROP COLUMN coding_transcript" ); | ||
| 124 | $dbh->do( "ALTER TABLE $table DROP COLUMN noncoding_transcript" ); | ||
| 125 | $dbh->do( "ALTER TABLE $table DROP COLUMN exon" ); | ||
| 126 | $dbh->do( "ALTER TABLE $table DROP COLUMN utr5" ); | ||
| 127 | $dbh->do( "ALTER TABLE $table DROP COLUMN cds" ); | ||
| 128 | $dbh->do( "ALTER TABLE $table DROP COLUMN utr3" ); | ||
| 129 | }); | ||
| 130 | }; | ||
| 131 | } | ||
| 132 | |||
| 133 | try { | ||
| 134 | warn "Creating columns transcript, exon, coding_transcript, utr5, cds, utr3\n" if $self->verbose; | ||
| 135 | $reads_collection->schema->storage->dbh_do( sub { | ||
| 136 | my ($storage, $dbh, @cols) = @_; | ||
| 137 | $dbh->do( "ALTER TABLE $table ADD COLUMN transcript INT(1)" ); | ||
| 138 | $dbh->do( "ALTER TABLE $table ADD COLUMN coding_transcript INT(1)" ); | ||
| 139 | $dbh->do( "ALTER TABLE $table ADD COLUMN noncoding_transcript INT(1)" ); | ||
| 140 | $dbh->do( "ALTER TABLE $table ADD COLUMN exon INT(1)" ); | ||
| 141 | $dbh->do( "ALTER TABLE $table ADD COLUMN utr5 INT(1)" ); | ||
| 142 | $dbh->do( "ALTER TABLE $table ADD COLUMN cds INT(1)" ); | ||
| 143 | $dbh->do( "ALTER TABLE $table ADD COLUMN utr3 INT(1)" ); | ||
| 144 | }); | ||
| 145 | } catch { | ||
| 146 | warn "Warning: Column creation failed. Maybe some columns already exist.\n" if $self->verbose; | ||
| 147 | warn "Caught error: $_\n" if $self->verbose > 1; | ||
| 148 | }; | ||
| 149 | |||
| 150 | warn "Looping on transcripts to annotate records.\nThis might take a long time. Relax...\n" if $self->verbose; | ||
| 151 | $reads_collection->schema->txn_do(sub { | ||
| 152 | $transcript_collection->foreach_record_do( sub { | ||
| 153 | my ($transcript) = @_; | ||
| 154 | |||
| 155 | my $transcript_reads_rs = $reads_rs->search({ | ||
| 156 | strand => $transcript->strand, | ||
| 157 | rname => $transcript->rname, | ||
| 158 | start => { '-between' => [$transcript->start, $transcript->stop] }, | ||
| 159 | stop => { '-between' => [$transcript->start, $transcript->stop] }, | ||
| 160 | }); | ||
| 161 | |||
| 162 | $transcript_reads_rs->update({transcript => 1}); | ||
| 163 | |||
| 164 | foreach my $exon (@{$transcript->exons}) { | ||
| 165 | my $exon_reads_rs = $transcript_reads_rs->search([ | ||
| 166 | start => { '-between' => [$exon->start, $exon->stop] }, | ||
| 167 | stop => { '-between' => [$exon->start, $exon->stop] }, | ||
| 168 | ]); | ||
| 169 | |||
| 170 | $exon_reads_rs->update({exon => 1}); | ||
| 171 | } | ||
| 172 | |||
| 173 | if ($transcript->is_coding) { | ||
| 174 | foreach my $part_type ('utr5', 'cds', 'utr3') { | ||
| 175 | my $part = $transcript->$part_type() or next; | ||
| 176 | my $part_reads_rs = $transcript_reads_rs->search([ | ||
| 177 | start => { '-between' => [$part->start, $part->stop] }, | ||
| 178 | stop => { '-between' => [$part->start, $part->stop] }, | ||
| 179 | ]); | ||
| 180 | |||
| 181 | $part_reads_rs->update({$part_type => 1}); | ||
| 182 | } | ||
| 183 | |||
| 184 | $transcript_reads_rs->update({coding_transcript => 1}); | ||
| 185 | } else { | ||
| 186 | $transcript_reads_rs->update({noncoding_transcript => 1}); | ||
| 187 | } | ||
| 188 | }); | ||
| 189 | }); | ||
| 190 | } | ||
| 191 | |||
| 192 | |||
| 193 | 1; |