ivytony Posted May 14, 2010 Share Posted May 14, 2010 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. Link to comment https://forums.phpfreaks.com/topic/201703-how-to-find-a-characters-all-occurrences-and-positions-in-a-string/ Share on other sites More sharing options...
ivytony Posted May 14, 2010 Author Share Posted May 14, 2010 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); ?> Link to comment https://forums.phpfreaks.com/topic/201703-how-to-find-a-characters-all-occurrences-and-positions-in-a-string/#findComment-1058092 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.