#!/usr/bin/env perl
#
# cat disease | disease.pl
#

use strict;
use warnings;

my $entry = "";
my $head = "";
my $abbr = "disease";
my $db = "";
while (my $l = <STDIN>) {
  if ($l =~ /^(\S+)/) {
    $head = $1;
  }
  if ($l =~ /^ENTRY +(\S+)/) {
    $entry = $1;
  }
  if ($head eq "DBLINKS") {
    id(substr($l,12)) if length $l > 12;
  }
  elsif ($head eq "REFERENCE") {
    reference(substr($l,12)) if length $l > 12;
  }
  elsif ($head eq "PATHOGEN"||$head eq "MARKER") {
    bracket(substr($l,12)) if length $l > 12;
  }
  if ($l =~ /^\/\/\//) {
    $entry = "";
    $head = "";
    $db = "";
  }
}

sub reference {
  my $s = shift;
  my @r = ($s =~ /(PMID:\d+)/g);
  foreach my $e (@r) {
    id($e);
  }
}

sub bracket {
  my $s = shift;
  my @r = ($s =~ /\[(TAX:[^\]]+)\]/g);
  foreach my $e (@r) {
    id($e);
  }
  @r = ($s =~ /\[(UP:[^\]]+)\]/g);
  foreach my $e (@r) {
    id($e);
  }
  @r = ($s =~ /\[(RS:[^\]]+)\]/g);
  foreach my $e (@r) {
    id($e);
  }
  @r = ($s =~ /\[(PMID:[^\]]+)\]/g);
  foreach my $e (@r) {
    id($e);
  }
}

sub id {
  my $s = shift;
  my @r = split /\s+/,$s;
  foreach my $e (@r) {
    if ($e =~ s/^([^:]+)://) {
      $db = lc $1;
    }
    if ($e) {
      if ($db =~ /icd/) {
        next;
      }
      if ($db eq 'pmid') {
        print "${abbr}:$entry\tpubmed:$e\n";
      }
      elsif ($db eq 'tax') {
        print "${abbr}:$entry\ttaxonomy:$e\n";
      }
      elsif ($db eq 'up') {
        print "${abbr}:$entry\tuniprot:$e\n";
      }
      elsif ($db eq 'rs') {
        print "${abbr}:$entry\trefseq:$e\n";
      }
      else {
        print "${abbr}:$entry\t${db}:$e\n";
      }
    }
  }
}

