-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractp.c
366 lines (316 loc) · 7.53 KB
/
extractp.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
/* extract.c lookup protein sequences in the library */
/* copyright (c) 1984, 1985 William R. Pearson */
/* this is a version of libprot that uses an index file */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#ifdef THINK_C
#include <stdlib.h>
#define getenv mgetenv
#include <MacTypes.h>
#include <StdFilePkg.h>
SFReply freply;
Point wpos;
int tval;
char prompt[256];
#else
#define TRUE 1
#define FALSE 0
#endif
#define NAMLEN 11
FILE *tptr, *lptr, *optr; /* file pointers for input, lib, output */
FILE *finx;
int fidx; /* fd for index */
#ifndef UNIX
#define BMODE 0x8000
#else
#define BMODE 0x0000
#endif
#define RMODE 0
long filen, lseek();
long maxidx;
long lmark;
int lfn;
struct {
char nam[NAMLEN];
char fn;
long lmark;
} seq;
long lboff; /* offset into index file for new type */
#define MAXLF 20
#define MAXLN 50
char libenv[80];
char lbnarr[1000]; /* name array of libraries to be opened in list */
char *lbnames[MAXLF]; /* names of libraries to be opened */
int libfn; /* current library file being searched */
int iln, nln;
char lline[512], oline[512], seqnam[120], oname[40];
#ifdef THINK_C
int glvRef,anvRef, sqvRef, ouvRef;
#endif
main(argc,argv)
int argc; char **argv;
{
char tname[80], lname[80], iname[80], inname[80], rline[40], *ep, *getenv();
int i, ilb, tmode;
char *bp;
#ifdef THINK_C
if (OpenResFile("\pFASTA.rsrc")<0) {
SysBeep(100); fprintf(stderr," WARNING FASTA.rsrc file could not be found\n");
}
GetVol(prompt,&ouvRef);
sqvRef=ouvRef;
wpos.h=50; wpos.v=100;
#endif
tname[0]='\0';
printf(" extractp [May '90 1.4c] - get sequences from a sequence library\n");
if (argc < 2) {
if ((ep=getenv("AABANK"))==NULL) {
ep="\0";
l1: printf(" library file name: ");
fgets(lname,40,stdin);
if (lname[strlen(lname)-1]=='\n')
lname[strlen(lname)-1]='\0';
if (*lname==0) {
if (*ep==0) goto l1;
else strncpy(lname,ep,80);
}
}
else {
strncpy(lname,ep,80);
printf(" using %s: library file\n",lname);
}
newname(iname,lname,"ixx",80);
newname(inname,lname,"inx",sizeof(inname));
}
else {
strncpy(lname,argv[1],80);
printf(" using %s library\n",lname);
newname(iname,lname,"ixx",80);
newname(inname,lname,"inx",sizeof(inname));
}
l2: if ((fidx=open(iname,BMODE+RMODE)) == -1) {
#ifndef THINK_C
printf(" could not open index file: %s\n",iname);
printf(" index file name [%s]: ",iname);
fgets(iname,40,stdin);
if (iname[strlen(iname)-1]=='\n') iname[strlen(iname)-1]='\0';
goto l2;
#else
sprintf(prompt," could not open index file: %s\r Select index filename",iname);
FileDlog(prompt,&freply);
if (freply.good==TRUE) {
strcpy(libenv,"\0");
PtoCstr((char *)freply.fName);
strcpy(iname,(char *)freply.fName);
sqvRef=freply.vRefNum;
SetVol("\p\0",sqvRef);
}
goto l2;
#endif
}
if ((finx=fopen(inname,"r"))==NULL) {
printf(" could not open inx file: %s\n",inname);
exit(0);
}
ilb = i = 0;
while ((fgets(lline,sizeof(lline),finx))!=NULL) {
if ((bp=strchr(lline,'\n'))!=NULL) *bp='\0';
if (lline[0]=='<') {
strncpy(libenv,&lline[1],sizeof(libenv));
}
else {
lbnames[i++]= &lbnarr[ilb];
strncpy(&lbnarr[ilb],lline,sizeof(lbnarr)-ilb);
ilb += strlen(lline)+1;
if (i>=MAXLF) break;
}
}
fclose(finx);
nln = i;
libfn = -1;
lboff = 0L;
filen = lseek(fidx,0L,2);
maxidx = (filen-lboff)/(long)sizeof(seq);
if (argc<3) {
/* get the sequence names and hash them */
getnames(tname,40);
}
else for (i=2; i<argc; i++)
if (lookup(argv[i],&lmark,&lfn)==0)
printf(" sequence %s not found\n",argv[i]);
else putfile(argv[i],lmark,lfn);
}
/* newname generates a new filename with prefix oname and suffix suff */
newname(nname,oname,suff,maxn)
char *nname, *oname, *suff;
{
char *tptr;
int i;
if (*oname!='@') strncpy(nname,oname,maxn);
else strncpy(nname,oname+1,maxn);
for (i=strlen(nname)-1; i>=0 && nname[i]!='.'; i--);
/* get to '.' or BOS */
if (i>0) nname[i+1]='\0';
else {nname[i=strlen(nname)]='.'; nname[i+1]='\0';}
strncat(nname,suff,maxn);
}
getnames(tname,maxnam) /* read in the names and hash them */
char *tname; int maxnam;
{
int i;
char tline[40];
l1: if (strlen(tname)==0) { /* get names from keyboard */
printf(" protein sequence identifier: ");
fgets(tname,maxnam,stdin);
if (tname[i=strlen(tname)-1]=='\n') tname[i]='\0';
if (tname[0]=='\0') return;
}
if (tname[0]=='@') {
if ((tptr=fopen(&tname[1],"r"))==0) {
printf(" cannot open name file %s\n",&tname[1]);
tname[0]='\0';
goto l1;
}
while (fgets(tline,40,tptr)!=0) {
if (tline[i=strlen(tline)-1]=='\n') tline[i]='\0';
if (lookup(tline,&lmark,&lfn)==0)
printf(" sequence %s not found\n",tline);
else {
putfile(tline,lmark,lfn);
printf(" found %s, creating %s.aa\n",tline,tline);
}
}
}
else {
if (lookup(tname,&lmark,&lfn)==0)
printf(" sequence %s not found\n",tname);
else putfile(tname,lmark,lfn);
tname[0]='\0';
goto l1;
}
}
ucase(str) /* convert a string to upper case */
char *str;
{
while (*str) {
if (*str >= 'a' && *str <= 'z') *str -= 'a' - 'A';
str++;
}
}
lcase(str) /* convert a string to lower case */
char *str;
{
while (*str) {
if (*str >= 'A' && *str <= 'Z') *str += 'a' - 'A';
str++;
}
}
lookup(name,mark,seqfn) /* lookup names in library */
char *name; long *mark; int *seqfn;
{
long hi, lo, mid, diff;
long pos;
ucase(name);
/* binary search for code */
lo = 0;
hi = maxidx;
while (hi >= lo) {
mid = (hi + lo)/2l;
pos = (long)mid * (long)sizeof(seq);
lseek(fidx, pos+lboff, 0);
read(fidx,(char *)&seq, sizeof(seq));
if ((diff = strcmp(name, seq.nam)) == 0) {
*mark = seq.lmark;
*seqfn = seq.fn;
return 1;
}
else if (diff < 0)
hi = mid - 1l;
else
lo = mid + 1l;
}
return 0;
}
putfile(seqnam,seqmark,seqfn)
char *seqnam; long seqmark; int seqfn;
{
int i;
strncpy(oname,seqnam,40);
lcase(oname);
strcat(oname,".aa");
#ifdef THINK_C
SetVol("\p0\0",ouvRef);
#endif
if ((optr=fopen(oname,"w"))==0)
printf(" cannot open %s\n",oname);
else {
if (seqfn!=libfn) {
closelib();
if (openlib(lbnames[seqfn],libenv)<0) return;
libfn=seqfn;
}
fseek(lptr,seqmark,0);
fgets(lline,512,lptr);
if (strlen(lline)>72) {lline[72]='\n'; lline[73]='\0';}
fputs(lline,optr);
lline[72]='\0';
while(fgets(lline,71,lptr) && lline[0]!='>') {
if (lline[(i=strlen(lline)-1)]!='\n') {
lline[i+1]='\n'; lline[i+2]='\0';
}
fputs(lline,optr);
}
}
fclose(optr);
}
openlib(lname,libenv)
char *lname, *libenv;
{
char lbname[80],rline[10];
long ftell();
int wcnt;
wcnt=0;
if (*libenv!='\0') {
strncpy(lbname,libenv,sizeof(lbname));
#ifdef UNIX
strcat(lbname,"/");
#endif
}
else *lbname='\0';
strncat(lbname,lname,sizeof(lbname)-strlen(lbname));
#ifdef THINK_C
SetVol("\p",sqvRef);
l1: if ((lptr=fopen(lbname,"r"))==NULL) {
sprintf(prompt," cannot open %s\r Select library filename",lbname);
FileDlog(prompt,&freply);
if (freply.good==TRUE) {
strcpy(libenv,"\0");
PtoCstr((char *)freply.fName);
strcpy(lbname,(char *)freply.fName);
sqvRef=anvRef=freply.vRefNum;
SetVol("\p\0",sqvRef);
goto l1;
}
else return -1;
}
#else /* MSDOS */
l1: if ((lptr=fopen(lbname,"r"))==0) {
rline[0]='\0';
fprintf(stderr," cannot open %s library\n",lbname);
fprintf(stderr," insert another disk or type Y to skip <N> ");
fflush(stderr);
if (fgets(rline,10,stdin)==NULL) return -1;
if (toupper(rline[0])=='Y') return 0;
if (++wcnt > 10) return -1;
goto l1;
}
#endif
return 1;
}
closelib()
{
if (lptr!=NULL) fclose(lptr);
}