-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrandseq.c
421 lines (365 loc) · 8.18 KB
/
crandseq.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/* crandseq.c May, 1995/Feb, 1997
copyright (c) 1995,1997 William R. Pearson
crandseq shuffles DNA sequences, keeping the codons intact.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int isatty();
char *verstr="version 2.1, Feb, 1997";
#define TRUE 1
#define FALSE 0
#ifndef BIGMEM
#define QFILE_SIZE 40
#define MAXTST 2000 /* longest test sequence */
#define MAXLIB 10000
#else
#define QFILE_SIZE 256
#define MAXTST 10000
#define MAXLIB 50000
#endif
#define MAXHIST 201 /* number of histogram divisions */
#include "upam.gbl"
FILE *outfd; /* fd for output file */
/* globals for matching */
unsigned char *aa0, *aa10; /* amino acid sequence data */
int maxn; /* max space for lib sequence (MAXDIAG-n0) */
int n0, cn0; /* length of aa0, length of aa1, n0+n1,
diagonal offset */
int dnaseq = 0; /* true if DNA query sequence */
FILE *outfd;
char rline[20],sline[20];
char resfile[QFILE_SIZE];
extern int optind;
char *getenv(), *smptr, *cptr; /* scoring matrix env */
extern int outtty;
char smstr[QFILE_SIZE];
long sq0off=1;
int wflag = -1;
int wsiz;
extern int getntseq(char *, unsigned char *, int, int *);
extern void irand();
extern int nrand();
void initenv();
void shuffle();
void wshuffle();
int
main(argc, argv)
int argc; char **argv;
{
char tname[40], lname[40], qline[40];
char *bp;
int i;
#ifdef UNIX
outtty=isatty(1);
#endif
#ifdef __MWERKS__
SIOUXSettings.asktosaveonclose=TRUE;
SIOUXSettings.showstatusline=FALSE;
SIOUXSettings.autocloseonquit=TRUE;
argc = ccommand(&argv);
if (GetResource('DLOG',IntroDID)==0L && OpenResFile("\pFASTA.rsrc")<0) {
SysBeep(100); fprintf(stderr," WARNING FASTA.rsrc file could not be found\n");
}
InitEvent();
GetVol((unsigned char *)prompt,&ouvRef);
wpos.h=50; wpos.v=100;
#endif
if ((aa0=calloc(MAXTST+MAXLIB,sizeof(char)))==0) {
printf(" cannot allocate sequence array\n");
exit(1);
}
maxn = MAXTST+MAXLIB;
if ((aa10=calloc(MAXLIB,sizeof(char)))==0) {
printf(" cannot allocate sequence array\n");
exit(1);
}
initenv(argc,argv);
if (argc-optind < 2) {
printf(" crandseq 2.1 [Feb, 1997] shuffles codons in a sequence\n");
l1: printf(" seed sequence file name: ");
fgets(tname,40,stdin);
if (tname[strlen(tname)-1]=='\n') tname[strlen(tname)-1]='\0';
if (strlen(tname)==0) goto l1;
if ((n0=getntseq(tname,aa0,maxn,&dnaseq))==0) {
printf(" %s : sequence not found\n",tname);
goto l1;
}
if (wflag<=0) {
printf(" local (window) (w) or uniform (u) shuffle [u]? ");
fgets(qline,40,stdin);
if ((bp=strchr(qline,'\n'))!=NULL) *bp='\0';
}
else qline[0]='\0';
if (tolower(qline[0])=='w' || wflag==1) {
wflag = 1;
wsiz = 10;
printf(" local shuffle codon window size [10] ");
fgets(qline,40,stdin);
if (qline[0]!='\0' && qline[0]!='\n') {
sscanf(qline,"%d",&wsiz);
if (wsiz <= 2) wsiz = 10;
}
}
else wflag=0;
}
else {
strncpy(tname,argv[optind+1],40);
if ((n0=getntseq(tname,aa0,maxn,&dnaseq))==0) {
printf(" %s : sequence not found\n",tname);
exit(1);
}
}
cn0 = n0/3;
if (wsiz > cn0) wsiz = cn0;
for (i=0; i<n0; i++) aa10[i]=aa0[i];
aa10[n0]= -1;
irand(); /* seed the random number generator */
if (wflag==1) wshuffle(aa10,aa0,n0,wsiz);
else shuffle(aa0,n0);
#ifdef __MWERKS__
SetVol("\p",ouvRef);
#endif
outfd = stdout;
if (outtty && resfile[0]=='\0') {
printf(" Enter filename for results : ");
fgets(rline,sizeof(rline),stdin);
if ((bp=strchr(rline,'\n'))!=NULL) *bp='\0';
if (rline[0]!='\0') strncpy(resfile,rline,sizeof(resfile));
}
if (resfile[0]!='\0')
if ((outfd=fopen(resfile,"w"))==NULL) exit(1);
fprintf(outfd, ">%s shuffled\n",tname);
for (i=0; i<n0; i++) {
fputc(sq[aa0[i]],outfd);
if (i%60 == 59) fputc('\n',outfd);
else if (i%10 == 9) fputc(' ',outfd);
}
fputc('\n',outfd);
#ifdef __MWERKS__
SIOUXSettings.asktosaveonclose=FALSE;
SIOUXSettings.autocloseonquit=TRUE;
#endif
}
extern int *sascii, nascii[], aascii[];
void
initenv(argc,argv)
int argc; char **argv;
{
char *cptr, *getenv();
int copt, getopt();
extern char *optarg;
sascii = nascii;
sq = nt;
hsq = hnt;
nsq = nnt;
dnaseq = 1;
while ((copt=getopt(argc,argv,"nw:O:"))!=EOF)
switch(copt) {
case 'n': dnaseq=1;
sascii = nascii;
sq = nt;
nsq = nnt;
hsq = hnt;
pam = npam;
strcpy(sqnam,"nt");
strcpy(sqtype,"DNA");
break;
case 'O': strncpy(resfile,optarg,sizeof(resfile));
break;
case 'w': wflag = 1; sscanf(optarg,"%d",&wsiz);
break;
default : fprintf(stderr," illegal option -%c\n",copt);
}
optind--;
}
int ieven = 1;
void
wshuffle(from,to,n,wsiz) /* copies from from to from shuffling */
char *from, *to; int n, wsiz;
{
int i,j, k, mm; char tmp, *top;
int cn, i3, j3;
cn = n/3;
memcpy(to,from,n);
mm = cn%wsiz;
if (ieven) {
for (k=0; k<(cn-wsiz); k += wsiz) { /* for each window starting from 0 */
top = &to[k]; /* destination window */
for (i=wsiz; i>1; i--) {
j = nrand(i); /* generate wsiz swaps */
j3 = j*3;
i3 = (i-1)*3;
tmp = top[j3];
top[j3] = top[i3];
top[i3] = tmp;
tmp = top[j3+1];
top[j3+1] = top[i3+1];
top[i3+1] = tmp;
tmp = top[j3+2];
top[j3+2] = top[i3+2];
top[i3+2] = tmp;
}
/* do the same thing again */
for (i=wsiz; i>1; i--) {
j = nrand(i);
j3 = j*3;
i3 = (i-1)*3;
tmp = top[j3];
top[j3] = top[i3];
top[i3] = tmp;
tmp = top[j3+1];
top[j3+1] = top[i3+1];
top[i3+1] = tmp;
tmp = top[j3+2];
top[j3+2] = top[i3+2];
top[i3+2] = tmp;
}
}
top = &to[n-mm];
for (i=mm; i>1; i--) {
j = nrand(i);
j3 = j*3;
i3 = (i-1)*3;
tmp = top[j3];
top[j3] = top[i3];
top[i3] = tmp;
tmp = top[j3+1];
top[j3+1] = top[i3+1];
top[i3+1] = tmp;
tmp = top[j3+2];
top[j3+2] = top[i3+2];
top[i3+2] = tmp;
}
/* do the same thing again */
for (i=mm; i>1; i--) {
j = nrand(i);
j3 = j*3;
i3 = (i-1)*3;
tmp = top[j3];
top[j3] = top[i3];
top[i3] = tmp;
tmp = top[j3+1];
top[j3+1] = top[i3+1];
top[i3+1] = tmp;
tmp = top[j3+2];
top[j3+2] = top[i3+2];
top[i3+2] = tmp;
}
ieven = 0;
}
else {
for (k=n; k>=wsiz; k -= wsiz) {
top = &to[k-wsiz];
for (i=wsiz; i>1; i--) {
j = nrand(i);
j3 = j*3;
i3 = (i-1)*3;
tmp = top[j3];
top[j3] = top[i3];
top[i3] = tmp;
tmp = top[j3+1];
top[j3+1] = top[i3+1];
top[i3+1] = tmp;
tmp = top[j3+2];
top[j3+2] = top[i3+2];
top[i3+2] = tmp;
}
/* do the same thing again */
for (i=wsiz; i>1; i--) {
j = nrand(i);
j3 = j*3;
i3 = (i-1)*3;
tmp = top[j3];
top[j3] = top[i3];
top[i3] = tmp;
tmp = top[j3+1];
top[j3+1] = top[i3+1];
top[i3+1] = tmp;
tmp = top[j3+2];
top[j3+2] = top[i3+2];
top[i3+2] = tmp;
}
}
}
top = &to[0];
for (i=mm; i>1; i--) {
j = nrand(i);
j3 = j*3;
i3 = (i-1)*3;
tmp = top[j3];
top[j3] = top[i3];
top[i3] = tmp;
tmp = top[j3+1];
top[j3+1] = top[i3+1];
top[i3+1] = tmp;
tmp = top[j3+2];
top[j3+2] = top[i3+2];
top[i3+2] = tmp;
}
/* do the same thing again */
for (i=mm; i>1; i--) {
j = nrand(i);
j3 = j*3;
i3 = (i-1)*3;
tmp = top[j3];
top[j3] = top[i3];
top[i3] = tmp;
tmp = top[j3+1];
top[j3+1] = top[i3+1];
top[i3+1] = tmp;
tmp = top[j3+2];
top[j3+2] = top[i3+2];
top[i3+2] = tmp;
}
ieven = 1;
to[n] = -1;
}
void
shuffle(from,n) /* copies from from to from shuffling */
char *from; int n;
{
int i,j; char tmp;
int cn, i3, j3;
cn = n/3;
for (i=cn; i>1; i--) {
j = nrand(i);
j3 = j*3;
i3 = (i-1)*3;
tmp = from[j3];
from[j3] = from[i3];
from[i3] = tmp;
tmp = from[j3+1];
from[j3+1] = from[i3+1];
from[i3+1] = tmp;
tmp = from[j3+2];
from[j3+2] = from[i3+2];
from[i3+2] = tmp;
}
from[n] = -1;
}
/* stubs for linking */
int llen;
void
aancpy()
{}
int markx;
void
disgraph()
{}
int
min(v0,v1)
int v0, v1;
{
return (v0>v1) ? v1 : v0;
}
void
ALIGN()
{}
void
discons()
{}
#ifdef VMS
memset(){}
#endif