#!/usr/bin/env perl

=head1 NAME
  flybase-fasta-hyperlink.pl

=head1 SYNOPSIS
  hyperlink flybase fasta defline to flybase.net web reports

The flybase fasta deflines have various IDs, as of Jan 2004. Some of
these are in transition from old to new database IDs. Because of this
transition, the current leading '>id' is not the best to use just now,
esp. for translation, mRNA, CDS features.  We will fix this if it
doesn't cause other problems.  Check for 'ID=...' in defline for best
current ID, and use this ID url lookup algorithm:

=cut

%db_lookup_urls=(
  FlyBase => "http://flybase.net/cgi-bin/fbidq.html?",# for id of type FBxx000000
  GadFly => "http://flybase.net/cgi-bin/fbannq.html?acc=", # for id of type Symbol-xxx; CG0000-xxx
  FlyBase_anything => "http://flybase.net/cgi-bin/fbgenq.html?", # general gene search
  );

while (<>){

if (/^>(\S+)/) {
  $fasta_id=$1;
  if ( m/ID=(\w+):([^;,:\s]+)/ ) {
    $lookup_url= $db_lookup_urls{$1} . $2;
    }
  elsif ( m/ID=([^;,:\s]+)/ ) {
    $lookup_url= $db_lookup_urls{FlyBase} . $1;
    }
  elsif( m/FlyBase:(\w+)/ ) {
    $lookup_url= $db_lookup_urls{FlyBase} . $1;
    }
  else {
    # try fasta_id in general gene search 
    $lookup_url= $db_lookup_urls{FlyBase_anything} . $fasta_id;
    }  
  s,^>(\S+),&gt;<a href="$lookup_url">$1</a>,;  
  print;
  }

}     

=head1 EXAMPLE output

=begin html

<html><body>
<pre>
dmel_2R_CDS_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=TpnC41C-PA">CG2981-PA</a> selected_feature: CDS loc=2R:complement(205302..205333,205398..205793,205847..205874,208852..208857);ID=GadFly:TpnC41C-PA;name=TpnC41C-PA;db_xref='GadFly:CG2981 ; FlyBase:FBgn0013348'; 462 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=CG3107-PA">CG3107-PA</a> selected_feature: CDS loc=2R:join(230769..231346,231397..233920);ID=GadFly:CG3107-PA;name=CG3107-PA;db_xref='GadFly:CG3107 ; FlyBase:FBgn0033005'; 3102 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=CG3107-PB">CG3107-PB</a> selected_feature: CDS loc=2R:join(230769..231346,231397..233920);ID=GadFly:CG3107-PB;name=CG3107-PB;db_xref='GadFly:CG3107 ; FlyBase:FBgn0033005'; 3102 bp 

------------
dmel_2R_gene_extended5000_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0013348">FBgn0013348</a> selected_feature: gene loc=2R:complement(205161..208984);ID=FBgn0013348;name=TpnC41C;map=41E5-41E5;db_xref='GadFly:CG2981 ; FlyBase:FBan0002981'; 10001 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0033005">FBgn0033005</a> selected_feature: gene loc=2R:229457..233960;ID=FBgn0033005;name=CG3107;map=41E6-41E6;db_xref='GadFly:CG3107 ; FlyBase:FBan0003107'; 10001 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0050442">FBgn0050442</a> selected_feature: gene loc=2R:complement(234314..235623);ID=FBgn0050442;name=CG30442;map=41E6-41E6;db_xref='GadFly:CG30442 ; FlyBase:FBan0030442'; 10001 bp 

------------
dmel_2R_gene_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0013348">FBgn0013348</a> selected_feature: gene loc=2R:complement(205161..208984);ID=FBgn0013348;name=TpnC41C;map=41E5-41E5;db_xref='GadFly:CG2981 ; FlyBase:FBan0002981'; 3824 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0033005">FBgn0033005</a> selected_feature: gene loc=2R:229457..233960;ID=FBgn0033005;name=CG3107;map=41E6-41E6;db_xref='GadFly:CG3107 ; FlyBase:FBan0003107'; 4504 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0050442">FBgn0050442</a> selected_feature: gene loc=2R:complement(234314..235623);ID=FBgn0050442;name=CG30442;map=41E6-41E6;db_xref='GadFly:CG30442 ; FlyBase:FBan0030442'; 1310 bp 
------------
dmel_2R_mRNA_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=TpnC41C-RA">CG2981-RA</a> selected_feature: mRNA loc=2R:complement(205161..205333,205398..205793,205847..205874,208852..208984);ID=GadFly:TpnC41C-RA;name=TpnC41C-RA;db_xref='GadFly:CG2981 ; FlyBase:FBgn0013348'; 730 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=CG3107-RA">CG3107-RA</a> selected_feature: mRNA loc=2R:join(229457..229511,230742..231346,231397..233960);ID=GadFly:CG3107-RA;name=CG3107-RA;db_xref='GadFly:CG3107 ; FlyBase:FBgn0033005'; 3224 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=CG3107-RB">CG3107-RB</a> selected_feature: mRNA loc=2R:join(229487..229612,230742..231346,231397..233960);ID=GadFly:CG3107-RB;name=CG3107-RB;db_xref='GadFly:CG3107 ; FlyBase:FBgn0033005'; 3295 bp 
------------
dmel_2R_miscRNA_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0063356">FBgn0063356</a> selected_feature: ncRNA loc=2R:join(3325677..3325937,3326051..3326205,3326646..3326985,3327043..3327100);ID=FBgn0063356;name=BcDNA:AT05157;map=44C3-44C3;db_xref='GadFly:CR30370 ; FlyBase:FBan0030370'; 814 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0046827">FBgn0046827</a> selected_feature: ncRNA loc=2R:4614434..4614454;ID=FBgn0046827;name=mir-14;map=45F1-45F1;db_xref='GadFly:CR33010 ; FlyBase:FBan0033010'; 21 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0050009">FBgn0050009</a> selected_feature: ncRNA loc=2R:complement(5321948..5324250,5324314..5324459);ID=FBgn0050009;name=CR30009;map=46F9-46F9;db_xref='GadFly:CR30009 ; FlyBase:FBan0030009'; 2449 bp 
------------
dmel_2R_pseudogene_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0011812">FBgn0011812</a> selected_feature: pseudogene loc=2R:complement(3493633..3494056,3494100..3494131);ID=FBgn0011812;name=Lcp1Psi;map=44C6-44C6;db_xref='GadFly:CR30367 ; FlyBase:FBan0030367'; 456 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0033303">FBgn0033303</a> selected_feature: pseudogene loc=2R:join(3630116..3630515,3630630..3630894);ID=FBgn0033303;name=Cyp6a15Psi;map=44D3-44D3;db_xref='GadFly:CR14753 ; FlyBase:FBan0014753'; 665 bp 
------------
dmel_2R_tRNA_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0011958">FBgn0011958</a> selected_feature: tRNA loc=2R:1281033..1281105;ID=FBgn0011958;name=tRNA:R2:42Ad;map=42A12-42A12;db_xref='GadFly:CR30304 ; FlyBase:FBan0030304'; 73 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0011891">FBgn0011891</a> selected_feature: tRNA loc=2R:complement(1281240..1281312);ID=FBgn0011891;name=tRNA:K2:42Ae;map=42A12-42A12;db_xref='GadFly:CR32837 ; FlyBase:FBan0032837'; 73 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBgn0011938">FBgn0011938</a> selected_feature: tRNA loc=2R:complement(1291471..1291544);ID=FBgn0011938;name=tRNA:N5:42Ah;map=42A12-42A12;db_xref='GadFly:CR30513 ; FlyBase:FBan0030513'; 74 bp 
------------
dmel_2R_translation_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=TpnC41C-PA">CG2981-PA</a> selected_feature: CDS loc=2R:complement(205302..205333,205398..205793,205847..205874,208852..208857);ID=GadFly:TpnC41C-PA;name=TpnC41C-PA;db_xref='GadFly:CG2981 ; FlyBase:FBgn0013348'; 462 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=CG3107-PA">CG3107-PA</a> selected_feature: CDS loc=2R:join(230769..231346,231397..233920);ID=GadFly:CG3107-PA;name=CG3107-PA;db_xref='GadFly:CG3107 ; FlyBase:FBgn0033005'; 3102 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbannq.html?acc=CG3107-PB">CG3107-PB</a> selected_feature: CDS loc=2R:join(230769..231346,231397..233920);ID=GadFly:CG3107-PB;name=CG3107-PB;db_xref='GadFly:CG3107 ; FlyBase:FBgn0033005'; 3102 bp 
------------
dmel_2R_transposon_r3.1.0d.fasta.gz
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBti0019883">FBti0019883</a> selected_feature: transposable_element loc=2R:complement(2401..7765);ID=FBti0019883;name=R1-element{}582;map=41E3-41E3;db_xref='GadFly:R1-element{}582 ; FlyBase:FBan0019883'; 5365 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBti0019884">FBti0019884</a> selected_feature: transposable_element loc=2R:complement(9633..10121);ID=FBti0019884;name=R1-element{}583;map=41E3-41E3;db_xref='GadFly:R1-element{}583 ; FlyBase:FBan0019884'; 489 bp 
&gt;<a href="http://flybase.net/cgi-bin/fbidq.html?FBti0018887">FBti0018887</a> selected_feature: transposable_element loc=2R:18973..19250;ID=FBti0018887;name=Cr1a{}584;map=41E3-41E3;db_xref='GadFly:Cr1a{}584 ; FlyBase:FBan0018887'; 278 bp 
</pre>
</body></html>

=end html

=cut
