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