title: Rosalind consensus and profile python
body: <p>I'm working on the problem "Consensus nd Profile" on the Rosalind Bioinformatics website (<a href="http://rosalind.info/problems/cons/" rel="nofollow">http://rosalind.info/problems/cons/</a>). I tried my code using the sample input on the website and my output matches the sample output. But when I tried the larger dataset the website said my output is wrong. Could someone help me identify where my problem is? Thank you!</p>
  
  <p>Sample input:</p>
  
  <pre><code>&gt;Rosalind_1
  ATCCAGCT
  &gt;Rosalind_2
  GGGCAACT
  &gt;Rosalind_3
  ATGGATCT
  &gt;Rosalind_4
  AAGCAACC
  &gt;Rosalind_5
  TTGGAACT
  &gt;Rosalind_6
  ATGCCATT
  &gt;Rosalind_7
  ATGGCACT
  </code></pre>
  
  <p>I've extracted the dna strings and stored them in a list called strings (my trial with the larger dataset is correct at this step so I omitted my code here):</p>
  
  <pre><code>['ATCCAGCT', 'GGGCAACT', 'ATGGATCT', 'AAGCAACC', 'TTGGAACT', 'ATGCCATT', 'ATGGCACT']
  </code></pre>
  
  <p>My code afterwards:</p>
  
  <pre><code>#convert strings into matrix
  matrix = []
  for i in strings:
      matrix.append([j for j in i])
  M = np.array(matrix).reshape(len(matrix),len(matrix[0]))
  </code></pre>
  
  <p>M looks like this for sample input:</p>
  
  <pre><code>[['A' 'T' 'C' 'C' 'A' 'G' 'C' 'T']
   ['G' 'G' 'G' 'C' 'A' 'A' 'C' 'T']
   ['A' 'T' 'G' 'G' 'A' 'T' 'C' 'T']
   ['A' 'A' 'G' 'C' 'A' 'A' 'C' 'C']
   ['T' 'T' 'G' 'G' 'A' 'A' 'C' 'T']
   ['A' 'T' 'G' 'C' 'C' 'A' 'T' 'T']
   ['A' 'T' 'G' 'G' 'C' 'A' 'C' 'T']]
  </code></pre>
  
  <p>My code afterwards:</p>
  
  <pre><code>#convert string matrix into profile matrix
  A = []
  C = []
  G = []
  T = []
  for i in range(len(matrix[0])):
      A_count = 0
      C_count = 0
      G_count = 0
      T_count = 0
      for j in M[:,i]:
          if j == "A":
              A_count += 1
          elif j == "C":
              C_count += 1
          elif j == "G":
              G_count += 1
          elif j == "T":
              T_count += 1
      A.append(A_count)
      C.append(C_count)
      G.append(G_count)
      T.append(T_count)
  
  profile_matrix = {"A": A, "C": C, "G": G, "T": T}
  for k, v in profile_matrix.items():
      print k + ":" + " ".join(str(x) for x in v)
  
  #get consensus string
  P = []
  P.append(A)
  P.append(C)
  P.append(G)
  P.append(T)
  profile = np.array(P).reshape(4, len(A))
  consensus = []
  for i in range(len(A)):
      if max(profile[:,i]) == profile[0,i]:
          consensus.append("A")
      elif max(profile[:,i]) == profile[1,i]:
          consensus.append("C")
      elif max(profile[:,i]) == profile[2,i]:
          consensus.append("G")
      elif max(profile[:,i]) == profile[3,i]:
          consensus.append("T")
  print "".join(consensus)
  </code></pre>
  
  <p>These codes give the correct sample output:</p>
  
  <pre><code>A:5 1 0 0 5 5 0 0
  C:0 0 1 4 2 0 6 1
  T:1 5 0 0 0 1 1 6
  G:1 1 6 3 0 1 0 0
  ATGCAACT
  </code></pre>
  
  <p>But when I tried the larger dataset the website said my answer was wrong...Could someone point out where I'm wrong? (I'm a beginner, thank you for your patience!)</p>