# -*- coding: utf-8 -*-
# !usr/bin/env python
import sys
import math
import numpy as np
np.random.seed(7)


seq_file = open('Protein_OXTR.txt', 'U') #input
abc_file = open('Music_OXTR.txt', 'w') #output

# Read the protein sequence #
heavychain = ''
##lightchain = ''
for eachline in seq_file:
	if 'protseq' in eachline:
		for seq in seq_file:
			if seq.strip():
				heavychain += seq.strip()
			else:
				break

#	if 'light chain' in eachline:
#		for seq in seq_file:
#			if seq.strip():
#				lightchain += seq.strip()
#			else:
#				break

# Read the protein properties #
dict_occurrence = {
	'T': 'C',
	'G': 'C',
	'D': 'C',
	'Y': 'C',
	'L': 'D',
	'N': 'D',
	'K': 'E',
	'Q': 'E',
	'P': 'F',
	'R': 'F',
	'C': 'F',
	'A': 'G',
	'I': 'G',
	'M': 'G',
	'E': 'G',
	'V': 'A',
	'F': 'A',
	'H': 'A',
	'S': 'B',
	'W': 'B'}

dict_aacomposition = {
	'I': 'C',
	'F': 'C',
	'G': 'C',
	'P': 'C',
	'A': 'D',
	'C': 'D',
	'H': 'D',
	'Y': 'E',
	'K': 'E',
	'V': 'F',
	'D': 'F',
	'E': 'G',
	'Q': 'G',
	'N': 'G',
	'L': 'G',
	'R': 'A',
	'M': 'A',
	'T': 'A',
	'S': 'B',
	'W': 'B'}

#property_file_netcharge = open('Property_NetCharge.txt', 'U')
#dict_netcharge = {}
#for eachline in property_file_netcharge:
#	content = eachline.strip().split('\t')
#	dict_netcharge[content[0]] = int(content[1])
dict_netcharge={"L":-1,"I":-1,"N":-1,"G":-1,"V":-1,"E":-1, "P":1,"H":1,"K":1,"A":1,"Y":1,"W":1,"Q":1,"M":1,"S":1,"C":1,"T":1,"F":1,"R":1,"D":1}
dict_hydropathy={"I":-1,"L":-1,"F":-1,"W":-1,"Y":-1,"V":-1,"A":1,"R":1,"N":1,"D":1,"C":1,"Q":1,"E":1,"G":1,"H":1,"K":1,"M":1,"P":1,"S":1,"T":1}
#property_file_size = open('Property_Size.txt', 'U')
#dict_size = {}
#for eachline in property_file_size:
#	content = eachline.strip().split('\t')
#	dict_size[content[0]] = float(content[1])



property_file_resvol = open('Property_ResidueVol.txt', 'U')
dict_resvol = {}
for eachline in property_file_resvol:
	content = eachline.strip().split('\t')
	dict_resvol[content[0]] = float(content[1])

#property_file_hydropathy = open('Property_Hydropathy.txt', 'U')
#dict_hydropathy = {}
#for eachline in property_file_hydropathy
#	content = eachline.strip().split('\t')
#	dict_hydropathy[content[0]] = float(content[1])

#########find main theme#################
noteM=[]
lengthM=[]
note_number={"C":1,"D":3,"E":5,"F":6,"G":8,"A":10,"B":12}
print(len(heavychain))

for i in range(0, len(heavychain)):
        j= i-1

        if j == -1:
                j = len(heavychain)-1


	note=note_number[dict_occurrence[heavychain[i]]]


        if heavychain[j] in ["Y"]: lengthM.append(8)
        elif heavychain[j] in ["W"]: lengthM.append(6)
        elif heavychain[j] in ["F","M","Q","L"]: lengthM.append(4)
	elif heavychain[j] in ["R"] : lengthM.append(3)
        elif heavychain[j] in ["N","C","K","E"]: lengthM.append(2)
        elif heavychain[j] in ["G","A","P","S","T","V","H","I","D"]: lengthM.append(1)

	octave=4

        if dict_netcharge[heavychain[i]] == 1 and heavychain[j] in ["R","L"] and octave < 6:
                octave += 1
        elif dict_netcharge[heavychain[i]] == 1 and  heavychain[j] in ["R","L"] and octave == 6:
                octave -= 1
        elif dict_netcharge[heavychain[i]] == -1 and  heavychain[j] in ["D","E"] and octave > 3:
                octave -= 1
        elif dict_netcharge[heavychain[i]] == -1 and  heavychain[j] in ["D","E"] and octave == 3:
                octave += 1

	if heavychain[j] == 'P' and dict_occurrence[heavychain[i]] in ["A","C","D","F","G"]:
		note +=1

	noteM.append(note+12*octave)


max=0
maxs=0
maxe=0

for start in range(0, len(heavychain)-1):
	for end in range(1,len(heavychain)):
		totalNL=0
		for int in range(start,end):
			totalNL+=lengthM[int]
		if totalNL == 16 or totalNL == 32 or totalNL == 64:
			judge=1
			for int in range(start,end):
				if abs(noteM[int]-noteM[int+1])>5 :
					judge=0
			if judge == 1:
				#print(start,end)
				#break
				if (end -start) >max:
					max=end-start
					maxs=start
					maxe=end


	#else:
	#	continue
	#break


#print(noteM)
#print(lengthM)
print (maxs,maxe)
if (max == 0):
	print ("no main theme found!")


# Generate ABC files #
abc_file.write('X: 1\nT: SMP RGS\nM: 4/4\nL: 1/16\nQ:1/4=80\nK:C % 0 sharps\n')

abc_main = ''
##j = 0
octave = 4
notelength = 0
abc_file.write('[V:1] ')

noteS=[]
#octaveS=[]
octS=[]
noOctaveS=[]
lengthS=[]
for i in range(0, len(heavychain)):
        j= i-1

	if j == -1:
		j = len(heavychain)-1

    # above reads light chain and loops #
	note = dict_occurrence[heavychain[i]]

	if heavychain[j] == 'P' and dict_occurrence[heavychain[i]] in ["A","C","D","F","G"]:
                note = '^' + note

	#if note == 'D': note = '[DGB]'
	#elif note == 'A': note = '[ACF]'
	#elif note == 'B': note = '[BDG]'
	#elif note == 'G': note = '[GCE]'
#########chord section

	if heavychain[i] in ["D", "Y"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[CE]'
		else:
			note= '[CEG]'

	if heavychain[i] in ["P", "R", "C"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[FA]'
		else:
			note= '[FAC]'

	if heavychain[i] == "M":
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[GC]'
		else:
			note= '[GCE]'

	if heavychain[i] == "M" and i < len(heavychain)-1 and heavychain[i+1] in ["P","R","C"]:  ###i+1 might have potential bug
		note = '[CEF_B]'
	if heavychain[i] in ["P","R","C"] and i >0 and heavychain[i-1] == "M":
		note = '[FAC]'

	if heavychain[i] in ["F", "H"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[AC]'
		else:
			note= '[ACF]'

	if heavychain[i] in ["F","H"] and i < len(heavychain)-1 and heavychain[i+1] in ["L"]:  ###i+1 might have potential bug
                note = '[A^CEG]'
        if heavychain[i] in ["L"] and i >0 and heavychain[i-1] in ["F","H"]:
                note = '[DFA]'

	if heavychain[i] in ["L"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[DF]'
		else:
			note= '[DFA]'

	if heavychain[i] in ["L"] and i < len(heavychain)-1 and heavychain[i+1] in ["W"]:  ###i+1 might have potential bug
		rng=np.random.randint(1,2)
		if(rng==1):
			note='[D^FAC]'
		elif(rng==2):
			note='[D^FA]'
	if heavychain[i] in ["W"] and i >0 and heavychain[i-1] in ["L"]:
		note = '[GBD]'

	if heavychain[i] in ["W"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[BD]'
		elif (rng >=2):
			note= '[BDG]'
		elif (rng ==1):
			note= '[BDGF]'

	if heavychain[i] in ["R"]:
		if heavychain[j] in ["M","L"]:
			note= '!ff!'+note
		elif heavychain[j] in ["F", "I","A","C","V","W","T"]:
			note = '!f!' + note
		elif heavychain[j] in ["S"]:
			note = '!mf!'+note
		elif heavychain[j] in ["P","Y","G","Q","R"]:
			note= "!p!"+ note
		elif heavychain[j] in ["H","N","E","K","D"]:
			note="!pp!"+note

	noOctave=note
	noOctaveS.append(noOctave)
	if dict_netcharge[heavychain[i]] == 1 and heavychain[j] in ["R","L"] and octave < 6:
                octave += 1
        elif dict_netcharge[heavychain[i]] == 1 and  heavychain[j] in ["R","L"] and octave == 6:
                octave -= 1
        elif dict_netcharge[heavychain[i]] == -1 and  heavychain[j] in ["D","E"] and octave > 3:
                octave -= 1
        elif dict_netcharge[heavychain[i]] == -1 and  heavychain[j] in ["D","E"] and octave == 3:
                octave += 1


	if octave == 1: note = note + ',,,'
	elif octave == 2: note = note + ',,'
	elif octave == 3: note = note + ','
	elif octave == 4: note = note
	elif octave == 5: note = note.lower()
	octS.append(octave)
	#if heavychain[j] == 'P': note = '^' + note

	length = ''
        if heavychain[j] in ["Y"]: length='8'
        elif heavychain[j] in ["W"]: length='6'
        elif heavychain[j] in ["F","M","Q","L"]: length='4'
        elif heavychain[j] in ["R"] : length='3'
        elif heavychain[j] in ["N","C","K","E"]: length='2'
       # elif heavychain[j] in ["G","A","P","S","T","V","H","I","D"]: lengthM.append(1)


	note += length
	lengthS.append(length)
#######dynamics
	noteS.append(note)

	#if notelength == 10:
	#	notelength = 0
	#	abc_file.write(note + '| \\\n')
	#else:
	#	abc_file.write(note)

	#j += 1
	#notelength += 1




#abc_file.write('\n')
notelength=0
i=0
while i <len(heavychain):
	if heavychain[i] in ["W","Y"]:
		for theme in range(maxs,maxe):
			note=noOctaveS[theme]

			if octS[i] == 1: note = note + ',,,'
       			elif octS[i] == 2: note = note + ',,'
        		elif octS[i] == 3: note = note + ','
        		elif octS[i] == 4: note = note
        		elif octS[i] == 5: note = note.lower()
			note += lengthS[theme]
			if notelength==10:
                		notelength=0
                		abc_file.write(note+'| \\\n')
        		else:
                		abc_file.write(note)
        		notelength +=1

	if heavychain[i] in ["F","R"] and i <len(heavychain)-maxe+maxs:

		for theme in range(maxs,maxe):
			note=noOctaveS[i]

			if octS[i] == 1: note = note + ',,,'
       			elif octS[i] == 2: note = note + ',,'
        		elif octS[i] == 3: note = note + ','
        		elif octS[i] == 4: note = note
        		elif octS[i] == 5: note = note.lower()
			note += lengthS[theme]
			if notelength==10:
                		notelength=0
                		abc_file.write(note+'| \\\n')
        		else:
                		abc_file.write(note)
        		notelength +=1
			i+=1
	else:
		if notelength==10:
                	notelength=0
               		abc_file.write(noteS[i]+'| \\\n')
        	else:
                	abc_file.write(noteS[i])
        	notelength +=1
		i+=1

####========left hand

abc_file.write('[V:2] ')
octave = 3

for i in range(0, len(heavychain)):
        j= i-1

	if j == -1:
		j = len(heavychain)-1

    # above reads light chain and loops #
	note = dict_aacomposition[heavychain[i]]

	if heavychain[j] == 'P' and dict_occurrence[heavychain[i]] in ["A","C","D","F","G"]:
                note = '^' + note
	#elif note == 'B': note = '[BDG]'
	#elif note == 'G': note = '[GCE]'

	if heavychain[i] in ["P", "F"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[CE]'
		else:
			note= '[CEG]'

	if heavychain[i] in ["C", "H"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[DF]'
		else:
			note= '[DFA]'

	if heavychain[i] in ["C","H"] and i < len(heavychain)-1 and heavychain[i+1] in ["W"]:  ###i+1 might have potential bug
		rng=np.random.randint(1,2)
		if(rng==1):
			note='[D^FAC]'
		elif(rng==2):
			note='[D^FA]'
	if heavychain[i] in ["W"] and i >0 and heavychain[i-1] in ["C","H"]:
		note = '[GBD]'

	if heavychain[i] in ["Y"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[EG]'
		elif(rng>=2):
			note= '[EGC]'
		elif(rng==1):
			note= '[EGBC]'

	if heavychain[i] in ["D"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[FA]'
		else:
			note= '[FAC]'

	if heavychain[i] in ["L"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[GB]'
		elif(rng >=2):
			note= '[GBD]'
		elif(rng ==1):
			note= '[GBDF]'

	if heavychain[i] in ["R", "M"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[AC]'
		else:
			note= '[ACF]'

	if heavychain[i] in ["R","M"] and i < len(heavychain)-1 and heavychain[i+1] in ["H","C"]:  ###i+1 might have potential bug
		note = '[A^CEG]'
	if heavychain[i] in ["H","C"] and i >0 and heavychain[i-1] in ["R","M"]:
		note = '[DFA]'

	if heavychain[i] in ["W"]:
		rng=np.random.randint(1, 10)
		if(rng >= 4):
			note= '[BD]'
		elif(rng >=2):
			note= '[BDG]'
		elif(rng ==1):
			note= '[BDFG]'

	if dict_hydropathy[heavychain[i]] == 1 and heavychain[j] in ["R","L"] and  octave < 4:
		octave += 1
	elif dict_hydropathy[heavychain[i]] == -1 and heavychain[j] in ["R","L"] and  octave > 1:
		octave -= 1
	elif dict_hydropathy[heavychain[i]] == 1 and  heavychain[j] in ["I","V"] and  octave == 4:
		octave -= 1
	elif dict_hydropathy[heavychain[i]] == -1 and heavychain[j] in ["I","V"] and  octave == 1:
		octave += 1

	if octave == 1: note = note + ',,,'
	elif octave == 2: note = note + ',,'
	elif octave == 3: note = note + ','
	elif octave == 4: note = note
	elif octave == 5: note = note.lower()



	length = ''
	if heavychain[j] in ["G"]: length = '8'
	elif heavychain[j] in ["S","A"] : length = '6'
	elif heavychain[j] in ["D"]: length = '4'
	elif heavychain[j] in ["C"]: length = '3'
	elif heavychain[j] in ["M","H","Q","V","E","N","P","T"]: length ='2'
	#elif heavychain[i] in ["W","Y","F","R","K","I","L"]: length='1'
	note += length

	#if note == 'D': note = '[DGB]'
	#elif note == 'A': note = '[ACF]'
	if heavychain[i] in ["R"]:
                if heavychain[j] in ["M","L"]:
                        note= '!ff!'+note
                elif heavychain[j] in ["F", "I","A","C","V","W","T"]:
                        note = '!f!' + note
                elif heavychain[j] in ["S"]:
                        note = '!mf!'+note
                elif heavychain[j] in ["P","Y","G","Q","R"]:
                        note= "!p!"+ note
                elif heavychain[j] in ["H","N","E","K","D"]:
                        note="!pp!"+note



	if notelength == 10:
		notelength = 0
		abc_file.write(note + '| \\\n')
	else:
		abc_file.write(note)


	notelength += 1

abc_file.write('\n')