From dfe4789af2f9e49ee288896aaaee3ad19099838b Mon Sep 17 00:00:00 2001 From: nicolas-zimmermann Date: Wed, 18 Sep 2019 16:01:27 +0200 Subject: [PATCH] modified code (__init__) to save distance matrixes in Conformations instances --- src/projet8.py | 38 +++++++++++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 9 deletions(-) diff --git a/src/projet8.py b/src/projet8.py index 2b7a029..8f1e702 100644 --- a/src/projet8.py +++ b/src/projet8.py @@ -21,11 +21,23 @@ class Conformations: def __init__(self, filename): """ - df : pd.DataFrame object + filename : a .pdb file or several pdb file containing the conformations + + Attribute : + - df : pd.DataFrame object + - filename : a list of str, paths to the structure files + - simple_dist : matrix distance computed with the identity method + Initiality empty, must be computed + - dissimilarity_dist : matrix distance computed with the + dissimilarity method, initialy empty because + the computation time is long Each row of the dataframe is a conformation and each column a position of the sequence. """ self.df = pd.DataFrame() + self.filename = filename + self.identity_dist = None + self.dissimilarity_dist = None for chain_name, chain in pbx.chains_from_files([filename]): dihedrals = chain.get_phi_psi_angles() pb_seq = pbx.assign(dihedrals) @@ -34,7 +46,7 @@ class Conformations: def identity(self): """ - Returns a matrix of the distance between all conformations based on + Computes a distance matrix between all conformations based on wether the PB at each position is identical or not. Returns the matrix as a numpy.ndarray object. """ @@ -43,8 +55,10 @@ class Conformations: 'o':15,'p':16} # dict to transform PB to int, # necessary for the pdsit function dfnum = self.df.replace(dict_str_to_float) + dist = squareform(pdist(dfnum, metric ='jaccard')) + self.identity_dist = dist - return squareform(pdist(dfnum, metric ='jaccard')) + return dist def dissimilarity(self, matrix=None): """ @@ -69,9 +83,9 @@ class Conformations: dissimilarity[i][j] += matrix[confs.df.loc[i,k]][confs.df.loc[j,k]] dissimilarity = dissimilarity + dissimilarity.T # fills the whole matrix - return dissimilarity + return self.dist_from_dissimilarity(dissimilarity) - def dist_from_dissimilarity(diss_matrix): + def dist_from_dissimilarity(self, diss_matrix): """ Using the substitution matrix from the PBxplore package, the obtained dissimilarity matrix has both positive and negative values. Low value @@ -85,14 +99,20 @@ class Conformations: diss_matrix = -diss_matrix diss_matrix = (diss_matrix - np.min(diss_matrix))/np.ptp(diss_matrix) dist = diss_matrix * abs((np.identity(confs.df.shape[0])-1)) - + + self.dissimilarity_dist = dist + return dist + + #def small_kmedoid(self, ncluster): + # pass + if __name__ == "__main__": - if len(sys.argv) != 2: + if len(sys.argv) < 2: sys.exit("Error : usage '$ python3 projet8 md.pdb'") - + confs = Conformations(sys.argv[1]) - + print(confs.df)