-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulate_pair.c
109 lines (97 loc) · 2.95 KB
/
simulate_pair.c
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
/**
>HEADER
Copyright (c) 2004 Haixu Tang [email protected]
This file is part of the RepGraph package.
RepGraph is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RepGraph is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RepGraph. If not, see <http://www.gnu.org/licenses/>.
<HEADER
**/
#include <stdinc.h>
#include <extvab.h>
#include <extfunc.h>
double simulate_pair(int length1, int length2, int copynum1, int copynum2, int lengththresh,
double *distchr, int *num_chrseg, int num_chr, int num_seg, int seglength);
double simulate_pair_all(int length1, int length2, int copynum1, int copynum2, int lengththresh,
double *distchr, int *num_chrseg, int num_chr, int num_seg, int seglength, int simtimes);
double simulate_pair_all(int length1, int length2, int copynum1, int copynum2, int lengththresh,
double *distchr, int *num_chrseg, int num_chr, int num_seg, int seglength, int simtimes)
{
int i, j, k, l, m, n;
double d;
d = 0;
for(i = 0; i < simtimes; i ++) {
d += simulate_pair(length1, length2, copynum1, copynum2, lengththresh, distchr, num_chrseg, num_chr, num_seg, seglength);
}
d /= simtimes;
return(d);
}
double simulate_pair(int length1, int length2, int copynum1, int copynum2, int lengththresh,
double *distchr, int *num_chrseg, int num_chr, int num_seg, int seglength)
{
int i, j, k, l, m, n;
int k1, k2, *d1, *d2;
double s1, s2, s0, r;
int nclosepair;
if(lengththresh > 2 * seglength) {
return(1.0);
}
d1 = (int *) ckalloc(copynum1 * sizeof(int));
d2 = (int *) ckalloc(copynum2 * sizeof(int));
for(j = 0; j < copynum1; j ++) {
s1 = random1(&idum);
s0 = 0;
for(i = 0; i < num_seg; i ++) {
if(s1 > s0) {
d1[j] = i;
break;
}
s0 += distchr[i];
}
}
for(j = 0; j < copynum2; j ++) {
s2 = random1(&idum);
s0 = 0;
for(i = 0; i < num_seg; i ++) {
if(s2 > s0) {
d2[j] = i;
break;
}
s0 += distchr[i];
}
}
r = 0;
for(i = 0; i < copynum1; i ++) {
n = k1 = 0;
for(k = 0; k < num_chr; k ++) {
if(d1[i] == n) {
k1 = 1;
}
n = num_chrseg[k];
}
for(j = 0; j < copynum2; j ++) {
n = k2 = 0;
for(k = 0; k < num_chr; k ++) {
if(d2[j] == n) {
k2 = 1;
}
n = num_chrseg[k];
}
if(d1[i] == d2[j]) {
r += ((double) lengththresh) / (seglength - length1 - length2);
} else if(d1[i] - d2[j] == 1 && k1 == 0 || d2[j] - d1[i] == 1 && k2 == 0) {
r += ((double) lengththresh) / (seglength - length1) * ((double) lengththresh) / (seglength - length2);
}
}
}
free((void *) d1);
free((void *) d2);
return(r);
}