| 1 | package Bio::Graphics::Browser2::Plugin::Blat; | ||
| 2 | # $Id: Blat.pm - Sean O'Keeffe | ||
| 3 | |||
| 4 | use strict; | ||
| 5 | use Bio::Graphics::Browser2::Plugin; | ||
| 6 | use Bio::Graphics::Feature; | ||
| 7 | use File::Temp qw/ tempfile /; | ||
| 8 | use CGI qw(:standard *table); | ||
| 9 | use CGI::Carp qw(fatalsToBrowser); | ||
| 10 | use vars '$VERSION','@ISA','$blat_executable','$twobit_dir','$host','$port'; | ||
| 11 | |||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Bio::Graphics::Browser2::Plugin::Blat -- plugin to map sequences against the genome | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | in human.conf: | ||
| 20 | |||
| 21 | [Blat:plugin] | ||
| 22 | blat_executable = /usr/local/gfClient | ||
| 23 | 2bit_dir = /project/gbrowse/2bit_genomes | ||
| 24 | host = blat.server.host.name | ||
| 25 | port = 17780 | ||
| 26 | |||
| 27 | in mouse.conf: | ||
| 28 | |||
| 29 | [Blat:plugin] | ||
| 30 | blat_executable = /usr/local/gfClient | ||
| 31 | 2bit_dir = /project/gbrowse/2bit_genomes | ||
| 32 | host = blat.server.host.name | ||
| 33 | port = 17781 | ||
| 34 | |||
| 35 | |||
| 36 | =head1 DESCRIPTION | ||
| 37 | |||
| 38 | This Gbrowse plugin will take a sequence (entered in the configuration screen) | ||
| 39 | and BLAT it against the genome of the current organism ( port from conf file). | ||
| 40 | |||
| 41 | You must, of course, have the Blat server(gfServer) and client(gfClient) installed, | ||
| 42 | and you must set plugin parameters in the conf file: | ||
| 43 | [Blat:plugin] | ||
| 44 | blat_executable = /path/to/your/blat_client | ||
| 45 | |||
| 46 | The plugin only works with default psl output format for the moment. | ||
| 47 | |||
| 48 | =head1 AUTHOR | ||
| 49 | |||
| 50 | Sean O'Keeffe E<lt>okeeffe@molgen.mpg.deE<gt>. | ||
| 51 | |||
| 52 | =cut | ||
| 53 | |||
| 54 | |||
| 55 | $blat_executable = ""; | ||
| 56 | $twobit_dir = ""; | ||
| 57 | $host = ""; | ||
| 58 | $port = ""; | ||
| 59 | |||
| 60 | $VERSION = '0.02'; | ||
| 61 | |||
| 62 | @ISA = qw(Bio::Graphics::Browser2::Plugin); | ||
| 63 | |||
| 64 | sub name { "BLAT Alignment" } | ||
| 65 | |||
| 66 | sub description { | ||
| 67 | p("This plugin will take an input DNA sequence and run BLAT's gfClient (a Blat client to a local server)."); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub type { 'finder' } | ||
| 71 | |||
| 72 | sub init { | ||
| 73 | my $self = shift; | ||
| 74 | my $conf = $self->browser_config; | ||
| 75 | $blat_executable = $conf->plugin_setting('blat_executable'); | ||
| 76 | $twobit_dir = $conf->plugin_setting('2bit_dir'); | ||
| 77 | $host = $conf->plugin_setting('host'); | ||
| 78 | $port = $conf->plugin_setting('port'); | ||
| 79 | } | ||
| 80 | sub config_defaults { | ||
| 81 | my $self = shift; | ||
| 82 | return {'sequence_to_blat' => '', | ||
| 83 | 'hits' => '5'} | ||
| 84 | } | ||
| 85 | |||
| 86 | sub configure_form { | ||
| 87 | my $self = shift; | ||
| 88 | my $current_config = $self->configuration; | ||
| 89 | |||
| 90 | my $form .= h3("Enter parameters below for alignment of sequences using a Client to a local BLAT Server:") | ||
| 91 | .start_table({-border => 0}) | ||
| 92 | .TR([ | ||
| 93 | td([b("Input Sequence To Align:"), textarea(-align=>'center', -name=>$self->config_name('sequence_to_blat'),-rows=>10,-cols=>80,-value=>$current_config->{'sequence_to_blat'})]) | ||
| 94 | ]); | ||
| 95 | |||
| 96 | $form .= end_table(); | ||
| 97 | $form .= start_table({-border => 0}) . Tr(td(p())) . Tr(td(p())) . Tr(td(p())) . end_table(); | ||
| 98 | $form .= start_table({-border => 0}) | ||
| 99 | .TR([ | ||
| 100 | td(b("Number of Hits to Return:"), textfield(-align=>'center', -name=>$self->config_name('hits'),-size=>10, -value=>$current_config->{'hits'})) | ||
| 101 | ]); | ||
| 102 | $form .= end_table(); | ||
| 103 | |||
| 104 | return $form; | ||
| 105 | } | ||
| 106 | |||
| 107 | sub find { | ||
| 108 | my $self = shift; | ||
| 109 | my ($i,@hit_starts,@block_sizes,@results); | ||
| 110 | my $query = $self->config_param('sequence_to_blat'); | ||
| 111 | my $hits = $self->config_param('hits'); | ||
| 112 | my ($i_f, $in_file) = tempfile(); | ||
| 113 | my ($o_f, $out_file) = tempfile(); | ||
| 114 | |||
| 115 | my $query_type = check_seq($query) or return; # check for dna or protein (dna queries must be compared against dna databases only & vice versa) | ||
| 116 | if ($query !~ /^\s*>/) { print $i_f ">segment\n";} # add FASTA defline if needed | ||
| 117 | print $i_f $query; # print it to a temp file | ||
| 118 | |||
| 119 | my $error = `$blat_executable $host $port $twobit_dir -nohead -q=dna $in_file $out_file 2>&1 > /dev/null`; | ||
| 120 | die "$error" if $error; | ||
| 121 | |||
| 122 | open (IN, "$out_file") || die "couldn't open $out_file $!\n"; | ||
| 123 | my $hit_count = 0; | ||
| 124 | while(<IN>) { | ||
| 125 | last if ++$hit_count > $hits; | ||
| 126 | # this could probably be done better but ... | ||
| 127 | my ( $matches,$mismatches,$rep_matches,$n_count,$q_num_insert,$q_base_insert,$t_num_insert, $t_base_insert, | ||
| 128 | $strand,$q_name,$q_length,$q_start,$q_end,$t_name,$t_length,$t_start,$t_end,$block_count,$block_sizes, | ||
| 129 | $q_starts,$t_starts) = split; | ||
| 130 | |||
| 131 | $block_sizes =~ s/\,$//; # remove trailing comma from block_sizes string | ||
| 132 | $t_starts =~ s/\,$//; # .. and from t_starts string | ||
| 133 | my $score = sprintf "%.2f", ( 100 * ( $matches + $mismatches + $rep_matches ) / $q_length ); | ||
| 134 | my $percent_id = sprintf "%.2f", ( 100 * ($matches + $rep_matches)/( $matches + $mismatches + $rep_matches )); | ||
| 135 | my $alignment = Bio::Graphics::Feature->new(-start=>$t_start, | ||
| 136 | -end =>$t_end, | ||
| 137 | -ref => $t_name, | ||
| 138 | -type=>'BLAT', | ||
| 139 | -name => "Alignment$hit_count", | ||
| 140 | -strand => $strand, | ||
| 141 | -score => $score | ||
| 142 | ); | ||
| 143 | |||
| 144 | @hit_starts = split(",", $t_starts); | ||
| 145 | @block_sizes = split(",", $block_sizes); | ||
| 146 | for($i=0;$i<$block_count;$i++){ # if multihit alignments (block_count > 1), aggregate. | ||
| 147 | my $sub_alignment = Bio::Graphics::Feature->new(-start=>$hit_starts[$i], | ||
| 148 | -end =>($hit_starts[$i]+$block_sizes[$i]), | ||
| 149 | -ref => $t_name, | ||
| 150 | -type=>'BLAT', | ||
| 151 | -name => 'Alignment', | ||
| 152 | -strand => $strand, | ||
| 153 | -score => $percent_id | ||
| 154 | ); | ||
| 155 | $alignment->add_segment($sub_alignment); | ||
| 156 | } | ||
| 157 | push @results, $alignment; | ||
| 158 | } | ||
| 159 | |||
| 160 | unlink $in_file; | ||
| 161 | unlink $out_file; | ||
| 162 | return (\@results, @results ? '' : 'No alignments found'); | ||
| 163 | } | ||
| 164 | |||
| 165 | sub check_seq{ | ||
| 166 | my $query = shift; | ||
| 167 | # if any sequence contains a character that is an amino acid and not a | ||
| 168 | # nucleotide, consider input to be "prot" | ||
| 169 | if ($query =~ /^(?!\s*>).*([DEFHIKLMPQRSVWY])/mi) {return "prot";} | ||
| 170 | else {return "dna";} | ||
| 171 | } | ||
| 172 | |||
| 173 | 1; | ||
| 174 | |||
| 175 | =head1 AUTHOR | ||
| 176 | |||
| 177 | Sean O'Keeffe E<lt>okeeffe@molgen.mpg.deE<gt>. | ||
| 178 | |||
| 179 | =cut |