# Path: grit/config.py
# MAX_NUM_TRANSCRIPTS_TO_QUANTIFY = 1000
# MAX_NUM_CANDIDATE_TRANSCRIPTS = 50000
# CB_SIG_LEVEL = 0.025
# VERBOSE = None
# DEBUG_VERBOSE = None
# WRITE_DEBUG_DATA = None
# FIX_CHRM_NAMES_FOR_UCSC = None
# ONLY_BUILD_CANDIDATE_TRANSCRIPTS = None
# NTHREADS = None
# TOTAL_MAPPED_READS = None
# ESTIMATE_UPPER_CONFIDENCE_BOUNDS = True
# ESTIMATE_LOWER_CONFIDENCE_BOUNDS = True
# ELEMENT_FILTER_FRAC = 0.05
# MIN_INTRON_FLANKING_SIZE = 12 
# MIN_ENTROPY = 0
# MIN_INTRON_SIZE = 40
# MAX_EMPTY_REGION_SIZE = 200 #MIN_INTRON_SIZE
# MAX_INTRON_SIZE = int(1e6)
# MIN_GENE_LENGTH = 400
# MAX_GENE_EXPANSION = 1000
# NOISE_JN_FILTER_FRAC = 0.01
# MAX_JN_OFFSET_FILTER = 15
# MAX_FRAGMENT_LENGTH = 1000
# MIN_FRAGMENT_LENGTH = 75
# ONLY_USE_REFERENCE_JUNCTIONS = False
# MIN_EXON_FPKM = 0.1
# MAX_EXPRESSION_RATIO = 20
# BUILD_MODELS_WITH_RETAINED_INTRONS = False
# MIN_NUM_CAGE_TAGS = 5
# NUM_TSS_BASES_TO_SKIP = 200
# MIN_NUM_POLYA_TAGS = 2
# NUM_TES_BASES_TO_SKIP = 300
# TES_EXON_MERGE_DISTANCE = 0
# TSS_EXON_MERGE_DISTANCE = 0
# MAX_DISTAL_SIZE_FOR_MATCH_OFFSET = 100
# def get_gene_tmp_fname(gene_id, sample_type=None, rep_id=None):
# def get_fmat_tmp_fname(gene_id, sample_type=None, rep_id=None):
# def log_statement(*args, **kwargs):


import sys
import numpy
import time
import re
import itertools
import multiprocessing
from itertools import product, izip
from collections import defaultdict, namedtuple
from scipy.stats import beta, binom
try: from reads import get_strand, get_contigs_and_lens
from grit import config
        from multiprocessing import Process, Manager
        rv.append((jn, int(cnts.sum()), entropy))
    
    return rv

def extract_junctions_in_contig( reads, chrm, strand ):
    return extract_junctions_in_region( 
        reads, chrm, strand, start=None, end=None )

def load_junctions_worker(all_jns, all_jns_lock, 
                          segments_queue, segments_queue_lock, reads):
    jns = defaultdict(list)
    while len(segments_queue) > 0:
        with segments_queue_lock:
            if len(segments_queue) == 0: break
            chrm, strand, start, stop = segments_queue.pop()
        if config.VERBOSE: 
            config.log_statement("Finding jns in '%s:%s:%i:%i'" % 
                          (chrm, strand, start, stop))
        jns[(chrm, strand)].extend(
            extract_junctions_in_region(
                reads, chrm, strand, start, stop, True))
    
    # finally, block until we can offload the remaining junctions
    with all_jns_lock:
        for key, region_jns in jns.iteritems():
            if key not in all_jns: all_jns_key = []
            else: all_jns_key = all_jns[key]
            all_jns_key.extend( region_jns )
            all_jns[key] = all_jns_key
    del jns

# Path: grit/config.py
# MAX_NUM_TRANSCRIPTS_TO_QUANTIFY = 1000
# MAX_NUM_CANDIDATE_TRANSCRIPTS = 50000
# CB_SIG_LEVEL = 0.025
# VERBOSE = None
# DEBUG_VERBOSE = None
# WRITE_DEBUG_DATA = None
# FIX_CHRM_NAMES_FOR_UCSC = None
# ONLY_BUILD_CANDIDATE_TRANSCRIPTS = None
# NTHREADS = None
# TOTAL_MAPPED_READS = None
# ESTIMATE_UPPER_CONFIDENCE_BOUNDS = True
# ESTIMATE_LOWER_CONFIDENCE_BOUNDS = True
# ELEMENT_FILTER_FRAC = 0.05
# MIN_INTRON_FLANKING_SIZE = 12 
# MIN_ENTROPY = 0
# MIN_INTRON_SIZE = 40
# MAX_EMPTY_REGION_SIZE = 200 #MIN_INTRON_SIZE
# MAX_INTRON_SIZE = int(1e6)
# MIN_GENE_LENGTH = 400
# MAX_GENE_EXPANSION = 1000
# NOISE_JN_FILTER_FRAC = 0.01
# MAX_JN_OFFSET_FILTER = 15
# MAX_FRAGMENT_LENGTH = 1000
# MIN_FRAGMENT_LENGTH = 75
# ONLY_USE_REFERENCE_JUNCTIONS = False
# MIN_EXON_FPKM = 0.1
# MAX_EXPRESSION_RATIO = 20
# BUILD_MODELS_WITH_RETAINED_INTRONS = False
# MIN_NUM_CAGE_TAGS = 5
# NUM_TSS_BASES_TO_SKIP = 200
# MIN_NUM_POLYA_TAGS = 2
# NUM_TES_BASES_TO_SKIP = 300
# TES_EXON_MERGE_DISTANCE = 0
# TSS_EXON_MERGE_DISTANCE = 0
# MAX_DISTAL_SIZE_FOR_MATCH_OFFSET = 100
# def get_gene_tmp_fname(gene_id, sample_type=None, rep_id=None):
# def get_fmat_tmp_fname(gene_id, sample_type=None, rep_id=None):
# def log_statement(*args, **kwargs):


# Path: grit/files/junctions.py
import sys
import numpy
import time
import re
import itertools
import multiprocessing
from itertools import product, izip
from collections import defaultdict, namedtuple
from scipy.stats import beta, binom
try: from reads import get_strand, get_contigs_and_lens
from grit import config
        from multiprocessing import Process, Manager

        rv.append((jn, int(cnts.sum()), entropy))
    
    return rv

def extract_junctions_in_contig( reads, chrm, strand ):
    return extract_junctions_in_region( 
        reads, chrm, strand, start=None, end=None )

def load_junctions_worker(all_jns, all_jns_lock, 
                          segments_queue, segments_queue_lock, reads):
    jns = defaultdict(list)
    while len(segments_queue) > 0:
        with segments_queue_lock:
            if len(segments_queue) == 0: break
            chrm, strand, start, stop = segments_queue.pop()
        if config.VERBOSE: 
            config.log_statement("Finding jns in '%s:%s:%i:%i'" % 
                          (chrm, strand, start, stop))
        jns[(chrm, strand)].extend(
            extract_junctions_in_region(
                reads, chrm, strand, start, stop, True))
    
    # finally, block until we can offload the remaining junctions
    with all_jns_lock:
        for key, region_jns in jns.iteritems():
            if key not in all_jns: all_jns_key = []
            else: all_jns_key = all_jns[key]
            all_jns_key.extend( region_jns )
            all_jns[key] = all_jns_key
    del jns

    if config.VERBOSE: config.log_statement( "" )