## PDDI_NER_XMLparse.py
#
# An XML parser for the XML generated by the PK DDI NER program (same
# as Bioportal Annotator output). This extracts the annotated concepts
# with their location in the corresponding text and then writes the
# data to JSON. 

# Author: Richard D Boyce and Peter Randall
#
# 
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Library General Public
## License as published by the Free Software Foundation; either
## version 2 of the License, or (at your option) any later version.

## This library is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
## Library General Public License for more details.

## You should have received a copy of the GNU Library General Public
## License along with this library; if not, write to the
## Free Software Foundation, Inc., 59 Temple Place - Suite 330,
## Boston, MA 02111-1307, USA.


from lxml import etree
import itertools
import json
import sys
import os.path
import csv
from os import listdir
from os.path import isfile, join


def findAnnotsDDI(treepath):
    unique_annots = []
    beans = treepath.xpath('//annotationBean')

    for bean in beans:
        if len(bean.xpath('concept/fullId'))>0:
            fd = bean.xpath('concept/fullId')[0].text.strip()
            nm = bean.xpath('concept/preferredName')[0].text.strip()
            orig = bean.xpath('context/term/name')[0].text.strip()
            to = bean.xpath('context/to')[0].text.strip()
            fro = bean.xpath('context/from')[0].text.strip()
            annot = [orig, nm, fd]
            if annot not in unique_annots:
            	unique_annots.append(annot)
        else:
            continue

    return unique_annots


INpath = str(os.getcwd()) + '/from_NER/'
print INpath

OUTpath = 'unique_annots/'

NER_files = [f for f in listdir(INpath) if isfile(join(INpath, f))]
print NER_files

for f in NER_files:
	file_name = str(os.getcwd()) + '/from_NER/' + f
	print file_name
	tree = etree.parse(file_name)
	print tree
	annots = findAnnotsDDI(tree)
	print annots
	
	with open(OUTpath + f + '.txt', 'wb') as csvfile:
		writer = csv.writer(csvfile, delimiter='\t')
		for row in annots:
			writer.writerow(row)
