import sys
sys.path.append('/home/wireland/')
sys.path.append('/home/wireland/sortseq/utils/')
import scipy as sp
import pymc
import scipy.ndimage
import glob
import MCMC_utils_mscs
import MCMC_utils
import ConfigParser
import glob
import mscscfg
import MCMC_utils
import os
import numpy as np
import csv

import readuniqueseqssingleend



numbins = 4
config = ConfigParser.RawConfigParser()
# everything that needs to be changed goes here ########

config.read('/home/wireland/mscS4-8-15/data/oldmscL.cfg') #location of config file, change this to run on different datasets
mut_region_start = config.getint('Input','mut_region_start') #not base pair number, distance from start of mut region
mut_region_length = config.getint('Input','mut_region_length')
data_fn = config.get('Input','data_fn')
seq_start = config.getint('Input','seq_start')
seq_end = config.getint('Input','seq_end')
#data_fnbase = config.get('Input','data_fnbase')
#expname = config.get('Input','expname') #ex MscS mut1, describes the experiment without the different batch numbers.
#fnnames = glob.glob(data_fnbase + expname + '*.fasta')
#fnnames.sort()

#this section determines from the barcode file where the mutated region starts for this oligo
'''
barcodefn = config.get('Input','barcodefn')

barcode_dict = {}
reverse_dict = {}
csvfile = open(barcodefn,'r')
reader = csv.DictReader(csvfile)
for row in reader:
    barcode_dict[row['experiment_name']] = row['fwd_barcode']
    reverse_dict[row['experiment_name']] = row['rev_barcode']
'''

seq_mat_temp,batch_vec_temp = MCMC_utils.load_unique_seqs_batches(data_fn,seq_start + mut_region_start,mut_region_length)


emat_0 = MCMC_utils.fix_matrix_gauge(sp.randn(4,mut_region_length))




# shuffle the elements of seq_mat and batch_vec. This will prevent
# spuriously high mutual information values

index_shuf = range(len(batch_vec_temp))
sp.random.shuffle(index_shuf)
seq_mat = sp.zeros([4,len(seq_mat_temp[0,:,0]),len(seq_mat_temp[0,0,:])],dtype = 'int')
batch_vec = sp.zeros_like(batch_vec_temp)
for i, i_s in enumerate(index_shuf):
    seq_mat[:,:,i] = seq_mat_temp[:,:,i_s]
    batch_vec[i] = batch_vec_temp[i_s]

###############################################################################
############### Stochastics go here ###########################################
###############################################################################
@pymc.stochastic(observed=True,dtype=int)
def sequences(value=seq_mat):
    return 0

@pymc.stochastic(observed=True,dtype=int)
def batches(value=batch_vec):
    return 0

@pymc.stochastic(dtype=float)
def emat(s=sequences,b=batches,value=emat_0):
    n_seqs = len(b)
    MI, f_reg = MCMC_utils_mscs.compute_MI(s,b,value)
    return n_seqs*MI
