from __future__ import division,print_function
import time,re,sys,os,ast,collections
import pandas as pd
import pymongo
from pymongo import MongoClient
from bson.son import SON
from bson.codec_options import CodecOptions
import re,csv
from add_sugar_protein_site_class import add_sugar_protein_site_class
from os import listdir

def Is_protein(protein_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset):


    #if arg_head overlaps with a protein entity from database, return True
    for ei in protein_list:
        if (int(ei[1])>=arg_head_offset[0] and int(ei[1])<=arg_head_offset[1]) \
                or (arg_head_offset[0]>=ei[1] and arg_head_offset[0]<=ei[2]):
            return (True,ei[3],ei[0],ei[4])

    #if there is no match, return False
    return (False,None,'','')

def get_protein(protein_list,arg_head_offset):


    #if arg_head overlaps with a protein entity from database, return offset
    for ei in protein_list:
        if (int(ei[1])>=arg_head_offset[0] and int(ei[1])<=arg_head_offset[1]) \
                or (arg_head_offset[0]>=ei[1] and arg_head_offset[0]<=ei[2]):
            return ei[0]



def Is_domain(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset):

    domain_name=['sequon','face','head','tail','fragment','lobe','peptide','faceside','domain','motif','region','terminus','epitope','sequence','arm',\
                 'sequons','faces','heads','tails','fragments','lobes','peptides','facesides','domains','motifs','regions','termini','epitopes','sequences','arms']

    if arg_head.lower() in domain_name:
        return True
    else:
        return False


def Is_site(site_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset):



    #if arg_head overlaps with a site entity from database, return True
    for ei in site_list:
        if (int(ei[1])>=arg_head_offset[0] and int(ei[1])<=arg_head_offset[1]) \
                or (arg_head_offset[0]>=ei[1] and arg_head_offset[0]<=ei[2]):
            return (True,ei[3],ei[0])


    return (False,'','')

def get_site(site_list,arg_head_offset):



    #if arg_head overlaps with a site entity from database, return offset
    for ei in site_list:
        if (int(ei[1])>=arg_head_offset[0] and int(ei[1])<=arg_head_offset[1]) \
                or (arg_head_offset[0]>=ei[1] and arg_head_offset[0]<=ei[2]):
            return ei[0]


def Is_sugar(sugar_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset):


    #if arg_head overlaps with a sugar entity from database, return True
    for ei in sugar_list:
        if (int(ei[1])>=arg_head_offset[0] and int(ei[1])<=arg_head_offset[1]) \
                or (arg_head_offset[0]>=ei[1] and arg_head_offset[0]<=ei[2]):
            return True

    return False

def get_sugar(sugar_list,arg_head_offset):


    #if arg_head overlaps with a sugar entity from database, return offset
    for ei in sugar_list:
        if (int(ei[1])>=arg_head_offset[0] and int(ei[1])<=arg_head_offset[1]) \
                or (arg_head_offset[0]>=ei[1] and arg_head_offset[0]<=ei[2]):
            return ei[0]


def generate_new_row(new_row, old_row, old_row_contenct):
    new_row_content=[]
    for ni in new_row:
        if not old_row_contenct[old_row.index(ni)]:
            new_row_content.append(old_row_contenct[old_row.index(ni)])
        else:
            new_row_content.append(old_row_contenct[old_row.index(ni)].encode('utf-8'))#
    return new_row_content

def contain_digit(string):

    for si in string:
        if si.isdigit():
            return True
    return False
def process_edg_output2(tsv_input,tsv_output,tsv_output2,tsv_output_c_term,entity_obj,pmidList):

    col_new = ["doc_id", "sent_index", "relation_id", "relation", "trigger", "trigger_offset", \
               "arg_num", "arg_base_np","arg_protein","arg_domain","arg_site","arg_sugar",'PSource','SiteSource','NProtein','NID', 'SiteName',"sent_text"]
    protein_name_pattern='(.*)glycosylations?$'

    glyc_pattern='(glyc)'
    com_glyc_pattern=re.compile(glyc_pattern)

    with open(tsv_output_c_term, 'w') as csvfile:
        spamwriter_cterm = csv.writer(csvfile, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)
        #write column names
        spamwriter_cterm.writerow(col_new)
        with open(tsv_output2, 'w') as csvfile:
            spamwriter_o2 = csv.writer(csvfile, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)

            #write column names
            spamwriter_o2.writerow(col_new)
            with open(tsv_output, 'w') as csvfile:
                spamwriter_o = csv.writer(csvfile, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)

                #write column names
                spamwriter_o.writerow(col_new)
                with open(tsv_input) as csvfile:
                    spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')

                    for row in spamreader:
                        col_title=row
                        #print(col_title)
                        break

                    #print(col_title)

                    pmid_index=col_title.index('doc_id')
                    arg_trigger_index=col_title.index('trigger')
                    arg_trigger_offset_index=col_title.index('trigger_offset')
                    arg_head_index=col_title.index('arg_head')
                    arg_head_offset_index=col_title.index('arg_head_offset')
                    arg_base_np_index=col_title.index('arg_base_np')
                    arg_protein_index=col_title.index('arg_protein')
                    arg_domain_index=col_title.index('arg_domain')
                    arg_sugar_index=col_title.index('arg_sugar')
                    arg_site_index=col_title.index('arg_site')
                    arg_base_np_offset_index=col_title.index('arg_base_np_offset')

                    #for relation and arg_n
                    relation_index=col_title.index('relation')
                    arg_num_index=col_title.index('arg_num')
                    sent_index_index=col_title.index('sent_index')
                    relation_id_index=col_title.index('relation_id')

                    #this is for protein source,normallization
                    Source_index=col_title.index('PSource')
                    SiteSource_index=col_title.index('SiteSource')
                    NProtein_index=col_title.index('NProtein')
                    NID_index=col_title.index('NID')
                    SiteName_index=col_title.index('SiteName')


                    #relation dictionary
                    rela_dic_list=collections.defaultdict(list)
                    rela_dic_other={}
                    rela_id_set_contain=set()
                    for row in spamreader:
                        row=[ri.decode('utf-8') for ri in row]
                        rela_dic={}
                        pmid=row[pmid_index]
                        if str(pmid) not in pmidList:
                            continue
                        trigger=row[arg_trigger_index]
                        trigger_offset=row[arg_trigger_offset_index]
                        arg_head=row[arg_head_index]
                        arg_base_np=row[arg_base_np_index]


                        arg_head_offset=row[arg_head_offset_index]
                        arg_base_np_offset=row[arg_base_np_offset_index]

                        #parse the offset to get the real offset tuple
                        trigger_offset_split=trigger_offset.split(':')
                        trigger_offset=(int(trigger_offset_split[0]),int(trigger_offset_split[1]))

                        arg_head_offset_split=arg_head_offset.split(':')
                        arg_head_offset=(int(arg_head_offset_split[0]),int(arg_head_offset_split[1]))

                        arg_base_np_offset_split=arg_base_np_offset.split(':')
                        arg_base_np_offset=(int(arg_base_np_offset_split[0]),int(arg_base_np_offset_split[1]))

                        protein_list,site_list,sugar_list=entity_obj.get_entity_list_from_mongoDB_after(pmid)

                        m_ofin=re.finditer(protein_name_pattern,trigger)

                        count_ofin=[1 for i in m_ofin]
                        count_from=count_ofin
                        #deal with different relations
                        arg1_sign=False
                        arg2_sign=False
                        arg3_sign=False
                        relation=row[relation_index].strip()
                        arg_num=row[arg_num_index].strip()
                        rela_id=str(pmid)+'-'+str(row[sent_index_index])+'-' \
                                +str(row[relation_id_index])+'-'+relation+'-'
                        #case 1: gly
                        if relation =='gly':
                            is_protein_gly=Is_protein(protein_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset)
                            is_site_gly=Is_site(site_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset)
                            if arg_num=='arg1':

                                if is_protein_gly[0]:
                                    row[arg_protein_index]=row[arg_base_np_index]
                                    row[Source_index]=is_protein_gly[1]
                                    row[NProtein_index]=is_protein_gly[2]
                                    row[NID_index]=is_protein_gly[3]
                                    row[SiteSource_index]=is_site_gly[1]
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o.writerow(new_row)
                                    if is_protein_gly[1]!='Fterm':
                                        spamwriter_cterm.writerow(new_row)
                                elif is_site_gly[0] or \
                                        Is_domain(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset):
                                    row[arg_site_index]=row[arg_base_np_index]
                                    row[SiteName_index]=is_site_gly[2]
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o.writerow(new_row)
                                elif Is_sugar(sugar_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset):
                                    row[arg_sugar_index]=row[arg_base_np_index]
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o.writerow(new_row)
                                else:
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o2.writerow(new_row)
                            elif arg_num=='arg2':
                                if is_site_gly[0] or \
                                        Is_domain(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset):
                                    row[arg_site_index]=row[arg_base_np_index]
                                    row[SiteName_index]=is_site_gly[2]
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o.writerow(new_row)
                                else:
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o2.writerow(new_row)
                            elif arg_num=='arg3':
                                if Is_sugar(sugar_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset):
                                    row[arg_sugar_index]=row[arg_base_np_index]
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o.writerow(new_row)
                                else:
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o2.writerow(new_row)
                        #case 2: attach
                        elif relation =='attach':
                            rela_dic_list[rela_id+arg_num].append((arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset))
                            if rela_id+'arg1' in rela_dic_list and rela_id+'arg2' in rela_dic_list and arg_num in ['arg1','arg2']:
                                if arg_num=='arg1':
                                    rela_dic[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                                    arg1_sign=True
                                elif arg_num=='arg2':
                                    rela_dic[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                                    arg2_sign=True

                                arg_1_str=rela_id+'arg1'
                                arg_2_str=rela_id+'arg2'
                                if arg1_sign:
                                    list_arg=rela_dic_list[arg_2_str]
                                elif arg2_sign:
                                    list_arg=rela_dic_list[arg_1_str]
                                for li in  list_arg:
                                    if arg1_sign:
                                        rela_dic[arg_2_str]=li
                                    elif arg2_sign:
                                        rela_dic[arg_1_str]=li
                                    is_protein_attach=Is_protein(protein_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])
                                    is_site_attach_arg1=Is_site(site_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])

                                    if ( is_site_attach_arg1[0] or \
                                            Is_domain(rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3]))\
                                        and Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_site_index]=rela_dic[arg_1_str][2]
                                        row[SiteName_index]=is_site_attach_arg1[2]
                                        row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'

                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)
                                        #print('attach:',new_row)
                                    elif  is_protein_attach[0] and \
                                            Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_protein_index]=rela_dic[arg_1_str][2]
                                        row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'
                                        row[Source_index]=is_protein_attach[1]
                                        row[NProtein_index]=is_protein_attach[2]
                                        row[NID_index]=is_protein_attach[3]

                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)

                                        if is_protein_attach[1]!='Fterm':
                                            spamwriter_cterm.writerow(new_row)
                                        #print('attach:',new_row)
                                    else:
                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o2.writerow(new_row)
                            if rela_id+'arg2' in rela_dic_list and rela_id+'arg3' in rela_dic_list and arg_num in ['arg2','arg3']:
                                if arg_num=='arg2':
                                    rela_dic[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                                    arg2_sign=True
                                elif arg_num=='arg3':
                                    rela_dic[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                                    arg3_sign=True

                                arg_1_str=rela_id+'arg2'
                                arg_2_str=rela_id+'arg3'
                                if arg2_sign:
                                    list_arg=rela_dic_list[arg_2_str]
                                elif arg3_sign:
                                    list_arg=rela_dic_list[arg_1_str]
                                for li in  list_arg:
                                    if arg2_sign:
                                        rela_dic[arg_2_str]=li
                                    elif arg3_sign:
                                        rela_dic[arg_1_str]=li
                                    is_site_attach_arg2=Is_site(site_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3])
                                    if (is_site_attach_arg2[0] or \
                                        Is_domain(rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3])) \
                                            and Is_sugar(sugar_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_site_index]=rela_dic[arg_2_str][2]
                                        row[SiteName_index]=is_site_attach_arg2[2]
                                        row[arg_sugar_index]=rela_dic[arg_1_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'

                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)
                                        #print('attach:',new_row)
                        #case 2: contain
                        elif relation =='contain':

                            rela_dic_list[rela_id+arg_num].append((arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset,tuple(row)))
                            rela_id_set_contain.add(rela_id)


                        #case 3: located
                        elif relation =='located':
                            rela_dic_list[rela_id+arg_num].append((arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset))
                            if rela_id+'arg1' in rela_dic_list and rela_id+'arg2' in rela_dic_list:
                                if arg_num=='arg1':
                                    rela_dic[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                                    arg1_sign=True
                                elif arg_num=='arg2':
                                    rela_dic[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                                    arg2_sign=True

                                arg_1_str=rela_id+'arg1'
                                arg_2_str=rela_id+'arg2'
                                if arg1_sign:
                                    list_arg=rela_dic_list[arg_2_str]
                                elif arg2_sign:
                                    list_arg=rela_dic_list[arg_1_str]
                                for li in  list_arg:
                                    if arg1_sign:
                                        rela_dic[arg_2_str]=li
                                    elif arg2_sign:
                                        rela_dic[arg_1_str]=li

                                    is_protein_located=Is_protein(protein_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])
                                    is_site_located_arg1=Is_site(site_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])
                                    is_site_located_arg2=Is_site(site_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3])
                                    if (is_site_located_arg1[0] or \
                                        Is_domain(rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])) \
                                            and Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_site_index]=rela_dic[arg_1_str][2]
                                        row[SiteName_index]=is_site_located_arg1[2]
                                        row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'

                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)
                                        #print('located:',new_row)
                                    elif is_protein_located[0] and \
                                            Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_protein_index]=rela_dic[arg_1_str][2]
                                        row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'

                                        row[Source_index]=is_protein_located[1]
                                        row[NProtein_index]=is_protein_located[2]
                                        row[NID_index]=is_protein_located[3]

                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)

                                        if is_protein_located[1]!='Fterm':
                                            spamwriter_cterm.writerow(new_row)
                                        #print('located:',new_row)
                                    elif (is_site_located_arg2[0] or \
                                            Is_domain(rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3])) and \
                                            is_protein_located[0]:
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_protein_index]=rela_dic[arg_1_str][2]
                                        row[arg_site_index]=rela_dic[arg_2_str][2]
                                        row[SiteName_index]=is_site_located_arg2[2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='part_of'
                                        row[Source_index]=is_protein_located[1]
                                        row[NProtein_index]=is_protein_located[2]
                                        row[NID_index]=is_protein_located[3]
                                        row[SiteSource_index]=is_site_located_arg2[1]
                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)

                                        if is_protein_located[1]!='Fterm' and is_site_located_arg2[0] \
                                                and (is_site_located_arg2[1]in ['AminoAcid','SpecificSite','SiteSequence']) and \
                                                contain_digit(is_site_located_arg2[2]):
                                            spamwriter_cterm.writerow(new_row)
                                        #print('located:',new_row)
                                    else:
                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o2.writerow(new_row)
                        #case 4: theme
                        elif relation =='theme':
                            rela_dic_other[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                            arg_1_str=rela_id+'arg1'
                            is_protein_theme=Is_protein(protein_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            is_site_theme_trigger=Is_site(site_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            is_site_theme_arg1=Is_site(site_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            if (is_site_theme_trigger[0] or \
                                Is_domain(rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])) and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_site_index]=get_site(site_list,rela_dic_other[arg_1_str][5])
                                row[SiteName_index]=is_site_theme_trigger[2]
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                #print('theme:',new_row)

                            elif is_protein_theme[0] and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][5])
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'
                                row[Source_index]=is_protein_theme[1]
                                row[NProtein_index]=is_protein_theme[2]
                                row[NID_index]=is_protein_theme[3]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_theme[1]!='Fterm':
                                    spamwriter_cterm.writerow(new_row)
                                #print('theme:',new_row)
                            elif (is_site_theme_arg1[0] or \
                                  Is_domain(rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]) ) and \
                                    is_protein_theme[0]:

                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][5])
                                row[arg_site_index]=rela_dic_other[arg_1_str][2]
                                row[SiteName_index]=is_site_theme_arg1[2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='part_of'
                                row[Source_index]=is_protein_theme[1]
                                row[NProtein_index]=is_protein_theme[2]
                                row[NID_index]=is_protein_theme[3]
                                row[SiteSource_index]=is_site_theme_arg1[1]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_theme[1]!='Fterm' and is_site_theme_arg1[0] \
                                        and (is_site_theme_arg1[1]in ['AminoAcid','SpecificSite','SiteSequence']) and \
                                        contain_digit(is_site_theme_arg1[2]):
                                    spamwriter_cterm.writerow(new_row)
                                #print('theme:',new_row)




                        #case 5: from
                        elif relation =='from':
                            rela_dic_other[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)

                            arg_1_str=rela_id+'arg1'

                            is_protein_from_arg1=Is_protein(protein_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            is_protein_from_trigger=Is_protein(protein_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            is_site_from_trigger=Is_site(site_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            is_site_from_arg1=Is_site(site_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            if (is_site_from_trigger[0] or \
                                Is_domain(rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])) and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_site_index]=get_site(site_list,rela_dic_other[arg_1_str][5])
                                row[SiteName_index]=is_site_from_trigger[2]
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                #print('from:',new_row)

                            elif is_protein_from_trigger[0] and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][5])
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'
                                row[Source_index]=is_protein_from_trigger[1]
                                row[NProtein_index]=is_protein_from_trigger[2]
                                row[NID_index]=is_protein_from_trigger[3]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_from_trigger[1]!='Fterm':
                                    spamwriter_cterm.writerow(new_row)
                                #print('from:',new_row)
                            elif Is_sugar(sugar_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]) and \
                                    is_protein_from_arg1[0]:
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][1])
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][4]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'
                                row[Source_index]=is_protein_from_arg1[1]
                                row[NProtein_index]=is_protein_from_arg1[2]
                                row[NID_index]=is_protein_from_arg1[3]

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_from_arg1[1]!='Fterm':
                                    spamwriter_cterm.writerow(new_row)
                                #print('from:',new_row)
                            elif (is_site_from_arg1[0] or \
                                  Is_domain(rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]) ) and \
                                    is_protein_from_trigger[0]:

                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][5])
                                row[arg_site_index]=rela_dic_other[arg_1_str][2]
                                row[SiteName_index]=is_site_from_arg1[2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='part_of'
                                row[Source_index]=is_protein_from_trigger[1]
                                row[NProtein_index]=is_protein_from_trigger[2]
                                row[NID_index]=is_protein_from_trigger[3]
                                row[SiteSource_index]=is_site_from_arg1[1]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_from_trigger[1]!='Fterm' and is_site_from_arg1[0] \
                                        and (is_site_from_arg1[1]in ['AminoAcid','SpecificSite','SiteSequence']) and \
                                        contain_digit(is_site_from_arg1[2]):
                                    spamwriter_cterm.writerow(new_row)
                                #print('from:',new_row)
                            ###Need to work on this !!!!!!
                            elif (is_site_from_arg1[0] or \
                                  Is_domain(rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]) ) and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):

                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_site_index]=rela_dic_other[arg_1_str][2]
                                row[SiteName_index]=is_site_from_arg1[2]
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'
                                row[SiteSource_index]=is_site_from_arg1[1]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_from_trigger[1]!='Fterm' and is_site_from_arg1[0] \
                                        and (is_site_from_arg1[1]in ['AminoAcid','SpecificSite','SiteSequence']) and \
                                        contain_digit(is_site_from_arg1[2]):
                                    spamwriter_cterm.writerow(new_row)
                                #print('from:',new_row)
                            elif (is_site_from_trigger[0] or \
                                  Is_domain(rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]) ) and \
                                    is_protein_from_arg1[0]:

                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][1])
                                row[arg_site_index]=rela_dic_other[arg_1_str][4]
                                row[SiteName_index]=is_site_from_trigger[2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='part_of'
                                row[Source_index]=is_protein_from_arg1[1]
                                row[NProtein_index]=is_protein_from_arg1[2]
                                row[NID_index]=is_protein_from_arg1[3]
                                row[SiteSource_index]=is_site_from_trigger[1]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_from_arg1[1]!='Fterm' and is_site_from_trigger[0] \
                                        and (is_site_from_trigger[1]in ['AminoAcid','SpecificSite','SiteSequence']) and \
                                        contain_digit(is_site_from_trigger[2]):
                                    spamwriter_cterm.writerow(new_row)

                                #print('from:',new_row)
                            elif is_site_from_arg1[0] and \
                                    Is_domain(rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]) :

                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=rela_dic_other[arg_1_str][4]
                                row[arg_site_index]=rela_dic_other[arg_1_str][2]
                                row[SiteName_index]=is_site_from_arg1[2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='part_of'

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                #print('from:',new_row)
                            elif (is_site_from_arg1[0] or \
                                  Is_domain(rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])) and \
                                    sum(count_from)>0:
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_site_index]=get_site(site_list,rela_dic_other[arg_1_str][1])
                                row[SiteName_index]=is_site_from_arg1[2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                #print('from:',new_row)
                        #case 6: with
                        elif relation =='with':
                            rela_dic_other[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                            arg_1_str=rela_id+'arg1'

                            is_protein_with=Is_protein(protein_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            is_site_with_trigger=Is_site(site_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            is_site_with_arg1=Is_site(site_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            if (is_site_with_trigger[0] or \
                                Is_domain(rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])) and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_site_index]=get_site(site_list,rela_dic_other[arg_1_str][5])
                                row[SiteName_index]=is_site_with_trigger[2]
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                #print('with:',new_row)

                            elif is_protein_with[0] and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][5])
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'
                                row[Source_index]=is_protein_with[1]
                                row[NProtein_index]=is_protein_with[2]
                                row[NID_index]=is_protein_with[3]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_with[1]!='Fterm':
                                    spamwriter_cterm.writerow(new_row)

                                #print('with:',new_row)
                            elif (is_site_with_arg1[0] or \
                                  Is_domain(rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]) ) and \
                                    is_protein_with[0]:

                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][5])
                                row[arg_site_index]=rela_dic_other[arg_1_str][2]
                                row[SiteName_index]=is_site_with_arg1[2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='part_of'
                                row[Source_index]=is_protein_with[1]
                                row[NProtein_index]=is_protein_with[2]
                                row[NID_index]=is_protein_with[3]
                                row[SiteSource_index]=is_site_with_arg1[1]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                if is_protein_with[1]!='Fterm' and is_site_with_arg1[0] \
                                        and (is_site_with_arg1[1]in ['AminoAcid','SpecificSite','SiteSequence']) and \
                                        contain_digit(is_site_with_arg1[2]):
                                    spamwriter_cterm.writerow(new_row)

                                #print('with:',new_row)
                            elif is_site_with_arg1[0] and \
                                  Is_domain(rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]) :

                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=rela_dic_other[arg_1_str][4]
                                row[arg_site_index]=rela_dic_other[arg_1_str][2]
                                row[SiteName_index]=is_site_with_arg1[2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='part_of'

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                #print('with:',new_row)
                        #case 6: link
                        elif relation =='link':
                            rela_dic_other[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                            arg_1_str=rela_id+'arg1'

                            is_protein_link=Is_protein(protein_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])


                            if is_protein_link[0] and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][5])
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'
                                row[Source_index]=is_protein_link[1]
                                row[NProtein_index]=is_protein_link[2]
                                row[NID_index]=is_protein_link[3]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_link[1]!='Fterm':
                                    spamwriter_cterm.writerow(new_row)
                                #print('link:',new_row)

                        #case 7: mod
                        elif relation =='mod':
                            rela_dic_list[rela_id+arg_num].append((arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset))
                            if rela_id+'arg1' in rela_dic_list and rela_id+'arg3' in rela_dic_list and arg_num in ['arg1','arg3']:
                                if arg_num=='arg1':
                                    rela_dic[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                                    arg1_sign=True
                                elif arg_num=='arg3':
                                    rela_dic[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                                    arg2_sign=True

                                arg_1_str=rela_id+'arg1'
                                arg_2_str=rela_id+'arg3'
                                if arg1_sign:
                                    list_arg=rela_dic_list[arg_2_str]
                                elif arg2_sign:
                                    list_arg=rela_dic_list[arg_1_str]
                                for li in  list_arg:
                                    if arg1_sign:
                                        rela_dic[arg_2_str]=li
                                    elif arg2_sign:
                                        rela_dic[arg_1_str]=li
                                    is_protein_mod=Is_protein(protein_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])
                                    is_site_mod_arg1=Is_site(site_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])
                                    if (is_site_mod_arg1[0] or \
                                        Is_domain(rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])) \
                                            and Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_site_index]=rela_dic[arg_1_str][2]
                                        row[SiteName_index]=is_site_mod_arg1[2]
                                        row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'

                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)
                                        #print('mod:',new_row)
                                    elif is_protein_mod[0] and \
                                            Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_protein_index]=rela_dic[arg_1_str][2]
                                        row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'
                                        row[Source_index]=is_protein_mod[1]
                                        row[NProtein_index]=is_protein_mod[2]
                                        row[NID_index]=is_protein_mod[3]
                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)
                                        if is_protein_mod[1]!='Fterm':
                                            spamwriter_cterm.writerow(new_row)
                                        #print('mod:',new_row)
                                    else:
                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o2.writerow(new_row)
                        #case 8: ofin
                        elif relation =='ofin':
                            rela_dic_other[rela_id+arg_num]=(arg_head,arg_head_offset,arg_base_np,arg_base_np_offset,trigger,trigger_offset)
                            arg_1_str=rela_id+'arg1'

                            is_protein_ofin=Is_protein(protein_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            is_site_ofin_arg1=Is_site(site_list,rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])
                            if (is_site_ofin_arg1[0] or \
                                Is_domain(rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])) and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_site_index]=get_site(site_list,rela_dic_other[arg_1_str][1])
                                row[SiteName_index]=is_site_ofin_arg1[2]
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][4]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                #print('ofin:',new_row)

                            elif is_protein_ofin[0] and \
                                    Is_sugar(sugar_list,rela_dic_other[arg_1_str][4],rela_dic_other[arg_1_str][5],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3]):
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][1])
                                row[arg_sugar_index]=rela_dic_other[arg_1_str][4]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'
                                row[Source_index]=is_protein_ofin[1]
                                row[NProtein_index]=is_protein_ofin[2]
                                row[NID_index]=is_protein_ofin[3]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_ofin[1]!='Fterm':
                                    spamwriter_cterm.writerow(new_row)
                                #print('ofin:',new_row)
                            elif (is_site_ofin_arg1[0] or \
                                Is_domain(rela_dic_other[arg_1_str][0],rela_dic_other[arg_1_str][1],rela_dic_other[arg_1_str][2],rela_dic_other[arg_1_str][3])) and \
                                    sum(count_ofin)>0:
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_site_index]=get_site(site_list,rela_dic_other[arg_1_str][1])
                                row[SiteName_index]=is_site_ofin_arg1[2]
                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'

                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)
                                #print('ofin:',new_row)

                            elif is_protein_ofin[0] and sum(count_ofin)>0:
                                #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                row[arg_protein_index]=get_protein(protein_list,rela_dic_other[arg_1_str][1])

                                row[arg_base_np_index]=rela_dic_other[arg_1_str][2]
                                row[relation_index]='gly'
                                row[Source_index]=is_protein_ofin[1]
                                row[NProtein_index]=is_protein_ofin[2]
                                row[NID_index]=is_protein_ofin[3]
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                                if is_protein_ofin[1]!='Fterm':
                                    spamwriter_cterm.writerow(new_row)
                                #print('ofin:',new_row)
                        elif relation =='dep':

                            is_site_dep=Is_site(site_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset)
                            raw_doc=entity_obj.get_abstract_text(pmid)
                            #match the glyc text
                            if raw_doc:
                                abstract_text=raw_doc["text"]

                                sentence = raw_doc["sentence"]
                                for senInfo in sentence:

                                    senIndex = senInfo["index"]

                                    if str(row[sent_index_index])==str(senIndex):
                                        sent_start_of_site=int(senInfo["charStart"])
                                        break
                                text_before_site=abstract_text[sent_start_of_site:arg_head_offset[0]+1]

                                sr=com_glyc_pattern.search(text_before_site)

                                if is_site_dep[0] and sr:
                                    row[arg_site_index]=row[arg_base_np_index]
                                    row[SiteName_index]=is_site_dep[2]
                                    row[relation_index]='gly'
                                    new_row=generate_new_row(col_new,col_title,row)
                                    spamwriter_o.writerow(new_row)
                        elif relation =='used':

                            is_site_used=Is_site(site_list,arg_head,arg_head_offset,arg_base_np,arg_base_np_offset)

                            if is_site_used[0]:
                                row[arg_site_index]=row[arg_base_np_index]
                                row[SiteName_index]=is_site_used[2]
                                row[relation_index]='gly'
                                new_row=generate_new_row(col_new,col_title,row)
                                spamwriter_o.writerow(new_row)

                    #for the contain case
                    for rela_id in list(rela_id_set_contain):
                        pmid=rela_id.split('-')[0]
                        protein_list,site_list,sugar_list=entity_obj.get_entity_list_from_mongoDB_after(pmid)
                        row=rela_dic_list
                        arg_1_str=rela_id+'arg1'
                        arg_2_str=rela_id+'arg2'
                        arg_3_str=rela_id+'arg3'
                        if arg_1_str in rela_dic_list:
                            row=rela_dic_list[arg_1_str][0][6]
                        elif arg_2_str in rela_dic_list:
                            row=rela_dic_list[arg_2_str][0][6]
                        elif arg_3_str in rela_dic_list:
                            row=rela_dic_list[arg_3_str][0][6]
                        row=list(row)
                        rela_dic={}
                        if rela_id+'arg1' in rela_dic_list and rela_id+'arg2' in rela_dic_list and rela_id+'arg3' in rela_dic_list:
                            #print('contain3:',rela_id,',',rela_dic_list[arg_1_str],',',rela_dic_list[arg_2_str],',',rela_dic_list[arg_3_str])
                            for arg1_i in rela_dic_list[arg_1_str]:
                                for arg2_i in rela_dic_list[arg_2_str]:
                                    for arg3_i in rela_dic_list[arg_3_str]:
                                        rela_dic[arg_1_str]=arg1_i
                                        rela_dic[arg_2_str]=arg2_i
                                        rela_dic[arg_3_str]=arg3_i
                                        is_protein_contain=Is_protein(protein_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])
                                        is_site_contain_arg3=Is_site(site_list,rela_dic[arg_3_str][0],rela_dic[arg_3_str][1],rela_dic[arg_3_str][2],rela_dic[arg_3_str][3])
                                        if is_protein_contain[0] and \
                                                Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]) and \
                                                is_site_contain_arg3[0]:
                                            #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                            row[arg_protein_index]=rela_dic[arg_1_str][2]
                                            row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                            row[arg_site_index]=rela_dic[arg_3_str][2]
                                            row[SiteName_index]=is_site_contain_arg3[2]
                                            row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                            row[relation_index]='gly'
                                            row[Source_index]=is_protein_contain[1]
                                            row[NProtein_index]=is_protein_contain[2]
                                            row[NID_index]=is_protein_contain[3]
                                            row[SiteSource_index]=is_site_contain_arg3[1]
                                            new_row=generate_new_row(col_new,col_title,row)
                                            spamwriter_o.writerow(new_row)

                                            if is_protein_contain[1]!='Fterm' and is_site_contain_arg3[0] \
                                                    and (is_site_contain_arg3[1]in ['AminoAcid','SpecificSite','SiteSequence']) and \
                                                    contain_digit(is_site_contain_arg3[2]):
                                                spamwriter_cterm.writerow(new_row)
                                            #print('contain3:',new_row)
                                        else:
                                            new_row=generate_new_row(col_new,col_title,row)
                                            spamwriter_o2.writerow(new_row)

                        elif rela_id+'arg1' in rela_dic_list and rela_id+'arg2' in rela_dic_list:


                            for arg1_i in rela_dic_list[arg_1_str]:
                                for arg2_i in rela_dic_list[arg_2_str]:
                                    rela_dic[arg_1_str]=arg1_i
                                    rela_dic[arg_2_str]=arg2_i

                                    is_protein_contain=Is_protein(protein_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])
                                    is_site_contain_arg1=Is_site(site_list,rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])
                                    is_site_contain_arg2=Is_site(site_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3])
                                    if (is_site_contain_arg1[0] or \
                                            Is_domain(rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3])) and \
                                            Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_site_index]=rela_dic[arg_1_str][2]
                                        row[SiteName_index]=is_site_contain_arg1[2]
                                        row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'

                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)
                                        #print('contain2:',new_row)
                                    elif is_protein_contain[0] and \
                                            Is_sugar(sugar_list,rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_protein_index]=rela_dic[arg_1_str][2]
                                        row[arg_sugar_index]=rela_dic[arg_2_str][2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='gly'
                                        row[Source_index]=is_protein_contain[1]
                                        row[NProtein_index]=is_protein_contain[2]
                                        row[NID_index]=is_protein_contain[3]
                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)

                                        if is_protein_contain[1]!='Fterm':
                                            spamwriter_cterm.writerow(new_row)
                                        #print('contain2:',new_row)
                                    elif is_protein_contain[0] and \
                                            (is_site_contain_arg2[0] or \
                                             Is_domain(rela_dic[arg_2_str][0],rela_dic[arg_2_str][1],rela_dic[arg_2_str][2],rela_dic[arg_2_str][3])):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_protein_index]=rela_dic[arg_1_str][2]
                                        row[arg_site_index]=rela_dic[arg_2_str][2]
                                        row[SiteName_index]=is_site_contain_arg2[2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='part_of'
                                        row[Source_index]=is_protein_contain[1]
                                        row[NProtein_index]=is_protein_contain[2]
                                        row[NID_index]=is_protein_contain[3]
                                        row[SiteSource_index]=is_site_contain_arg2[1]
                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)

                                        if is_protein_contain[1]!='Fterm' and is_site_contain_arg2[0] \
                                                and (is_site_contain_arg2[1]in ['AminoAcid','SpecificSite','SiteSequence']) and \
                                                contain_digit(is_site_contain_arg2[2]):
                                            spamwriter_cterm.writerow(new_row)
                                        #print('contain2:',new_row)
                                    elif is_site_contain_arg2[0] and \
                                             Is_domain(rela_dic[arg_1_str][0],rela_dic[arg_1_str][1],rela_dic[arg_1_str][2],rela_dic[arg_1_str][3]):
                                        #how to update the row content: arg_num, arg_head, arg_base_np, etc?
                                        row[arg_protein_index]=rela_dic[arg_1_str][2]
                                        row[arg_site_index]=rela_dic[arg_2_str][2]
                                        row[SiteName_index]=is_site_contain_arg2[2]
                                        row[arg_base_np_index]=rela_dic[arg_1_str][2]+' AND '+rela_dic[arg_2_str][2]
                                        row[relation_index]='part_of'

                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o.writerow(new_row)
                                        #print('contain2:',new_row)
                                    else:
                                        new_row=generate_new_row(col_new,col_title,row)
                                        spamwriter_o2.writerow(new_row)


def propagation_rules(input_file,output_file):


    with open(output_file, 'w') as csvfile:
        spamwriter_o = csv.writer(csvfile, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)


        part_of_dic={}
        with open(input_file) as csvfile:
            spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')

            for row in spamreader:
                col_title=row
                #print(col_title)
                break
            #write column names
            spamwriter_o.writerow(col_title)
            #print(col_title)
            pmid_index=col_title.index('doc_id')

            arg_protein_index=col_title.index('arg_protein')

            arg_site_index=col_title.index('arg_site')

            #for relation and arg_n
            relation_index=col_title.index('relation')
            arg_num_index=col_title.index('arg_num')
            sent_index_index=col_title.index('sent_index')
            relation_id_index=col_title.index('relation_id')

            for row in spamreader:
                pmid=row[pmid_index]


                #deal with different relations
                relation=row[relation_index].strip()

                rela_id=str(pmid)+'-'+str(row[sent_index_index])+'-' \
                        +relation+'-'
                if relation == 'part_of':
                    part_of_dic[rela_id]=(row[arg_site_index],row[arg_protein_index])

        #print(part_of_dic)
        with open(input_file) as csvfile:
            spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')

            for row in spamreader:
                col_title=row

                break

            #print(col_title)
            pmid_index=col_title.index('doc_id')

            arg_protein_index=col_title.index('arg_protein')
            arg_domain_index=col_title.index('arg_domain')
            arg_sugar_index=col_title.index('arg_sugar')
            arg_site_index=col_title.index('arg_site')

            #for relation and arg_n
            relation_index=col_title.index('relation')
            arg_num_index=col_title.index('arg_num')
            sent_index_index=col_title.index('sent_index')
            relation_id_index=col_title.index('relation_id')



            for row in spamreader:
                pmid=row[pmid_index]
                #deal with different relations
                relation=row[relation_index].strip()
                arg_num=row[arg_num_index].strip()
                rela_id=str(pmid)+'-'+str(row[sent_index_index])+'-' \
                        +'part_of-'
                if relation == 'gly' and rela_id in part_of_dic and row[arg_site_index]==part_of_dic[rela_id][0]:
                    row[arg_protein_index]=part_of_dic[rela_id][1]
                    #print(row)
                spamwriter_o.writerow(row)

def merge_rows(input_file,output_file):


    with open(output_file, 'w') as csvfile:
        spamwriter_o = csv.writer(csvfile, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)


        row_dic={}
        merged_row_dic={}
        with open(input_file) as csvfile:
            spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')

            for row in spamreader:
                col_title=row
                #print(col_title)
                break
            #write column names
            spamwriter_o.writerow(col_title)
            #print(col_title)
            pmid_index=col_title.index('doc_id')

            arg_protein_index=col_title.index('arg_protein')

            arg_site_index=col_title.index('arg_site')
            arg_sugar_index=col_title.index('arg_sugar')
            #for relation and arg_n
            relation_index=col_title.index('relation')
            arg_num_index=col_title.index('arg_num')
            sent_index_index=col_title.index('sent_index')
            trigger_offset_index=col_title.index('trigger_offset')

            #this is for protein source,normallization
            Source_index=col_title.index('PSource')
            SiteSource_index=col_title.index('SiteSource')
            NProtein_index=col_title.index('NProtein')
            NID_index=col_title.index('NID')
            SiteName_index=col_title.index('SiteName')

            for row in spamreader:
                row=[ri.decode('utf-8') for ri in row ]
                pmid=row[pmid_index]
                #for test purpose and remove this after test
                #row[-1]=''
                #print('PMID:',pmid)
                #deal with different relations
                relation=row[relation_index].strip()

                rela_id=str(pmid)+'-'+str(row[sent_index_index])+'-' \
                        +relation+'-'
                if rela_id in row_dic:
                    row_dic[rela_id].append(row)
                else:
                    row_dic[rela_id]=[row]

        #output the data
        for ri in row_dic:
            for rii in range(len(row_dic[ri])):
                cur_row=tuple(row_dic[ri][rii])
                #check if they need to merge
                for riii in range(rii,len(row_dic[ri])):
                    if riii==rii:
                        continue
                    loop_row=tuple(row_dic[ri][riii])
                    if loop_row[trigger_offset_index]==cur_row[trigger_offset_index]:

                        protein_cmp=compatible(loop_row[NProtein_index],cur_row[NProtein_index])
                        site_cmp=compatible(loop_row[SiteName_index],cur_row[SiteName_index])
                        sugar_cmp=compatible(loop_row[arg_sugar_index],cur_row[arg_sugar_index])
                        #print('Current row:',row[:-1])
                        #print('Rows in dic:',row_dic[rela_id])
                        #print('Protein_cmp:',protein_cmp,'--','Site_cmp:',site_cmp,'--','sugar_cmp:',sugar_cmp)
                        if protein_cmp[0] and site_cmp[0] and sugar_cmp[0]:
                            merged_row=list(loop_row)
                            merged_row[arg_protein_index]=protein_cmp[1]
                            merged_row[arg_site_index]=site_cmp[1]
                            merged_row[arg_sugar_index]=sugar_cmp[1]

                            #update the important information
                            merged_row[Source_index]=compatible(merged_row[Source_index],cur_row[Source_index])[1]
                            merged_row[SiteSource_index]=compatible(merged_row[SiteSource_index],cur_row[SiteSource_index])[1]
                            merged_row[NProtein_index]=compatible(merged_row[NProtein_index],cur_row[NProtein_index])[1]
                            merged_row[NID_index]=compatible(merged_row[NID_index],cur_row[NID_index])[1]
                            merged_row[SiteName_index]=compatible(merged_row[SiteName_index],cur_row[SiteName_index])[1]
                            if ri in merged_row_dic:
                                merged_row_dic[ri].append(merged_row)
                            else:
                                merged_row_dic[ri]=[merged_row]
        for ri in row_dic:
            for rii in row_dic[ri]:
                already_merged=False
                #skip the merged rows:
                if ri in merged_row_dic:
                    for rmi in merged_row_dic[ri]:
                        if rmi[trigger_offset_index]==rii[trigger_offset_index]:

                            protein_cmp=compatible(rmi[NProtein_index],rii[NProtein_index])
                            site_cmp=compatible(rmi[SiteName_index],rii[SiteName_index])
                            sugar_cmp=compatible(rmi[arg_sugar_index],rii[arg_sugar_index])
                            #print('Current row:',row[:-1])
                            #print('Rows in dic:',row_dic[rela_id])
                            #print('Protein_cmp:',protein_cmp,'--','Site_cmp:',site_cmp,'--','sugar_cmp:',sugar_cmp)
                            if protein_cmp[0] and site_cmp[0] and sugar_cmp[0]:
                                already_merged=True
                                break
                if already_merged:
                    continue
                rii=[rri.encode('utf-8') for rri in rii]
                spamwriter_o.writerow(rii)
            if ri in merged_row_dic:
                for rii in merged_row_dic[ri]:
                    rii=[rri.encode('utf-8') for rri in rii]
                    spamwriter_o.writerow(rii)


def compatible(e1,e2):

    if e1 and e2 and e1==e2:
        return (True, e1)
    elif e1 and e2 and e1!=e2:
        return (False,'')
    elif e1 and not e2:
        return (True,e1)
    elif e2 and not e1:
        return (True,e2)
    elif not e1 and not e2:
        return (True,'')

def extrac_certain_columns_from_postprocessed_file(input_file,output_file):
    col_new = ["doc_id", "sent_index","trigger","PSource", "NProtein",'NID','SiteName']
    with open(output_file, 'w') as csvfile:
        spamwriter_o = csv.writer(csvfile, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)

        #write column names
        spamwriter_o.writerow(col_new)

        with open(input_file) as csvfile:
            spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
            for row in spamreader:
                col_title=row
                relation_index=col_title.index('relation')
                break

            for row in spamreader:
                row=[ri.decode('utf-8') for ri in row ]
                if row[relation_index]=='part_of':
                    continue
                #row=[str(ri, 'utf-8', 'ignore') for ri in row ]
                new_row=generate_new_row(col_new, col_title, row)
                #new_row=[ri.encode('utf-8') for ri in new_row ]
                spamwriter_o.writerow(new_row)




def add_columns(file_input,added_file,columns_list):
    with open(added_file, 'w') as csvfile:
        spamwriter_o = csv.writer(csvfile, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)



        with open(file_input) as csvfile:
            spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')

            for row in spamreader:

                col_title=row
                #print(col_title)
                break

            #write column names
            col_title=col_title+columns_list
            spamwriter_o.writerow(col_title)
            for row in spamreader:

                #row=[ri.decode('utf-8') for ri in row ]
                row=row+['']*len(columns_list)
                spamwriter_o.writerow(row)


def substract_file(file1,file2,file3):

    with open(file3, 'w') as csvfile:
        spamwriter_o = csv.writer(csvfile, delimiter='\t', quotechar='|', quoting=csv.QUOTE_MINIMAL)


        row_list1=[]
        row_list2=[]
        with open(file1) as csvfile:
            spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')

            for row in spamreader:

                col_title=row
                #print(col_title)
                break
            for row in spamreader:
                #row=[ri.decode('utf-8') for ri in row ]
                row_list1.append(row)
        with open(file2) as csvfile:
            spamreader = csv.reader(csvfile, delimiter='\t', quotechar='|')

            for row in spamreader:
                col_title=row
                #print(col_title)
                break
            for row in spamreader:
                #row=[ri.decode('utf-8') for ri in row ]
                row_list2.append(row)
        #write column names
        spamwriter_o.writerow(col_title)
        for ri in row_list1:
            if ri not in row_list2:
                #ri=[rri.encode('utf-8') for rri in ri ]
                spamwriter_o.writerow(ri)

if __name__ == '__main__':
    #test

    pmidFile = sys.argv[1]
    dbF = sys.argv[2]
    dbT = sys.argv[3]
    colF = sys.argv[4]
    colT = sys.argv[5]
    textT = sys.argv[6]
    edg = sys.argv[7]

    ASPSC=add_sugar_protein_site_class(dbF,dbT,colF,colT,textT,textT)
    tsv_input=edg

    pmidList = pd.read_csv(pmidFile,header=None).iloc[:,0].tolist()
    pmidList=[str(i) for i in pmidList]

    tsv_input_split=tsv_input.split('.')
    tsv_output=tsv_input_split[0]+'_postprocessing_before_merge.tsv'
    tsv_output2=tsv_input_split[0]+'_filtered_out_before_merge.tsv'
    tsv_output_cterm=tsv_input_split[0]+'_cterm_before_merge.tsv'
    tsv_centain_colums=tsv_input_split[0]+'_output.tsv'

    tsv_input_new=tsv_input_split[0]+'_new.tsv'
    #add some columns on the files
    add_columns(tsv_input,tsv_input_new,['PSource','NProtein','NID','SiteSource','SiteName'])
    process_edg_output2(tsv_input_new,tsv_output,tsv_output2,tsv_output_cterm,ASPSC,pmidList)
    #extrac_certain_columns_from_postprocessed_file(tsv_output,tsv_centain_colums)
    #tsv_output_new='glyco_postprocessing_new.tsv'
    #propagation_rules(tsv_output,tsv_output_new)

    output_file=tsv_input_split[0]+'_postprocessing.tsv'
    output_file2=tsv_input_split[0]+'_filtered_out.tsv'
    output_file_cterm=tsv_input_split[0]+'_cterm.tsv'
    output_file_other=tsv_input_split[0]+'_other.tsv'
    final_file=tsv_input_split[0]+'_final.tsv'
    
    merge_rows(tsv_output,output_file)
    merge_rows(tsv_output2,output_file2)
    merge_rows(tsv_output_cterm,output_file_cterm)
    substract_file(output_file,output_file_cterm,output_file_other)
    
    
    extrac_certain_columns_from_postprocessed_file(output_file,final_file)
    
    '''
    #final step
    tsv_input_split=['unicarb']
    output_file_cterm=tsv_input_split[0]+'_cterm.tsv'
    output_file_other=tsv_input_split[0]+'_other.tsv'
    final_file=tsv_input_split[0]+'_final.tsv'
    extrac_certain_columns_from_postprocessed_file(output_file_cterm,final_file)

    '''