Help with functions

I need so help with functions

Page 1 of 1

4 Replies - 561 Views - Last Post: 11 September 2008 - 08:07 AM

#1 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Help with functions

Posted 08 September 2008 - 04:59 AM

I need to help with a function i need to write for a course i am doing. I will post the code I have written so far as it is quite lenghty and explain the problem after it.

<html>
<head>
<title>M150 TMA 5 : Programming : Task 5 - Displaying a Gene</title>

<script language="Javascript" type="text/javascript">


	//YOU DO NOT NEED TO MODIFY THIS FUNCTION
	function getCodonArray (baseString)
	{
		var codonLength = 3;
		
		var codonArray = new Array();

		var baseArray;		

		baseArray = baseString.split('');

		var loopCount = 0;
		
		while (baseArray.length >= codonLength)
		{
			codonArray[loopCount] = baseArray.slice(0, codonLength);
			baseArray = baseArray.slice(codonLength, baseArray.length);
			loopCount = loopCount + 1;
		}
		return codonArray;
	}


	//YOU DO NOT NEED TO MODIFY THIS FUNCTION
	function findStartCodon(baseString)
	{

		var startSearchIndex = 0;

		var startCodonAt = -1;

		var codons = getCodonArray(baseString);

		codons = codons.slice(startSearchIndex, codons.length);

		var codonFound = false;

		var loopCount = 0;

		while ((codonFound == false)&&(loopCount + 1 <= codons.length))
		{
			if (codons[loopCount].join('') == 'ATG')
			{
				startCodonAt = loopCount;
				codonFound = true;
			}
			else
			{
				loopCount = loopCount + 1;
			}
		}

		return startCodonAt;
	}


	
	function findStopCodon(baseString)
	{
	var startSearchIndex = 0;
			
		var stopCodonAt = -1;

		var codons = getCodonArray(baseString);

		codons = codons.slice(startSearchIndex, codons.length);

		var codonFound = false;

		var loopCount = 0;
		
		
		while ((codonFound == false) && (loopCount + 1 <= codons.length))
		{
			
			if (codons[loopCount].join('') == 'TGA' || codons[loopCount].join('') == 'TAA' || codons[loopCount].join('') == 'TAG' )
			
			{	
				stopCodonAt = loopCount;
				codonFound = true;
			}
			else
			{	
				loopCount = loopCount + 1;
			}
		}
		
		return stopCodonAt;
	}



	// YOU MUST COMPLETE THIS FUNCTION FOR THIS PART OF THE TMA
	
	function displayGene()
	{

	var startPosition = this.document.testForm.sequenceString.value;
	
	var startCodon = findStartCodon(startPosition);
	
	var stopPosition = this.document.testForm.sequenceString.value;
	
	var stopCodon = findStopCodon(stopPosition);
		
	window.alert(startCodon + ' ' + stopCodon)
	}
	




</script>
</head>

<body>
<!--
YOU DO NOT NEED TO MODIFY ANY OF THE HTML BELOW
-->
<strong>A program to display information about a gene.<br></strong>
	<form action="" method="post" name="testForm">
		<p>
		Input sequence:
		</p>
		<p>
		<input name="sequenceString" type="text" size="150" maxlength="150">
		</p>
		<p>
		First gene:
		</p>
		<p>
		<input name="geneString" type="text" size="150" maxlength="150">
			</p>
		<input name="resetButton" type="reset" value="Clear Form">
		
		<input name="submitButton" type="button" value="Display the Gene!" onclick="displayGene();">
	</form>
</body>
</html>



For the course what i need to do is take a gene string find the start gene and stop gene then display the gene. The problem i am having is that in the example gene string i have to use the first stop gene is before the start gene.

This is the gene string i have to use.

AATGGATCGTAGACTATGCCTGTGTACCGATCCGCGTCTAGGAGGCCCCGCCGACTGGCTCTACTCCGCTTCGGGCGATAATGG

I have written two functions to find the stop and start genes and need to write the displayGene() function to display it. The problem i have is that i need the display gene to find the first instance of a stop gene after the start thus ignoring the one that appears before it in the string above.

The closest i think i have been was by copying the findStopCodon() and putting it in the displayGene() function and amending this line


{
	var startSearchIndex = 0;
			
		var stopCodonAt = -1;



to this

{
	var startSearchIndex = findStartCodon(baseString);			
		var stopCodonAt = -1;




I cannot change any of the functions other than displayGene()

Hope someone can help with this it is really doing my head in :D

Is This A Good Question/Topic? 0
  • +

Replies To: Help with functions

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4361
  • View blog
  • Posts: 12,180
  • Joined: 18-April 07

Re: Help with functions

Posted 08 September 2008 - 10:10 PM

Well if I understand this correctly all you need to do is find the first start of the gene, record its position, start a new search from this position to find the first stop gene position and then pull the string from the start position to the stop position. Is this the idea you are looking for?
Was This Post Helpful? 0
  • +
  • -

#3 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Help with functions

Posted 09 September 2008 - 01:20 AM

View PostMartyr2, on 8 Sep, 2008 - 10:10 PM, said:

Well if I understand this correctly all you need to do is find the first start of the gene, record its position, start a new search from this position to find the first stop gene position and then pull the string from the start position to the stop position. Is this the idea you are looking for?

Hi marty2

yes that is exactly what i am trying to do. I have yet not succeeded.
Was This Post Helpful? 0
  • +
  • -

#4 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Help with functions

Posted 09 September 2008 - 08:24 AM

this is what i have tried to do to the displayGene() function so far, but it doesn't want to work not sure if i am on the right track though??

function displayGene()
	
	{

 	var startPosition = this.document.testForm.sequenceString.value;
	
	var startCodon = findStartCodon(startPosition);
	
	var stopPosition = this.document.testForm.sequenceString.value;
	
	var stopCodon = findStopCodon(stopPosition);

	if(stopcodon < startCodon)
		
		{
		function findStopCodonAgain()
		
		var startSearchIndex = findStartCodon(baseString);
			
		var stopCodonAt = -1;

		var codons = getCodonArray(baseString);

		codons = codons.slice(startSearchIndex, codons.length);

		var codonFound = false;

		var loopCount = 0;
		
		
		while ((codonFound == false) && (loopCount + 1 <= codons.length))
		{
			
			if (codons[loopCount].join('') == 'TGA' || codons[loopCount].join('') == 'TAA' || codons[loopCount].join('') == 'TAG' )
			
			{	
				stopCodonAt = loopCount;
				codonFound = true;
			}
			else
			{	
				loopCount = loopCount + 1;
			}
		}
		
		return stopCodonAt;
	}
		
	
	
	}


Was This Post Helpful? 0
  • +
  • -

#5 truetoon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 22-May 08

Re: Help with functions

Posted 11 September 2008 - 08:07 AM

Or i have tried this, there is soemthing missing that i can't figure out. I am struggling creating the subarray from the inputted string.

function displayGene()
	{
	var startPosition = this.document.testForm.sequenceString.value;
	var startCodon = findStartCodon(startPosition);
	var stopPosition = this.document.testForm.sequenceString.value;
	var stopCodon = findStopCodon(stopPosition);
	
[color=#FF0000]//it is obviously this section that i cannot do![/color]								

								var myArray = new Array()

		var geneArray = myArray.slice(startPosition, stopPosition)
	
		this.document.testForm.geneString.value = geneArray
	}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1