#!/usr/bin/perl

use strict;
use warnings;

use File::Basename qw( basename );
use Getopt::Long::Descriptive;
use SmilesScripts::Isomorphism qw( smi_compare );

my $basename = basename $0;
my( $opt, $usage ) = describe_options( <<"END" . 'OPTIONS',
USAGE
    $basename [<args>] [<files>]

DESCRIPTION
    $basename reads in files with three tab-separated columns:

    <ID> <SMILES_1> <SMILES_2>

    The program then attempts to detect isomorphism between <SMILES_1> and
    <SMILES_2> on each line. If immediate isomorphism is not detected,
    step-by-step simplifications are performed in order to lead both
    SMILES to isomorphism.

END
    [ 'remove-alkali-bonds',
      'add alkali bond removal to the list of simplifications' ],
    [ 'print-side-of-superfluous-moieties',
      'identify which of the compared sides has superfluous molecular entities' ],
    [ 'check-isomorphism',
      'perform an extra isomorphism check on molecular entities detected ' .
      'as isomorphic by comparing their canonical representations' ],
    [],
    [ 'help', 'print usage message and exit', { shortcircuit => 1 } ],
);

if( $opt->help ) {
    print $usage->text;
    exit;
}

my $options = {};
$options->{check_isomorphism} = 1         if $opt->check_isomorphism;
$options->{remove_alkali_bonds} = 1       if $opt->remove_alkali_bonds;
$options->{superfluous_moieties_side} = 1 if $opt->print_side_of_superfluous_moieties;

PAIR:
while( <> ) {
    next if /^#/;
    s/\n$//;

    my( $id, $first_smiles, $second_smiles ) = split "\t", $_;

    local $SIG{__WARN__} = sub {
        print STDERR "$basename: $ARGV($.) $id: $_[0]"
    };

    my $reason;
    eval {
        $reason = smi_compare( $first_smiles, $second_smiles, $options );
    };
    if( $@ ) {
        print STDERR "$basename: $ARGV($.) $id: $@";
    } else {
        local $\ = "\n";
        print join "\t", $id, $first_smiles, $second_smiles, $reason;
    }
}
