Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import turtle
- from folding import score
- def create_chains_base(path):
- """
- Reads 'chains' of amino acids from file
- and creates a nested list, with an
- element==one chain of amino acid
- length of the list == number of chains
- :param path: path to the data-file
- :return: nested list of chains
- """
- def read_from_file():
- global mystring
- mystring = ''
- myfile = open(path)
- while True:
- mystring += myfile.readline()
- if len(myfile.readline())==0:
- break
- def list_the_string():
- global listofvecs, mylist
- listofvecs = []
- mylist = mystring.split("\n")
- for c in range(0,len(mylist)-1):
- listofvecs.append(list(mylist[c]))
- read_from_file()
- list_the_string()
- return listofvecs
- def visualize(chain,folding):
- """
- visualising the fold of an amino-acid
- chain. (requires turtle.py)
- :param chain: a chain of amino-acid
- :param folding: vector of positioning of the
- next element of chain
- :return: None
- Graphicaly visualising the folding of
- amino-acid chains.
- """
- def create_screen():
- global bubble, wn
- wn = turtle.Screen()
- wn.bgcolor("white")
- wn.title('Visualisation of folding of amino-acids')
- bubble = turtle.Turtle()
- if len(chain)>20:
- wn.screensize(2500,2500)
- bubble.speed(1000)
- bubble.hideturtle()
- bubble.speed(1)
- def draw_first_bubble():
- bubble.color("blue")
- bubble.dot(10)
- bubble.forward(30)
- def draw_bubbles():
- p = 0
- c = 0
- draw_first_bubble()
- for i in chain:
- if i=='0' or i==0:
- bubble.color('blue')
- else:
- bubble.color('red')
- bubble.begin_fill()
- bubble.dot(10)
- if p!=len(folding):
- if folding[c]==1:
- bubble.seth(0)
- if folding[c]==1j:
- bubble.seth(90)
- if folding[c]==-1:
- bubble.seth(180)
- if folding[c]==-1j:
- bubble.seth(270)
- c+=1
- p+=1
- if p!=(len(chain)):
- bubble.forward(30)
- bubble.end_fill()
- create_screen()
- draw_bubbles()
- wn.mainloop()
- def solver(chain):
- """
- Returns the possible 'best' folding of amino-acid
- :param chain: current chain of amino-acid
- :return: folding of amino-acid
- """
- folding=[]
- coords = []
- N = len(chain)*len(chain)
- X_cord = len(chain)
- Y_cord = len(chain)
- for i in range(0,len(chain)-1):
- folding.append(1)
- for i in range(0,N):
- coords.append([0] * (N))
- for j in range(0,len(chain)-1):
- for i in range(0,len(chain)-1):
- basic_score = score(chain,folding)
- folding[i] = 1j
- if score(chain,folding)<=basic_score and coords[X_cord][Y_cord+1]!=1:
- continue
- folding[i] = -1j
- if score(chain,folding)<=basic_score and coords[X_cord][Y_cord-1]!=1:
- continue
- folding[i] = -1
- if score(chain,folding)<=basic_score and coords[X_cord-1][Y_cord]!=1:
- continue
- folding[i] = 1
- if folding[i] == 1j:
- coords[X_cord][Y_cord+1] = 1
- Y_cord+=1
- if folding[i] == -1j:
- coords[X_cord][Y_cord-1] = 1
- Y_cord-=1
- if folding[i] == -1:
- coords[X_cord-1][Y_cord] = 1
- Y_cord-=1
- if folding[i] == 1:
- coords[X_cord+1][Y_cord] = 1
- X_cord+=1
- for c in coords:
- for i in coords:
- print(c)
- print('\n')
- return folding
- def create_foldings_base(chains):
- """
- Takes all foldings together is a list
- :param chains:list of all chains of amino-acids
- :return: list of all foldings of amino-acids
- """
- global foldings
- foldings=[]
- for chain in chains:
- foldings.append(solver(chain))
- return foldings
- def scoring(chains,foldings):
- """
- Sums the scores of all chain-folding pairs
- :param chains: list of all chains of amino-acids
- :param foldings: list of all foldings of amino-acids
- :return: result of writing this code
- """
- sum=0
- for i in range(0,5):
- sum+=score(chains[i],foldings[i])
- return sum/len(chains)
- visualize([0,1,1,1,1,0,],solver([0,1,1,1,1,0]))
- #chains = create_chains_base("testsuite.txt")
- #foldings = create_foldings_base(chains[:5])
- #print("%.1f" % scoring(chains,foldings))
- #print(foldings)
- #print(foldings)
- #print(chains)
- #visualize(chains[3],foldings[3])
- #visualize([0, 1, 1, 1, 1, 0],solver([0,1,1,1,1,0,]))
- #print(score([0, 1, 1, 1, 1, 0],[1, 1, 1j, -1, -1]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement