#!/usr/bin/perl

#use strict;
#use warnings;
use diagnostics;

# required modules
use Net::FTP;
use Archive::Extract;
use File::Fetch;

%eco = (		
	IBA	 => "ECO:0000318",
	IBD	 => "ECO:0000319",
	IC	 => "ECO:0000001",
	IDA	 => "ECO:0000314",
	IEA	 => "ECO:0000501",
	IEP	 => "ECO:0000008",
	IGC	 => "ECO:0000317",
	IGI	 => "ECO:0000316",
	IKR	 => "ECO:0000320",
	IPI	 => "ECO:0000353",
	IRD	 => "ECO:0000321",
	ISA	 => "ECO:0000200",
	ISM	 => "ECO:0000202",
	ISO	 => "ECO:0000201",
	ISS	 => "ECO:0000044",
	NAS	 => "ECO:0000303",
	ND	 => "ECO:0000035",
	RCA	 => "ECO:0000245",
	TAS	 => "ECO:0000304",
	EXP	 => "ECO:0000006",
	IMP	 => "ECO:0000315",
	);
	
%gpi_type = (		
	"protein coding gene" => "protein",
	"non-coding RNA gene" => "ncRNA",
	"rRNA gene" => "rRNA", 
	"tRNA gene" => "tRNA", 
	"snRNA gene" => "snRNA", 
	"snoRNA gene" => "snoRNA", 
	"miRNA gene" => "miRNA", 
	"scRNA gene" => "scRNA", 
	"lincRNA gene" => "lincRNA", 
	"SRP RNA gene" => "SRP_RNA", 
	"RNase P RNA gene" => "RNase_P_RNA", 
	"RNase MRP RNA gene" => "RNase_MRP_RNA", 
	"telomerase RNA gene" => "telomerase_RNA",
	);	

########
#	fetch GAF and marker list from MGI
	# set the user and password 
	$user = 'anonymous';
	$pass = 'Mary.Dolan@jax.org';

	$host = 'ftp.informatics.jax.org';
	$home = '/pub/reports/';
	$ftp = Net::FTP->new($host) or die "Cannot connect to $host";
	$ftp->login($user, $pass) or die "Cannot login";
	$ftp->cwd($home) or die "Cannot change working directory";
	
	$ftp->get('gene_association.mgi') or die "get failed GAF";
	$ftp->get('MRK_List2.rpt') or die "get failed MRK";  
	$ftp->quit;

open (GAF, "<gene_association.mgi") or die "cannot open GAF\n";
open (MRK, "<MRK_List2.rpt") or die "cannot open MRK\n";
open (GPI, ">mgi.gpi");
open (GPAD, ">mgi.gpad"); 
%prot2mgi = ();	
%mgi2symbol = ();
%mgi2name = ();
%mgi2syn = ();
%printed = ();

#gpad header
print GPAD "!gpad-version: 1.2\n";
print GPAD "!CVS Version: Revision: 1.0 \$";
print GPAD "\n\n!Project_name: Mouse Genome Informatics (MGI)";
print GPAD "\n!URL: http://www.informatics.jax.org/";
print GPAD "\n!Contact Email: mgi-help\@jax.org";
print GPAD "\n\n!Columns:\n";
print GPAD "!DB   DB_Object_ID   Relationship   GO ID   Reference   Evidence Code   With/From   Interacting taxon   Date   Assigned By   Annotation XP	Properties\n";

#gpi header
print GPI "!gpi-version: 1.2";
print GPI "\n!CVS Version: Revision: 1.0 \$";
print GPI "\n\n!Project_name: Mouse Genome Informatics (MGI)";
print GPI "\n!URL: http://www.informatics.jax.org/";
print GPI "\n!Contact Email: mgi-help\@jax.org";
print GPI "\n\n!Columns:\n";
print GPI "!DB   DB_Object_ID   DB_Object_Symbol	DB_Object_Name	DB_Object_Synonym(s)	DB_Object_Type	Taxon	Parent_Object_ID	DB_Xref(s)	Properties";

##	write GPAD file
while ($gaf_line = <GAF>) {
	#copy comments
	if ($gaf_line =~ /!software version: \$Revision:/) {
		print GPAD "\n$gaf_line";
		print GPI "\n$gaf_line";
		}
	elsif ($gaf_line =~ /!date: /) {
		print GPAD "$gaf_line\n";
		print GPI "$gaf_line\n";
		}
			
	#check for annotation record
	elsif ($gaf_line =~ /^(.*)\t(.*)\t(.*)\t(.*)\t(GO:\d+)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(taxon:.*?)\t(.*)\t(.*)\t(.*)\t(.*)/) {
		$db = $1;
		$object = $2;
		#$symbol = $3;
		$qual = $4;
		$id = $5;
		$ref = $6;
		$evid = $7;
		$with = $8;
		#$aspect = $9;
		#$name = $10;
		#$syn = $11;
		#$type = $12;
		#$taxon = $13;
		$date = $14;
		$by = $15;
		$xp = $16;
		$prot = $17;
		
		$inttax = "";
		if ($prot =~ /(.+):(.+)/) {
			$prot_db = $1;
			$prot_id = $2;
			$prot2mgi{$prot} = $object;
			}
		else {
			$prot_db = $db;
			$prot_id = $object;
			}
			
#	DB   DB_Object_ID   Relationship   GO ID   Reference   Evidence Code   With/From   Interacting taxon   Date   Assigned By   Annotation XP 			
		$remove_duplicate = "$prot_db\t$prot_id\t$qual\t$id\t$ref\t$eco{$evid}\t$with\t$inttax\t$date\t$by\t$xp\t\n";
		if (! exists $printed{$remove_duplicate}) {
			print GPAD $remove_duplicate;
			$printed{$remove_duplicate}++;
			}
		}
	}	

##	write GPI file
while ($mgi_line = <MRK>) {
	#check for markers
	if ($mgi_line =~ /^(MGI:\d+.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(.*)\t(O)\t(.*)\t(Gene)\t(.*?)\t(.*)$/) {
	    #
		$mgi_id = $1;
		#$chr = $2;
		#$cm = $3;
		#$start = $4;
		#$end = $5;
		#$strand = $6;
		$symbol = $7;
		#$status = $8;
		$name = $9;
		#$type = $10;
		$ftype = $11;
		$syn = $12;
		
		$db = "MGI";
		$taxon = "taxon:10090";
		$xref = "";
		$parent = "";
		$mgi2symbol{$mgi_id} = $symbol;
		$mgi2name{$mgi_id} = $name;
		$mgi2syn{$mgi_id} = $syn;
			
		if (exists $gpi_type{$ftype}) {
			$remove_duplicate = "$db\tMGI:$mgi_id\t$symbol\t$name\t$syn\t$gpi_type{$ftype}\t$taxon\t$parent\t$xref\t\n";
			if (! exists $printed{$remove_duplicate}) {
				print GPI $remove_duplicate;
				$printed{$remove_duplicate}++;
				}
			}
		}
	}
	
foreach $prot (sort keys %prot2mgi) {	
	if ($prot =~ /(.+):(.+)/) {
		$prot_db = $1;
		$prot_id = $2;
		}
	$mgi_id = $prot2mgi{$prot};
	$symbol = $mgi2symbol{$mgi_id};
	$name = $mgi2name{$mgi_id};
	$syn = $mgi2syn{$mgi_id};
	$remove_duplicate = "$prot_db\t$prot_id\t$symbol\t$name\t$syn\tprotein\t$taxon\tMGI:$mgi_id\t$xref\t\n";
		if (! exists $printed{$remove_duplicate}) {
			print GPI $remove_duplicate;
			$printed{$remove_duplicate}++;
			}
	}
	
close (GAF) || die "GAF file was not closed\n";
close (GPAD) || die "GPAD file was not closed\n";
close (MRK) || die "MRK file was not closed\n";
close (GPI) || die "GPI file was not closed\n";

print "DONE\n";
exit 0;

__END__

gene (42,171)
	protein coding gene (22,890)
	non-coding RNA gene (10,605)
		rRNA gene (408)
		tRNA gene (459)
		snRNA gene (1,467)
		snoRNA gene (1,610)
		miRNA gene (2,245)
		scRNA gene (2)
		lincRNA gene (1,744)
		SRP RNA gene (13)
		RNase P RNA gene (4)
		RNase MRP RNA gene (1)
		telomerase RNA gene (1)

		unclassified non-coding RNA gene (2,638)
		heritable phenotypic marker (2,612)
		gene segment (677)
		unclassified gene (5,381)

GPI 		
!Columns
!   name                   required? cardinality   GAF column #  Example content
!   DB_Object_ID           required 	1             2/17          Q4VCS5-1
!   DB_Object_Symbol       required  1             3             AMOT
!   DB_Object_Name         optional  0 or greater  10            Angiomotin
!   DB_Object_Synonym(s)   optional  0 or greater  11            AMOT|KIAA1071
!   DB_Object_Type         required  1             12            protein
!   Taxon                  required  1             13            taxon:9606
!   Parent_Object_ID       optional  0 or 1        -             UniProtKB:Q4VCS5
!   DB_Xref(s)             optional  0 or greater  -             (not populated in gp_information files supplied by UniProt-GOA)
!   Properties             optional  0 or greater  -             db_subset=Swiss-Prot

ECO:0000318	IBA	synonym		IBA: Inferred from Biological aspect of Ancestor
ECO:0000319	IBD	synonym		IBD: Inferred from Biological aspect of Descendant
ECO:0000001	IC	synonym		IC: Inferred by Curator
ECO:0000314	IDA	synonym		IDA: Inferred from Direct Assay
ECO:0000501	IEA	synonym		IEA: Inferred from Electronic Annotation
ECO:0000008	IEP	synonym		IEP: Inferred from Expression Pattern
ECO:0000317	IGC	synonym		IGC: Inferred from Genomic Context
ECO:0000316	IGI	synonym		IGI: Inferred from Genetic Interaction
ECO:0000320	IKR	synonym		IKR: Inferred from Key Residues
ECO:0000353	IPI	synonym		IPI: Inferred from Physical Interaction
ECO:0000321	IRD	synonym		IRD: Inferred from Rapid Divergence
ECO:0000200	ISA	synonym		ISA: Inferred from Sequence Alignment
ECO:0000202	ISM	synonym		ISM: Inferred from Sequence Model
ECO:0000201	ISO	synonym		ISO: Inferred from Sequence Orthology
ECO:0000044	ISS	synonym		ISS: Inferred from Sequence or Structural Similarity
ECO:0000303	NAS	synonym		NAS: Non-traceable Author Statement
ECO:0000035	ND	synonym		ND: No biological Data available
ECO:0000245	RCA	synonym		RCA: inferred from Reviewed Computational Analysis
ECO:0000304	TAS	synonym		TAS: Traceable Author Statement
ECO:0000006	EXP	synonym		EXP: Inferred from Experiment
ECO:0000315	IMP	synonym		IMP: Inferred from Mutant Phenotype
