| 1 | ############################################################################# | ||
| 2 | # $Author: markus $ | ||
| 3 | # $Date: 2009-11-12 19:54:03 +0100 (Thu, 12 Nov 2009) $ | ||
| 4 | # $Revision: 1848 $ | ||
| 5 | ############################################################################# | ||
| 6 | |||
| 7 | package Bio::Grep::Backend::RE; | ||
| 8 | |||
| 9 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 2 | ▶ | use version; our $VERSION = qv('0.10.6'); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 2 | ▶ | use Carp::Assert; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 2 | ▶ | use autodie qw(open close seek); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | |||
| 17 | 2 | ▶ | use Bio::Grep::SearchResult; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 18 | 0 | ▶ | use Bio::Grep::Backend::BackendI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | |||
| 20 | use base 'Bio::Grep::Backend::Agrep'; | ||
| 21 | |||
| 22 | sub new { | ||
| 23 | my $self = shift; | ||
| 24 | $self = $self->SUPER::new; | ||
| 25 | return $self; | ||
| 26 | } | ||
| 27 | |||
| 28 | sub _init { | ||
| 29 | my ($self) = @_; | ||
| 30 | my %all_features = $self->features; | ||
| 31 | delete $all_features{GUMISMATCHES}; | ||
| 32 | delete $all_features{EVALUE}; | ||
| 33 | delete $all_features{PERCENT_IDENTITY}; | ||
| 34 | delete $all_features{ONLINE}; | ||
| 35 | delete $all_features{COMPLETE}; | ||
| 36 | delete $all_features{QUERY_FILE}; | ||
| 37 | delete $all_features{QUERY_LENGTH}; | ||
| 38 | delete $all_features{SHOWDESC}; | ||
| 39 | delete $all_features{QSPEEDUP}; | ||
| 40 | delete $all_features{HXDROP}; | ||
| 41 | delete $all_features{EXDROP}; | ||
| 42 | delete $all_features{REVCOM_DEFAULT}; | ||
| 43 | delete $all_features{NATIVE_D_A_REV_COM}; | ||
| 44 | delete $all_features{MISMATCHES}; | ||
| 45 | delete $all_features{DELETIONS}; | ||
| 46 | delete $all_features{INSERTIONS}; | ||
| 47 | delete $all_features{EDITDISTANCE}; | ||
| 48 | $self->features(%all_features); | ||
| 49 | $self->{_line_regex} = qr{\A(.*):(.*)\z}xmso; | ||
| 50 | return; | ||
| 51 | } | ||
| 52 | |||
| 53 | sub search { | ||
| 54 | my ( $self, $arg_ref ) = @_; | ||
| 55 | my $s = $self->settings; | ||
| 56 | $self->_check_search_settings($arg_ref); | ||
| 57 | my ( $query, $query_obj, $db_alphabet ) = $self->_bioseq_query(); | ||
| 58 | my $regex = $query; | ||
| 59 | |||
| 60 | # regular expressions can't be Bio::Seq objects | ||
| 61 | if ( defined $query_obj->seq ) { | ||
| 62 | $self->{_qmapping} = { lc( $query_obj->seq ) => $query_obj }; | ||
| 63 | } | ||
| 64 | $self->{_qmapping} = { default => $query_obj }; | ||
| 65 | |||
| 66 | if ( $s->direct_and_rev_com || $s->reverse_complement ) { | ||
| 67 | if ( $regex !~ m{\A [gactu]+ \z}xmsi ) { | ||
| 68 | $self->throw( | ||
| 69 | -class => 'Bio::Root::BadParameter', | ||
| 70 | -text => | ||
| 71 | "While doing a reverse-complement the query does not look like a DNA/RNA\n" | ||
| 72 | . 'sequence.', | ||
| 73 | -value => $regex, | ||
| 74 | ); | ||
| 75 | } | ||
| 76 | my $query_revcom_obj = Bio::Seq->new( | ||
| 77 | -id => $query_obj->id, | ||
| 78 | -desc => $query_obj->desc . ' (reverse complement)', | ||
| 79 | -seq => $query_obj->revcom->seq | ||
| 80 | ); | ||
| 81 | $self->{_qmapping}->{ lc $query_revcom_obj->seq } = $query_revcom_obj; | ||
| 82 | if ( $s->direct_and_rev_com ) { | ||
| 83 | $regex = $regex . q{|} . $query_revcom_obj->seq; | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | $regex = $query_revcom_obj->seq; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | # compile the regex | ||
| 91 | $self->{_regex} = qr{$regex}imsx; | ||
| 92 | $self->{_puffer} = []; | ||
| 93 | |||
| 94 | open my $FH, '<', | ||
| 95 | $self->_cat_path_filename( $s->datapath, $s->database . '.dat' ); | ||
| 96 | $self->_output_fh($FH); | ||
| 97 | |||
| 98 | my $indexfile = $s->datapath . q{/} . $s->database . '.idx'; | ||
| 99 | $self->{'_idx'} = Bio::Index::Fasta->new($indexfile); | ||
| 100 | |||
| 101 | $self->_load_mapping(); | ||
| 102 | $self->_prepare_results; | ||
| 103 | return 1; | ||
| 104 | } | ||
| 105 | |||
| 106 | sub _parse_next_res { | ||
| 107 | my $self = shift; | ||
| 108 | my $s = $self->settings; | ||
| 109 | if ( scalar @{ $self->{_puffer} } > 0 ) { | ||
| 110 | return shift @{ $self->{_puffer} }; | ||
| 111 | } | ||
| 112 | |||
| 113 | my $FH = $self->_output_fh; | ||
| 114 | while ( my $line = <$FH> ) { | ||
| 115 | chomp $line; | ||
| 116 | my ( $linenumber, $complete_seq ) = $line =~ $self->{_line_regex}; | ||
| 117 | |||
| 118 | # store sequence for multiple queries (TODO) | ||
| 119 | my $seq = $complete_seq; | ||
| 120 | my $found_hits = 0; | ||
| 121 | my $seq_obj; | ||
| 122 | HIT: | ||
| 123 | while ( $seq =~ /$self->{_regex}/gxms ) { | ||
| 124 | if ( $s->maxhits_isset | ||
| 125 | && $self->_current_res_id + $found_hits > $s->maxhits ) | ||
| 126 | { | ||
| 127 | seek $FH, 0, 2; | ||
| 128 | last HIT; | ||
| 129 | } | ||
| 130 | if ( $found_hits == 0 ) { | ||
| 131 | $seq_obj = $self->{_idx} | ||
| 132 | ->fetch( $self->{_mapping}->{$linenumber} ); | ||
| 133 | } | ||
| 134 | $found_hits++; | ||
| 135 | |||
| 136 | # get coordinates of hit | ||
| 137 | my $subject_begin = $-[0]; | ||
| 138 | my $subject_seq = substr $seq, $-[0], $+[0] - $-[0]; | ||
| 139 | |||
| 140 | my $subject_end = $+[0]; | ||
| 141 | |||
| 142 | my ( $upstream_seq, $dummy, $downstream_seq ) | ||
| 143 | = $self->_parse_regions( | ||
| 144 | { complete_seq => $complete_seq, | ||
| 145 | subject_begin => $subject_begin, | ||
| 146 | subject_end => $subject_end, | ||
| 147 | } | ||
| 148 | ); | ||
| 149 | |||
| 150 | assert( $dummy eq $subject_seq ) if DEBUG; | ||
| 151 | |||
| 152 | my $sequence = Bio::Seq->new( | ||
| 153 | -id => $seq_obj->id, | ||
| 154 | -desc => $seq_obj->desc, | ||
| 155 | -seq => $upstream_seq . $subject_seq . $downstream_seq, | ||
| 156 | ); | ||
| 157 | |||
| 158 | my $tmp_aln = Bio::SimpleAlign->new( -source => 'Bio::Grep' ); | ||
| 159 | $tmp_aln->add_seq( | ||
| 160 | Bio::LocatableSeq->new( | ||
| 161 | -id => 'Subject', | ||
| 162 | -seq => $subject_seq, | ||
| 163 | -start => $subject_begin + 1, # starts at 1, not 0 | ||
| 164 | -end => $subject_end, | ||
| 165 | ) | ||
| 166 | ); | ||
| 167 | |||
| 168 | my $query_obj = $self->{_qmapping}->{ lc $subject_seq }; | ||
| 169 | if ( !defined $query_obj ) { | ||
| 170 | $query_obj = $self->{_qmapping}->{default}; | ||
| 171 | $query_obj->seq($subject_seq); | ||
| 172 | } | ||
| 173 | $tmp_aln->add_seq( | ||
| 174 | Bio::LocatableSeq->new( | ||
| 175 | -id => $query_obj->id, | ||
| 176 | -seq => $subject_seq, | ||
| 177 | -start => 1, | ||
| 178 | -end => length($subject_seq), | ||
| 179 | ) | ||
| 180 | ); | ||
| 181 | |||
| 182 | my $res = $self->_filter_result( | ||
| 183 | Bio::Grep::SearchResult->new( | ||
| 184 | { sequence => $sequence, | ||
| 185 | begin => length($upstream_seq), | ||
| 186 | end => length( $upstream_seq . $subject_seq ), | ||
| 187 | alignment => $tmp_aln, | ||
| 188 | sequence_id => $sequence->id, | ||
| 189 | remark => q{}, | ||
| 190 | query => $query_obj, | ||
| 191 | } | ||
| 192 | ) | ||
| 193 | ); | ||
| 194 | |||
| 195 | if ($res) { | ||
| 196 | push @{ $self->{_puffer} }, $res; | ||
| 197 | } | ||
| 198 | |||
| 199 | } | ||
| 200 | if ( scalar @{ $self->{_puffer} } > 0 ) { | ||
| 201 | return shift @{ $self->{_puffer} }; | ||
| 202 | } | ||
| 203 | } | ||
| 204 | return; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub available_sort_modes { | ||
| 208 | my ($self) = @_; | ||
| 209 | return ( | ||
| 210 | ga => 'ascending order of dG', | ||
| 211 | gd => 'descending order of dG', | ||
| 212 | ); | ||
| 213 | } | ||
| 214 | |||
| 215 | 1; # Magic true value required at end of module | ||
| 216 | __END__ | ||