0
matrix = []
for index, value in enumerate(['A','C','G','T']):
    matrix.append([])
    matrix[index].append(value + ':')
    for i in range(len(lines[0])):
        total = 0
        for sequence in lines:
            if sequence[i] == value:
               total += 1
        matrix[index].append(total)

unity = ''
for i in range(len(lines[0])):
  column = []
  for row in matrix:
    column.append(row[1:][i])
  maximum = column.index(max(column))
  unity += ['A', 'C', 'G', 'T'][maximum]
print("Unity: " + unity)

for row in matrix:
    print(' '.join(map(str, row)))

OUTPUT:

Unity: GGCTACGC
A: 1 2 0 2 3 2 0 0
C: 0 1 4 2 1 3 2 4
G: 3 3 2 0 1 2 4 1
T: 3 1 1 3 2 0 1 2

With this code I get this matrix but I want to form the matrix like this:

   A  C  G  T 
G: 1  0  3  3
G: 2  1  3  1
C: 0  4  2  1
T: 2  2  0  3
A: 3  1  1  2
C: 2  3  2  0
G: 0  2  4  1
C: 0  4  1  2

But I don't know how. I hope someone can help me. Thanks already for the answers.

The sequences are:
AGCTACGT
TAGCTAGC
TAGCTACG
GCTAGCGC
TGCTAGCC
GGCTACGT
GTCACGTC

ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46
BerTu
  • 91
  • 1
  • 1
  • 6
  • I think this question could be improved if you provide: *a)* an explanation of how your desired matrix reflects your sequences and *b)* how your sequences are stored. – sacuL Nov 02 '18 at 22:40
  • 1
    Are you aware of [numpy](http://www.numpy.org/) and/or [pandas](https://pandas.pydata.org/)? It is just `matrix.T` with those. If you don't want to use them, [this question](https://stackoverflow.com/questions/4937491/matrix-transpose-in-python) contains your answer already. – RunOrVeith Nov 02 '18 at 22:46

2 Answers2

1

You're needing to do a transpose of your matrix. I've added comments in the code below to explain what has been changed to make the table.

matrix = []
for index, value in enumerate(['A','C','G','T']):
    matrix.append([])

    # Don't put colons in column headers
    matrix[index].append(value)
    for i in range(len(lines[0])):
        total = 0
        for sequence in lines:
            if sequence[i] == value:
               total += 1
        matrix[index].append(total)

unity = ''
for i in range(len(lines[0])):
  column = []
  for row in matrix:
    column.append(row[1:][i])
  maximum = column.index(max(column))
  unity += ['A', 'C', 'G', 'T'][maximum]

# Tranpose matrix
matrix = list(map(list, zip(*matrix)))

# Print header with tabs to make it look pretty
print( '\t'+'\t'.join(matrix[0]))

# Print rows in matrix
for row,unit in zip(matrix[1:],unity):
    print(unit + ':\t'+'\t'.join(map(str, row)))

The following will be printed:

    A   C   G   T
G:  1   0   3   3
G:  2   1   3   1
C:  0   4   2   1
T:  2   2   0   3
A:  3   1   1   2
C:  2   3   2   0
G:  0   2   4   1
C:  0   4   1   2
A Kruger
  • 2,289
  • 12
  • 17
0

I think that the best way is to convert your matrix to pandas dataframe and to then use transpose function. https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.transpose.html

ChaosPredictor
  • 3,777
  • 1
  • 36
  • 46