« CLIPSeqTools/PreprocessApp/annotate_with_deletions.pm
↑ Summary
CLIPSeqTools/CompareApp/compare_counts.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::conservation_distribution - Measure reads at each conservation level | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools conservation_distribution [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Measure reads at each conservation level | ||
| 12 | |||
| 13 | =head1 OPTIONS | ||
| 14 | |||
| 15 | Input options for library. | ||
| 16 | --driver <Str> driver for database connection (eg. mysql, | ||
| 17 | SQLite). | ||
| 18 | --database <Str> database name or path to database file for file | ||
| 19 | based databases (eg. SQLite). | ||
| 20 | --table <Str> database table. | ||
| 21 | --host <Str> hostname for database connection. | ||
| 22 | --user <Str> username for database connection. | ||
| 23 | --password <Str> password for database connection. | ||
| 24 | --records_class <Str> type of records stored in database. | ||
| 25 | --filter <Filter> filter library. May be used multiple times. | ||
| 26 | Syntax: column_name="pattern" | ||
| 27 | e.g. keep reads with deletions AND not repeat | ||
| 28 | masked AND longer than 31 | ||
| 29 | --filter deletion="def" | ||
| 30 | --filter rmsk="undef" . | ||
| 31 | --filter query_length=">31". | ||
| 32 | Operators: >, >=, <, <=, =, !=, def, undef | ||
| 33 | |||
| 34 | Output | ||
| 35 | --o_prefix <Str> output path prefix. Script will create and add | ||
| 36 | extension to path. [Default: ./] | ||
| 37 | |||
| 38 | Other options. | ||
| 39 | --plot call plotting script to create plots. | ||
| 40 | -v --verbose print progress lines and extra information. | ||
| 41 | -h -? --usage --help print help message | ||
| 42 | |||
| 43 | =cut | ||
| 44 | |||
| 45 | package CLIPSeqTools::App::conservation_distribution; | ||
| 46 | $CLIPSeqTools::App::conservation_distribution::VERSION = '1.0.0'; | ||
| 47 | |||
| 48 | # Make it an app command | ||
| 49 | use MooseX::App::Command; | ||
| 50 | extends 'CLIPSeqTools::App'; | ||
| 51 | |||
| 52 | |||
| 53 | ####################################################################### | ||
| 54 | ####################### Load External modules ##################### | ||
| 55 | ####################################################################### | ||
| 56 | use Modern::Perl; | ||
| 57 | use autodie; | ||
| 58 | use namespace::autoclean; | ||
| 59 | use File::Spec; | ||
| 60 | use Data::Dumper; | ||
| 61 | |||
| 62 | |||
| 63 | ####################################################################### | ||
| 64 | ######################## Load GenOO modules ####################### | ||
| 65 | ####################################################################### | ||
| 66 | use GenOO::GenomicRegion; | ||
| 67 | |||
| 68 | |||
| 69 | ####################################################################### | ||
| 70 | ########################## Consume Roles ########################## | ||
| 71 | ####################################################################### | ||
| 72 | with | ||
| 73 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 74 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 75 | -excludes => 'validate_args', | ||
| 76 | }, | ||
| 77 | "CLIPSeqTools::Role::Option::Plot" => { | ||
| 78 | -alias => { validate_args => '_validate_args_for_plot' }, | ||
| 79 | -excludes => 'validate_args', | ||
| 80 | }, | ||
| 81 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 82 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 83 | -excludes => 'validate_args', | ||
| 84 | }; | ||
| 85 | |||
| 86 | |||
| 87 | ####################################################################### | ||
| 88 | ######################## Interface Methods ######################## | ||
| 89 | ####################################################################### | ||
| 90 | sub validate_args { | ||
| 91 | my ($self) = @_; | ||
| 92 | |||
| 93 | $self->_validate_args_for_library; | ||
| 94 | $self->_validate_args_for_plot; | ||
| 95 | $self->_validate_args_for_output_prefix; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub run { | ||
| 99 | my ($self) = @_; | ||
| 100 | |||
| 101 | warn "Starting analysis: conservation_distribution\n"; | ||
| 102 | |||
| 103 | warn "Validating arguments\n" if $self->verbose; | ||
| 104 | $self->validate_args(); | ||
| 105 | |||
| 106 | warn "Creating reads collection\n" if $self->verbose; | ||
| 107 | my $reads_collection = $self->reads_collection; | ||
| 108 | $reads_collection->schema->storage->debug(1) if $self->verbose > 1; | ||
| 109 | # Make GenOO aware of conservation column | ||
| 110 | my $conservation_params = { | ||
| 111 | data_type => 'integer', | ||
| 112 | is_numeric => 1 | ||
| 113 | }; | ||
| 114 | $reads_collection->resultset->result_source->add_columns('conservation' => $conservation_params); | ||
| 115 | $reads_collection->resultset->result_class->add_columns('conservation' => $conservation_params); | ||
| 116 | $reads_collection->resultset->result_class->register_column('conservation'); | ||
| 117 | |||
| 118 | warn "Measuring reads at each conservation level\n" if $self->verbose; | ||
| 119 | my %conservation_count; | ||
| 120 | my %conservation_count_nocopynumber; | ||
| 121 | $reads_collection->foreach_record_do( sub { | ||
| 122 | my ($rec) = @_; | ||
| 123 | |||
| 124 | $conservation_count{$rec->conservation} += $rec->copy_number; | ||
| 125 | $conservation_count_nocopynumber{$rec->conservation} += 1; | ||
| 126 | |||
| 127 | return 0; | ||
| 128 | }); | ||
| 129 | |||
| 130 | warn "Creating output path\n" if $self->verbose; | ||
| 131 | $self->make_path_for_output_prefix(); | ||
| 132 | |||
| 133 | warn "Printing results\n" if $self->verbose; | ||
| 134 | open (my $OUT1, '>', $self->o_prefix.'conservation_distribution.tab'); | ||
| 135 | say $OUT1 join("\t", 'conservation_score', 'count', 'count_no_copy_number'); | ||
| 136 | say $OUT1 join("\t", $_, $conservation_count{$_}, $conservation_count_nocopynumber{$_}) for sort {$a <=> $b} keys %conservation_count; | ||
| 137 | close $OUT1; | ||
| 138 | |||
| 139 | if ($self->plot) { | ||
| 140 | warn "Creating plot\n" if $self->verbose; | ||
| 141 | CLIPSeqTools::PlotApp->initialize_command_class('CLIPSeqTools::PlotApp::conservation_distribution', | ||
| 142 | file => $self->o_prefix.'conservation_distribution.tab', | ||
| 143 | o_prefix => $self->o_prefix | ||
| 144 | )->run(); | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | 1; |