#!/usr/bin/python3
'''
Remaps id's in cluster/fasta/pairwise files.
Pipeline usage: POSTPROC:REMAP
'''

import os
import argparse
import sys

def print_error(s):
    print(('ERROR   : %s')%s,file=sys.stderr)

def read_fdesc( fdesc ):
    result = {}
    with open(fdesc,'r') as fh:
        for line in fh:
            line = line.strip().rstrip('\n')
            if len(line)==0: continue
            if line[0]=='#': continue
            elems = line.split('\t')
            if len(elems) != 8:
                return None
            result[elems[0]] = '\t'.join(elems[1:])
    return result

def process( options ):
    cldesc = read_fdesc( options.fdesc )
    
    with open(options.fhits,'r') as fh:
        print('\t'.join(['#query', 'ODB_OG', 'evalue', 'score', 'COG_category', 'Description', 'GOs_mf', 'GOs_bp', 'EC', 'KEGG_ko', 'Interpro' ]))
        for line in fh:
            line = line.strip().rstrip('\n')
            if len(line)==0: continue
            if line[0]=='#': continue
            elems = line.split('\t')
            if len(elems) < 8:
                return None
            clid = elems[0]
            query = elems[1]
            pid = elems[6]
            score = elems[7]
            evalue = elems[8]
            annotation = ''
            if clid in cldesc:
                annotation = cldesc[clid]
            else:
                nodata = [ '-' , 'NO ANNOTATION', '-' , '-' , '-' , '-', '-' ]
                annotation = '\t'.join(nodata)
            
            print('\t'.join([query,clid,evalue,score,annotation]))

#-----------------------------------------------------------------
# MAIN
if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Prepare fasta file')

    parser.add_argument('-u', '--fhits',    dest='fhits',    help='hits file',                  metavar='FILE', required=True)
    parser.add_argument('-d', '--fdesc',    dest='fdesc',    help='desc file (annotations)',    metavar='FILE', required=True)

    options = parser.parse_args()
    process( options )



# OrthoDB format
# 0               1       2       3       4               5       6       7       8     
# cluster_id      gene_id type    n       ali_start       ali_end pid     score   evalue  cog     annotation      mf_go   bp_go   ec      kegg    interpro
# 77at1489911     XXXX:0014b0     0       1       1       176     100     244.32  4.03e-110       -       F-box only protein 2-like       -       -       -       -       IPR007397,IPR008979,IPR039752


# EGGNog format
#query	seed_ortholog	evalue	score	eggNOG_OGs	max_annot_lvl	COG_category	Description	Preferred_name	GOs	EC	KEGG_ko	KEGG_Pathway	KEGG_Module	KEGG_ReactionKEGG_rclass	BRITE	KEGG_TC	CAZy	BiGG_Reaction	PFAMs
#AGINM00000000001	13037.EHJ69495	0.0	1679.0	KOG1172@1|root,KOG1172@2759|Eukaryota,38BCI@33154|Opisthokonta,3BA46@33208|Metazoa,3CU8U@33213|Bilateria,41X4S@6656|Arthropoda,3SG8P@50557|Insecta,442GG@7088|Lepidoptera	50557|Insecta	P	Band 3 cytoplasmic domain	-	GO:0003674,GO:0005215,GO:0005575,GO:0005623,GO:0005886,GO:0005887,GO:0006810,GO:0006811,GO:0006812,GO:0006814,GO:0006820,GO:0006873,GO:0006885,GO:0008150,GO:0008324,GO:0008509,GO:0008510,GO:0008514,GO:0009987,GO:0015075,GO:0015077,GO:0015081,GO:0015106,GO:0015291,GO:0015293,GO:0015294,GO:0015318,GO:0015370,GO:0015672,GO:0015701,GO:0015711,GO:0016020,GO:0016021,GO:0019725,GO:0022804,GO:0022857,GO:0022890,GO:0030001,GO:0030003,GO:0030004,GO:0030641,GO:0031224,GO:0031226,GO:0034220,GO:0035725,GO:0042592,GO:0044425,GO:0044459,GO:0044464,GO:0046873,GO:0048878,GO:0050801,GO:0051179,GO:0051234,GO:0051453,GO:0055067,GO:0055080,GO:0055082,GO:0055085,GO:0065007,GO:0065008,GO:0071702,GO:0071944,GO:0098655,GO:0098656,GO:0098660,GO:0098662,GO:0098771	-	ko:K13855	ko04970,ko04971,ko04972,ko04976,map04970,map04971,map04972,map04976	--	-	ko00000,ko00001,ko02000	2.A.31.1.2	-	-	Band_3_cyto,HCO3_cotransp
