Jump to content

how to find a character's all occurrences and positions in a string?


ivytony

Recommended Posts

I want to search all the positions of the character combo 'SQ' and 'TQ' in this amino acid sequence:

MATSQHILTQSQTPPILVMAWRESQ
. After finding all the positions, I would also like to compute the distance between each occurrences.

 

I appreciate your help.

I have tried to write the following messy code, and it works. But I am looking for improvements, if you guys can help me.

 

<?php
$seq = "MAHTSQHILTQSQTPSQPILVSQMASQWRESQ";

echo "Original sequence is: $seq\n";
// $stq_sites = count_stq($seq); //count the number of [s/T]Q sites

$sq_sites = substr_count($seq, 'SQ');

$distri_sq = array();  // array container for all the distributions of SQ sites

for($i = 0; $i < $sq_sites; $i++){ 

$needle = "SQ";
$length_needle = strlen($needle);
$length_stack = strlen($seq);

$pos = strpos($seq, $needle);
// $pos += 1;
if($pos){
$pos += $length_needle; //the position where the new stack will start
$length_stack -= $pos;
$seq = substr($seq, $pos, -1);
echo "<br />new sequence is: $seq\t with position $pos\n ";

  if($i > 0) array_push($distri_sq, $pos); 
}

}

echo "<br />Size of array: ". sizeof($distri_sq) ."<br />";

echo "avaraged distance is: ". array_sum($distri_sq)/sizeof($distri_sq);
?>


Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.