Lower Triangle Matrix

Rune Toft

I am trying to create a lower triangle matrix, without imports (though I use a math import in the beginning). The function I have written takes a list of DNA sequences, and compares them and inputs a float in a list of lists. The problem, however is that I accidentally created the upper triangle. Here is my code (I'll include the other functions so it might make sense):

def sequence_difference(seq1, seq2):
    counter = 0

    if len(seq1) == len(seq2):
        for i in range(len(seq1)):
            if seq1[i] != seq2[i]:
                 counter += 1
        return counter / len(seq1)

    else:
         return ''

def jukes_cantor(diff):
    K = -(3/4) * log(1 - (4/3) * diff)
    return K

def lower_trian_matrix(list_of_seq):
    matrix = []

    for i in range(len(list_of_seq)):
         matrix.append([])

         for j in range(i + 1, len(list_of_seq)):
            matrix[i].append(jukes_cantor(sequence_difference(list_of_seq[j], list_of_seq[i])))     
    return matrix

sequences = ['TAAAAAAAAAAA',
             'TTAAAAAAAAAA', 
             'AAAAAAAAAAGG', 
             'AAAAAAAAGGGG']

print(lower_trian_matrix(sequences))

It outputs:

[[0.08833727674228764, 0.30409883108112323, 0.6081976621622466], 
 [0.4408399986765892, 0.8239592165010822], 
 [0.18848582121067953], 
 []]

But I want it to output:

[[], 
 [0.08833727674228764], 
 [0.30409883108112323, 0.4408399986765892], 
 [0.6081976621622466, 0.8239592165010822, 0.18848582121067953]]

The sequence_difference returns how different two sequences are in frequencies. The jukes_cantor returns a corrected estimate of how much the sequences actually have changed over the course of evolution.

Mr. T

Just reverse the indexing for i and j:

from math import log

def sequence_difference(seq1, seq2):
    counter = 0

    if len(seq1) == len(seq2):
        for i in range(len(seq1)):
            if seq1[i] != seq2[i]:
                counter += 1
        return counter / len(seq1)

    else:
        return ''

def jukes_cantor(diff):
    K = -(3/4) * log(1 - (4/3) * diff)
    return K

def lower_trian_matrix(list_of_seq):
    matrix = [[]]

    for i in range(1, len(list_of_seq)):
        matrix.append([])

        for j in range(i):
            matrix[i].append(jukes_cantor(sequence_difference(list_of_seq[j], list_of_seq[i])))     
    return matrix

sequences = ['TAAAAAAAAAAA',
             'TTAAAAAAAAAA', 
             'AAAAAAAAAAGG', 
             'AAAAAAAAGGGG']

print(lower_trian_matrix(sequences))

Alternatively, rename it to def upper_trian_matrix(list_of_seq): and tell your teacher, you always wanted to create the upper triangle.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Storing lower triangle of a matrix into an array

Extract lower triangle portion of a matrix

Copy upper triangle to lower triangle in a python matrix

matrix - mirror lower triangle to upper triangle

R reverse lower triangle matrix loop

Is there a way to parallelize a lower triangle matrix solver?

how to count the lower triangle elements in a matrix

How to reset the 'lower triangle' of a 3 dimentional matrix

Python generate a mask for the lower triangle of a matrix

Forcing upper triangle values to lower triangle values in matrix after melt

Extract respective upper and lower triangle's elements of matrix in R

R / creating a symmetric matrix out of the vector of the lower triangle elements

Pandas equivalent of max(lower triangle, upper triangle) on square matrix-like dataFrame

Restructure a matrix (which contains non-symmetrical values) into a lower triangle matrix format

How can I create a correlation matrix from 2 vectors of its upper and lower triangle in python?

Identifying the nth inserted element of matrix which fills by inserting alternating upper and lower triangle elements

breeze Copy lower triangle not working?

Plot lower triangle in a seaborn Pairgrid

Lower triangle mask with seaborn clustermap

numpy array to triangle (matrix)

Lower triangular matrix in julia

Highlight the cells on the left lower half of the table(triangle)

Border around a upper/lower triangle of imshow

select the corresponding cell in the upper/lower triangle

How to keep lower triangle plus diagonal in r

How to show only the lower triangle in ggpairs?

R / Index lower triangle vector by pairwise indices

Wrong output when creating a lower diagonal triangle

Replacing part of a triangle matrix by a vector