
%affyhash = ();
%llhash = ();
%acchash = ();
%outhash = ();
while( <BASE> ) {
    chomp;
    @vals = split /\t/;
    $affyhash{$vals[0]} = $vals[1]. "\t". $vals[2];
    if($vals[2] eq "NA"){
        $outhash{$vals[0]} = $vals[1]. "\t". $vals[2]. "\t".
                  "NA". "\t". "NA". "\t". "NA". "\t". "NA". "\t".
                  "NA". "\t". "NA". "\t". "NA". "\t". "NA". "\t".
                  "NA". "\t". "NA". "\t". "NA". "\t". "NA";
    }else{
        if (exists $llhash{$vals[2]}){
	    $llhash{$vals[2]} = $llhash{$vals[2]} . ";" . $vals[0];
        }else{
            $llhash{$vals[2]} = $vals[0];
        }  

        $acchash{$vals[0]} = $vals[1];
    }
}
close BASE; 

$/ = ">>";
%outFile;
while( <DATA> ) {
    $outFile{"Affy"} = "NA";
    $outFile{"Locus"} = "NA";
    $outFile{"Symbol"} = "NA";
    $outFile{"Name"} = "NA";
    $outFile{"Unigene"} = "NA";
    $outFile{"Chrom"} = "NA";
    $outFile{"Cyto"} = "NA";
    $outFile{"Pmid"} = "NA";
    $outFile{"Grif"} = "NA";
    $outFile{"Sumfun"} = "NA";
    $outFile{"Go"} = "NA";
    $outFile{"Nm"} = "NA";
    $outFile{"Np"} = "NA";
    $outFile{"Omim"} = "NA";

    $keep = 0;

    @lines = split("\n");

    foreach $x (@lines) {

	if( $x =~ /^LOCUSID:/ ) {
	    @temp = split/\s+/, $x;
	    if(exists $llhash{$temp[1]}){
	        $outFile{"Affy"} = $llhash{$temp[1]};
	        $outFile{"Locus"} = $temp[1];  
	        $keep = 1;
	    }
	    next;
	}
	if($keep == 1){
	    if( $x =~ /^OFFICIAL_SYMBOL:/ ) {
		@temp = split/\s+/, $x;
		$outFile{"Symbol"} = $temp[1];
		next;
	    }
	    if( $x =~ /^PREFERRED_SYMBOL:/ ) {
		@temp = split/\s+/, $x;
		$outFile{"Symbol"} = $temp[1];
		next;
	    }
	    if( $x =~ /^OFFICIAL_GENE_NAME:/ ) {
		@temp = split/:/, $x;
		$outFile{"Name"} = $temp[1];
		next;
	    }
	    if( $x =~ /^PREFERRED_GENE_NAME:/ ) {
		@temp = split/:/, $x;
		$outFile{"Name"} = $temp[1];
		next;
	    }
	    if( $x =~ /^CHR:/ ) {
		@temp = split/\s+/, $x;
		if($outFile{"Chrom"} ne "NA"){
		    $outFile{"Chrom"} = $outFile{"Chrom"} .  
					";" . $temp[1];  
		}else{
		    $outFile{"Chrom"} = $temp[1];
		}
                next;
	    }
	    if( $x =~ /^UNIGENE:/ ) {
		@temp = split/\s+/, $x;
		$outFile{"Unigene"} = $temp[1];
                next;
	    }
	    if( $x =~ /^MAP:/ ){
		@temp = split/\|/, $x;
                @temp = split/\s+/, $temp[0];
		if($outFile{"Cyto"} ne "NA"){
		    $outFile{"Cyto"} = $outFile{"Cyto"} .  
					";" . $temp[1];  
		}else{
		    $outFile{"Cyto"} = $temp[1]; 
		}
		next;
	    }
            if( $x =~ /^PMID:/ ) {
		@temp = split/\s+/, $x;
                $temp[1] =~ s/,/;/g;
		$outFile{"Pmid"} = $temp[1];
                next;
	    }
	    if( $x =~/^GRIF:/){
		@temp = split/\|/, $x;
                @temp = split/\s+/, $temp[0];
		$outFile{"Grif"} = $temp[1];
		next;
	    }
	    if( $x =~ /^SUMFUNC:/ ) {
		@temp = split/:/, $x;
                @temp = split/\|/, $temp[1];
		$outFile{"Sumfun"} = $temp[0];
                next;
	    }
	    if( $x =~ /^GO:/ ) {
		@temp = split/\|/, $x;
		if($outFile{"Go"} ne "NA"){
		    $outFile{"Go"} = $outFile{"Go"} .  
					";" . $temp[3] . "@" . $temp[2];  
		}else{
		    $outFile{"Go"} = $temp[3] . "@" . $temp[2]; 
		}
		next;
	    }
            if( $x =~ /^OMIM:/ ){
                @temp = split/\s+/, $x;
		if($outFile{"Omim"} ne "NA"){
		    $outFile{"Omim"} = $outFile{"Omim"} .  
					";" . $temp[1];  
		}else{
		    $outFile{"Omim"} = $temp[1]; 
		}
		next;
	    }
            if( $x =~ /^NM:/){
                @temp = split/\s+/, $x;
                @temp = split/\|/, $temp[1];
		$outFile{"Nm"} = $temp[0];
		next;
	    }
            if( $x =~ /^NP:/){
                @temp = split/\s+/, $x;
                @temp = split/\|/, $temp[1];
		$outFile{"Np"} = $temp[0];
		next;
            }
	}
    }
    if($keep == 1){
        &writeOut;
    }
}

close DATA;

sub writeOut {
    @affys = split/;/, $outFile{"Affy"};
    foreach $x (@affys){
        $outhash{$x} = $acchash{$x}. "\t". $outFile{"Locus"}. "\t".
        $outFile{"Unigene"}. "\t". $outFile{"Name"}."\t".
        $outFile{"Symbol"}. "\t". $outFile{"Chrom"}. "\t". 
        $outFile{"Cyto"}. "\t". $outFile{"Pmid"}. "\t".
        $outFile{"Grif"}. "\t". $outFile{"Sumfun"}. "\t".
        $outFile{"Go"}. "\t". $outFile{"Omim"}. "\t".
        $outFile{"Nm"}. "\t". $outFile{"Np"};
    }
}

foreach $x (keys %affyhash){
    if (exists $outhash{$x}){
        print OUT $x, "\t", $outhash{$x}, "\n";
    }else{
        print OUT $x, "\t", $affyhash{$x}, "\tNA", "\tNA", "\tNA", "\tNA",
                  "\tNA", "\tNA", "\tNA", "\tNA", "\tNA", "\tNA",
	          "\tNA", "\tNA\n";
    }  
}

#sub writeOut {
#    @affys = split/;/, $outFile{"Affy"};
#    foreach $x (@affys){
#        print OUT $x, "\t",
#        $acchash{$x}, "\t",
#        $outFile{"Locus"}, "\t",
#        $outFile{"Unigene"}, "\t",   
#        $outFile{"Name"},"\t",
#        $outFile{"Symbol"}, "\t",
#        $outFile{"Chrom"}, "\t", 
#        $outFile{"Cyto"}, "\t",
#        $outFile{"Pmid"}, "\t",
#        $outFile{"Grif"}, "\t",
#	$outFile{"Sumfun"}, "\t",
#        $outFile{"Go"}, "\n";
#    }
#}













