#!/usr/local/bin/perl
#
# Purpose:    Downconvert a GAF 2.0 file to GAF 1.0
#
# Usage:      perl gaf_convert.pl < gene_association.gaf2-format > gene_association.gaf1-format
use strict;

while (<>) {
	if (/^!\s*gaf-version:\s*(\d)(\.0)?/) {
		if ($1 == 1) {
			print STDERR "Input file is already in GAF 1.0 format.\n";
			exit 1;
		}
		elsif ($1 == 2) {
			print "!gaf-version: 1.0\n";
		}
		else {
			print STDERR "Unsupported GAF version: $1\n";
			exit 1;
		}
	}
	elsif (/^!/) {
		print;
	}
	else {
		chomp;
		my ($db, $dbid, $dbsym, $qual, $goid, $ref, $ev, $with, $aspect, $dbname, $dbsyn, $dbtype, $taxon, $date, $assigned, $extension, $spliceform) = split(/\t/, $_);
		if ($spliceform ne "") {
			my ($prefix, $suffix) = split(/:/, $spliceform);
			print "$db\t$suffix\t$dbsym\t$qual\t$goid\t$ref\t$ev\t$with\t$aspect\t$dbname\t$dbsyn\t$dbtype\t$taxon\t$date\t$assigned\n";
		}
		else {
			print "$db\t$dbid\t$dbsym\t$qual\t$goid\t$ref\t$ev\t$with\t$aspect\t$dbname\t$dbsyn\t$dbtype\t$taxon\t$date\t$assigned\n";
		}
	}
}
