<s> """<STR_LIT>""" <EOL> import numpy <EOL> import itertools <EOL> import pyRMSD . RMSDCalculator <EOL> import pyRMSD . condensedMatrix <EOL> def combopermutations ( elements_list , prefix = [ ] ) : <EOL> """<STR_LIT>""" <EOL> if len ( elements_list ) <= <NUM_LIT:1> : <EOL> for perm in itertools . permutations ( elements_list [ <NUM_LIT:0> ] ) : <EOL> yield prefix + list ( perm ) <EOL> else : <EOL> for perm in itertools . permutations ( elements_list [ <NUM_LIT:0> ] ) : <EOL> for perm2 in combopermutations ( elements_list [ <NUM_LIT:1> : ] , prefix + list ( perm ) ) : <EOL> yield list ( perm2 ) <EOL> class ChainMappingBuilder : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> pass <EOL> @ classmethod <EOL> def getStructureChains ( cls , structure , in_chain_selection ) : <EOL> """<STR_LIT:U+0020>""" <EOL> chains = structure . getHierView ( ) <EOL> number_of_models = structure . numCoordsets ( ) <EOL> structure_chains = [ ] <EOL> for i in range ( number_of_models ) : <EOL> struct_chain = { } <EOL> for chain in chains : <EOL> struct_chain [ chain . getChid ( ) ] = chain . getCoordsets ( i ) <EOL> structure_chains . append ( struct_chain ) <EOL> return structure_chains <EOL> @ classmethod <EOL> def reorderCoordinates ( cls , structure_chain , chain_permutation ) : <EOL> """<STR_LIT:U+0020>""" <EOL> new_coordinates = [ ] <EOL> for chain_id in chain_permutation : <EOL> for atom_coords in structure_chain [ chain_id ] : <EOL> new_coordinates . append ( atom_coords ) <EOL> return numpy . array ( new_coordinates ) <EOL> @ classmethod <EOL> def getReorderedCoordinatesByLenGroups ( cls , structure , selection , len_groups ) : <EOL> """<STR_LIT>""" <EOL> chains = structure . getHierView ( ) <EOL> coordsets = None <EOL> for group_len in sorted ( len_groups . keys ( ) ) : <EOL> for chid in sorted ( len_groups [ group_len ] ) : <EOL> if coordsets is None : <EOL> coordsets = chains [ chid ] . getCoordsets ( ) <EOL> else : <EOL> coordsets = numpy . concatenate ( ( coordsets , chains [ chid ] . getCoordsets ( ) ) , axis = <NUM_LIT:1> ) <EOL> return coordsets <EOL> @ classmethod <EOL> def getChainLengths ( cls , structure , selection ) : <EOL> chains = structure . getHierView ( ) <EOL> chain_len_map = { } <EOL> for chain in chains : <EOL> chain_len_map [ chain . getChid ( ) ] = chain . numAtoms ( ) <EOL> return chain_len_map <EOL> @ classmethod <EOL> def getChainLenGroups ( cls , chain_len_map ) : <EOL> groups = { } <EOL> for chid in chain_len_map : <EOL> try : <EOL> groups [ chain_len_map [ chid ] ] . append ( chid ) <EOL> except : <EOL> groups [ chain_len_map [ chid ] ] = [ chid ] <EOL> return groups <EOL> @ classmethod <EOL> def getPermGroups ( cls , chain_len_groups ) : <EOL> perm_groups = [ ] <EOL> for group_len in sorted ( chain_len_groups . keys ( ) ) : <EOL> perm_groups . append ( chain_len_groups [ group_len ] ) <EOL> return perm_groups <EOL> @ classmethod <EOL> def calcRMSDMatrix ( cls , structure_data , calculator_type , in_chain_selection ) : <EOL> """<STR_LIT:U+0020>""" <EOL> structure = structure_data . getSelection ( in_chain_selection ) <EOL> chain_structures = cls . getStructureChains ( structure , in_chain_selection ) <EOL> chain_len_map = cls . getChainLengths ( structure , in_chain_selection ) <EOL> chain_len_groups = cls . getChainLenGroups ( chain_len_map ) <EOL> chain_coordsets = cls . getReorderedCoordinatesByLenGroups ( structure , in_chain_selection , chain_len_groups ) <EOL> perm_groups = cls . getPermGroups ( chain_len_groups ) <EOL> matrix_data = [ ] <EOL> for i in range ( len ( chain_structures ) - <NUM_LIT:1> ) : <EOL> chain_structure = chain_structures [ i ] <EOL> min_rmsd = None <EOL> for chain_perm in combopermutations ( perm_groups ) : <EOL> new_coords = cls . reorderCoordinates ( chain_structure , chain_perm ) <EOL> calculator = pyRMSD . RMSDCalculator . RMSDCalculator ( calculator_type , <EOL> numpy . concatenate ( [ [ new_coords ] , chain_coordsets [ i + <NUM_LIT:1> : ] ] ) ) <EOL> rmsd = calculator . oneVsFollowing ( <NUM_LIT:0> ) <EOL> if min_rmsd is None : <EOL> min_rmsd = rmsd <EOL> else : <EOL> min_rmsd = numpy . minimum ( rmsd , min_rmsd ) <EOL> matrix_data . extend ( min_rmsd ) <EOL> return pyRMSD . condensedMatrix . CondensedMatrix ( numpy . array ( matrix_data ) ) </s>
