<jupyter_start><jupyter_text>Displaying marker pairs as network   Neccessary Imports<jupyter_code>%%javascript
$.getScript('https://kmahelona.github.io/ipython_notebook_goodies/ipython_notebook_toc.js')
import sys
code = "./../../code/"
data = "./../../data/"
sys.path.append(code)
import pandas
import pypairs as pairs
from sklearn.preprocessing import QuantileTransformer
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import plotly.graph_objs as go
import numpy as np
from pathlib import Path
from tqdm import tqdm_notebook as tqdm
import helper
from itertools import chain
import networkx as nx

init_notebook_mode(connected=True)<jupyter_output><empty_output><jupyter_text>Load oscope marker pairs<jupyter_code>oscope_marker_pairs = helper.load_ocope_marker(data, fraction=0.6)<jupyter_output>[__set_matrix] Original Matrix 'x' has shape 19084 x 247
[__set_matrix] Removed 16689 genes that were not in 'subset_genes'. 2395 genes remaining.
[__set_matrix] Removed 61 genes that were not expressed in any samples. 2334 genes remaining.
[__set_matrix] Removed 0 samples that were not annotated in 'phases'. 247 samples remaining.
[__set_matrix] Matrix truncation done. Working with 2334 genes for 247 samples.
[sandbag] Identifying marker pairs...Processing in parallel with 10 processes...
 Done!
[sandbag] Identified 8146 marker pairs (phase: count): {'G1': 2575, 'S': 4101, 'G2M': 1470}<jupyter_text>Construct network from G2M marker pairs  Adding Nodes and Edges from pairs (A,B) => (A)->(B)<jupyter_code># Load matrix
oscope_gencounts = pandas.read_csv(Path(data + "GSE64016_H1andFUCCI_normalized_EC_human.csv"))

# Set index right
oscope_gencounts.set_index("Unnamed: 0", inplace=True)

# Subset sorted
oscope_gencounts_sorted = oscope_gencounts.iloc[:, [oscope_gencounts.columns.get_loc(c) for c in oscope_gencounts.columns if "G1_" in c or "G2_" in c or "S_" in c]]


genes = oscope_gencounts_sorted.index.tolist()

marker_edges = []
for marker in oscope_marker_pairs["G2M"]:
    marker_edges.append((genes.index(marker[0]), genes.index(marker[1])))


marker_edges
net_G2M=nx.DiGraph()

net_G2M.add_edges_from(marker_edges)<jupyter_output><empty_output><jupyter_text>Position on plot<jupyter_code>pos=nx.spring_layout(net_G2M)

dmin=1
ncenter=0
for n in pos:
    x,y=pos[n]
    d=(x-0.5)**2+(y-0.5)**2
    if d<dmin:
        ncenter=n
        dmin=d

pos<jupyter_output><empty_output><jupyter_text>Plot, color according to degree of connectivity<jupyter_code>p=nx.single_source_shortest_path_length(net_G2M,ncenter)

edge_trace = go.Scatter(
    x=[],
    y=[],
    line=go.Line(width=0.5,color='#888'),
    hoverinfo='none',
    mode='lines')

for edge in net_G2M.edges():
    x0, y0 = pos[edge[0]]
    x1, y1 = pos[edge[1]]
    edge_trace['x'] += [x0, x1, None]
    edge_trace['y'] += [y0, y1, None]

node_trace = go.Scatter(
    x=[],
    y=[],
    text=[],
    mode='markers',
    hoverinfo='text',
    marker=go.Marker(
        showscale=True,
        # colorscale options
        # 'Greys' | 'Greens' | 'Bluered' | 'Hot' | 'Picnic' | 'Portland' |
        # Jet' | 'RdBu' | 'Blackbody' | 'Earth' | 'Electric' | 'YIOrRd' | 'YIGnBu'
        colorscale='YIGnBu',
        reversescale=True,
        color=[],
        size=10,
        colorbar=dict(
            thickness=15,
            title='Node Connections',
            xanchor='left',
            titleside='right'
        ),
        line=dict(width=2)))

for node in net_G2M.nodes():
    x, y = pos[node]
    node_trace['x'].append(x)
    node_trace['y'].append(y)
    
for node in net_G2M.nodes:
    node_trace['marker']['color'].append(net_G2M.degree(node))
    node_info = '# of connections: '+str(net_G2M.degree(node))
    node_trace['text'].append(node_info)

fig = go.Figure(data=go.Data([edge_trace, node_trace]),
             layout=go.Layout(
                title='Network of Marker pairs',
                titlefont=dict(size=16),
                showlegend=False,
                hovermode='closest',
                margin=dict(b=20,l=5,r=5,t=40),
                annotations=[ dict(
                    showarrow=False,
                    xref="paper", yref="paper",
                    x=0.005, y=-0.002 ) ],
                xaxis=go.XAxis(showgrid=False, zeroline=False, showticklabels=False),
                yaxis=go.YAxis(showgrid=False, zeroline=False, showticklabels=False)))

iplot(fig, filename='networkx')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13209239739851986, 'fraction_numerical': 0.027360394707333483, 'mean_word_length': 3.74468085106383, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 22, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '520960', 'license': 'MIT', 'n_tokens_mistral': 1686, 'n_tokens_neox': 1573, 'n_words': 331, 'path': 'supplementary/notebooks/5. Extension/5.2 Pairs network.ipynb', 'repo_name': 'bebatut/pypairs', 'size': '4459'}