/    Sign up×
Community /Pin to ProfileBookmark

Need help with simple program

Greetings,

I am new to PHP and I am trying to write a simple script that will take an input string and translate it based off of some translation table. Below is the code:

[CODE]<?php

echo “<html>n

<head>n

<title>Translation, first reading frame</title>

</head>n” ;

echo “<body>” ;

echo “<h1><b>Amino acid translation, frame 1</h1>” ;

// Code for translating nucleic acid sequence into amino acids

$translater = array(

“TCA” => “S”, // SERINE

“TCC” => “S”, // SERINE

“TCG” => “S”, // SERINE

“TCT” => “S”, // SERINE

“TTC” => “F”, // PHENYLALANINE

“TTT” => “F”, // PHENYLALANINE

“TTA” => “L”, // LEUCINE

“TTG” => “L”, // LEUCINE

“TAC” => “Y”, // TYROSINE

“TAT” => “Y”, // TYROSINE

“TAA” => “-“, // STOP

“TAG” => “-“, // STOP

“TGC” => “C”, // CYSTEINE

“TGT” => “C”, // CYSTEINE

“TGA” => “-“, // STOP

“TGG” => “W”, // TRYPTOPHAN

“CTA” => “L”, // LEUCINE

“CTC” => “L”, // LEUCINE

“CTT” => “L”, // LEUCINE

“CTG” => “L”, // LEUCINE

“CCA” => “P”, // PROLINE

“CCC” => “P”, // PROLINE

“CCG” => “P”, // PROLINE

“CCT” => “P”, // PROLINE

“CAC” => “H”, // HISTIDINE

“CAT” => “H”, // HISTIDINE

“CAA” => “Q”, // GLUTAMINE

“CAG” => “Q”, // GLUTAMINE

“CGA” => “R”, // ARGININE

“CGC” => “R”, // ARGININE

“CGG” => “R”, // ARGININE

“CGT” => “R”, // ARGININE

“ATA” => “I”, // ISOLEUCINE

“ATC” => “I”, // ISOLEUCINE

“ATT” => “I”, // ISOLEUCINE

“ATG” => “M”, // METHIONINE

“ACA” => “T”, // THREONINE

“ACC” => “T”, // THREONINE

“ACG” => “T”, // THREONINE

“ACT” => “T”, // THREONINE

“AAC” => “N”, // ASPARAGINE

“AAT” => “N”, // ASPARAGINE

“AAA” => “K”, // LYSINE

“AAG” => “K”, // LYSINE

“AGC” => “S”, // SERINE

“AGT” => “S”, // SERINE

“AGA” => “R”, // ARGININE

“AGG” => “R”, // ARGININE

“GTA” => “V”, // VALINE

“GTC” => “V”, // VALINE

“GTG” => “V”, // VALINE

“GTT” => “V”, // VALINE

“GCA” => “A”, // ALANINE

“GCC” => “A”, // ALANINE

“GCG” => “A”, // ALANINE

“GCT” => “A”, // ALANINE

“GAC” => “D”, // ASPARTIC ACID

“GAT” => “D”, // ASPARTIC ACID

“GAA” => “E”, // GLUTAMIC ACID

“GAG” => “E”, // GLUTAMIC ACID

“GGA” => “G”, // GLYCINE

“GGC” => “G”, // GLYCINE

“GGT” => “G”, // GLYCINE

“GGG” => “G”, // GLYCINE

);

$sequence = $_POST[‘INPUT’] ;

for($i = 0 ; $i<(strlen($sequence)-2) ; $i+3)

{

$firstLetter = $sequence[$i] ;

$secondLetter = $sequence[$i+1] ;

$thirdLetter = $sequence[$i+2] ;

$codon = $firstLetter.$secondLetter.$thirdLetter ;

$x = 0 ;

$polyPeptide[$x] = $translater[“$codon”] ;

$x++ ;

}

?>[/CODE]

I am getting the error “Notice: Undefined index”

Any suggestions would be great.

Cheers!

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ratcatemeSep 21.2008 — on line...

it looks like you are treating $sequence as an array

is that right most $_POST data is a string

that would cause that error(i think but i need line numbers)

Scott.
Copy linkTweet thisAlerts:
@TheDragonRebornauthorSep 21.2008 — yes, it is a string. its coming from an html input textarea. I wasn't sure how to turn it into an array. I am familiar with explode() in perl but, it needs a delimeter; which my input string does not have. It is just a continuous string of letters.

As for the line numbers, is there a way in the forum to display them? My text editor has line numbers, but I do not think they will paste over.
Copy linkTweet thisAlerts:
@TheDragonRebornauthorSep 21.2008 — Okay, I have modified my code, but now I am getting an error:

"Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 71 bytes) "

Heres the new code:

[CODE]<?php





echo "<html>n

<head>n

<title>Translation, first reading frame</title>

</head>n" ;



echo "<body>" ;

echo "<h1><b>Amino acid translation, frame 1</h1>" ;



// Code for translating nucleic acid sequence into amino acids



$translater = array(



"TCA" => "S", // SERINE

"TCC" => "S", // SERINE

"TCG" => "S", // SERINE

"TCT" => "S", // SERINE

"TTC" => "F", // PHENYLALANINE

"TTT" => "F", // PHENYLALANINE

"TTA" => "L", // LEUCINE

"TTG" => "L", // LEUCINE

"TAC" => "Y", // TYROSINE

"TAT" => "Y", // TYROSINE

"TAA" => "-", // STOP

"TAG" => "-", // STOP

"TGC" => "C", // CYSTEINE

"TGT" => "C", // CYSTEINE

"TGA" => "-", // STOP

"TGG" => "W", // TRYPTOPHAN

"CTA" => "L", // LEUCINE

"CTC" => "L", // LEUCINE

"CTT" => "L", // LEUCINE

"CTG" => "L", // LEUCINE

"CCA" => "P", // PROLINE

"CCC" => "P", // PROLINE

"CCG" => "P", // PROLINE

"CCT" => "P", // PROLINE

"CAC" => "H", // HISTIDINE

"CAT" => "H", // HISTIDINE

"CAA" => "Q", // GLUTAMINE

"CAG" => "Q", // GLUTAMINE

"CGA" => "R", // ARGININE

"CGC" => "R", // ARGININE

"CGG" => "R", // ARGININE

"CGT" => "R", // ARGININE

"ATA" => "I", // ISOLEUCINE

"ATC" => "I", // ISOLEUCINE

"ATT" => "I", // ISOLEUCINE

"ATG" => "M", // METHIONINE

"ACA" => "T", // THREONINE

"ACC" => "T", // THREONINE

"ACG" => "T", // THREONINE

"ACT" => "T", // THREONINE

"AAC" => "N", // ASPARAGINE

"AAT" => "N", // ASPARAGINE

"AAA" => "K", // LYSINE

"AAG" => "K", // LYSINE

"AGC" => "S", // SERINE

"AGT" => "S", // SERINE

"AGA" => "R", // ARGININE

"AGG" => "R", // ARGININE

"GTA" => "V", // VALINE

"GTC" => "V", // VALINE

"GTG" => "V", // VALINE

"GTT" => "V", // VALINE

"GCA" => "A", // ALANINE

"GCC" => "A", // ALANINE

"GCG" => "A", // ALANINE

"GCT" => "A", // ALANINE

"GAC" => "D", // ASPARTIC ACID

"GAT" => "D", // ASPARTIC ACID

"GAA" => "E", // GLUTAMIC ACID

"GAG" => "E", // GLUTAMIC ACID

"GGA" => "G", // GLYCINE

"GGC" => "G", // GLYCINE

"GGT" => "G", // GLYCINE

"GGG" => "G", // GLYCINE

);





$input = $_POST['INPUT'] ;



// Chomp off newline

$data = rtrim($input, "n") ;





// Get length and convert to uppercase



$rawSequence = strtoupper($data) ;

$seqLength = strlen($rawSequence) ;



$x = 0 ;



// Convert string to array



$sequence = str_split($rawSequence,1) ;



for($i = 0 ; $i<($seqLength-2) ; $i+3)

{

$firstLetter = $sequence[$i] ;

$secondLetter = $sequence[$i+1] ;

$thirdLetter = $sequence[$i+2] ;



$codon = $firstLetter.$secondLetter.$thirdLetter ;





$polyPeptide[$x] = $translater["$codon"] ;

$x++ ;

}



// echo $_POST['INPUT'] ; // Test to see if data transfers


?>[/CODE]


Any help would be great.

Thanks
Copy linkTweet thisAlerts:
@ratcatemeSep 21.2008 — how long is the string that your are inputting?

because a script that needs 128MB of ram is really crazy and properly takes quite a long time to run.

i am still kinda of foggy on what your script actually does but i kinda get it. but i can't think of a more efficient way to go about this sorry.

Scott.
Copy linkTweet thisAlerts:
@NogDogSep 21.2008 — See the new thread the OP opened for the memory error: http://www.webdeveloper.com/forum/showthread.php?t=191730
×

Success!

Help @TheDragonReborn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...