def to_structure(matrix, alpha=1):
    """Compute best matching 3D genome structure from underlying input matrix
    using ShRec3D-derived method from Lesne et al., 2014.

    Link: https://www.ncbi.nlm.nih.gov/pubmed/25240436

    The method performs two steps: first compute distance matrix by treating
    contact data as an adjacency graph (of weights equal to a power law
    function of the data), then embed the resulting distance matrix into
    3D space.

    The alpha parameter influences the weighting of contacts: if alpha < 1
    long-range interactions are prioritized; if alpha >> 1 short-range
    interactions have more weight wahen computing the distance matrix.
    """

    connected = largest_connected_component(matrix)
    distances = to_distance(connected, alpha)
    n, m = connected.shape
    bary = np.sum(np.triu(distances, 1)) / (n**2)  # barycenters
    d = np.array(np.sum(distances**2, 0) / n - bary)  # distances to origin
    gram = np.array([(d[i] + d[j] - distances[i][j]**2) / 2 for i,
                     j in itertools.product(range(n), range(m))]).reshape(n, m)
    normalized = gram / np.linalg.norm(gram, 'fro')

    try:
        symmetric = np.array((normalized + normalized.T) / 2,
                             dtype=np.longfloat)  # just in case
    except AttributeError:
        symmetric = np.array((normalized + normalized.T) / 2)

    from scipy import linalg
    eigen_values, eigen_vectors = linalg.eigh(symmetric)
    if not (eigen_values >= 0).all():
        warnings.warn("Negative eigen values were found.")
    idx = eigen_values.argsort()[-3:][::-1]
    values = eigen_values[idx]
    vectors = eigen_vectors[:, idx]
    coordinates = vectors * np.sqrt(values)
    return coordinates
def to_structure ( matrix , alpha = 1 ) : connected = largest_connected_component ( matrix ) distances = to_distance ( connected , alpha ) n , m = connected . shape bary = np . sum ( np . triu ( distances , 1 ) ) / ( n ** 2 ) d = np . array ( np . sum ( distances ** 2 , 0 ) / n - bary ) gram = np . array ( [ ( d [ i ] + d [ j ] - distances [ i ] [ j ] ** 2 ) / 2 for i , j in itertools . product ( range ( n ) , range ( m ) ) ] ) . reshape ( n , m ) normalized = gram / np . linalg . norm ( gram , 'fro' ) try : symmetric = np . array ( ( normalized + normalized . T ) / 2 , dtype = np . longfloat ) except AttributeError : symmetric = np . array ( ( normalized + normalized . T ) / 2 ) from scipy import linalg eigen_values , eigen_vectors = linalg . eigh ( symmetric ) if not ( eigen_values >= 0 ) . all ( ) : warnings . warn ( "Negative eigen values were found." ) idx = eigen_values . argsort ( ) [ - 3 : ] [ : : - 1 ] values = eigen_values [ idx ] vectors = eigen_vectors [ : , idx ] coordinates = vectors * np . sqrt ( values ) return coordinates
Compute best matching 3D genome structure from underlying input matrix
    using ShRec3D-derived method from Lesne et al., 2014.

    Link: https://www.ncbi.nlm.nih.gov/pubmed/25240436

    The method performs two steps: first compute distance matrix by treating
    contact data as an adjacency graph (of weights equal to a power law
    function of the data), then embed the resulting distance matrix into
    3D space.

    The alpha parameter influences the weighting of contacts: if alpha < 1
    long-range interactions are prioritized; if alpha >> 1 short-range
    interactions have more weight wahen computing the distance matrix.