-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsatr.php
152 lines (142 loc) · 3.84 KB
/
msatr.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
header("Access-Control-Allow-Origin: *");
error_reporting(E_ALL & ~ E_NOTICE);
// input format:
// $seq: DNA senquence
// $min_len: min length of the repeat
// $max_len: max length of the repeat
// $p: min repeat time
// $r: gate value
// $seq='CGCAGCGTGGCTGGAGAAACGTTGTCAAAAAGACGAGTCGCCACTCACTGGGAATAGTCGGCAAGCATCGTACACAATGTTTAACTCTCAGTCACCGTTCCGACTCGCGGAGCCGTGTATCGG';
// $seq=str_split('CGCAGCGTGGCTGGAGAAACGTTGTCAAAAAGACGAGTCGCCACTCACTGGGAATAGTCGGCAAGCATCGTACACAATGTTTAACTCTCAGTCACCGTTCCGACTCGCGGAGCCGTGTATCGG');
// $seq=str_split('ACCGGGCGTTGAGTTGCCTGACTCAGTCGCGTCTGATAGTCTGATAGAGTAGGACGCACCTGTCTGAATAGCGAGACAACTAGAGCCAAACCTCAGCTAG');
$seq=str_split($_POST["senquence"]);
$min_len=$_POST["min_len"];
$max_len=$_POST["max_len"];
$p=$_POST["repeat"];
$r=$_POST["r"];
// input over
//sample input
// $seq=str_split('CAAA');
// $min_len=2;
// $max_len=2;
// $p=2;
// $r=0.5;
//sample input over
$stroage=array();
$msatr=array();
$total=0;
$repeat=1;
$buffer=array();
function compare($compare, $r) {
// compare the two senquences
// $stroage: senquence used to be compared
// $compare: senquence used to compare
// $r: gate value
global $stroage;
$len=count($compare);
foreach (array_chunk($stroage, $len) as $key => $value) {
$count=0;
for ($i=0; $i<$len; $i++) {
if ($value[$i]==$compare[$i]) {
$count++;
}
}
if ($count/$len<$r) {
return false;
}
}
return true;
}
function check($p, $i) {
// check the repeat times of the buffer
// $target
// $len
// $p
// $i position adjustment
global $stroage, $msatr, $total, $repeat;
global $key_of_buffer;
if ($repeat>=2) {
if ($repeat>=$p) {
$msatr[$total]['senquence']=implode($stroage);
$msatr[$total]['repeat']=$repeat;
$msatr[$total]['length']=strlen($msatr[$total]['senquence'])/$repeat;
$head=$key_of_buffer*$msatr[$total]['length']+$i;
$tail=strlen($msatr[$total]['senquence'])+$head-1;
$msatr[$total]['senquence']=chunk_split($msatr[$total]['senquence'], $msatr[$total]['length'], ',');
$msatr[$total]['senquence']=$head.','.$msatr[$total]['senquence'].$tail;
$total++;
}
}
$stroage=array();
$repeat=1;
}
function checkback($test, $p, $r) {
$len=count($test);
$count=0;
for ($i=0; $i<$len; $i++) {
if ($test[$i]==$p[$i]) {
$count++;
}
}
if ($count/$len<$r) {
return false;
} else {
return true;
}
}
for ($rep=$min_len; $rep<=$max_len; $rep++) {
// divide the senquence
for ($i=0; $i<$rep; $i++) {
$array[$i]=array_chunk(array_slice($seq, $i), $rep);
if (count($array[$i][count($array[$i])-1])!=$rep) {
array_pop($array[$i]);
}
}
// divide over
for ($i=0; $i<$rep; $i++) {
$stroage=array();
$buffer=$array[$i][0];
$stroage=$buffer;
$key_of_buffer=0;
$tip=$key_of_buffer+1;
while (($key_of_buffer<count($array[$i]))&&($tip<count($array[$i]))) {
if ((compare($array[$i][$tip], $r))&&(count($array[$i][$tip])==$rep)) {
$stroage=array_merge($stroage, $array[$i][$tip]);
$repeat++;
} else {
check($p, $i);
// $buffer=$array[$i][$tip];
// $stroage=$buffer;
// $key_of_buffer=$tip;
$temp=$array[$i][$tip];
// print_r(checkback($array[$i][$tip-1], $temp, $r));
while (($tip>=1)&&(checkback($array[$i][$tip-1], $temp, $r))) {
$tip-=1;
}
$key_of_buffer=$tip;
$buffer=$array[$i][$key_of_buffer];
$stroage=$buffer;
}
$tip+=1;
}
check($p, $i);
}
// // check the devision
// for ($i=0; $i<$rep; $i++) {
// for ($j=0; $j<floor(count($seq)/$rep); $j++) {
// for ($k=0; $k<$rep; $k++) {
// echo $array[$i][$j][$k];
// }
// echo " ";
// }
// echo "<br/>";
// }
}
// print_r($total);
// for ($i=0; $i<$total; $i++) {
// print_r("<br/>");
// print_r(json_encode($msatr[$i]));
// }
echo json_encode($msatr);
?>