Debian Codegraph @

leaff/simseq.C

raw
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <ctype.h>
6
7 #include "bio.h"
8
9 // This is Liliana Florea's sequencing error simulator. Bri hacked
10 // it to use a real RNG, and to make it work from leaff.
11
12 typedef struct edit_script {
13 int optype;
14 int num;
15 struct edit_script *next;
16 } EditScript_t;
17
18 typedef struct align {
19 int offset, len;
20 EditScript_t *script;
21 } Align_t;
22
23
24
25 // This guy is provided by leaff
26 extern mt_s *mtctx;
27
28 // RAND returns x numbers, starting at number y.
29 //
30 #define RAND(x,y) (int)((y) + (mtRandom32(mtctx) % (x)))
31
32 #define max(x,y) ((x)>=(y) ? (x):(y))
33 #define min(x,y) ((x)<=(y) ? (x):(y))
34
35 #define MOV 3
36 #define SUB 2
37 #define INS 1
38 #define DEL 0
39
40
41
42 EditScript_t *
43 new_script(int optype, int num, EditScript_t *next) {
44 EditScript_t *newtp = (EditScript_t *)malloc(sizeof(EditScript_t));
45
46 newtp->optype = optype;
47 newtp->num = num;
48 newtp->next = next;
49
50 return newtp;
51 }
52
53
54
55 /* DEL(pos), SUB(pos) - modifY position pos; INS - insert right before pos */
56 void
57 insert(Align_t *aln, int in_pos, int in_optype) {
58 int i, num, optype;
59 EditScript_t *t, *tp;
60
61 //fprintf(stderr, "Modify script op=%d pos=%d\n", in_optype, in_pos);
62
63 for (t=aln->script, i=0, tp=NULL; t; tp=t, t=t->next) {
64 num = t->num;
65 optype = t->optype;
66
67 switch (optype) {
68 case INS:
69 if (in_pos==i+1) {
70 if (tp)
71 tp->next = new_script(in_optype, 1, tp->next);
72 else
73 aln->script = new_script(in_optype, 1, aln->script);
74 return;
75 }
76 break;
77
78 case DEL:
79 i += num;
80 break;
81
82 case SUB:
83
84 case MOV:
85 if (i<in_pos && in_pos<=i+num) {
86 int l = (in_optype==INS) ? (in_pos-i) : (in_pos-i-1);
87 int r = (in_optype==INS) ? (num-l) : (num-l-1);
88 if (l && l!=num) {
89 t->num = l; tp = t;
90 tp->next = new_script(in_optype, 1, tp->next);
91 tp = tp->next;
92 tp->next = new_script(optype, r, tp->next);
93 } else if (!l) {
94 if (tp)
95 tp->next = new_script(in_optype, 1, t);
96 else
97 aln->script = new_script(in_optype, 1, aln->script);
98 if (in_optype!=INS) t->num -= 1;
99 } else {
100 tp = t;
101 tp->next = new_script(in_optype, 1, tp->next);
102 if (in_optype!=INS) t->num -= 1;
103 }
104 return;
105 }
106 i += num;
107 break;
108
109 default:
110 fprintf(stderr, "Unrecognized optype (%d).\n", in_optype);
111 break;
112 }
113 }
114
115 //fprintf(stderr, "Failed to modify sequence (%d,%d).\n", in_optype, in_pos);
116 }
117
118
119 void
120 print_simseq(char *seq, char *hdr, Align_t *aln, double P, int CUT, int COPY) {
121 int k, e;
122 char *s;
123 char let_4[4] = {'A','C','G','T'};
124 char let_3A[3] = {'C','G','T'};
125 char let_3C[3] = {'A','G','T'};
126 char let_3G[3] = {'A','C','T'};
127 char let_3T[3] = {'A','C','G'};
128 EditScript_t *t;
129
130 fprintf(stdout, ">");
131
132 while ((*hdr) && !isspace(*hdr))
133 fprintf(stdout, "%c", *hdr++);
134
135 fprintf(stdout, ":seq=%d:copy=%d:loc=%d-%d:err=%1.2f\n", CUT+1, COPY+1, aln->offset, aln->offset+aln->len-1, P);
136
137 s = seq + aln->offset-1;
138
139 for (t=aln->script; t; t=t->next) {
140 if (*s == 0)
141 break;
142
143 switch (t->optype) {
144 case INS:
145 for (k=0; k<t->num; k++) {
146 e = RAND(4,0);
147 fprintf(stdout, "%c", let_4[e]);
148 }
149 break;
150
151 case DEL:
152 while (*s && t->num) {
153 s++;
154 t->num--;
155 }
156 break;
157
158 case SUB:
159 for (k=0; k<t->num; k++) {
160 e = RAND(3,0);
161 if (*s=='A') fprintf(stdout, "%c", let_3A[e]);
162 else if (*s=='C') fprintf(stdout, "%c", let_3C[e]);
163 else if (*s=='G') fprintf(stdout, "%c", let_3G[e]);
164 else if (*s=='T') fprintf(stdout, "%c", let_3T[e]);
165 else fprintf(stdout, "%c", 'A');
166 s++;
167 }
168 break;
169
170 case MOV:
171 for (k=0; k<t->num; k++) {
172 if (*s == 0) {
173 k = t->num;
174 } else {
175 fprintf(stdout, "%c", *s);
176 s++;
177 }
178 }
179 break;
180
181 default:
182 fprintf(stderr, "Unrecognized optype (%d).\n", t->optype);
183 break;
184 }
185 }
186 fprintf(stdout, "\n");
187 }
188
189
190
191
192 void
193 simseq(char *seq, char *hdr, int len, int N, int L, int C, double P) {
194 Align_t align;
195 int i, j, k;
196 int start;
197 EditScript_t *s;
198
199 for (i=0; i<N; i++) {
200 /* generate a new sequence of length min(len,N) */
201 start = RAND((len-L+1),1);
202
203 /* now create in_C non-identical copies */
204 for (j=0; j<C; j++) {
205 /* generate a 'trivial' script for the sequence */
206
207 align.offset = start;
208 align.len = L;
209 align.script = new_script(MOV,L,NULL);
210
211 for (k=0; k<L*P; k++) {
212 int optype = RAND(3,0);
213 int pos = RAND(L,1);
214
215 insert(&align, pos, optype);
216 }
217
218 print_simseq(seq, hdr, &align, P, i, j);
219
220 while (align.script) {
221 s = align.script;
222 align.script = s->next;
223 free(s);
224 }
225 }
226 }
227 }