« CLIPSeqTools/PreprocessApp/annotate_with_conservation.pm
↑ Summary
CLIPSeqTools/App/conservation_distribution.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::PreprocessApp::annotate_with_deletions - Annotate alignments in a database table with deletions. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools-preprocess annotate_with_deletions [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Annotate alignments in a database table with deletions. | ||
| 12 | This may by particularly useful for HITS-CLIP analysis. | ||
| 13 | Add a column named deletion that will be NOT NULL if the alignment has a deletion 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 | Database options. | ||
| 37 | --drop drop column if it already exists (not | ||
| 38 | supported in SQlite). | ||
| 39 | |||
| 40 | Other options. | ||
| 41 | -v --verbose print progress lines and extra information. | ||
| 42 | -h -? --usage --help print help message | ||
| 43 | |||
| 44 | =cut | ||
| 45 | |||
| 46 | package CLIPSeqTools::PreprocessApp::annotate_with_deletions; | ||
| 47 | $CLIPSeqTools::PreprocessApp::annotate_with_deletions::VERSION = '1.0.0'; | ||
| 48 | |||
| 49 | # Make it an app command | ||
| 50 | use MooseX::App::Command; | ||
| 51 | extends 'CLIPSeqTools::PreprocessApp'; | ||
| 52 | |||
| 53 | |||
| 54 | ####################################################################### | ||
| 55 | ####################### Load External modules ##################### | ||
| 56 | ####################################################################### | ||
| 57 | use Modern::Perl; | ||
| 58 | use autodie; | ||
| 59 | use namespace::autoclean; | ||
| 60 | use Try::Tiny; | ||
| 61 | |||
| 62 | |||
| 63 | ####################################################################### | ||
| 64 | ############################ Attributes ########################### | ||
| 65 | ####################################################################### | ||
| 66 | has 'column' => ( | ||
| 67 | is => 'rw', | ||
| 68 | isa => 'Str', | ||
| 69 | default => 'deletion', | ||
| 70 | documentation => 'name for the new annotation column.', | ||
| 71 | ); | ||
| 72 | |||
| 73 | |||
| 74 | ####################################################################### | ||
| 75 | ####################### Command line options ###################### | ||
| 76 | ####################################################################### | ||
| 77 | option 'drop' => ( | ||
| 78 | is => 'rw', | ||
| 79 | isa => 'Bool', | ||
| 80 | documentation => 'drop columns if they already exist (not supported in SQlite).', | ||
| 81 | ); | ||
| 82 | |||
| 83 | |||
| 84 | ####################################################################### | ||
| 85 | ########################## Consume Roles ########################## | ||
| 86 | ####################################################################### | ||
| 87 | with | ||
| 88 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 89 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 90 | -excludes => 'validate_args', | ||
| 91 | }; | ||
| 92 | |||
| 93 | |||
| 94 | ####################################################################### | ||
| 95 | ######################## Interface Methods ######################## | ||
| 96 | ####################################################################### | ||
| 97 | sub validate_args { | ||
| 98 | my ($self) = @_; | ||
| 99 | |||
| 100 | $self->_validate_args_for_library; | ||
| 101 | } | ||
| 102 | |||
| 103 | sub run { | ||
| 104 | my ($self) = @_; | ||
| 105 | |||
| 106 | warn "Starting: annotate_with_deletions\n"; | ||
| 107 | |||
| 108 | warn "Validating arguments\n" if $self->verbose; | ||
| 109 | $self->validate_args(); | ||
| 110 | |||
| 111 | warn "Opening reads collection\n" if $self->verbose; | ||
| 112 | my $reads_collection = $self->reads_collection; | ||
| 113 | my $reads_rs = $reads_collection->resultset; | ||
| 114 | |||
| 115 | if ($self->drop) { | ||
| 116 | warn "Droping column ".$self->column."\n" if $self->verbose; | ||
| 117 | try { | ||
| 118 | $reads_collection->schema->storage->dbh_do( sub { | ||
| 119 | my ($storage, $dbh, @cols) = @_; | ||
| 120 | $dbh->do('ALTER TABLE '.$self->table.' DROP COLUMN '.$self->column); | ||
| 121 | }); | ||
| 122 | }; | ||
| 123 | } | ||
| 124 | |||
| 125 | try { | ||
| 126 | warn "Creating column ".$self->column."\n" if $self->verbose; | ||
| 127 | $reads_collection->schema->storage->dbh_do( sub { | ||
| 128 | my ($storage, $dbh, @cols) = @_; | ||
| 129 | $dbh->do('ALTER TABLE '.$self->table.' ADD COLUMN '.$self->column.' INT(1)'); | ||
| 130 | }); | ||
| 131 | } catch { | ||
| 132 | warn "Warning: Column creation failed. Maybe column already exist.\n" if $self->verbose; | ||
| 133 | warn "$_\n" if $self->verbose > 1; | ||
| 134 | }; | ||
| 135 | |||
| 136 | warn "Looping on annotation file to annotate records.\nThis might take a long time. Relax...\n" if $self->verbose; | ||
| 137 | $reads_collection->schema->txn_do( sub { | ||
| 138 | my $rs = $reads_collection->resultset->search({ | ||
| 139 | cigar => { like => '%D%' }, | ||
| 140 | }); | ||
| 141 | |||
| 142 | $rs->update({deletion => 1}); | ||
| 143 | |||
| 144 | return 0; | ||
| 145 | }); | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; |