var result;

sequence = "MQIFVKTLTGKTITLEVEPSDTIENVKAKIQDKEGIPPDQQRLIFAGKQLEDGRTLSDYNIQKESTLHLVLRLRGG";
//sequence = "MQIFVKTLTGK";

info.append ("Ubiquitin " + sequence);

for (i=0;  i<sequence.length-10;  ++i)
{
	window.menubar.menu_File.Close_All.trigger();
	result = sequence.substring (i, i+10);

	subsequence = sequence.substring (i, i+10);
	alpha_subsequence (subsequence);

	//subsequence = sequence.substring (i, i+4);
	//beta_subsequence  (subsequence);

	subsequence = sequence.substring (i, i+10);
	turn_subsequence  (subsequence);

	info.append (result);
}
info.append ("Finish");


function alpha_subsequence (sequence)
{
	//window.menubar.menu_Build.Chain.trigger();
	window.menubar.menu_File.New.trigger();
	var model  = project.current_model();
	var editor = model.sequence();
	//editor.change_repository ('- Amino Acids');
	editor.set_chain_type      ('- Amino Acids');
	result += ' ';

	build_alpha (sequence);
	model.fit_to_screen();
	window.menubar.menu_Window.Minimize.trigger();
	estimate ();
}

function beta_subsequence (sequence)
{
	window.menubar.menu_File.New.trigger();
	var model  = project.current_model();
	var editor = model.sequence();
	editor.set_chain_type      ('- Amino Acids');
	result += ' ';

	build_beta (sequence);
	model.fit_to_screen();
	window.menubar.menu_Window.Minimize.trigger();
	estimate ();
}

function turn_subsequence (sequence)
{
	window.menubar.menu_File.New.trigger();
	var model  = project.current_model();
	var editor = model.sequence();
	editor.set_chain_type      ('- Amino Acids');
	result += ' ';

	build_turn (sequence);
	model.fit_to_screen();
	window.menubar.menu_Window.Minimize.trigger();
	estimate ();
}

function estimate ()
{
	var model  = project.current_model();
	var MD     = model.MD();
	var min    = model.optimize();

	window.menubar.menu_Build.Remove_clashes.trigger();

	window.menubar.menu_Compute.Optimize.trigger();
	min.run();

	window.menubar.menu_Compute.Dynamics.trigger();

	MD.set_thermostat ('Berendsen');
	MD.set_duration (20);              // ps
	MD.set_desired_temperature (10);   // K
	MD.run();
	MD.set_desired_temperature (100); 
	MD.run();
	MD.set_desired_temperature (75); 
	MD.run();
	MD.set_desired_temperature (50);  
	MD.run();
	MD.set_desired_temperature (25);  
	MD.run();

	window.menubar.menu_Compute.Optimize.trigger();
	min.run();
	result += project.current_model().interaction().potential() + ' ';
}

function build_alpha (seq)
{
	var model  = project.current_model();
	var editor = model.sequence();
	editor.set_conformational_type ('Alpha helix');
	editor.build (seq);
}

function build_beta (seq)
{
	var model  = project.current_model();
	var editor = model.sequence();
	editor.set_conformational_type ('II strand');	editor.build ('AAA');
	editor.set_conformational_type ('II turn');		editor.build ('ANG');

	editor.build (seq.charAt (0));
	editor.set_conformational_type ('II strand'); 
	editor.build (seq.substring (1, seq.length-1));
	editor.set_conformational_type ('II turn');    
	editor.build (seq.charAt (seq.length-1));

	editor.build ('NGA');
	editor.set_conformational_type ('II strand');	editor.build ('AAA');
}

function build_turn (seq)
{
	var model  = project.current_model();
	var editor = model.sequence();

	editor.set_conformational_type ('II strand');	editor.build (seq.substring (0, 3));
	editor.set_conformational_type ('II turn');		editor.build (seq.substring (3, 7));
	editor.set_conformational_type ('II strand');	editor.build (seq.substring (7, 10));
}