# import data from url
from py2cytoscape.data.cyrest_client import CyRestClient
from IPython.display import Image

# Create REST client for Cytoscape
cy = CyRestClient()

# Reset current session for fresh start
cy.session.delete()

# Load a sample network
network = cy.network.create_from('http://chianti.ucsd.edu/~kono/data/galFiltered.sif')

# Apply layout to the cytoscape network object
cy.layout.apply(network = network)

# Show it!!
network_png = network.get_png()
Image(network_png)

"""
Explanation: Analysis

When you vizualize your network, you also want to analize the network. In this section, you can learn basic analysis methods to network. The methods used in this section are prepared in the Python's libraries, igraph, networkx and so on. So, we will also use them to analyze network and if you want to get more detail about this examples or find more information, please look at these documentations.
Further Information about igraph


igraph : http://igraph.org
Python-igraph : http://igraph.org/python/
networkx : https://networkx.github.io

Table of contents



Global Network Analysis

Density
Transivity
community detection



Node Analysis

Closeness
Degree
PageRank
community detection



Edge Analysis

EdgeBetweenness
community detection



Network Data Preparation

To prepare network analysis, let's load network data.
End of explanation
"""


# covert cytoscape network object to igraph object
import igraph as ig
import py2cytoscape.util.util_igraph as util_ig

# convert cytoscape object to igraph object
g = util_ig.to_igraph(network.to_json())

"""
Explanation: Global Network Analysis

In this example, we will use "igraph" to analyze global network propety. 
"igraph" are the very popular and useful package and you can analyze your own network by this.
And py2cytoscape can convert their own network to igraph object. So, first, we have to convert cytoscape network object to igraph object.
End of explanation
"""


density = g.density()
print(density)

"""
Explanation: Density

Calculates the density of the graph.
Further information : http://igraph.org/python/doc/igraph.GraphBase-class.html#density
End of explanation
"""


transitivity  = g.transitivity_undirected()
print(transitivity)

"""
Explanation: Transitivity

There are three methods to calculate the transitivity in igraph. In the following methods, we will use "transitivity_undirected" method.
Method : transitivity_avglocal_undirected
Calculates the average of the vertex transitivities of the graph.
http://igraph.org/python/doc/igraph.GraphBase-class.html#transitivity_avglocal_undirected
Method : transitivity_local_undirected
Calculates the local transitivity (clustering coefficient) of the given vertices in the graph.
http://igraph.org/python/doc/igraph.GraphBase-class.html#transitivity_local_undirected
Method : transitivity_undirected
Calculates the global transitivity (clustering coefficient) of the graph.
http://igraph.org/python/doc/igraph.GraphBase-class.html#transitivity_undirected
End of explanation
"""


# If you want to use this method, you have to use the non-multiple-edges graph object.
#community_fastgreedy = g.community_fastgreedy()
#print(community_fastgreedy)

"""
Explanation: community detection

Finds the community structure of the graph according to the algorithm of Clauset et al based on the greedy optimization of modularity.
http://igraph.org/python/doc/igraph.GraphBase-class.html#community_fastgreedy
End of explanation
"""


closeness = g.closeness()

# Show 10 results of node closeness
print(closeness[0:9])

"""
Explanation: Node Analysis

Closeness

Calculates the closeness centralities of given vertices in a graph.
http://igraph.org/python/doc/igraph.GraphBase-class.html#closeness
End of explanation
"""


indegree = g.indegree()
outdegree = g.outdegree()
# Show 10 results of node degree
print(indegree[0:9])
print(outdegree[0:9])

"""
Explanation: Degree


indegree
Returns the in-degrees in a list.   source code
http://igraph.org/python/doc/igraph.Graph-class.html#indegree
outdegree
Returns the out-degrees in a list.
http://igraph.org/python/doc/igraph.Graph-class.html#outdegree
End of explanation
"""


pagerank = g.pagerank()

# Show 10 results of node degree
print(pagerank[0:9])

"""
Explanation: PageRank

Calculates the Google PageRank values of a graph.
http://igraph.org/python/doc/igraph.Graph-class.html#pagerank
End of explanation
"""


# If you want to use this method, you have to use the non-multiple-edges graph object.
#community_fastgreedy = g.community_fastgreedy()
#print(community_fastgreedy)

"""
Explanation: community detection

Finds the community structure of the graph according to the algorithm of Clauset et al based on the greedy optimization of modularity.
http://igraph.org/python/doc/igraph.GraphBase-class.html#community_fastgreedy
End of explanation
"""


edge_betweenness = g.edge_betweenness()
print(edge_betweenness[0:9])

"""
Explanation: Edge Analysis

EdgeBetweenness

Calculates or estimates the edge betweennesses in a graph.
http://igraph.org/python/doc/igraph.GraphBase-class.html#edge_betweenness
End of explanation
"""


community_edge_betweenness_detection = g.community_edge_betweenness()
print(community_edge_betweenness_detection)

"""
Explanation: community detection

Community structure detection based on the betweenness of the edges in the network. 
http://igraph.org/python/doc/igraph.GraphBase-class.html#community_edge_betweenness
End of explanation
"""


