import os
import json
import tempfile
from indra.java_vm import autoclass, JavaException
import indra.databases.pmc_client as pmc_client
from processor import ReachProcessor


def process_pmc(pmc_id):
    xml_str = pmc_client.get_xml(pmc_id)
    with tempfile.NamedTemporaryFile() as fh:
        fh.write(xml_str)
        fh.flush()
        rp = process_nxml(fh.name)
    return rp
        

def process_nxml(file_name, use_tempdir=False):
    base = os.path.basename(file_name)
    file_id = os.path.splitext(base)[0]
    if use_tempdir:
        tmp_dir = tempfile.mkdtemp()
    else:
        tmp_dir = '.'
    try:
        paper_reader = autoclass('edu.arizona.sista.reach.ReadPaper')
        paper_reader.main([file_name, tmp_dir])
    except JavaException:
        print 'Could not process file %s.' % file_name
        return None
    
    json_file_name = os.path.join(tmp_dir, file_id + '.uaz.events.json')
    return process_json_file(json_file_name)

def process_json_file(file_name):
    try:
        with open(file_name, 'rt') as fh:
            json_str = fh.read()
            return process_json_str(json_str)
    except IOError:
        print 'Could not read file %s.' % file_name


def process_json_str(json_str):
    json_dict = json.loads(json_str)
    rp = ReachProcessor(json_dict)
    rp.get_phosphorylation()
    rp.get_complexes()
    return rp

if __name__ == '__main__':
    rp = process_json_file('PMC0000001.uaz.events.json')

@@ -375,24 +375,278 @@
 turn rp%0A
-        
+%0Adef process_text(txt, use_tempdir=False):%0A    nxml_txt = '%3Carticle%3E%3Cbody%3E%3Csec%3E%3Cp%3E%25s%3C/p%3E%3C/sec%3E%3C/body%3E%3C/article%3E' %25 txt%0A    tmp_file = tempfile.NamedTemporaryFile()%0A    tmp_file.file.write(nxml_txt)%0A    tmp_file.file.flush()%0A    return process_nxml(tmp_file.name)
 %0A%0Adef pr

Add process_text function to reach_api to allow reading pieces of text through the nxml reader