| 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::GUUGle; | ||
| 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 | |||
| 14 | 2 | ▶ | use autodie qw(open close); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 15 | 2 | ▶ | use English qw( -no_match_vars ); |
|
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::BackendI'; | ||
| 21 | |||
| 22 | use List::Util qw(max); | ||
| 23 | |||
| 24 | sub new { | ||
| 25 | my $self = shift; | ||
| 26 | $self = $self->SUPER::new; | ||
| 27 | my %all_features = $self->features; | ||
| 28 | delete $all_features{EVALUE}; | ||
| 29 | delete $all_features{PERCENT_IDENTITY}; | ||
| 30 | delete $all_features{MISMATCHES}; | ||
| 31 | delete $all_features{DELETIONS}; | ||
| 32 | delete $all_features{INSERTIONS}; | ||
| 33 | delete $all_features{EDITDISTANCE}; | ||
| 34 | delete $all_features{ONLINE}; | ||
| 35 | delete $all_features{COMPLETE}; | ||
| 36 | delete $all_features{SHOWDESC}; | ||
| 37 | delete $all_features{QSPEEDUP}; | ||
| 38 | delete $all_features{PROTEINS}; | ||
| 39 | delete $all_features{HXDROP}; | ||
| 40 | delete $all_features{EXDROP}; | ||
| 41 | delete $all_features{NATIVE_D_A_REV_COM}; | ||
| 42 | $self->settings->gumismatches(0); | ||
| 43 | $self->features(%all_features); | ||
| 44 | return $self; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub search { | ||
| 48 | my ( $self, $arg_ref ) = @_; | ||
| 49 | my $s = $self->settings; | ||
| 50 | $self->_check_search_settings($arg_ref); | ||
| 51 | |||
| 52 | my ( $query, $query_file, $tmp_query_file ) | ||
| 53 | = $self->_create_tmp_query_file(); | ||
| 54 | |||
| 55 | # now generate the command string | ||
| 56 | my $eflag = q{}; | ||
| 57 | if ( $s->upstream > 0 || $s->downstream > 0 ) { | ||
| 58 | $eflag = ' -e ' . max( $s->upstream, $s->downstream ) . q{ }; | ||
| 59 | } | ||
| 60 | |||
| 61 | if ( $s->gumismatches > 0 && !$s->direct_and_rev_com ) { | ||
| 62 | $self->warn( "GUUGle counts GU always as no mismatch.\n" | ||
| 63 | . 'Set gumismatches(0) to hide this warning.' ); | ||
| 64 | } | ||
| 65 | if ( !$s->reverse_complement && $s->query_file ) { | ||
| 66 | $self->throw( | ||
| 67 | -class => 'Bio::Root::BadParameter', | ||
| 68 | -text => 'GUUGle searches only for the reverse ' . 'complement. ', | ||
| 69 | -value => $s->reverse_complement, | ||
| 70 | ); | ||
| 71 | } | ||
| 72 | |||
| 73 | my $lflag = q{}; | ||
| 74 | if ( $s->maxhits_isset ) { | ||
| 75 | $lflag = ' -l ' . $s->maxhits . q{ }; | ||
| 76 | } | ||
| 77 | |||
| 78 | #set query_length automatically | ||
| 79 | my $auto_query_length = 0; | ||
| 80 | if ( !defined $s->query_length ) { | ||
| 81 | if ($query) { | ||
| 82 | $s->query_length( length $query ); | ||
| 83 | $auto_query_length = 1; | ||
| 84 | } | ||
| 85 | else { | ||
| 86 | $self->throw( | ||
| 87 | -class => 'Bio::Root::BadParameter', | ||
| 88 | -text => 'query_length not set. See -d flag in the ' | ||
| 89 | . 'GUUGle documentation. ', | ||
| 90 | ); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | my $command | ||
| 95 | = $self->_cat_path_filename( $s->execpath, 'guugle' ) | ||
| 96 | . $eflag | ||
| 97 | . $lflag . ' -d ' | ||
| 98 | . $s->query_length . q{ } | ||
| 99 | . $self->_cat_path_filename( $s->datapath, $s->database ) . q{ } | ||
| 100 | . $tmp_query_file; | ||
| 101 | |||
| 102 | if ( $ENV{BIOGREPDEBUG} ) { | ||
| 103 | warn $command . "\n"; | ||
| 104 | } | ||
| 105 | |||
| 106 | my $cmd_ok = $self->_execute_command($command); | ||
| 107 | |||
| 108 | if ( !$cmd_ok ) { | ||
| 109 | $self->throw( | ||
| 110 | -class => 'Bio::Root::SystemException', | ||
| 111 | -text => "GUUGle call failed. Command was:\n\t$command" | ||
| 112 | ); | ||
| 113 | } | ||
| 114 | |||
| 115 | $self->_skip_header(); | ||
| 116 | my %query_desc_lookup; | ||
| 117 | foreach my $query_seq ( @{ $self->_query_seqs } ) { | ||
| 118 | |||
| 119 | # simulate how this sequence would look in guugle output | ||
| 120 | my $query_desc = $query_seq->id; | ||
| 121 | if ( $query_seq->desc && length($query_desc) > 0 ) { | ||
| 122 | $query_desc .= q{ } . $query_seq->desc; | ||
| 123 | } | ||
| 124 | $query_desc_lookup{$query_desc} = $query_seq; | ||
| 125 | } | ||
| 126 | $self->{_mapping} = \%query_desc_lookup; | ||
| 127 | $self->_prepare_results; | ||
| 128 | if ($auto_query_length) { | ||
| 129 | $self->settings->query_length_reset; | ||
| 130 | } | ||
| 131 | return 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub get_databases { | ||
| 135 | my $self = shift; | ||
| 136 | return $self->_get_databases('.al1'); | ||
| 137 | } | ||
| 138 | |||
| 139 | sub generate_database { | ||
| 140 | my ( $self, @args ) = @_; | ||
| 141 | |||
| 142 | my %args = $self->_prepare_generate_database(@args); | ||
| 143 | |||
| 144 | if ( defined $args{skip} ) { | ||
| 145 | return 0; | ||
| 146 | } | ||
| 147 | |||
| 148 | $self->_create_index_and_alphabet_file( $args{filename} ); | ||
| 149 | return 1; | ||
| 150 | } | ||
| 151 | |||
| 152 | sub _skip_header { | ||
| 153 | my ($self) = @_; | ||
| 154 | my $FH = $self->_output_fh; | ||
| 155 | my $blank_lines = 0; | ||
| 156 | while ( my $line = <$FH> ) { | ||
| 157 | chomp $line; | ||
| 158 | |||
| 159 | # skip everything before first line | ||
| 160 | if ( $line =~ m{\A \s* \z}xms ) { | ||
| 161 | $blank_lines++; | ||
| 162 | return if $blank_lines == 2; | ||
| 163 | } | ||
| 164 | } | ||
| 165 | return; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub _rnas_match { | ||
| 169 | my ( $self, $s1, $s2 ) = @_; | ||
| 170 | return 1 if $s1 eq $s2; | ||
| 171 | return 0 if length $s1 != length $s2; | ||
| 172 | |||
| 173 | # now check for wobble pairs | ||
| 174 | for my $i ( 0 .. ( length($s1) - 1 ) ) { | ||
| 175 | my $base_a = substr $s1, $i, 1; | ||
| 176 | my $base_b = substr $s2, $i, 1; | ||
| 177 | if ( $base_a ne $base_b | ||
| 178 | && join( q{}, $base_a, $base_b ) ne 'uc' | ||
| 179 | && join( q{}, $base_a, $base_b ) ne 'ga' ) | ||
| 180 | { | ||
| 181 | return 0; | ||
| 182 | } | ||
| 183 | } | ||
| 184 | return 1; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub _get_subject_id_and_desc { | ||
| 188 | my ( $self, $text ) = @_; | ||
| 189 | if ( $text =~ m{(.*?)\s(.*)}xms ) { | ||
| 190 | return ( $1, $2 ); | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | return ( $text, q{} ); | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | sub _parse_next_res { | ||
| 198 | my $self = shift; | ||
| 199 | my $s = $self->settings; | ||
| 200 | my @query_seqs = $self->_query_seqs; | ||
| 201 | |||
| 202 | # temp variables. for parsing only | ||
| 203 | my ($subject_id, $subject_desc, $subject_pos, | ||
| 204 | $subject_seq, $query_desc, $query_pos, | ||
| 205 | $query_seq, $matchlength, $upstream | ||
| 206 | ) = ( 0, q{}, 0, 0, q{}, 0, 0, 0, 0 ); | ||
| 207 | |||
| 208 | my ( $up_seq, $down_seq ) = ( q{}, q{} ); | ||
| 209 | |||
| 210 | my $FH = $self->_output_fh; | ||
| 211 | LINE: | ||
| 212 | while ( my $line = <$FH> ) { | ||
| 213 | chomp $line; | ||
| 214 | |||
| 215 | if ($line =~ m{\A | ||
| 216 | MatchLength:\s(\d+)\s | ||
| 217 | "(.*?)" | ||
| 218 | \sat\s(\d+)\s | ||
| 219 | vs\.\s | ||
| 220 | "(.*?)"\s | ||
| 221 | at\s(\d+) | ||
| 222 | }xms | ||
| 223 | ) | ||
| 224 | { | ||
| 225 | ( $matchlength, $subject_id, $subject_pos, | ||
| 226 | $query_desc, $query_pos | ||
| 227 | ) = ( $1, $2, $3, $4, $5 ); | ||
| 228 | ( $subject_id, $subject_desc ) | ||
| 229 | = $self->_get_subject_id_and_desc($subject_id); | ||
| 230 | |||
| 231 | next LINE; | ||
| 232 | } | ||
| 233 | |||
| 234 | if ($line =~ m{\A | ||
| 235 | >(.*?) | ||
| 236 | _at_(\d+) | ||
| 237 | _with_ | ||
| 238 | (.*?) | ||
| 239 | _at_(\d+) | ||
| 240 | }xms | ||
| 241 | ) | ||
| 242 | { # -e mode | ||
| 243 | |||
| 244 | ( $subject_id, $subject_pos, $query_desc, $query_pos, ) | ||
| 245 | = ( $1, $2, $3, $4, ); | ||
| 246 | ( $subject_id, $subject_desc ) | ||
| 247 | = $self->_get_subject_id_and_desc($subject_id); | ||
| 248 | next LINE; | ||
| 249 | } | ||
| 250 | |||
| 251 | if ( $line =~ m{ \A 5 (.*) 3 \z}xms ) { | ||
| 252 | $subject_seq = $1; | ||
| 253 | next LINE; | ||
| 254 | } | ||
| 255 | |||
| 256 | if ( $line =~ m{ \A 3 (.*) 5 \z}xms ) { | ||
| 257 | $query_seq = $1; | ||
| 258 | next LINE; | ||
| 259 | } | ||
| 260 | |||
| 261 | if ( $line =~ m{\A Maximum\snumber}xms ) { | ||
| 262 | last LINE; | ||
| 263 | } | ||
| 264 | |||
| 265 | # find the query that belongs to the match | ||
| 266 | my $query = $self->{_mapping}->{$query_desc}; | ||
| 267 | |||
| 268 | # already reverse, so just the complement here: | ||
| 269 | $query_seq =~ tr[UTGCAutgca][AACGUaacgu]; | ||
| 270 | |||
| 271 | # -e mode | ||
| 272 | if ( $line =~ m{ \A [gucaGUCA]+ \z }xms ) { | ||
| 273 | |||
| 274 | # find subject in string. a little bit complicated because | ||
| 275 | # don't know if the upstream/downstream region is as large | ||
| 276 | # as we request | ||
| 277 | my $qrc = lc $query->revcom->seq; | ||
| 278 | $qrc =~ s/t/u/gxms; | ||
| 279 | my $ql = $s->query_length || length $query->seq; | ||
| 280 | SUBSTRING: | ||
| 281 | for my $length ( reverse $ql .. length $query->seq ) { | ||
| 282 | STARTPOSITION: | ||
| 283 | for my $start ( | ||
| 284 | reverse 0 .. max( $s->upstream, $s->downstream ) ) | ||
| 285 | { | ||
| 286 | my $query_start | ||
| 287 | = length( $query->seq ) - $query_pos + 1 - $length; | ||
| 288 | my $qs = substr $qrc, $query_start, $length; | ||
| 289 | my $ss = substr $line, $start, $length; | ||
| 290 | |||
| 291 | #warn "L:$length S:$start QS:$query_start $qrc $qs $line $ss"; | ||
| 292 | next STARTPOSITION if !( $self->_rnas_match( $ss, $qs ) ); | ||
| 293 | $subject_pos = $start; | ||
| 294 | $subject_seq = $ss; | ||
| 295 | $query_seq = $qs; | ||
| 296 | $matchlength = $length; | ||
| 297 | $upstream = $start; | ||
| 298 | $up_seq = substr $line, 0, $upstream; | ||
| 299 | $down_seq = substr $line, $upstream + $length; | ||
| 300 | |||
| 301 | # now check if regions are larger than | ||
| 302 | # expected. can happen when up/down differ | ||
| 303 | if ( length($up_seq) > $s->upstream ) { | ||
| 304 | $up_seq = substr $up_seq, | ||
| 305 | length($up_seq) - $s->upstream; | ||
| 306 | $upstream = length $up_seq; | ||
| 307 | } | ||
| 308 | if ( length($down_seq) > $s->downstream ) { | ||
| 309 | $down_seq = substr $down_seq, 0, $s->downstream; | ||
| 310 | } | ||
| 311 | last SUBSTRING; | ||
| 312 | } | ||
| 313 | } | ||
| 314 | } | ||
| 315 | my $fasta = Bio::Seq->new( | ||
| 316 | -id => $subject_id, | ||
| 317 | -seq => $up_seq . $subject_seq . $down_seq, | ||
| 318 | -desc => $subject_desc, | ||
| 319 | ); | ||
| 320 | my $tmp_aln = Bio::SimpleAlign->new( -source => 'Bio::Grep' ); | ||
| 321 | $tmp_aln->add_seq( | ||
| 322 | Bio::LocatableSeq->new( | ||
| 323 | -id => 'Subject', | ||
| 324 | -seq => $subject_seq, | ||
| 325 | -start => $subject_pos, | ||
| 326 | -end => $subject_pos + length($subject_seq) - 1, | ||
| 327 | ) | ||
| 328 | ); | ||
| 329 | $tmp_aln->add_seq( | ||
| 330 | Bio::LocatableSeq->new( | ||
| 331 | -id => $query->id, | ||
| 332 | -desc => $query->desc, | ||
| 333 | -seq => $query_seq, | ||
| 334 | -start => $query_pos, | ||
| 335 | -end => $query_pos + length($query_seq) - 1, | ||
| 336 | ) | ||
| 337 | ); | ||
| 338 | my $res = $self->_filter_result( | ||
| 339 | Bio::Grep::SearchResult->new( | ||
| 340 | { sequence => $fasta, | ||
| 341 | begin => $upstream, | ||
| 342 | end => $upstream + $matchlength, | ||
| 343 | alignment => $tmp_aln, | ||
| 344 | sequence_id => $fasta->id, | ||
| 345 | remark => q{}, | ||
| 346 | query => $query, | ||
| 347 | } | ||
| 348 | ) | ||
| 349 | ); | ||
| 350 | if ($res) { | ||
| 351 | return $res; | ||
| 352 | } | ||
| 353 | } | ||
| 354 | close $FH; | ||
| 355 | |||
| 356 | #$self->_delete_output(); | ||
| 357 | return 0; | ||
| 358 | } | ||
| 359 | |||
| 360 | sub get_sequences { | ||
| 361 | my ( $self, $seqid ) = @_; | ||
| 362 | return $self->_get_sequences_from_bio_index($seqid); | ||
| 363 | } | ||
| 364 | |||
| 365 | sub available_sort_modes { | ||
| 366 | my ($self) = @_; | ||
| 367 | |||
| 368 | # get sort modes from superclass | ||
| 369 | return ( $self->SUPER::available_sort_modes() ); | ||
| 370 | } | ||
| 371 | 1; # Magic true value required at end of module | ||
| 372 | __END__ | ||