## a converter set of methods for obtaining  normal inputs
import rdflib
from ...term_parsers import *

def convert_mapping_to_rdf(input_mapping_file,extract_subnode_info=True,split_node_by=":",keep_index=1,layer_type="Protein",annotation_mapping_file="test.gaf"):
    
    ## generate input examples based on community assignment
    g = rdflib.graph.Graph()
    KT = rdflib.Namespace('http://kt.ijs.si/hedwig#')
    amp_uri = 'http://kt.ijs.si/ontology/hedwig#'
    obo_uri = "http://purl.obolibrary.org/obo/"
    AMP = rdflib.Namespace(amp_uri)
    
    ## include neighbors as instances or not..
    mapping_file = {}
    if extract_subnode_info:
        for k,v in input_mapping_file.items():
            node,layer = k
            if layer_type != False:
                if layer == layer_type:
                    mapping_file[node.split(split_node_by)[keep_index]] = v
    else:
        for k,v in input_mapping.items():
            node,layer = k
            if layer_type != False:
                if layer == layer_type:
                     mapping_file[node] = v

    id_identifier = 0
    if ".gaf" in annotation_mapping_file:
        uniGO = parse_gaf_file(annotation_mapping_file)
        
    else:
        print("Please, provide gaf-based item-term mappings")
    
    ## iterate through community assignments and construct the trainset
    ## tukaj morda dodaj example name    
    for node, com in mapping_file.items():
        try:
            id_identifier += 1        
            u = rdflib.term.URIRef('%sexample#%s%s' % (amp_uri, node,str(id_identifier)))
            g.add((u, rdflib.RDF.type, KT.Example))
            g.add((u, KT.class_label, rdflib.Literal(str(com)+"_partition")))
            for goterm in uniGO[node]:
                if "GO:" in goterm:
                    annotation_uri = rdflib.term.URIRef('%s%s' % (obo_uri, rdflib.Literal(goterm)))
                    blank = rdflib.BNode()
                    g.add((u, KT.annotated_with, blank))
                    g.add((blank, KT.annotation, annotation_uri))
                    

        except Exception as err:
            print(err)
            ## incorrect mappings are ignored..
            pass

    return g
