| 1 | =head1 NAME | ||
| 2 | |||
| 3 | CLIPSeqTools::Role::Option::Library - Role to enable reading a library with | ||
| 4 | reads from the command line | ||
| 5 | |||
| 6 | =head1 SYNOPSIS | ||
| 7 | |||
| 8 | Role to enable reading a library with reads from the command line | ||
| 9 | |||
| 10 | Defines options. | ||
| 11 | -driver <Str> driver for database connection (eg. mysql, SQLite). | ||
| 12 | -database <Str> database name or path to database file for file | ||
| 13 | based databases (eg. SQLite). | ||
| 14 | -table <Str> database table. | ||
| 15 | -host <Str> hostname for database connection. | ||
| 16 | -user <Str> username for database connection. | ||
| 17 | -password <Str> password for database connection. | ||
| 18 | -records_class <Str> type of records stored in database (Default: | ||
| 19 | GenOO::Data::DB::DBIC::Species::Schema:: | ||
| 20 | SampleResultBase::v3). | ||
| 21 | -filter <Filter> filter library. Option can be given multiple times. | ||
| 22 | Syntax: column_name="pattern" | ||
| 23 | e.g. -filter deletion="def" -filter rmsk="undef" | ||
| 24 | to keep reads with deletions and not repeat | ||
| 25 | masked. e.g. -filter query_length=">31" -filter | ||
| 26 | query_length="<=50" to keep reads longer than 31 | ||
| 27 | and shorter or equal to 50. Supported | ||
| 28 | operators: >, >=, <, <=, =, !=, def, undef. | ||
| 29 | |||
| 30 | Provides attributes. | ||
| 31 | reads_collection reads collection that is read from the specified | ||
| 32 | source. | ||
| 33 | |||
| 34 | =cut | ||
| 35 | |||
| 36 | |||
| 37 | package CLIPSeqTools::Role::Option::Library; | ||
| 38 | $CLIPSeqTools::Role::Option::Library::VERSION = '1.0.0'; | ||
| 39 | |||
| 40 | ####################################################################### | ||
| 41 | ####################### Load External modules ##################### | ||
| 42 | ####################################################################### | ||
| 43 | use Modern::Perl; | ||
| 44 | use MooseX::App::Role; | ||
| 45 | |||
| 46 | |||
| 47 | ####################################################################### | ||
| 48 | ######################## Load GenOO modules ####################### | ||
| 49 | ####################################################################### | ||
| 50 | use GenOO::RegionCollection::Factory; | ||
| 51 | |||
| 52 | |||
| 53 | ####################################################################### | ||
| 54 | ####################### Command line options ###################### | ||
| 55 | ####################################################################### | ||
| 56 | option 'driver' => ( | ||
| 57 | is => 'rw', | ||
| 58 | isa => 'Str', | ||
| 59 | default => 'SQLite', | ||
| 60 | documentation => 'driver for database connection (eg. mysql, SQLite).', | ||
| 61 | ); | ||
| 62 | |||
| 63 | option 'database' => ( | ||
| 64 | is => 'rw', | ||
| 65 | isa => 'Str', | ||
| 66 | required => 1, | ||
| 67 | documentation => 'database name or path.', | ||
| 68 | ); | ||
| 69 | |||
| 70 | option 'table' => ( | ||
| 71 | is => 'rw', | ||
| 72 | isa => 'Str', | ||
| 73 | default => 'sample', | ||
| 74 | documentation => 'database table.', | ||
| 75 | ); | ||
| 76 | |||
| 77 | option 'host' => ( | ||
| 78 | is => 'rw', | ||
| 79 | isa => 'Str', | ||
| 80 | documentation => 'hostname for database connection.', | ||
| 81 | ); | ||
| 82 | |||
| 83 | option 'user' => ( | ||
| 84 | is => 'rw', | ||
| 85 | isa => 'Str', | ||
| 86 | documentation => 'username for database connection.', | ||
| 87 | ); | ||
| 88 | |||
| 89 | option 'password' => ( | ||
| 90 | is => 'rw', | ||
| 91 | isa => 'Str', | ||
| 92 | documentation => 'password for database connection.', | ||
| 93 | ); | ||
| 94 | |||
| 95 | option 'filter' => ( | ||
| 96 | is => 'rw', | ||
| 97 | isa => 'ArrayRef', | ||
| 98 | default => sub { [] }, | ||
| 99 | documentation => 'filter library. Option can be given multiple times. '. | ||
| 100 | 'Syntax: column_name="pattern" '. | ||
| 101 | 'e.g. --filter deletion="def" --filter rmsk="undef" '. | ||
| 102 | '--filter query_length=">31" to keep reads with '. | ||
| 103 | 'deletions AND not repeats AND longer than 31. '. | ||
| 104 | 'Supported operators: >, >=, <, <=, =, !=, def, undef.', | ||
| 105 | ); | ||
| 106 | |||
| 107 | |||
| 108 | ####################################################################### | ||
| 109 | ###################### Interface Attributes ####################### | ||
| 110 | ####################################################################### | ||
| 111 | has 'reads_collection' => ( | ||
| 112 | is => 'rw', | ||
| 113 | builder => '_build_collection', | ||
| 114 | lazy => 1, | ||
| 115 | ); | ||
| 116 | |||
| 117 | has 'records_class' => ( | ||
| 118 | is => 'rw', | ||
| 119 | isa => 'Str', | ||
| 120 | default => 'GenOO::Data::DB::DBIC::Species::Schema::' . | ||
| 121 | 'SampleResultBase::v3', | ||
| 122 | documentation => 'type of records stored in database.', | ||
| 123 | ); | ||
| 124 | |||
| 125 | |||
| 126 | ####################################################################### | ||
| 127 | ######################## Interface Methods ######################## | ||
| 128 | ####################################################################### | ||
| 129 | sub validate_args {} | ||
| 130 | |||
| 131 | |||
| 132 | ####################################################################### | ||
| 133 | ######################### Private Methods ######################### | ||
| 134 | ####################################################################### | ||
| 135 | sub _build_collection { | ||
| 136 | my ($self) = @_; | ||
| 137 | |||
| 138 | my $collection = $self->_build_collection_from_database; | ||
| 139 | _apply_simple_filters_on_collection($self->filter, $collection); | ||
| 140 | return $collection; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub _build_collection_from_database { | ||
| 144 | my ($self) = @_; | ||
| 145 | |||
| 146 | return GenOO::RegionCollection::Factory->create('DBIC', { | ||
| 147 | driver => $self->driver, | ||
| 148 | host => $self->host, | ||
| 149 | database => $self->database, | ||
| 150 | user => $self->user, | ||
| 151 | password => $self->password, | ||
| 152 | table => $self->table, | ||
| 153 | records_class => $self->records_class, | ||
| 154 | })->read_collection; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | ####################################################################### | ||
| 159 | ######################## Private Functions ######################## | ||
| 160 | ####################################################################### | ||
| 161 | sub _apply_simple_filters_on_collection { | ||
| 162 | my ($filters, $collection) = @_; | ||
| 163 | |||
| 164 | my @elements = @{$filters}; | ||
| 165 | foreach my $element (@elements) { | ||
| 166 | $element =~ /^(.+?)=(.+?)$/; | ||
| 167 | my $col_name = $1; | ||
| 168 | my $filter = $2; | ||
| 169 | $collection->simple_filter($col_name, $filter); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | 1; |