Next version of JABA
[jabaws.git] / binaries / src / fasta34 / llgetaa.c
1 /* copyright (c) 1996, 1997, 1998, 1999 William R. Pearson and the
2    U. of Virginia */
3
4 /* $Name: fa_34_26_5 $ - $Id: llgetaa.c,v 1.25 2007/01/08 15:38:46 wrp Exp $ */
5
6 /*
7    Feb, 1998 - version for prss 
8
9    March, 2001 - modifications to support comp_thr.c: use libpos to indicate
10    whether the score is shuffled==1 or unshuffled==0.  This simplifies
11    complib.c and makes comp_thr.c possible
12
13    modified version of nxgetaa.c that generates random sequences
14    for a library
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "defs.h"
22 #include "mm_file.h"
23
24 #include "uascii.h"
25 #include "structs.h"
26
27 #define XTERNAL
28 #include "upam.h"
29 #undef XTERNAL
30
31 #define YES 1
32 #define NO 0
33 #define MAXLINE 512
34
35 #ifndef min
36 #define min(x,y) ((x) > (y) ? (y) : (x))
37 #endif
38
39 int nsfnum;     /* number of superfamily numbers */
40 int sfnum[10];  /* superfamily number from types 0 and 5 */
41 int nsfnum_n;
42 int sfnum_n[10];
43
44 static int use_stdin=0;
45 static char llibstr0[256];
46 static char llibstr1[256];
47 static char o_line[256];
48
49 #define NO_FORMAT 0
50 #define FASTA_FORMAT 1
51 #define GCG_FORMAT 2
52 static int seq_format=NO_FORMAT;
53 static char seq_title[200];
54
55 extern int irand(int);
56 extern void shuffle(unsigned char *from, unsigned char *to, int n);
57 extern void wshuffle(unsigned char *from, unsigned char *to, int n, int wsiz, int *ieven);
58
59 int
60 getseq(char *filen, int *qascii,
61        unsigned char *seq, int maxs, char *libstr,
62        int n_libstr, long *sq0off)
63 {
64   FILE *fptr;
65   char line[512],*bp;
66   int i, j, n;
67   int ic;
68   int sstart, sstop, sset=0;
69   int have_desc = 0;
70   int desc_complete = 0;
71   int llen, l_offset;
72
73   seq_title[0]='\0';
74
75   sstart = sstop = -1;
76 #ifndef DOS
77   if ((bp=strchr(filen,':'))!=NULL) {
78 #else
79   if ((bp=strchr(filen+3,':'))!=NULL) {
80 #endif
81     *bp='\0';
82     if (*(bp+1)=='-') sscanf(bp+2,"%d",&sstop);
83     else sscanf(bp+1,"%d-%d",&sstart,&sstop);
84     sset=1;
85   }
86
87   if (strcmp(filen,"-") && strcmp(filen,"@")) {
88     if ((fptr=fopen(filen,"r"))==NULL) {
89       fprintf(stderr," could not open %s\n",filen);
90       return 0;
91     }
92   }
93   else {
94     fptr = stdin;
95     use_stdin++;
96   }
97
98   if (use_stdin > 1) {
99     have_desc = 1;
100     if ((bp=strchr(o_line,'\001'))!=NULL) *bp='\0';
101     strncpy(llibstr1,o_line,sizeof(llibstr1));
102     strncpy(libstr,o_line,n_libstr);
103     libstr[n_libstr-1]='\0';
104     l_offset = 0;
105   }
106
107   if (sset==1) {
108     filen[strlen(filen)]=':';
109     if (*sq0off==1 || sstart>1) *sq0off = sstart;
110   }
111
112   desc_complete = 0;
113   n=0;
114   while(fgets(line,sizeof(line),fptr)!=NULL) {
115     if (line[0]=='>') {
116       if (have_desc) {
117         strncpy(o_line,line,sizeof(o_line));
118         goto last;
119       }
120       l_offset = 0;
121       seq_format = FASTA_FORMAT;
122 #ifdef STAR_X
123       qascii['*'] = qascii['X'];
124 #endif
125       sfnum[0] = nsfnum = 0;
126
127       if ((bp=(char *)strchr(line,'\n'))!=NULL) {
128         *bp='\0';                               /* have newline */
129         desc_complete = 1;
130       }
131
132       if ((bp=strchr(line+1,'\001'))!=NULL) *bp='\0';
133       strncpy(seq_title,line+1,sizeof(seq_title));
134       strncpy(llibstr0,line+1,sizeof(llibstr0));
135       if (n_libstr <= 20) {
136         if ((bp=(char *)strchr(line,' '))!=NULL) *bp='\0';
137       }
138       strncpy(libstr,line+1,n_libstr);
139       libstr[n_libstr-1]='\0';
140
141       if (!desc_complete) {
142         while (fgets(line, sizeof(line), fptr) != NULL) {
143           if (strchr(line,'\n') != NULL) {
144             line[0]='>';
145             break;
146           }
147         }
148         desc_complete = 1;
149       }
150     }
151     else if (seq_format==NO_FORMAT) {
152       seq_format = GCG_FORMAT;
153       qascii['*'] = qascii['X'];
154       l_offset = 10;
155       llen = strlen(line);
156       while (strncmp(&line[llen-3],"..\n",(size_t)3) != 0) {
157         if (fgets(line,sizeof(line),fptr)==NULL) return 0;
158         llen = strlen(line);
159       }
160       if (n_libstr <= 20) {
161         if ((bp=(char *)strchr(line,' '))!=NULL) *bp='\0';
162         else if ((bp=(char *)strchr(line,'\n'))!=NULL) *bp='\0';
163       }
164       strncpy(libstr,line,n_libstr);
165       libstr[n_libstr-1]='\0';
166       if (fgets(line,sizeof(line),fptr)==NULL) return 0;
167     }
168
169     if (seq_format==GCG_FORMAT && strlen(line)<l_offset) continue;
170
171     if (line[0]!='>'&& line[0]!=';') {
172       for (i=l_offset; (n<maxs)&&
173              ((ic=qascii[line[i]&AAMASK])<EL); i++)
174         if (ic<NA) seq[n++]= ic;
175       if (ic == ES) break;
176     }
177     else {
178       if (have_desc) {
179         strncpy(o_line,line,sizeof(o_line));
180         goto last;
181       }
182       else {
183         have_desc = 1;
184       }
185     }
186   }
187
188  last:
189   if (n==maxs) {
190     fprintf(stderr," sequence may be truncated %d %d\n",n,maxs);
191     fflush(stderr);
192   }
193   if ((bp=strchr(libstr,'\n'))!=NULL) *bp = '\0';
194   if ((bp=strchr(libstr,'\r'))!=NULL) *bp = '\0';
195   seq[n]= EOSEQ;
196
197   if (fptr!=stdin) fclose(fptr);
198
199   if (sset) {
200     if (sstart <= 0) sstart = 1;
201     if (sstop <= 0) sstop = n;
202     sstart--;
203     sstop--;
204     for (i=0, j=sstart; j<=sstop; i++,j++)
205       seq[i] = seq[j];
206     n = sstop - sstart +1;
207     seq[n]=EOSEQ;
208   }
209
210   return n;
211 }
212
213 int
214 gettitle(filen,title,len)
215   char *filen, *title; int len;
216 {
217   FILE *fptr;
218   char line[512];
219   char *bp;
220   int ll,sset;
221 #ifdef WIN32
222   char *strpbrk();
223 #endif
224   sset = 0;
225
226   if (use_stdin) {
227     if (use_stdin == 1) {
228       /*      use_stdin++; */
229       strncpy(title,llibstr0,len);
230     }
231     else {
232       strncpy(title,llibstr1,len);
233     }
234     if ((bp=strchr(title,'\001'))!=NULL) *bp='\0';
235     return strlen(title);
236   }
237
238   if ((bp=strchr(filen,':'))!=NULL) { *bp='\0'; sset=1;}
239           
240   if ((fptr=fopen(filen,"r"))==NULL) {
241     fprintf(stderr," file %s was not found\n",filen);
242     fflush(stderr);
243     return 0;
244   }
245
246   if (sset==1) filen[strlen(filen)]=':';
247
248   while(fgets(line,sizeof(line),fptr)!=0) {
249     if (line[0]=='>'|| line[0]==';') goto found;
250   }
251   fclose(fptr);
252   title[0]='\0';
253   return 0;
254
255  found:
256   if ((bp=strchr(line,'\001'))!=NULL) *bp = 0;
257 #ifdef WIN32
258   bp = strpbrk(line,"\n\r");
259 #else
260   bp = strchr(line,'\n');
261 #endif
262   if (bp!=NULL) *bp = 0;
263   strncpy(title,line,len);
264   title[len-1]='\0';
265   fclose(fptr);
266   return strlen(title);
267 }       
268
269 FILE *libf=NULL;
270
271 long lpos;
272 char lline[MAXLINE];
273 int lfflag=0;   /* flag for CRLF in EMBL CDROM files */
274 #define LFCHAR '\015'  /* for MWC 5.5 */
275
276 int agetlib(); void aranlib();  /* pearson fasta format */
277
278 /*      the following is from fgetgb.c */
279
280 /* a file name for openlib may now include a library type suffix */
281 /* only opens fasta format files */
282
283 static char libn_save[MAX_FN];
284 static int ldna_save=0;
285 static int do_shuffle;
286 static int shuff_cnt=10;
287 static int w_flag = 0;
288 #ifdef DEBUG
289 static FILE *dfile=NULL;
290 #endif
291 static unsigned char *aa_save;
292 static int n1_save;
293 static int i_even;
294
295 /* lmf_str * is used here for compatibility with the "normal" openlib,
296    but is largely unnecessary */
297
298 void 
299 set_shuffle(struct mngmsg m_msg) {
300   char dfname[MAX_FN];
301
302   if (m_msg.shuff_wid > 0) w_flag = m_msg.shuff_wid;
303   if (m_msg.shuff_max > shuff_cnt) shuff_cnt = m_msg.shuff_max;
304
305 #ifdef DEBUG
306   if (m_msg.dfile[0]!='\0') {
307     strncpy(dfname,m_msg.dfile,sizeof(dfname));
308     strncat(dfname,"_rlib",sizeof(dfname));
309     dfile = fopen(dfname,"w");
310   }
311 #endif
312 }
313
314 struct lmf_str *
315 openlib(char *lname, int ldnaseq, int *sascii, int quiet, struct lmf_str *m_fd)
316 {
317   char rline[10],libn[MAX_FN], *bp;
318   int wcnt, ll, opnflg;
319   int libtype;
320   struct lmf_str *m_fptr;
321
322   wcnt = 0;
323   libtype = 0;
324
325   strncpy(libn_save,lname,sizeof(libn_save));
326
327   /* now allocate a buffer for the opened text file */
328   if ((m_fptr = calloc(1,sizeof(struct lmf_str)))==NULL) {
329     fprintf(stderr," cannot allocate lmf_str (%ld) for %s\n",
330             sizeof(struct lmf_str),lname);
331     return NULL;
332   }
333
334   strncpy(m_fptr->lb_name,lname,MAX_FN);
335   m_fptr->lb_name[MAX_FN-1]='\0';
336
337   m_fptr->sascii = sascii;
338   m_fptr->getlib = agetlib;
339   m_fptr->ranlib = aranlib;
340   m_fptr->mm_flg = 0;
341
342   do_shuffle = 0;
343   irand(0);             /* initialize the random number generator */
344
345   return m_fptr;
346 }
347
348 void
349 closelib()
350 {
351   if (libf!=NULL) {
352     fclose(libf);
353     libf = NULL;
354   }
355 #ifdef DEBUG
356   if (dfile) fclose(dfile);
357 #endif
358 }
359
360 static int ieven=0;
361 static char *desc_save;
362
363 int
364 agetlib(unsigned char *seq, 
365         int maxs,
366         char *libstr,
367         int n_libstr,
368         fseek_t *libpos,
369         int *lcont, 
370         struct lmf_str *lf_fd,
371         long *l_off)
372 {
373   long sq1_off;
374   char lib_desc[120];
375   int i;
376
377   *l_off = 1;
378
379   if (!do_shuffle) {
380     do_shuffle = 1;
381     
382     if ((n1_save = getseq(libn_save,lf_fd->sascii,
383                           seq,maxs,lib_desc,sizeof(lib_desc),&sq1_off)) < 1)
384       return n1_save;
385
386     strncpy(libstr,lib_desc,n_libstr);
387     libstr[n_libstr-1]='\0';
388
389     if ((aa_save = (unsigned char *)calloc(n1_save+1,sizeof(unsigned char)))==
390         NULL) fprintf(stderr," cannot allocate %d for saved sequence\n",
391                        n1_save);
392     memcpy((void *)aa_save,(void *)seq,n1_save);
393
394     if ((desc_save =
395          (char *)calloc(strlen(lib_desc)+1,sizeof(char)))== NULL) {
396       fprintf(stderr," cannot allocate saved desciption [%d]\n",
397               strlen(lib_desc)+1);
398     }
399     else {
400       strncpy (desc_save,lib_desc,strlen(lib_desc));
401       desc_save[strlen(lib_desc)]=='\0';
402     }
403
404     *libpos = 0;
405     return n1_save;
406   }
407   else {        /* return a shuffled sequence - here we need a window size; */
408     strncpy(libstr,desc_save,n_libstr);
409     libstr[n_libstr-1]='\0';
410
411     if (shuff_cnt-- <= 0 ) return -1;
412     if (w_flag > 0) wshuffle(aa_save,seq,n1_save,w_flag,&ieven);
413     else shuffle(aa_save,seq,n1_save);
414     seq[n1_save] = EOSEQ;
415 #ifdef DEBUG
416     if (dfile!=NULL) {
417       fprintf(dfile,">%d\n",shuff_cnt);
418       for (i=0; i<n1_save; i++) {
419         if (aa[seq[i]]>0) fputc(aa[seq[i]],dfile);
420         else {fprintf(stderr,"error aa0[%d]: %d %d\n",
421                       i,seq[i],aa[seq[i]]);}
422         if (i%60 == 59) fputc('\n',dfile);
423       }
424       fputc('\n',dfile);
425     }
426 #endif
427     *libpos = 1;
428     return n1_save;
429   }
430 }
431
432 void
433 aranlib(char *str,
434         int cnt,
435         fseek_t seek,
436         char *libstr,
437         struct lmf_str *lm_fd)
438 {
439   char *bp;
440   int ll;
441
442   if (use_stdin == 2) {
443     if (llibstr1[0]=='>' || llibstr1[0]==';') {
444       strncpy(str,llibstr1+1,cnt);
445     }
446     else {
447       strncpy(str,llibstr1,cnt);
448     }
449   }
450   else {
451     strncpy(str,desc_save,cnt);
452   }
453   str[cnt-1]='\0';
454   if ((bp = strchr(str,'\001'))!=NULL) *bp='\0';
455   else if ((bp = strchr(str,'\n'))!=NULL) *bp='\0';
456   else str[cnt-1]='\0';
457 }
458
459 /*
460 void
461 revcomp(unsigned char *seq, int n, int *c_nt)
462 {
463   unsigned char tmp;
464   int i, ni;
465
466
467   for (i=0, ni = n-1; i< n/2; i++,ni--) {
468     tmp = c_nt[seq[i]];
469     seq[i] = c_nt[seq[ni]];
470     seq[ni] = tmp;
471   }
472   if ((n%2)==1) {
473     i = n/2;
474     seq[i] = c_nt[seq[i]];
475   }
476 }
477 */
478
479 struct lmf_str *
480 re_openlib(struct lmf_str *om_fptr, int outtty)
481 {
482   return om_fptr;
483 }
484
485 int re_getlib(unsigned char *aa1, int n1, int maxt3, int loff, int cont,
486               int term_code, long *loffset, long *l_off,
487               struct lmf_str *m_file_p)
488 {
489   *loffset = 0;
490   *l_off = 1;
491   return n1;
492 }
493