« CLIPSeqTools/App/size_distribution.pm
↑ Summary
CLIPSeqTools/PreprocessApp/annotate_with_conservation.pm »
| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::App::nucleotide_composition - Measure nucleotide composition along reads. | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | clipseqtools nucleotide_composition [options/parameters] | ||
| 8 | |||
| 9 | =head1 DESCRIPTION | ||
| 10 | |||
| 11 | Measure nucleotide composition along reads. | ||
| 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::nucleotide_composition; | ||
| 46 | $CLIPSeqTools::App::nucleotide_composition::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 List::Util qw(sum); | ||
| 60 | |||
| 61 | |||
| 62 | ####################################################################### | ||
| 63 | ########################## Consume Roles ########################## | ||
| 64 | ####################################################################### | ||
| 65 | with | ||
| 66 | "CLIPSeqTools::Role::Option::Library" => { | ||
| 67 | -alias => { validate_args => '_validate_args_for_library' }, | ||
| 68 | -excludes => 'validate_args', | ||
| 69 | }, | ||
| 70 | "CLIPSeqTools::Role::Option::Plot" => { | ||
| 71 | -alias => { validate_args => '_validate_args_for_plot' }, | ||
| 72 | -excludes => 'validate_args', | ||
| 73 | }, | ||
| 74 | "CLIPSeqTools::Role::Option::OutputPrefix" => { | ||
| 75 | -alias => { validate_args => '_validate_args_for_output_prefix' }, | ||
| 76 | -excludes => 'validate_args', | ||
| 77 | }; | ||
| 78 | |||
| 79 | |||
| 80 | ####################################################################### | ||
| 81 | ######################## Interface Methods ######################## | ||
| 82 | ####################################################################### | ||
| 83 | sub validate_args { | ||
| 84 | my ($self) = @_; | ||
| 85 | |||
| 86 | $self->_validate_args_for_library; | ||
| 87 | $self->_validate_args_for_plot; | ||
| 88 | $self->_validate_args_for_output_prefix; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub run { | ||
| 92 | my ($self) = @_; | ||
| 93 | |||
| 94 | warn "Starting analysis: nucleotide_composition\n"; | ||
| 95 | |||
| 96 | warn "Validating arguments\n" if $self->verbose; | ||
| 97 | $self->validate_args(); | ||
| 98 | |||
| 99 | my @nts = ('A', 'C', 'G', 'T', 'N'); | ||
| 100 | |||
| 101 | warn "Creating reads collection\n" if $self->verbose; | ||
| 102 | my $reads_collection = $self->reads_collection; | ||
| 103 | $reads_collection->schema->storage->debug(1) if $self->verbose > 1; | ||
| 104 | |||
| 105 | warn "Measuring nucleotide composition\n" if $self->verbose; | ||
| 106 | my @nt_count; | ||
| 107 | $reads_collection->foreach_record_do( sub { | ||
| 108 | my ($rec) = @_; | ||
| 109 | |||
| 110 | my @nts = split(//, uc($rec->sequence)); | ||
| 111 | for my $i (0..$#nts) { | ||
| 112 | $nt_count[$i]{$nts[$i]} += $rec->copy_number; | ||
| 113 | } | ||
| 114 | |||
| 115 | return 0; | ||
| 116 | }); | ||
| 117 | |||
| 118 | warn "Creating output path\n" if $self->verbose; | ||
| 119 | $self->make_path_for_output_prefix(); | ||
| 120 | |||
| 121 | warn "Printing results\n" if $self->verbose; | ||
| 122 | open (my $OUT1, '>', $self->o_prefix.'nucleotide_composition.tab'); | ||
| 123 | say $OUT1 join("\t", 'position', map {$_.'_count'} (@nts, 'total')); | ||
| 124 | for (my $i=0; $i<@nt_count; $i++) { | ||
| 125 | my @counts = map {$nt_count[$i]{$_} || 0} @nts; | ||
| 126 | say $OUT1 join("\t", $i, @counts, sum(@counts)); | ||
| 127 | } | ||
| 128 | close $OUT1; | ||
| 129 | |||
| 130 | if ($self->plot) { | ||
| 131 | warn "Creating plot\n" if $self->verbose; | ||
| 132 | CLIPSeqTools::PlotApp->initialize_command_class( | ||
| 133 | 'CLIPSeqTools::PlotApp::nucleotide_composition', | ||
| 134 | file => $self->o_prefix.'nucleotide_composition.tab', | ||
| 135 | o_prefix => $self->o_prefix | ||
| 136 | )->run(); | ||
| 137 | } | ||
| 138 | } | ||
| 139 | |||
| 140 | 1; |