#!/usr/bin/perl
use strict;

if ($ARGV[0] && $ARGV[0] =~ /^\-.+/) {
    my $opt = shift @ARGV;
    if ($opt eq '-h' || $opt eq '--help') {
        usage();
        exit 0;
    }
    else {
        print STDERR "Unknown option: $opt\n";
        exit 1;
    }
}

# ----------------------------------------
# initial pass:
#   build a lookup table mapping IDs to
#   namespaces
# ----------------------------------------

my %term = ();
my %ns = ();
my @all = ();
my $id; # current id
my $n = 0;
while (<>) {
    if (/^id:\s+(\S+)/) {
        $id=$1;
        $term{$1} = $_;
    }
    if (/^namespace:\s+(\S+)/) {
        $ns{$id} = $1;
    }
    push(@all,$_);
}

# ----------------------------------------
# filter:
#   write all lines except those
#   that are for inter-ontology links
# ----------------------------------------
foreach (@all) {
    if (/^id:\s+(\S+)/) {
        $id=$1;  
    }
    if (/^relationship: \w+ (\S+)/) {
        #print STDERR "$1 $id // $ns{$1} $ns{$id}\n";
        if ($ns{$1} ne $ns{$id}) {
            # inter-ontology link. do not write
            print STDERR "Filtering: $1 $id // $ns{$1} $ns{$id}\n";
            $n++;
        }
        else {
            print "$_";
        }
    }
    else {
            print "$_";
        
    }
}

print STDERR "\nRemoved: $n links\n";
exit(0);

sub scriptname {
    my @p = split(/\//,$0);
    pop @p;
}


sub usage {
    my $sn = scriptname();

    print <<EOM;
$sn FILE [FILE2..]

removes inter-ontology links. An inter-ontology link is a relationship
tag in a stanza T1 in namespace O1 that references a stanza T2 in a
namespace O2 where O1 != O2.

The filtered ontology is written on STDOUT

A report is written on STDERR

EOM
}

