from indra.util import _require_python3
import itertools
from indra.sources import biopax
from indra.tools.gene_network import GeneNetwork
import indra.tools.assemble_corpus as ac
from util import prefixed_pkl, based


phosphosite_owl_file = 'sources/Kinase_substrates.owl'


def read_phosphosite_owl(fname=phosphosite_owl_file):
    bp = biopax.process_owl(fname)
    for stmt in bp.statements:
        for ev in stmt.evidence:
            ev.source_api = 'phosphosite'
            ev.epistemics = {'direct': True}
    return bp.statements


def grouped_biopax_query(gene_names, database_filter, block_size=60):
    gene_blocks = [gene_names[i:i+block_size] for i in
                   range(0, len(gene_names), block_size)]
    stmts = []
    # Run pathsfromto between pairs of blocks and pathsbetween
    # within each block. This breaks up a single call with N genes into
    # (N/block_size)*(N/blocksize) calls with block_size genes
    for genes1, genes2 in itertools.product(gene_blocks, repeat=2):
        if genes1 == genes2:
            bp = biopax.process_pc_pathsbetween(genes1,
                                                database_filter=database_filter)
        else:
            bp = biopax.process_pc_pathsfromto(genes1, genes2,
                                               database_filter=database_filter)
        stmts += bp.statements
    # Filter out blacklist
    final_stmts = []
    for stmt in stmts:
        source_ids = [ev.source_id for ev in stmt.evidence]
        if set(source_ids) & set(biopax_blacklist):
            continue
        final_stmts.append(stmt)
    return final_stmts


def build_prior(gene_names):
    """Build a corpus of prior Statements from PC and BEL."""
    gn = GeneNetwork(gene_names, basen)
    bel_stmts = gn.get_bel_stmts(filter=False)
    ac.dump_statements(bel_stmts, prefixed_pkl('bel'))
    database_filter = ['reactome', 'kegg', 'pid']
    biopax_stmts = grouped_biopax_query(gene_names, database_filter)
    ac.dump_statements(biopax_stmts, prefixed_pkl('biopax'))
    phosphosite_stmts = read_phosphosite_owl(phosphosite_owl_file)
    ac.dump_statements(phosphosite_stmts, prefixed_pkl('phosphosite'))


biopax_blacklist = \
    ['http://pathwaycommons.org/pc2/Catalysis_13953a072d6f992f2388d85f9059a475']

@@ -37,25 +37,8 @@
 on3%0A
-import itertools%0A
 from
@@ -192,16 +192,23 @@
 l, based
+, basen
 %0A%0A%0Aphosp
@@ -530,1088 +530,8 @@
 s%0A%0A%0A
-def grouped_biopax_query(gene_names, database_filter, block_size=60):%0A    gene_blocks = %5Bgene_names%5Bi:i+block_size%5D for i in%0A                   range(0, len(gene_names), block_size)%5D%0A    stmts = %5B%5D%0A    # Run pathsfromto between pairs of blocks and pathsbetween%0A    # within each block. This breaks up a single call with N genes into%0A    # (N/block_size)*(N/blocksize) calls with block_size genes%0A    for genes1, genes2 in itertools.product(gene_blocks, repeat=2):%0A        if genes1 == genes2:%0A            bp = biopax.process_pc_pathsbetween(genes1,%0A                                                database_filter=database_filter)%0A        else:%0A            bp = biopax.process_pc_pathsfromto(genes1, genes2,%0A                                               database_filter=database_filter)%0A        stmts += bp.statements%0A    # Filter out blacklist%0A    final_stmts = %5B%5D%0A    for stmt in stmts:%0A        source_ids = %5Bev.source_id for ev in stmt.evidence%5D%0A        if set(source_ids) & set(biopax_blacklist):%0A            continue%0A        final_stmts.append(stmt)%0A    return final_stmts%0A%0A%0A
 def 
@@ -657,16 +657,42 @@
  basen)%0A
+    # Read BEL Statements%0A
     bel_
@@ -781,24 +781,62 @@
 pkl('bel'))%0A
+    # Read Pathway Commons Statements%0A
     database
@@ -897,22 +897,21 @@
  = g
-rouped
+n.get
 _biopax_
 quer
@@ -910,42 +910,329 @@
 pax_
-query(gene_names, database_filter)
+stmts(database_filter=database_filter)%0A    # Eliminate blacklisted interactions%0A    tmp_stmts = %5B%5D%0A    for stmt in biopax_stmts:%0A        source_ids = %5Bev.source_id for ev in stmt.evidence%5D%0A        if set(source_ids) & set(biopax_blacklist):%0A            continue%0A        tmp_stmts.append(stmt)%0A    biopax_stmts = tmp_stmts
 %0A   
@@ -1285,24 +1285,58 @@
 ('biopax'))%0A
+    # Read Phosphosite Statements%0A
     phosphos

Refactor building prior from databases