from indra.statements import Influence, Concept, Evidence

pos_rels = ['provide', 'led', 'lead', 'driv', 'support', 'enabl', 'develop']
neg_rels = ['restrict', 'worsen', 'declin', 'limit', 'constrain',
            'decreas', 'hinder', 'deplet', 'reduce', 'hamper']
neu_rels = ['affect']


class SofiaProcessor(object):
    def __init__(self, rows):
        self.statements = []
        header = [cell.value for cell in next(rows)]
        for row in rows:
            stmt = self._process_row(header, [r.value for r in row])
            if stmt:
                self.statements.append(stmt)

    @staticmethod
    def _process_row(header, row):
        row_dict = {h: v for h, v in zip(header, row)}
        subj = row_dict.get('Agent')
        obj = row_dict.get('Patient')
        if not obj or not subj:
            return None
        rel = row_dict.get('Relation')
        if _in_rels(rel, pos_rels):
            pol = 1
        elif _in_rels(rel, neg_rels):
            pol = -1
        elif _in_rels(rel, neu_rels):
            pol = None
        else:
            return None

        subj_concept = Concept(subj, db_refs={'TEXT': subj, 'SOFIA': subj})
        obj_concept = Concept(obj, db_refs={'TEXT': subj, 'SOFIA': subj})
        text = row_dict.get('Sentence')
        annot_keys = ['Relation', 'Event_Type', 'Location', 'Time']
        annots = {k: row_dict.get(k) for k in annot_keys}
        ref = row_dict.get('Source_File')
        ev = Evidence(source_api='sofia', pmid=ref, annotations=annots,
                      text=text)
        stmt = Influence(subj_concept, obj_concept, evidence=[ev])
        stmt.obj_delta['polarity'] = pol
        return stmt


def _in_rels(value, rels):
    for rel in rels:
        if value.startswith(rel):
            return True
    return False

@@ -1,8 +1,25 @@
+import itertools%0A
 from ind
@@ -295,16 +295,52 @@
 'affect'
+, 'impact', 'due', 'caus', 'because'
 %5D%0A%0A%0Aclas
@@ -518,16 +518,17 @@
     stmt
+s
  = self.
@@ -591,16 +591,17 @@
  if stmt
+s
 :%0A      
@@ -630,19 +630,19 @@
 nts.
-app
+ext
 end(stmt
 )%0A%0A 
@@ -637,16 +637,17 @@
 end(stmt
+s
 )%0A%0A    @
@@ -757,20 +757,29 @@
         
-subj
+cause_entries
  = row_d
@@ -791,13 +791,13 @@
 et('
-Agent
+Cause
 ')%0A 
@@ -803,19 +803,30 @@
         
-obj
+effect_entries
  = row_d
@@ -838,73 +838,369 @@
 et('
-Patient')%0A        if not obj or not subj:%0A            return None
+Effect')%0A        # FIXME: Handle cases in which there is a missing cause/effect%0A        if not cause_entries or not effect_entries:%0A            return None%0A        causes = %5Bc.strip() for c in cause_entries.split(',')%5D%0A        effects = %5Be.strip() for e in effect_entries.split(',')%5D%0A%0A        #subj = row_dict.get('Agent')%0A        #obj = row_dict.get('Patient')
 %0A   
@@ -1423,188 +1423,111 @@
     
-else:%0A            return None%0A%0A        subj_concept = Concept(subj, db_refs=%7B'TEXT': subj, 'SOFIA': subj%7D)%0A        obj_concept = Concept(obj, db_refs=%7B'TEXT': subj, 'SOFIA': subj%7D)
+# If we don't recognize this relation, we don't get any statements%0A        else:%0A            return %5B%5D%0A
 %0A   
@@ -1567,24 +1567,25 @@
 e')%0A        
+#
 annot_keys =
@@ -1632,16 +1632,109 @@
 'Time'%5D%0A
+        #annots = %7Bk: row_dict.get(k) for k in annot_keys%7D%0A        annot_keys = %5B'Relation'%5D%0A
         
@@ -1930,16 +1930,383 @@
 t=text)%0A
+%0A        stmts = %5B%5D%0A        for cause, effect in itertools.product(causes, effects):%0A            cause_concept = Concept(cause, db_refs=%7B'TEXT': cause,%0A                                                    'SOFIA': cause%7D)%0A            effect_concept = Concept(effect, db_refs=%7B'TEXT': effect,%0A                                                      'SOFIA': effect%7D)%0A    
         
@@ -2322,20 +2322,21 @@
 fluence(
-subj
+cause
 _concept
@@ -2337,19 +2337,22 @@
 oncept, 
-obj
+effect
 _concept
@@ -2368,16 +2368,20 @@
 e=%5Bev%5D)%0A
+    
         
@@ -2413,16 +2413,48 @@
 %5D = pol%0A
+            stmts.append(stmt)%0A%0A
         
@@ -2464,16 +2464,18 @@
 urn stmt
+s%0A
 %0A%0A%0Adef _
@@ -2534,16 +2534,24 @@
 f value.
+lower().
 startswi

Switch from processing events to relations