<jupyter_start><jupyter_text>__Author__: <NAME>__Email__: <EMAIL>__Date__:3/4/2020<jupyter_code>import cPickle as pickle
import numpy as np
import matplotlib.pylab as plt
import matplotlib
import matplotlib
matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['font.size']=15
matplotlib.rcParams['font.family']='Arial'<jupyter_output><empty_output><jupyter_text>Load contact probability derived from imaging (see Part1)<jupyter_code>im_med,im_fr,im_med_trans,im_med_cis,im_fr_trans,im_fr_cis,nlen=  pickle.load(open(r'mat_contact_IMR90_untreated.pkl','rb'))

#Use the same procedure for the other datasets<jupyter_output><empty_output><jupyter_text>Reorder contact matrix from imaging based on A/B<jupyter_code>#Number of regions/chromosome
lens = [76, 80, 66, 63, 60, 55, 53, 48, 40, 43, 44, 44, 33, 30, 31, 30, 33, 33, 33, 33, 31, 31, 51]
edges = np.cumsum([0]+lens)
#AB identity
AB = 'B,B,A,A,B,B,A,A,A,B,A,A,A,B,A,B,B,A,B,B,B,B,B,B,B,A,B,B,A,A,A,B,B,B,B,B,B,B,B,A,nan,A,A,A,B,A,B,A,B,A,B,A,B,A,A,A,B,B,B,A,A,A,B,B,A,B,B,A,B,B,B,B,B,B,B,A,B,B,A,A,B,B,B,A,A,B,A,B,A,A,B,B,B,A,B,B,A,B,A,B,A,B,B,B,B,B,nan,A,B,A,B,B,A,B,B,A,B,B,B,B,A,B,B,A,B,A,B,B,A,B,B,A,A,A,B,B,A,B,A,A,B,B,A,B,B,B,B,A,A,B,A,B,A,B,B,A,B,B,B,B,A,B,B,A,B,A,A,B,B,A,A,A,B,B,A,B,B,A,A,B,B,B,B,B,A,B,nan,B,A,A,B,A,B,A,B,A,A,A,A,B,B,A,B,B,B,A,B,B,B,B,B,A,A,B,A,B,A,A,B,B,A,A,A,B,B,B,A,B,B,A,A,B,B,B,A,A,B,B,nan,A,A,B,B,B,B,B,B,B,B,B,A,B,B,B,A,B,B,B,B,A,B,A,A,A,B,B,B,A,A,B,B,A,B,B,A,B,B,B,B,B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,B,B,B,A,A,B,B,B,B,B,A,A,A,A,A,B,B,B,A,A,B,nan,B,A,B,B,A,A,A,A,A,A,B,B,B,A,A,A,A,B,B,A,A,A,A,B,B,B,A,A,B,nan,nan,A,A,B,B,B,B,A,B,A,B,A,B,B,B,A,A,B,B,B,A,A,B,A,A,A,A,A,A,B,B,A,B,A,B,A,A,B,B,nan,nan,B,B,B,B,B,B,A,A,A,A,A,A,A,B,B,B,B,B,B,A,B,B,B,B,B,B,B,B,B,B,B,nan,nan,nan,A,A,A,B,B,B,B,B,B,A,B,B,B,B,B,B,A,nan,B,B,nan,nan,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,A,B,B,B,B,B,B,A,A,nan,nan,nan,nan,B,A,A,A,A,A,B,A,A,A,A,A,B,B,A,A,A,A,A,A,A,A,A,A,B,B,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,A,nan,A,A,A,A,A,A,A,A,A,A,A,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B,B'
AB=np.array(AB.split(','))
A,B = AB=='A',AB=='B'

#contact probability derived from imaging
logim = np.log(im_fr[:,:,1])
logim_o = logim.copy()
for ic in range(len(edges)-1):
    logim_o[edges[ic]:edges[ic+1],edges[ic]:edges[ic+1]]=np.nan
logim_o[np.isinf(logim_o)]=np.nan


for il in range(len(edges)-1):
    for jl in range(len(edges)-1):
        logim_o[edges[il]:edges[il+1],edges[jl]:edges[jl+1]]-=np.nanmedian(logim_o[edges[il]:edges[il+1],edges[jl]:edges[jl+1]])

logim_ord = np.concatenate([np.concatenate([logim_o[A,:][:,A],logim_o[B,:][:,A]]),
                np.concatenate([logim_o[A,:][:,B],logim_o[B,:][:,B]])],axis=-1)    

fig = plt.figure(figsize=(15,10))
edgeA = np.sum(A)
plt.plot([edgeA,edgeA],[0,len(logim_ord)],'k')
plt.plot([0,len(logim_ord)],[edgeA,edgeA],'k')
plt.imshow(logim_ord,cmap='seismic',vmax=np.log(2.5),vmin=np.log(0.5))#,vmin=-8.25,vmax=-3.5)
plt.colorbar()<jupyter_output><empty_output><jupyter_text>Organize same data as histogram or box plots<jupyter_code>AA = logim_ord[:edgeA,:edgeA].ravel()
AA = AA[~np.isnan(AA)]
fig = plt.figure()
h1 = plt.hist(AA,bins=np.linspace(-1,1,50),normed=True,alpha=0.7,color='r',label='AA');
BB = logim_ord[edgeA:,edgeA:].ravel()
BB = BB[~np.isnan(BB)]
h2 = plt.hist(BB,bins=np.linspace(-1,1,50),normed=True,alpha=0.75,color='b',label='BB');

AB = logim_ord[edgeA:,:edgeA].ravel()
AB = AB[~np.isnan(AB)]
plt.hist(AB,bins=100,normed=True,alpha=0.75,color='gray',label='BB');

plt.xlim([-1,1])

np.max(h2[0])
1./np.sum(h2[0])
yvals = [0,0.025,0.05,0.075]
plt.yticks(np.array(yvals)*np.sum(h2[0]),np.array(yvals)*100)

xvals = np.round(np.exp(np.linspace(-1,1,4)),1)
plt.xticks(np.log(xvals),xvals)
plt.xlabel("Contact enrichment")
plt.ylabel("Fraction of contacts")
plt.legend()

fig, axes = plt.subplots(nrows=1, ncols=1, figsize=(4,4))
labels = ['AA','AB','BB']
data = [np.exp(AA),np.exp(AB),np.exp(BB)]
bplot = axes.boxplot(data,showfliers=False,patch_artist=True,labels=labels,notch=True)
colors = ['red','grey','blue']
for patch, color in zip(bplot['boxes'],colors):
    patch.set_facecolor(color)
plt.yticks([0.5,1,1.5,2,2.5])
plt.ylabel('Contact enrichment')
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.32587064676616917, 'fraction_numerical': 0.02812858783008037, 'mean_word_length': 13.891737891737892, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '349820', 'license': 'MIT', 'n_tokens_mistral': 3562, 'n_tokens_neox': 3449, 'n_words': 178, 'path': 'combinatorial_tracing/PostAnalysis/.ipynb_checkpoints/Part2_Population-Averaged-Contacts-checkpoint.ipynb', 'repo_name': 'zhengpuas47/Chromatin_Tracing_Analysis', 'size': '5245'}