#!/usr/local/bin/perl5.6.1 -w 
#matching


# This program is designed to report errors in a gene association flat file.
#The line format used by the consortium has 14 tabs thus delimiting 15 fields.
#the number of the tabs on each line are counted and the contents of the field checked.
#It is specific to Plasmodium falciparum but the regular expressions can easily be modified 
#for use with any organism.




use strict;

my $filename = "@ARGV";
my @lines= ();

#my $i=0;
my $tab="[^\t]*\t";
my $emptycolumn="\t";
my @writelines=();
my $newfilename;
my $confirm ;


my @DB=();
my @DB_Object_ID=();
my @DB_Object_Symbol=();
my @QUAL = ();
my @GO_ID = ();
my @DBREFERENCE =();
my @EVIDENCE= ();
my @WITH = ();
my @ASPECT = ();
my @DBOBJECT_NAME = ();
my @DBOBJECT_SYN = ();
my @DBOBJECT_TYPE = ();
my @TAXON = ();
my @DATE =();
my @ASSIGN =();
		
###############################################################################


open (FILE, "<$filename") or print "couldn't open the file";
#@lines = <FILE>;
#close FILE;

#reads in the file in lines and loads up arrays for each entry. 

print ("START\n");
##############################################################################


#for $i (0 .. $#lines )
my $i = 1;
while (<FILE>)
#start of reading loop

{

	
		chomp $_;
		my @tmp = split ("\t", $_);
		

			$DB[$i] = $tmp[0];
			$DB_Object_ID[$i] = $tmp[1];
			$DB_Object_Symbol[$i] = $tmp[2];
			$QUAL[$i] = $tmp[3];
			$GO_ID[$i] = $tmp[4];
			$DBREFERENCE[$i] = $tmp[5];
			$EVIDENCE[$i]= $tmp[6];
	 		$WITH[$i] = $tmp[7];
			$ASPECT[$i] = $tmp[8];
			$DBOBJECT_NAME[$i] = $tmp[9];
			$DBOBJECT_SYN[$i] = $tmp[10];
			$DBOBJECT_TYPE[$i] = $tmp[11];
			$TAXON[$i] = $tmp[12];
			$DATE[$i] =$tmp[13];
			$ASSIGN[$i] =$tmp[14];
		 	
	
		
		
if (scalar @tmp != 15){
print "Tab error on line $i\n";
}
		
		$i++;

}# end of reading loop (loop 1)
close FILE;
print ("Data read in\n");
#######################################################################

# TO CHECK THE CONTENTS OF THE FIELDS ARE CORRECT AND TO REPORT ERRORS

open (FILE, "<$filename") or print "couldn't open the file";
$i = 1;
#for $i (0 .. $#lines) 
while (<FILE>)
{ 
#print ("line $i\n");
if ($DB[$i] !~/GeneDB_Pfalciparum/)
{
print ("error in DB name on line $i\t $DB[$i]\n")
}

if (($DB_Object_ID[$i]!~ /PF\w\d{4}\w/) && ($DB_Object_ID[$i]!~ /PF\d{2}_\d{4}/) && ($DB_Object_ID[$i]!~ /MAL\d{1,2}P\d\.\d{1,3}/)) 
{
print ("error in gene name on line: $i: $DB_Object_ID[$i]\n");
}

if ($GO_ID[$i] !~ /GO:\d{7}/)
{
print ("POSSIBLE error in GO TERM  on line $i\t $GO_ID[$i] , check \n");
}


if ($EVIDENCE[$i] !~ /IEA|IC|IDA|ISS|ND|TAS|NAS|IEP|IMP/)

{
print ("error in EVIDENCE CODE on line $i\t $EVIDENCE[$i] \n");

}


if (($DBREFERENCE[$i] !~ /GO_REF:nd|GOC:unpublished|PMID:unpublished/) && ($DBREFERENCE[$i] !~ /PMID:\d{6,8}/))

{
print ("error in DATABASE REFERENCE on line $i\t $DBREFERENCE[$i] \n");

}


if ($ASPECT[$i] !~ /P|F|C/)

{
print ("error in ASPECT on line $i\t $ASPECT[$i] \n");

}

if ($DBOBJECT_TYPE[$i]!~ /gene/)
#if ($DBOBJECT_TYPE[$i] ne "gene")

{
print ("error in DATABASE OBJECT TYPE on line $i\t $DBOBJECT_TYPE[$i]\n");
}


if ($TAXON[$i]!~ /taxon:5833/)

{
print ("error in TAXON on line $i\t $TAXON[$i] \n");

}

if ($DATE[$i]!~ /200[2 3 4 5][0 1][0 1 2 3 4 5 6 7 8 9 ][0 1 2 3][0 1 2 3 4 5 6 7 8 9]/)

{
print ("error in DATE format on line $i\t $DATE[$i]  , should be YYYYMMDD\n");

}


$i++;

} #end of loop
#############################################################################################################
