def add_data(cfg,data,_label,edge_filter=None,args=None):
    N2U = 'N2U'
    JSH = 'JSH'
    
    left = aux.read.into_list(cfg['mat']['left_nodes'])
    right = aux.read.into_list(cfg['mat']['right_nodes'])
    lrmap = aux.read.into_lr_dict(cfg['mat']['lrmap']) 
    #_remove = ['VC01','VD01','VB01','VB02','HSNL','HSNR','PVNL','PVNR']
    _remove = ['VC01','VD01','VB01','VB02','HSNL','HSNR','PVNL','PVNR','PLNL','PLNR','PVR','PVR.']

    label = ['Adult L/R'] + _label   
    n2u = from_db(N2U,adjacency=True,remove=_remove)
    if edge_filter: edge_filter(n2u.A,args)
    reflected = n2u.A.map_vertex_names(lrmap)
    add_neighborhood_similarity(data,label,n2u.A,reflected,left,right)
    add_overlap_similarity(data,label,n2u.A,left + right)
    
    label = ['L4 L/R'] + _label   
    jsh = from_db(JSH,adjacency=True,remove=_remove)
    if edge_filter: edge_filter(jsh.A,args)
    reflected = jsh.A.map_vertex_names(lrmap)
    add_neighborhood_similarity(data,label,jsh.A,reflected,left,right)
    add_overlap_similarity(data,label,jsh.A,left + right)
    
    label = ['Adult/L4'] + _label
    vertices = sorted((set(n2u.neurons)&set(jsh.neurons))-set(_remove))
    add_neighborhood_similarity(data,label,n2u.A,jsh.A,left,right)
    add_overlap_similarity(data,label,n2u.A,vertices)
    add_overlap_similarity(data,label,jsh.A,vertices)
def run(fout=None,source_data=None):
    N2U = 'N2U'
    JSH = 'JSH'
    _lrd = aux.read.into_dict(lr_pairs)
    lrd = {}
    for key,val in _lrd.items():
        lrd[key] = val
        lrd[val] = key
        
    _remove = ['VC01','VD01','VB01','VB02','HSNL','HSNR','PVNL','PVNR']
    n2u = from_db(N2U,adjacency=True,remove=_remove)
    rn2u = n2u.A.map_vertex_names(lrd)
    ntdbound = get_td_bounds(n2u.A.es['weight'])
    _edges,lntd,rntd = get_corresponding_edge_attr(n2u.A,rn2u)
    _edges,lntd,rntd = filter_corresponding_tds(_edges,lntd,rntd,[ntdbound,ntdbound])
    if source_data:
        fsplit = source_data.split('.')
        dout = fsplit[0] + '_adult_contralateral.' + fsplit[1]
        write_out(_edges,lntd,rntd,dout)

    jsh = from_db(JSH,adjacency=True,remove=_remove)
    rjsh = jsh.A.map_vertex_names(lrd)
    jtdbound = get_td_bounds(jsh.A.es['weight'])
    _edges,ljtd,rjtd = get_corresponding_edge_attr(jsh.A,rjsh)
    _edges,ljtd,rjtd = filter_corresponding_tds(_edges,ljtd,rjtd,[jtdbound,jtdbound])
    if source_data:
        fsplit = source_data.split('.')
        dout = fsplit[0] + '_l4_contralateral.' + fsplit[1]
        write_out(_edges,ljtd,rjtd,dout)

    
    _edges,bntd,bjtd = get_corresponding_edge_attr(n2u.A,jsh.A)
    _edges,bntd,bjtd = filter_corresponding_tds(_edges,bntd,bjtd,[ntdbound,jtdbound])
    if source_data:
        fsplit = source_data.split('.')
        dout = fsplit[0] + '_adult_l4_homologous.' + fsplit[1]
        write_out(_edges,bntd,bjtd,dout)     
    
    ndelta = np.array(lntd) - np.array(rntd)
    jdelta = np.array(ljtd) - np.array(rjtd)
    bdelta = np.array(bntd) - np.array(bjtd)

    data = [ndelta,jdelta,bdelta]

    print('Stats:')
    print_wilcoxon(ndelta,'Adult L/R')
    print_wilcoxon(jdelta,'L4 L/R')
    print_wilcoxon(bdelta,'Adult/L4')
    
    tval1,pval1 = ttest_ind(ndelta,jdelta)
    tval2,pval2 = ttest_ind(jdelta,bdelta)
    tval3,pval3 = ttest_ind(ndelta,bdelta)

    pval = [(0,1,pval1),(1,2,pval2)]

    fig,ax = plt.subplots(1,1,figsize=(12,10))
    tpair_adj_weight(ax,data,pval,fout=fout)
    plt.show()
Ejemplo n.º 3
0
def run(fout=None):
    n2u = from_db('N2U',
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  dataType='networkx',
                  remove=_remove)

    jsh = from_db('JSH',
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  dataType='networkx',
                  remove=_remove)

    print(n2u.A.number_of_nodes(), jsh.A.number_of_nodes())
    print(n2u.A.number_of_edges() - jsh.A.number_of_edges())
    print(n2u.C.number_of_edges() - jsh.C.number_of_edges())
    print(n2u.E.number_of_edges() - jsh.E.number_of_edges())
    print(set(n2u.A.nodes()) - set(jsh.A.nodes()))

    syn = get_venn_data(n2u.C, jsh.C)
    gap = get_venn_data(n2u.E, jsh.E)
    adj = get_venn_data(n2u.A, jsh.A)
    print(syn, adj)

    fig, ax = plt.subplots(1, 3, figsize=(12, 5))

    v = venn2(subsets=syn, ax=ax[0], set_labels=('Adult', 'L4'))
    v.get_patch_by_id('100').set_color(ADULT_COL)
    v.get_patch_by_id('010').set_color(L4_COL)
    v.get_patch_by_id('11').set_color(AL_COL)
    for text in v.set_labels:
        text.set_fontsize(24)
    for text in v.subset_labels:
        text.set_fontsize(14)
    ax[0].set_title('# of chemical\nsynaptic connections', fontsize=24)
    v = venn2(subsets=gap, ax=ax[1], set_labels=('Adult', 'L4'))
    v.get_patch_by_id('100').set_color(ADULT_COL)
    v.get_patch_by_id('010').set_color(L4_COL)
    v.get_patch_by_id('11').set_color(AL_COL)
    for text in v.set_labels:
        text.set_fontsize(24)
    for text in v.subset_labels:
        text.set_fontsize(14)
    ax[1].set_title('# of gap junction\nconnections', fontsize=24)
    v = venn2(subsets=adj, ax=ax[2], set_labels=('Adult', 'L4'))
    v.get_patch_by_id('100').set_color(ADULT_COL)
    v.get_patch_by_id('010').set_color(L4_COL)
    v.get_patch_by_id('11').set_color(AL_COL)
    for text in v.set_labels:
        text.set_fontsize(24)
    for text in v.subset_labels:
        text.set_fontsize(14)
    ax[2].set_title('# of adjacency\nconnections', fontsize=24)
    if fout: plt.savefig(fout)
    plt.show()
Ejemplo n.º 4
0
def run(fout=None, source_data=None):

    N2U = 'N2U'
    JSH = 'JSH'

    _lr = [l for l in aux.read.into_list2(lr_pairs) if '#' not in l[0]]
    lr = list(zip(*_lr))
    nodes = lr[0]
    _remove = ['VC01', 'VD01', 'VB01', 'VB02']

    n2u = from_db(N2U, adjacency=True, remove=_remove)
    lnd = get_adj_deg(n2u, vertices=lr[0])
    rnd = get_adj_deg(n2u, vertices=lr[1])
    if source_data:
        fsplit = source_data.split('.')
        dout = fsplit[0] + '_adult_contralateral.' + fsplit[1]
        write_out(_lr, lnd, rnd, dout)

    jsh = from_db(JSH, adjacency=True, remove=_remove)
    ljd = get_adj_deg(jsh, vertices=lr[0])
    rjd = get_adj_deg(jsh, vertices=lr[1])
    if source_data:
        fsplit = source_data.split('.')
        dout = fsplit[0] + '_l4_contralateral.' + fsplit[1]
        write_out(_lr, ljd, rjd, dout)

    cells = sorted((set(n2u.neurons) & set(jsh.neurons)) - set(_remove))
    bnd = get_adj_deg(n2u, vertices=cells)
    bjd = get_adj_deg(jsh, vertices=cells)
    if source_data:
        fsplit = source_data.split('.')
        dout = fsplit[0] + '_adult_l4_homologous.' + fsplit[1]
        write_out(zip(cells, cells), bnd, bjd, dout)

    ndelta = [lnd[k1] - rnd[k2] for (k1, k2) in _lr]
    jdelta = [ljd[k1] - rjd[k2] for (k1, k2) in _lr]
    bdelta = [bnd[k] - bjd[k] for k in bnd.keys() if k in bjd.keys()]

    data = [ndelta, jdelta, bdelta]

    print(bdelta)
    print('Stats:')
    print_wilcoxon(ndelta, 'Adult L/R')
    print_wilcoxon(jdelta, 'L4 L/R')
    print_wilcoxon(bdelta, 'Adult/L4', alternative="greater")

    tval1, pval1 = ttest_ind(ndelta, jdelta)
    tval2, pval2 = ttest_ind(jdelta, bdelta)
    tval3, pval3 = ttest_ind(ndelta, bdelta)

    pval = [(0, 1, pval1), (1, 2, pval3)]
    fig, ax = plt.subplots(1, 1, figsize=(12, 10))
    tpair_adj_deg(ax, data, pval, fout=fout)
    plt.show()
Ejemplo n.º 5
0
def run(fout=None, source_data=None):
    N2U = 'N2U'
    JSH = 'JSH'
    _remove = ['VC01', 'VD01', 'VB01', 'VB02']
    lrd = aux.read.into_lr_dict(lr_dict)
    left = aux.read.into_list(left_nodes)
    left.remove('CEHDL')
    left.remove('CEHVL')
    left.remove('HSNL')
    left.remove('PVNL')
    left.remove('PLNL')

    N2U = from_db(N2U,
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  remove=_remove,
                  dataType='networkx')
    N2U.reduce_to_adjacency()
    JSH = from_db(JSH,
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  remove=_remove,
                  dataType='networkx')
    JSH.reduce_to_adjacency()

    n2uspec = synspec.get_bilateral_specificity(N2U, lrd, left)
    jshspec = synspec.get_bilateral_specificity(JSH, lrd, left)

    if source_data:
        fsplit = source_data.split('.')
        nout = fsplit[0] + '_adult.' + fsplit[1]
        jout = fsplit[0] + '_l4.' + fsplit[1]
        src = get_source_data(N2U, n2uspec)
        aux.write.from_list(nout, src)
        src = get_source_data(JSH, jshspec)
        aux.write.from_list(jout, src)

    ndata = get_ratio(N2U, n2uspec)
    jdata = get_ratio(JSH, jshspec)

    data = [
        ndata['gap'], jdata['gap'], ndata['pre'], jdata['pre'], ndata['post'],
        jdata['post']
    ]

    fig, ax = plt.subplots(1, 1, figsize=(15, 10))
    tpair_syn_adj_ratio(ax, data, fout=fout)
    plt.show()
def pull_gap_junctions(data,_db,A4,_min=0,_max=500,imap={'JSH':[0,1],'N2U':[2,3]}):
    D = from_db(_db,electrical=True,remove=remove,dataType='networkx')
    D.split_left_right(left,right)  
    #D.map_right_graphs(lrmap)
   
    con = db.connect.default(_db)
    cur = con.cursor()
    adj = db.mine.get_synapse_data(cur,'electrical')
     
    for (a,b,sections,continNum,series) in tqdm(adj,desc='Gap j. iter:'):
        if a not in lrmap: continue 
        if b not in lrmap: continue 
        for (oname,x,y,sect,ifile) in db.mine.get_contin_xyz(cur,continNum):
            if sect < _min or sect > _max: continue
            ra = lrmap[a]
            rb = lrmap[b]
            inleft = D.El.has_edge(a,b) and A4.has_edge(a,b)
            inright = D.Er.has_edge(a,b) and A4.has_edge(ra,rb)
            if inleft: 
                idx = imap[_db][0]
                _sect = section_map(idx,sect)
                data.append([a,b,idx,ifile.split('.')[0],_sect,continNum])
            if inright: 
                idx = imap[_db][1]
                _sect = section_map(idx,sect)
                data.append([ra,rb,idx,ifile.split('.')[0],_sect,continNum])
def pull_adjacency(data,_db,A4,_min=0,_max=500,imap={'JSH':[0,1],'N2U':[2,3]}):
    D = from_db(_db,adjacency=True,remove=remove,dataType='networkx')
    D.A = filter_graph_edge(D.A,pct=edge_thresh)
    D.split_left_right(left,right)  
    #D.map_right_graphs(lrmap)
   
    con = db.connect.default(_db)
    cur = con.cursor()
    adj = db.mine.get_adjacency_data(cur)
    
    img = db.mine.get_img_number(cur)
    dimg = format_img(img,_db)    
    tst = ['AVKR','SIAVR']
    for (a,b,w,layer) in tqdm(adj,desc="Adjacency iter:"):
        sect = dimg[layer]
        if sect < _min or sect > _max: continue
        ra = lrmap[a]
        rb = lrmap[b]
        inleft = D.Al.has_edge(a,b) and A4.has_edge(a,b)
        inright = D.Ar.has_edge(a,b) and A4.has_edge(ra,rb)
        if inleft: 
            idx = imap[_db][0]
            _sect = section_map(idx,sect)
            data.append([a,b,idx,layer,_sect,w])
        if inright: 
            idx = imap[_db][1]
            _sect = section_map(idx,sect)
            data.append([ra,rb,idx,layer,_sect,w])
Ejemplo n.º 8
0
def run(fout=None):
    C = from_db(db,
                adjacency=True,
                chemical=True,
                electrical=True,
                remove=_remove,
                dataType='networkx')

    #C.remove_self_loops()
    C.reduce_to_adjacency()

    lrd = aux.read.into_lr_dict(lr_dict)

    nclass = aux.read.into_list2(homologs)
    ndict = {}

    for n in nclass:
        for _n in n[1:]:
            ndict[_n] = n[0]

    left = aux.read.into_list(left_nodes)

    left.remove('CEHDL')
    left.remove('CEHVL')
    left.remove('HSNL')
    left.remove('PVNL')
    left.remove('PLNL')

    _pre = synspec.bilateral_specificity(C, left, lrd)
    _post = synspec.bilateral_specificity(C, left, lrd, mode='post')

    prob_both, outliers, probs = get_outliers(_pre, _post, ndict)
    plot_pre_post_specificity(prob_both, outliers, probs, fout=fout)
    plt.show()
Ejemplo n.º 9
0
def get_log_scale(cfg, lower_log_thresh=4):
    left = aux.read.into_list(cfg['mat']['left_nodes'])
    right = aux.read.into_list(cfg['mat']['right_nodes'])
    remove = aux.read.into_list(cfg['mat']['remove'])
    edge_thresh = cfg.getint('params', 'lower_weight_threshold')
    dbs = cfg['input']['databases'].split(',')

    G = []
    for d in dbs:
        D = from_db(d,
                    adjacency=True,
                    chemical=False,
                    electrical=False,
                    remove=remove,
                    dataType='networkx')
        D.A = filter_graph_edge(D.A, pct=edge_thresh)
        D.split_left_right(left, right)
        G.append(D)

    H = [G[0].Al, G[0].Ar, G[1].Al, G[1].Ar]
    mu, std = [], []
    for i in range(4):
        w = np.array([w for (u, v, w) in H[i].edges.data('weight')])
        w = np.log(w)
        idx = np.where(w > lower_log_thresh)
        _mu, _std = np.mean(w[idx]), np.std(w[idx])
        mu.append(_mu)
        std.append(_std)
    return np.mean(std)
def run(fout=None):
    C = from_db(db, adjacency=True, remove=remove)
    td = np.array(C.A.es['weight']) * SCALE

    fig, ax = plt.subplots(1, 1, figsize=(15, 10))
    plot_dist(ax,
              td,
              density=True,
              cumulative=True,
              xlim=[0, 2],
              ylim=[0, 1],
              hrange=(0, 2),
              nbins=1000,
              xlabel='Surface area contact ($\mu$m$^2$)',
              ylabel='Cumulative distribution',
              fs=24)
    axins = inset_axes(
        ax,
        width="50%",  # width = 30% of parent_bbox
        height="50%",  # height : 1 inch
        loc=5)
    plot_lognorm_probplot(axins,
                          td,
                          fout=None,
                          fs=24,
                          ylabel='Ordered log($w$)')
    plt.tight_layout()
    if fout: plt.savefig(fout)
    plt.show()
def group_degrees(db, _neuron_class):
    _remove = ['VC01', 'VD01', 'VB01', 'VB02']
    nclass = aux.read.into_dict(_neuron_class)

    C = from_db(db, adjacency=True, remove=_remove)
    C.A.assign_membership_dict(nclass, key='group')

    sp_idx = get_group_index(C.A, ['Sp1', 'Sp2'])
    i1_idx = get_group_index(C.A, 'I1')
    i2_idx = get_group_index(C.A, 'I2')
    sa_idx = get_group_index(C.A, 'Sa')
    smn_idx = get_group_index(C.A, 'SMN')
    hmnp_idx = get_group_index(C.A, 'HMNp')
    hmna_idx = get_group_index(C.A, 'HMNa')

    degrees = [
        C.A.degree(sp_idx),
        C.A.degree(sa_idx),
        C.A.degree(i1_idx),
        C.A.degree(i2_idx),
        C.A.degree(smn_idx),
        C.A.degree(hmnp_idx),
        C.A.degree(hmna_idx)
    ]

    return degrees
def run(fout=None, source_data=None):
    N2U = 'N2U'
    JSH = 'JSH'
    _remove = ['VC01', 'VD01', 'VB01', 'VB02']
    lrd = aux.read.into_lr_dict(lr_dict)
    left = aux.read.into_list(left_nodes)
    left.remove('CEHDL')
    left.remove('CEHVL')
    left.remove('HSNL')
    left.remove('PVNL')
    left.remove('PLNL')

    N2U = from_db(N2U,
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  remove=_remove,
                  dataType='networkx')
    JSH = from_db(JSH,
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  remove=_remove,
                  dataType='networkx')

    both_nodes = set(N2U.A.nodes()) & set(JSH.A.nodes())
    both_nodes.remove('SABD')
    if 'VD01' in both_nodes: both_nodes.remove('VD01')

    n2uspec = synspec.get_bilateral_specificity(N2U, lrd, left)
    jshspec = synspec.get_bilateral_specificity(JSH, lrd, left)
    devspec = synspec.get_developmental_specificity(N2U,
                                                    JSH,
                                                    both_nodes=both_nodes)
    if source_data:
        fsplit = source_data.split('.')
        nout = fsplit[0] + '_adult_contralateral.' + fsplit[1]
        jout = fsplit[0] + '_l4_contralateral.' + fsplit[1]
        bout = fsplit[0] + '_adult_l4_homologous.' + fsplit[1]
        aux.write.from_dict(nout, n2uspec)
        aux.write.from_dict(jout, jshspec)
        aux.write.from_dict(bout, devspec)

    fig, ax = plt.subplots(1, 1, figsize=(18, 10))
    plot_specificity(ax, n2uspec, jshspec, devspec, fout=fout)
    plt.show()
Ejemplo n.º 13
0
def run(fout=None, source_data=None):
    C = from_db(db,
                adjacency=True,
                chemical=True,
                electrical=True,
                remove=remove)
    C.C.reduce_to(C.A)
    C.E.reduce_to(C.A)
    N = C.C.ecount()

    C.C.to_undirected(combine_edges=sum)

    data = get_data(C.C, C.A)
    data = data[data[:, 0].argsort()]

    X = data[:, 0].reshape(-1, 1)
    y = np.ravel(data[:, 1])
    _x = np.linspace(-4, 4, 81).reshape(-1, 1)

    # instantiate a logistic regression model, and fit with X and y
    model = LogisticRegression()
    model = model.fit(X, y)

    # check the accuracy on the training set
    print(model.score(X, y))
    print(y.mean())
    #print(model.predict_proba(_x))

    _data = result_data(C.C, C.A, model)
    if source_data:
        dout = []
        for i in range(C.A.vcount()):
            dout.append([C.A.vs[i]['name']] + _data[i, :].tolist())
        aux.write.from_list(source_data, dout)
    """
    plt.figure()
    plt.plot(data[:,1],data[:,2],'bo')
    plt.plot([0,50],[0,50],'r-',linewidth=3)
    plt.xlim([0,50])
    plt.ylim([0,50])
    plt.show()
    """

    fig, ax = plt.subplots(1, 1, figsize=(15, 10))
    plot_actual_vs_predict(ax, _data, colorbar=True)
    ax.set_xlim([0, 50])
    ax.set_ylim([0, 50])
    ax.set_title('Predicted number of synaptic connections per cell',
                 fontsize=32,
                 y=1.04)
    ax.set_xlabel('# actual connections', fontsize=32)
    ax.set_ylabel('# predicted connections', fontsize=32)
    plt.tight_layout()
    if fout: plt.savefig(fout)
    plt.show()
    """
def write_degrees(db, _neuron_class, fout):
    _remove = ['VC01', 'VD01', 'VB01', 'VB02']
    nclass = aux.read.into_dict(_neuron_class)

    C = from_db(db, adjacency=True, remove=_remove)
    C.A.assign_membership_dict(nclass, key='group')

    data = []
    for v in C.A.vs():
        data.append([v['name'], v['group'], C.A.degree(v)])

    aux.write.from_list(fout, data)
Ejemplo n.º 15
0
def plot_figure(db='N2U',fout=None,source_data=None):
    C = from_db(db,adjacency=True,remove=REMOVE)
    edges = [e['weight']*SCALE for e in C.A.es()]
    _edges = [[C.A.vs[e.source]['name'],C.A.vs[e.target]['name'],e['weight']*SCALE] for e in C.A.es()]
    _edges = sorted(_edges)
    print(_edges[0],_edges[-1])
    _edges = [['cell1','cell2','membrane_contact_microns^2']] + _edges

    print(np.max(edges))
    low = np.percentile(edges,35)
    high = np.percentile(edges,66)
    print(low,high)
    print('# edges',len(edges))

    fig,ax = plt.subplots(1,1,figsize=(2,1.8))
    plot_dist(ax,edges,density=True,cumulative=-1,xlim=[0,10],ylim=[0,1],
              hrange=(0,60),nbins=1000,
              xlabel='Membrane contact area ($\mu$m$^2$)',
              ylabel='Survival distribution',fs=7)
    hist, bin_edges = np.histogram(edges,bins=1000,range=(0,60),density=True,normed=True)
    dx = bin_edges[1] - bin_edges[0]
    hist = 1 - np.cumsum(hist*dx)
    ax.fill_between(bin_edges[:-1],hist,color=MIDCOL)
    ax.fill_between(bin_edges[:-1],hist, where = hist < 0.35,color=HIGHCOL)
    ax.fill_between(bin_edges[:-1],hist, where = hist > 0.66,color=LOWCOL)
    

    edges = np.array(edges)
    esum = np.sum(edges)
    elow = np.sum(edges[np.where(edges < low)]) / esum
    ehigh = np.sum(edges[np.where(edges > high)]) / esum
    emid = 1 - elow - ehigh 
    #fig,ax = plt.subplots(1,1)
    ax2 = plt.axes([0,0,1,1])
    ip = InsetPosition(ax, [0.1,0.4,0.5,0.5])
    ax2.set_axes_locator(ip)
    wedges, texts, autotexts = ax2.pie([elow,emid,ehigh],textprops={'fontsize':6},
            colors=[LOWCOL,MIDCOL,HIGHCOL],autopct='%1.1f%%')
    legend = ax2.legend(wedges,['Low','Mid','High'],title='Contact range',
                loc='center left',bbox_to_anchor=(1, 0, 0.5, 1),fontsize=6)
    ax2.set_title('Total surface area',fontsize=6)
    #plt.setp(autotexts, size=12, weight="bold")
    plt.setp(legend.get_title(),fontsize=6)
    plt.tight_layout()
    if fout: plt.savefig(fout)    
    #plt.show()
    if source_data: aux.write.from_list(source_data,_edges)
Ejemplo n.º 16
0
def run(fout=None):
    C = from_db(db,adjacency=True,chemical=True,electrical=True,remove=remove)
    C.reduce_to_adjacency()
    degree_in = C.C.degree(mode='in')
    degree_out = C.C.degree(mode='out')
    degree_gap = C.E.degree()

    fig,ax = plt.subplots(1,3,figsize=(15,5),sharey=True)
    plot_syn_degree(ax[0],degree_out,density=True,fit_mode='KDE',ylim=[0,0.15],
                    xlabel='Postsynaptic degree, $d^\mathrm{out}$',
                    ylabel='Probability',xfs=24,yfs=24)
    plot_syn_degree(ax[1],degree_in,density=True,fit_mode='KDE',ylim=[0,0.15],
                    xlabel="Presynaptic degree, $d^\mathrm{in}$",xfs=24)
    plot_syn_degree(ax[2],degree_gap,density=True,fit_mode='KDE',ylim=[0,0.15],
                    xlabel='Gap junction degree, $d^\mathrm{gap}$',xfs=24)
    if fout: plt.savefig(fout)
    plt.show()
def count_mon_poly(_db):
    _remove = ['VC01', 'VD01', 'VB01', 'VB02']
    C = from_db(_db,
                adjacency=True,
                chemical=True,
                add_poly=True,
                remove=_remove)
    C.reduce_to_adjacency()

    mon, poly = 0, 0
    for e in C.C.es:
        if e['S'] > 0 and e['Sp'] == 0:
            mon += 1
        elif e['Sp'] > 0:
            poly += 1

    return [mon, poly]
def run(fout=None,source_data=None):
    db = 'N2U'
    _remove = ['VC01','VD01','VB01','VB02']
    nclass = aux.read.into_dict(_nclass)

    C = from_db(db,adjacency=True,chemical=True,electrical=True,remove=_remove)
    C.C.assign_membership_dict(nclass,key='group')

    for e in C.C.es:
        if e['weight'] > 50:
            print(e['weight'],C.C.vs[e.source]['name'],C.C.vs[e.target]['name'])


    print(C.C.ecount(),C.E.ecount())
    cf = get_cf(C)
    
    data = []
    stype = ['gap','pre','post']
    grp = ['S','I','M']

    s_idx = [v.index for v in C.C.vs.select(group='S')]
    i_idx = [v.index for v in C.C.vs.select(group='I')]
    m_idx = [v.index for v in C.C.vs.select(group='M')]
    
    for s in stype:
        data.append(cf[s][s_idx])
        data.append(cf[s][i_idx])
        data.append(cf[s][m_idx])

    if source_data:
        dout = []
        vertices = [v['name'] for v in C.A.vs]
        for s in stype:
            for i in range(len(vertices)):
                dout.append([s,vertices[i],cf[s][i]])
        aux.write.from_list(source_data,dout)

    fig,ax = plt.subplots(1,1,figsize=(15,10))
    plot_confrac_subgroups(ax,data,fout=fout)

    plt.tight_layout()
    if fout: plt.savefig(fout)
    plt.show()

    """
def run(fout=None,source_data=None):
    C = from_db(db,adjacency=True,chemical=True,electrical=True,remove=remove)
    C.C.reduce_to(C.A)
    C.E.reduce_to(C.A)
    N = C.C.ecount()

    C.C.to_undirected(combine_edges=sum)

    data,edges = get_data(C.C,C.A)
    data = data[data[:,0].argsort()]
    pos = np.where(data[:,1] == 1)[0]
    neg = np.where(data[:,1] == 0)[0]

    if source_data:
        aux.write.from_list(source_data,edges)

    h1,bins1 = np.histogram(data[neg,0],bins=nbins,
                            range=(-4,4),normed=True,density=True)
    dx = bins1[1] - bins1[0]
    h1 = np.cumsum(h1)*dx
    h2,bins2 = np.histogram(data[pos,0],bins=nbins,
                            range=(-4,4),normed=True,density=True)
    dx = bins2[1] - bins2[0]
    h2 = np.cumsum(h2[::-1])[::-1]*dx
    imin = np.argmin(abs(h1-h2)) + 1

    fig,ax = plt.subplots(1,1,figsize=(15,10))
    n,bins,_ = ax.hist(data[neg,0],bins=nbins,range=(-4,4),histtype='step',
                       density=True,cumulative=False,linewidth=4,
                       label='(-) synapse ($n=%d$)'%len(data[neg,0]))
    n,bins,_ = ax.hist(data[pos,0],bins=nbins,range=(-4,4),histtype='step',
                       density=True,cumulative=False,linewidth=4,
                       label='(+) synapse ($n=%d$)'%len(data[pos,0]))
    ax.axvline(bins[imin],color='r',linewidth=3)
    #print(bins[imin])
    #plot_skew_norm_fit(ax,data[neg,0],bins)
    #plot_skew_norm_fit(ax,data[pos,0],bins)
    plt.legend(loc='upper left',fontsize=24)
    ax.set_xlim([-4,4])
    ax.set_xlabel('log(surface area contact)',fontsize=32)
    ax.set_ylabel('Probability density',fontsize=32)
    plt.tight_layout()
    if fout: plt.savefig(fout)
    plt.show()
Ejemplo n.º 20
0
def run(fout=None):
    db = 'N2U'
    remove = ['VC01', 'VD01', 'VB01', 'VB02']
    C = from_db(db, adjacency=True, remove=remove)
    degree = C.A.degree()

    fig, ax = plt.subplots(1, 1, figsize=(7, 5))
    plot_adj_degree(ax,
                    degree,
                    density=True,
                    fit_mode='GMM',
                    ylim=[0, 0.1],
                    xlabel='Adjacency degree, $d$',
                    ylabel='Probability',
                    yfs=24,
                    xfs=24)
    if fout: plt.savefig(fout)
    plt.tight_layout()
    plt.show()
Ejemplo n.º 21
0
def perturb_data(cfg, dbs, lscale, sig, spatial_domain=0, delta=-1):
    left = aux.read.into_list(cfg['mat']['left_nodes'])
    right = aux.read.into_list(cfg['mat']['right_nodes'])
    lrmap = aux.read.into_lr_dict(cfg['mat']['lrmap'])
    nodes = aux.read.into_list(cfg['mat']['nodes'])
    remove = aux.read.into_list(cfg['mat']['remove'])
    edge_thresh = cfg.getint('params', 'lower_weight_threshold')
    #dbs = cfg['input']['databases'].split(',')
    DEG = len(dbs) * 2
    G = []
    for d in dbs:
        D = from_db(d,
                    adjacency=True,
                    chemical=False,
                    electrical=False,
                    remove=remove,
                    dataType='networkx',
                    spatial_domain=spatial_domain)
        for (u, v) in D.A.edges():
            D.A[u][v]['weight'] *= np.exp(np.random.normal(scale=sig) * lscale)
        D.A = filter_graph_edge(D.A, pct=edge_thresh)
        D.A.remove_nodes_from(['PLNL', 'PVR', 'SABD'])
        D.split_left_right(left, right)
        D.map_right_graphs(lrmap)
        G.append(D)

    M = [nx.Graph() for i in range(DEG)]
    gsizes = []
    H = [G[0].Al, G[0].Ar]
    if DEG == 4: H = [G[0].Al, G[0].Ar, G[1].Al, G[1].Ar]

    for (i, m) in enumerate(M):
        deg = i + 1
        for g in G:
            normalize_edge_weight(g.Al)
            normalize_edge_weight(g.Ar)
            consensus_graph(m, H, deg, nodes, weight=['weight', 'wnorm'])
        m.remove_nodes_from(remove)
        gsizes.append(m.number_of_edges())

    if delta < 0: delta = DEG - 1
    #print('delta',delta)
    return gsizes, M[delta], H
Ejemplo n.º 22
0
    
    parser.add_argument('fin',
                        action="store",
                        help="Path to group file ")

    params = parser.parse_args()

    con = db.connect.default(params.db)
    cur = con.cursor()

    sql = "select distinct(cellsname) from cells"
    cur.execute(sql)
    rna = set([c[0] for c in cur.fetchall()])
    con.close()

    _group = aux.read.into_map(params.fin)
    group = {'map':_group,'key':'class'}

    C = from_db('N2U',adjacency=True,chemical=False,
                electrical=False,group=group,dataType='networkx')

    #groups = set([d[0] for d in aux.read.into_list2(params.fin)])
    groups = set(C.A.nodes())
    print('Cells in rnaseq not in volumetric: %d' %len(rna-groups))
    print(sorted(rna - groups))

    print('Cells in volumetric not in rnaseq: %d' %len(groups-rna))
    print(sorted(groups - rna))
    

            z = self.node[i]['loc'][2] * self.scale[2]
            ax.plot([z], [x], [y], 'go')
            ax.text(z, x, y, str(i), (1, 1, 0))


if __name__ == "__main__":

    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('db', action="store", help="Database name")
    parser.add_argument('fout', action="store", help="Output file")

    params = parser.parse_args()

    C = from_db(params.db, adjacency=True)

    END = 425
    if params.db == 'N2U': END = 325
    con = db.connect.default(params.db)
    cur = con.cursor()
    cell_data = []
    for cell in sorted(C.neurons):
        contins = db.mine.get_contins(cur, cell)
        for c in contins:
            skel = Skeleton(c)
            edges = db.mine.get_contin_edges(cur, c, end=END)
            skel.add_edges_from(edges)
            for o in skel.nodes():
                xyz = np.array(list(map(int, db.mine.get_object_xyz(cur, o))))
                skel.node[o]['loc'] = xyz
Ejemplo n.º 24
0
    params = parser.parse_args()

    _end = 500
    if params.db == 'N2U': _end = 325

    mode = 'post'
    con = db.connect.default(params.db)
    cur = con.cursor()
    nodes = sorted(db.mine.get_adjacency_cells(cur))
    e = Expression(cur, cam, nodes)
    e.assign_expression_patterns(mode=mode)

    C = from_db(params.db,
                adjacency=True,
                chemical=True,
                electrical=True,
                dataType='networkx')
    C.remove_self_loops()
    C.reduce_to_adjacency()

    wbe = cam_lus.wbe(e, C)
    wbe_data = wbe.get_data()
    aux.write.from_list('results/' + params.db + '_wbe.csv', wbe_data)

    sbe = cam_lus.sbe(e, end=_end)
    sbe_data = sbe.get_data()
    aux.write.from_list('results/' + params.db + '_sbe.csv', sbe_data)

    e.splice = True
    e.load_genes()
def run(fout=None, source_data=None):
    N2U = 'N2U'
    JSH = 'JSH'
    left = aux.read.into_list(left_nodes)
    right = aux.read.into_list(right_nodes)
    _lrd = aux.read.into_dict(lr_dict)
    lrd = {}
    for key, val in _lrd.items():
        lrd[key] = val
        lrd[val] = key
    _remove = ['VC01', 'VD01', 'VB01', 'VB02', 'HSNL', 'HSNR', 'PVNL', 'PVNR']

    n2u = from_db(N2U, adjacency=True, remove=_remove)
    a = n2u.A.copy()
    reflected = n2u.A.map_vertex_names(lrd)
    vertices = left + right
    ns, no = get_data(n2u.A, reflected, left, right)
    if source_data:
        fsplit = source_data.split('.')
        fsim = fsplit[0] + '_adult_homologous.' + fsplit[1]
        fovr = fsplit[0] + '_adult_overlap.' + fsplit[1]
        sim = get_neighborhood_similarity(n2u.A, reflected, left)
        dsim = []
        for i in range(len(left)):
            dsim.append([left[i], right[i], sim[i]])
        both = left + right
        overlap = get_neighborhood_overlap_similarity(n2u.A, left + right)
        dovr = []
        for i in range(len(both)):
            dovr.append([both[i], overlap[i]])
        aux.write.from_list(fsim, dsim)
        aux.write.from_list(fovr, dovr)

    jsh = from_db(JSH, adjacency=True, remove=_remove)
    reflected = jsh.A.map_vertex_names(lrd)
    vertices = left + right
    js, jo = get_data(jsh.A, reflected, left, right)
    #js,jo = arcsine(js),arcsine(jo)
    if source_data:
        fsplit = source_data.split('.')
        fsim = fsplit[0] + '_l4_homologous.' + fsplit[1]
        fovr = fsplit[0] + '_l4_overlap.' + fsplit[1]
        sim = get_neighborhood_similarity(jsh.A, reflected, left)
        dsim = []
        for i in range(len(left)):
            dsim.append([left[i], right[i], sim[i]])
        both = left + right
        overlap = get_neighborhood_overlap_similarity(jsh.A, left + right)
        dovr = []
        for i in range(len(both)):
            dovr.append([both[i], overlap[i]])
        aux.write.from_list(fsim, dsim)
        aux.write.from_list(fovr, dovr)

    vertices = sorted((set(n2u.neurons) & set(jsh.neurons)) - set(_remove))
    sim = get_neighborhood_similarity(n2u.A, jsh.A, vertices)
    noverlap = get_neighborhood_overlap_similarity(n2u.A, vertices)
    joverlap = get_neighborhood_overlap_similarity(jsh.A, vertices)
    bs = np.array([s for s in sim if s > -1])
    overlap = noverlap + joverlap
    bo = np.array([o for o in overlap if o > -1])
    if source_data:
        fsplit = source_data.split('.')
        fsim = fsplit[0] + '_adult_l4_homologous.' + fsplit[1]
        fovr = fsplit[0] + '_adult_l4_overlap.' + fsplit[1]
        dsim = []
        for i in range(len(vertices)):
            dsim.append([vertices[i], vertices[i], sim[i]])
        dovr = []
        for i in range(len(vertices)):
            dovr.append(['adult-' + vertices[i], noverlap[i]])
            dovr.append(['l4-' + vertices[i], joverlap[i]])
        aux.write.from_list(fsim, dsim)
        aux.write.from_list(fovr, dovr)

    #tval1,upval1 = ttest_ind(ns,no)
    #tval2,upval2 = ttest_ind(js,jo)
    #tval3,upval3 = ttest_ind(bs,bo)
    tval1, upval1 = mannwhitneyu(ns, no)
    tval2, upval2 = mannwhitneyu(js, jo)
    tval3, upval3 = mannwhitneyu(bs, bo)

    print(upval1, upval2, upval3)

    data = [ns, no, js, jo, bs, bo]
    pval = [(0, 1, upval1), (2, 3, upval2), (4, 5, upval3)]

    print('Stats:')
    print_wilcoxon(data[0], 'Adult L/R contra.')
    print_wilcoxon(data[1], 'Adult L/R overlap')
    print_wilcoxon(data[2], 'L4 L/R contra.')
    print_wilcoxon(data[3], 'L4 L/R overlap')
    print_wilcoxon(data[4], 'Adult/L4 contra.')
    print_wilcoxon(data[5], 'Adult/L4 overlap')

    fig, ax = plt.subplots(1, 1, figsize=(12, 10))
    plot_overlap_compare(ax, data, pval, fout=fout)
    plt.show()
Ejemplo n.º 26
0
def run(fout=None):
    N2U = 'N2U'
    JSH = 'JSH'
    _lr = aux.read.into_list2(lr_pairs)
    lr = list(zip(*_lr))
    _remove = ['VC01', 'VD01', 'VB01', 'VB02']
    n2u = from_db(N2U,
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  remove=_remove)
    ndeg_l, ndeg_r = get_deg_data(n2u, lr)

    jsh = from_db(JSH,
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  remove=_remove)
    jdeg_l, jdeg_r = get_deg_data(jsh, lr)

    cells = sorted((set(n2u.neurons) & set(jsh.neurons)) - set(_remove))
    bdeg_1, bdeg_2 = get_dev_deg_data(n2u, jsh, cells)

    ndeg, jdeg, bdeg = [None] * 4, [None] * 4, [None] * 4
    for i in range(4):
        ndeg[i] = [ndeg_l[i][k1] - ndeg_r[i][k2] for (k1, k2) in _lr]
        jdeg[i] = [jdeg_l[i][k1] - jdeg_r[i][k2] for (k1, k2) in _lr]
        bdeg[i] = [bdeg_1[i][k] - bdeg_2[i][k] for k in cells]

    ndeg = np.array(ndeg)
    jdeg = np.array(jdeg)
    bdeg = np.array(bdeg)
    data = []
    for A in [ndeg, jdeg, bdeg]:
        tmp = []
        for j in [1, 2, 3]:
            rho, p = spearmanr(A[0, :], A[j, :])
            tmp.append(rho**2)
        data.append(tmp)

    ind = np.arange(3)
    width = 0.15

    fig, ax = plt.subplots(1, 1, figsize=(10, 10))
    ax.axvspan(-0.5, 0.5, facecolor='#C3C3C3')
    ax.axvspan(0.5, 1.5, facecolor='#D8D7D7')
    ax.axvspan(1.5, 2.5, facecolor='#C3C3C3')
    ax.grid(axis='y', color='0.9', linestyle='-', linewidth=1)
    rects0 = ax.bar(ind - width, data[0], width, color=ADULT_COL)
    rects1 = ax.bar(ind, data[1], width, color=L4_COL)
    rects2 = ax.bar(ind + width, data[2], width, color=AL_COL)
    ax.set_xticks(ind)
    ax.set_xticklabels(('$r^2_{\mathrm{gap}}$', '$r^2_{\mathrm{pre}}$',
                        '$r^2_{\mathrm{post}}$'))
    ax.set_yticks([0, 0.05, 0.1, 0.15, 0.2])
    ax.set_ylim([0, 0.2])
    ax.set_xlim([-.5, 2.5])
    ax.legend((rects0[0], rects1[0], rects2[0]),
              ('Adult L/R', 'L4 L/R', 'Adult/L4'),
              fontsize=18)
    ax.set_ylabel('Coefficient of determination', fontsize=32)
    ax.set_title(
        'Correlation between synaptic and\nadjacency degree differences',
        fontsize=32)
    plt.tight_layout()
    if fout: plt.savefig(fout)
    plt.show()
Ejemplo n.º 27
0
def run(fout=None):
    N2U = 'N2U'
    JSH = 'JSH'
    lr = aux.read.into_list2(lr_pairs)
    lr = list(zip(*lr))
    _remove = ['VC01','VD01','VB01','VB02']

    n2u = from_db(N2U,adjacency=True,chemical=True,
                  electrical=True,remove=_remove)
    lncf = get_cf(n2u,vertices = lr[0],_arcsine=ARCSINE)
    rncf = get_cf(n2u,vertices = lr[1],_arcsine=ARCSINE)
    ancf = get_cf(n2u,vertices = set(n2u.neurons) - set(_remove),_arcsine=ARCSINE)
    nstd = {'gap': np.std(ancf['gap']),
            'pre': np.std(ancf['pre']),
            'post': np.std(ancf['post'])}

    jsh = from_db(JSH,adjacency=True,chemical=True,
                  electrical=True,remove=_remove)
    ljcf = get_cf(jsh,vertices = lr[0],_arcsine=ARCSINE)
    rjcf = get_cf(jsh,vertices = lr[1],_arcsine=ARCSINE)
    ajcf = get_cf(jsh,vertices = set(jsh.neurons) - set(_remove),_arcsine=ARCSINE)
    jstd = {'gap' : np.std(ajcf['gap']),
            'pre' : np.std(ajcf['pre']),
            'post': np.std(ajcf['post'])}

    cells = sorted((set(n2u.neurons)&set(jsh.neurons))-set(_remove))
    bncf = get_cf(n2u,vertices = cells,_arcsine=ARCSINE) 
    bjcf = get_cf(jsh,vertices = cells,_arcsine=ARCSINE)
    bstd = {'gap' : np.std(np.concatenate((ancf['gap'],ajcf['gap']))),
            'pre' : np.std(np.concatenate((ancf['pre'],ajcf['pre']))),
            'post': np.std(np.concatenate((ancf['post'],ajcf['post'])))}



    data = [(lncf['gap'] - rncf['gap'])/nstd['gap'],
            (ljcf['gap'] - rjcf['gap'])/jstd['gap'],
            (bncf['gap'] - bjcf['gap'])/bstd['gap'],
            (lncf['pre'] - rncf['pre'])/nstd['pre'],
            (ljcf['pre'] - rjcf['pre'])/jstd['pre'],
            (bncf['pre'] - bjcf['pre'])/bstd['pre'],
            (lncf['post'] - rncf['post'])/nstd['post'],
            (ljcf['post'] - rjcf['post'])/jstd['post'],
            (bncf['post'] - bjcf['post'])/bstd['post']]

    print('Stats:')
    print_wilcoxon(data[0],'Adult L/R gap')
    print_wilcoxon(data[1],'L4 L/R gap')
    print_wilcoxon(data[2],'Adult/L4 gap')
    print_wilcoxon(data[3],'Adult L/R pre')
    print_wilcoxon(data[4],'L4 L/R pre')
    print_wilcoxon(data[5],'Adult/L4 pre')
    print_wilcoxon(data[6],'Adult L/R post')
    print_wilcoxon(data[7],'L4 L/R post')
    print_wilcoxon(data[8],'Adult/L4 post')


    tval0,pval0 = mannwhitneyu(data[0],data[2])
    tval1,pval1 = mannwhitneyu(data[0],data[1])
    tval1,pval2 = mannwhitneyu(data[1],data[2])
    tval1,pval3 = mannwhitneyu(data[3],data[4])
    tval1,pval4 = mannwhitneyu(data[4],data[5])
    tval1,pval5 = mannwhitneyu(data[6],data[7])
    tval1,pval6 = mannwhitneyu(data[7],data[8])

    pval = [
        (0,1,pval1),(1,2,pval2),
        (3,4,pval3),(4,5,pval4),
        (6,7,pval5),(7,8,pval6)]

    fig,ax = plt.subplots(1,1,figsize=(12,10))
    #tpair_confrac(ax,data,pval,fout=fout)
    tpair_syn(ax,data,pval,
              fout=fout,
              ylabel='Normalized CF difference',
              title = 'Homologous connectivity fractions (CF)',
              xticklabels = ['$C^{\mathrm{gap}}$',
                             '$C^{\mathrm{pre}}$',
                             '$C^{\mathrm{post}}$'],
              ylim=[-3,3])

    plt.tight_layout()
    plt.show()
    parser.add_argument('matrix',
                        action = 'store',
                        help = 'Path to matrix file')
 
    params = parser.parse_args()
    
    M = MatLoader()
    M.load_left()
    M.load_right()
    M.load_lrmap()

    nclass = M.load_nerve_ring_classes()
    nodes = M.load_reduced_nodes()
    single = set(nodes) - set(M.left) - set(['ASER'])

    N = from_db('N2U',adjacency=True,chemical=True,electrical=True,
            remove=REMOVE,dataType='networkx')
    Nsa = N.split_graph(N.A,single)
    Nsc = N.split_graph(N.C,single)
    Nse = N.split_graph(N.E,single)
    N.split_left_right(M.left,M.right)
    N.map_right_graphs(M.lrmap)

    J = from_db('JSH',adjacency=True,chemical=True,
                electrical=True,dataType='networkx')
    Jsa = J.split_graph(J.A,single)
    Jsc = J.split_graph(J.C,single)
    Jse = J.split_graph(J.E,single)
    J.split_left_right(M.left,M.right)
    J.map_right_graphs(M.lrmap)

    e = Matrix(M.cam,params.matrix)
Ejemplo n.º 29
0
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('matrix', action='store', help='Path to matrix file')

    parser.add_argument('db', action='store', help='Database')

    #parser.add_argument('cell',action='store',help='Cell name')

    params = parser.parse_args()
    #cell = params.cell

    ML = MatLoader()
    ML.load_lrmap()
    nodes = sorted(ML.load_reduced_nodes())

    C = from_db(params.db, adjacency=True, dataType='networkx', remove=REMOVE)

    e = Matrix(ML.cam, params.matrix)
    e.load_genes()
    e.load_cells(nodes)
    e.assign_expression()
    e.binarize()
    #np.random.shuffle(e.M)

    M = e.E[:, idx_gene]

    #np.random.shuffle(M)

    if METHOD == 1:
        data = defaultdict(list)
        comb = combinations(nodes, 2)
def run(fout=None, source_data=None):
    left = aux.read.into_list(left_nodes)
    right = aux.read.into_list(right_nodes)
    _lrd = aux.read.into_dict(lr_dict)
    lrd = {}
    for key, val in _lrd.items():
        lrd[key] = val
        lrd[val] = key

    _remove = ['VC01', 'VD01', 'VB01', 'VB02']

    n2u = from_db('N2U',
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  dataType='networkx',
                  remove=_remove)
    jsh = from_db('JSH',
                  adjacency=True,
                  chemical=True,
                  electrical=True,
                  dataType='networkx',
                  remove=_remove)

    nsyn, sedges = get_discrepant_directed(n2u.C, lrd, left, right)
    ngap, gedges = get_discrepant_undirected(n2u.E, lrd, left, right)
    nadj, aedges = get_discrepant_undirected(n2u.A, lrd, left, right)
    print(nsyn, ngap, nadj)
    if source_data:
        dsource = 'adult'
        fsplit = source_data.split('.')
        fchem = fsplit[0] + '_' + dsource + '_chem.' + fsplit[1]
        fgap = fsplit[0] + '_' + dsource + '_gap.' + fsplit[1]
        fadj = fsplit[0] + '_' + dsource + '_adj.' + fsplit[1]
        aux.write.from_list(fchem, sedges)
        aux.write.from_list(fgap, gedges)
        aux.write.from_list(fadj, aedges)

    jsyn, sedges = get_discrepant_directed(jsh.C, lrd, left, right)
    jgap, gedges = get_discrepant_undirected(jsh.E, lrd, left, right)
    jadj, aedges = get_discrepant_undirected(jsh.A, lrd, left, right)
    print(jsyn, jgap, jadj)
    print(nsyn, ngap, nadj)
    if source_data:
        dsource = 'l4'
        fsplit = source_data.split('.')
        fchem = fsplit[0] + '_' + dsource + '_chem.' + fsplit[1]
        fgap = fsplit[0] + '_' + dsource + '_gap.' + fsplit[1]
        fadj = fsplit[0] + '_' + dsource + '_adj.' + fsplit[1]
        aux.write.from_list(fchem, sedges)
        aux.write.from_list(fgap, gedges)
        aux.write.from_list(fadj, aedges)
    _n2u = format_data(nsyn, ngap, nadj)
    _jsh = format_data(jsyn, jgap, jadj)

    print(_n2u)
    width = 0.25
    dx = 0.1
    ind = np.arange(3)
    col = ['#E69F00', '#56B4E9', '#F0E442']

    fig, ax = plt.subplots(1, 1, figsize=(12, 10))
    ax.axvspan(-0.5, 0.5, facecolor='#C3C3C3')
    ax.axvspan(0.5, 1.5, facecolor='#D8D7D7')
    ax.axvspan(1.5, 2.5, facecolor='#C3C3C3')

    ax.bar(ind - width / 2 - dx, _n2u[0, :], width, color=col[0], hatch='/')
    ax.bar(ind - width / 2 - dx,
           _n2u[1, :],
           width,
           color=col[1],
           bottom=_n2u[0, :],
           hatch='/')
    ax.bar(ind - width / 2 - dx,
           _n2u[2, :],
           width,
           color=col[2],
           bottom=_n2u[0, :] + _n2u[1, :])

    ax.bar(ind + width / 2 + dx, _jsh[0, :], width, color=col[0], hatch='/')
    ax.bar(ind + width / 2 + dx,
           _jsh[1, :],
           width,
           color=col[1],
           bottom=_jsh[0, :],
           hatch='/')
    ax.bar(ind + width / 2 + dx,
           _jsh[2, :],
           width,
           color=col[2],
           bottom=_jsh[0, :] + _jsh[1, :])

    ax.set_xticklabels([
        'Adult\n(n=%d)' % ngap[3],
        'L4\n(n=%d)' % jgap[3],
        'Adult\n(n=%d)' % nsyn[3],
        'L4\n(n=%d)' % jsyn[3],
        'Adult\n(n=%d)' % nadj[3],
        'L4\n(n=%d)' % jadj[3]
    ])
    ax.set_xticks([-0.25, 0.25, 0.75, 1.25, 1.75, 2.25])
    plt.xticks(fontsize=22)
    #plt.xticks(rotation=45,fontsize=18)

    ax.set_yticklabels([0, 0.25, 0.5, 0.75, 1.])
    ax.set_yticks([0, 0.25, 0.5, 0.75, 1.])

    ax.set_ylim([0, 1.3])
    ax.set_xlim([-0.5, 2.5])

    ax.set_ylabel('Fraction of connections', fontsize=32)

    ax.text(-0.2, 1.1, 'Gap J.', fontsize=34)
    ax.text(0.65, 1.1, 'Chemical', fontsize=34)
    ax.text(1.65, 1.1, 'Adjacency', fontsize=34)

    legend_elements = [
        Patch(facecolor=col[0],
              edgecolor='k',
              label='Left discrepant',
              hatch='//'),
        Patch(facecolor=col[1],
              edgecolor='k',
              label='Right discrepant',
              hatch='//'),
        Patch(facecolor=col[2], edgecolor='k', label='L/R reproducible')
    ]
    ax.legend(handles=legend_elements, loc='upper center', ncol=3, fontsize=18)
    plt.tight_layout()
    if fout: plt.savefig(fout)
    plt.show()