Advertisement
Guest User

Rosalind common ancestor

a guest
May 6th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. from collections import Counter
  2. #misread the question and made the program receive input from a text file as opposed to entering it in line by line.
  3. def populateMatrix(fileName): #File name as a string .txt included
  4.     matrixData = open(fileName,"r")
  5.     matrix = []
  6.     for i in matrixData:
  7.         toAppend = ''.join(i.split())
  8.         matrix.append(toAppend)
  9.     return matrix
  10.  
  11. def getPartialProfile(fileName): ##returns the profile without doing any counting
  12.     profiles = []
  13.     matrix = list(populateMatrix(fileName))
  14.     profileLen = len(matrix)
  15.     for i in range(len(matrix[0])):
  16.         toAppend = ""
  17.         single_profile = ""
  18.         for j in range(profileLen):
  19.             single_profile = single_profile +(matrix[j][i])
  20.         toAppend = ''.join(single_profile.split())
  21.         profiles.append(toAppend)
  22.     return profiles
  23.  
  24. def getSymbol(index):
  25.     options = {0: "A", 1: "C", 2: "G",3:"T"}
  26.     return options[index]
  27.  
  28. def getProfile(fileName): ##returns the count of each character(ACGT) from the partial string
  29.     profiles = list(getPartialProfile(fileName))
  30.     profileCount = []
  31.     finalProfile= []
  32.     DNA = ["A","C","G","T"]
  33.    
  34.     for i in profiles:
  35.         profileCount = []
  36.         for j in DNA:
  37.             profileCount.append(i.count(j))
  38.         finalProfile.append(profileCount)
  39.        
  40.     return finalProfile
  41.            
  42. def printProfile(fileName): ##prints the profile in the form asked by the website.Done in a seperate function as getConsensus needs some data from the getProfile
  43.     profile = list(getProfile(fileName))
  44.     profile = [["A: ","C: ","G: ","T: "]] + profile
  45.     for i in range(len(profile[0])):
  46.         toPrint = []
  47.         for j in range(len(profile)):
  48.             toPrint.append(profile[j][i])
  49.         for z in toPrint:
  50.             print(str(z) + " ", end = '')
  51.         print("")
  52.  
  53. def getConsensus(fileName): ##returns the consensus using the data from the getProfile function
  54.     profile = list(getProfile(fileName))
  55.     consensus = ""
  56.     for i in profile:
  57.         highestIndex = i.index(max(i))
  58.         consensus = consensus + getSymbol(highestIndex)
  59.     return consensus
  60.  
  61. print(getConsensus("test1.txt"))
  62. printProfile("test1.txt")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement