#!/usr/bin/perl -w
# Compute topological distances between ORFs based on distances between classes

open(CDIS, "mips_dist.txt");
while (<CDIS>) {
	chomp;
	@d = split;
	push @D, [@d];
}
close(CDIS);

$line = 0;
open(NODE, "mips_node.txt");
while (<NODE>) {
	chomp;
	$node = $_;
	$line{$node} = $line;
	$line++;
}
close(NODE);

open(ORF, "MIPS.txt");
while (<ORF>) {
	chomp;
	($orf, $mips) = split;

	unless ($mips eq "98" || $mips eq "99" || $mips eq "putative") {
		$orf =~ s/-//;
		$mips{$orf} = $mips;
	}
}
close(ORF);

@orf = sort(keys %mips);
for $i (0 .. $#orf) {
	$mips1 = $mips{$orf[$i]};
	@mips1 = split /:/, $mips1;
	for $j ($i + 1 .. $#orf) {
		$mips2 = $mips{$orf[$j]};
		@mips2 = split /:/, $mips2;
		print "$orf[$i]\t$orf[$j]\t$mips1\t$mips2\t";

		$d_min = 1000000000;
		for $mips1 (@mips1) {
			unless ($mips1 eq "98" || $mips1 eq "99" || $mips1 eq "putative") {
				$line1 = $line{$mips1};
				for $mips2 (@mips2) {
					unless ($mips2 eq "98" || $mips2 eq "99" || $mips2 eq "putative") {
						$line2 = $line{$mips2};
						unless (defined $line1 && defined $line2) {
							print "\tline1 = $line1\tline2 = $line2\n";
							print "\tmips1 = $mips1\tmips2 = $mips2\n";
							die;
						}
						$d = $D[$line1][$line2];
						if ($d < $d_min) {
							$d_min = $d;
						}
						printf("%d ", $d);
					}
				}
			}
		}
		print "\n";
		# printf("\t%d\n", $d_min);
	}
}
