Skip to content
Snippets Groups Projects
Commit 608b5253 authored by Mélanie  HENNART's avatar Mélanie HENNART
Browse files

Delete InheritanceAlgorithm_v08042021.py

parent 1be895e4
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 5 15:54:10 2018
@author: mhennart
"""
import pandas as pd
import networkx as nx
def Mappind_cluster_ST (data, seuil ) :
Df = pd.crosstab(data['ST'], data[seuil])
Df = Df.drop('NA')
B = nx.Graph()
B.add_nodes_from(Df.index, bipartite=1)
B.add_nodes_from(Df.columns, bipartite=0)
for i in Df.index :
for j in Df.columns :
if Df[j][i] > 0 :
B.add_edge(i,j, weight=Df[j][i])
dico = {}
ST_attribute = []
while len(B.edges) > 0 :
edges = B.edges(data=True)
max_weight_edges = max([edge[2]['weight'] for edge in edges])
edges_subgraph = [(u,v) for (u,v,d) in B.edges(data=True) if d['weight'] == max_weight_edges]
C = B.edge_subgraph(edges_subgraph).copy()
for connected_component in nx.connected_components(C) :
S = C.subgraph(connected_component).copy()
top_nodes = {n for n, d in S.nodes(data=True) if d["bipartite"] == 0}
bottom_nodes = set(S) - top_nodes
if len(top_nodes) == 1 :
u = list(top_nodes)[0]
v = min(bottom_nodes)
B.remove_nodes_from((u,v))
dico[u]= [ v , 'relative_ST' ]
ST_attribute.append(v)
else :
no_strains_nodes = [(sum(d['weight'] for (u,v,d) in C.edges(top_node, data=True)), top_node) for top_node in top_nodes]
no_strains_nodes.sort()
for (no_strains_node, node) in no_strains_nodes :
u = node
degree = [(S.degree[x], x) for x in S.adj[u] if x not in ST_attribute ]
if len(degree) > 0 :
v = min(degree)[1]
B.remove_nodes_from((u,v))
dico[u]= [ v , 'relative_ST' ]
ST_attribute.append(v)
top_nodes = {n for n, d in B.nodes(data=True) if d["bipartite"] == 0}
lambda_ = 10000
for u in top_nodes :
dico[u]= [lambda_ , "arbitrary"]
lambda_ += 1
mapping = data[seuil].apply(lambda x : dico[x][0])
attribute = data[seuil].apply(lambda x : dico[x][1])
return mapping, attribute
in_ = "PartionnementMLST_ALL.txt"
data = pd.read_csv(in_, sep='\t', index_col=0, dtype=str)
data = data.fillna('NA')
seuils = ['Seuil43', 'Seuil190']
for seuil in seuils :
mapping, attribute = Mappind_cluster_ST (data, seuil)
data['Mapping_'+seuil] = mapping
data['Attribute_'+seuil] = attribute
data.to_csv('ResultatsMapping_CG_SL.txt', sep="\t")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment