/* * CK2PHILIP.C * * Version 1.0: Christopher Putnam * cdputnam@scripps.edu * http://www.scripps.edu/~cdputnam * * Source released under GPL */ #include #include #include #ifndef TRUE #define TRUE (1) #define FALSE (!TRUE) #endif typedef struct seqs { char name[10]; char *seq; unsigned long length; unsigned long dim; } seqs; void seqs_free( seqs *seqlist, unsigned long numseq ) { unsigned long i; if (seqlist==NULL) return; for (i=0; imax) max=seqlist[i].length; } /* normalize length */ for (i=0; i 1st time */ while (!feof(fp)) { /* * Parse till ">" */ while (!feof(fp) && *p!='>') { if (fgets(buf,sizeof(buf),fp)!=NULL) { p = nonws(buf); } } /* * Grab name (fill with spaces until end, no \0 at end) */ if (seqlist==NULL) seqlist = *seqlistptr; if (seqlist==NULL) dimseq = seqs_init(&seqlist); if (*p=='>') { if (dimseq == numseq) dimseq = seqs_getmore(&seqlist,dimseq); currseq=numseq; numseq++; p++; eos=FALSE; for (i=0; i<10; ++i) { if (p[i]=='\0' || p[i]=='\r' || p[i]=='\n' ) eos=TRUE; seqlist[currseq].name[i] = ( eos==TRUE ) ? ' ' : p[i]; } seqlist[currseq].seq=NULL; seqlist[currseq].dim=0; seqlist[currseq].length=0; } while (!feof(fp) && *p!='>') { /* get next line and put p to 1st non ws */ if (fgets(buf,sizeof(buf),fp)!=NULL) { p = nonws(buf); } /* if couldn't get line or we're at new title go on...*/ if (!feof(fp) && *p!='>') { len = strlen(p) + seqlist[currseq].length + 1; if (seqlist[currseq].seq==NULL) { seqlist[currseq].seq = malloc(sizeof(char)*len); if (seqlist[currseq].seq==NULL) { fprintf(stderr,"Cannot alloc to size %ld!!!\n",len); exit(EXIT_FAILURE); } seqlist[currseq].dim = len; } else if (len>seqlist[currseq].dim) { seqlist[currseq].seq = realloc(seqlist[currseq].seq,sizeof(char)*len); if (seqlist[currseq].seq==NULL) { fprintf(stderr,"Cannot realloc to size %ld!!!\n",len); exit(EXIT_FAILURE); } seqlist[currseq].dim = len; } pos = seqlist[currseq].length; while (*p) { p = nonws(p); if (*p!='\0') { if (*p!='*') seqlist[currseq].seq[pos++]=*p; *p++; } } seqlist[currseq].seq[pos]='\0'; seqlist[currseq].length=pos; } } } *seqlistptr = seqlist; *numseqptr = numseq; *dimseqptr = dimseq; fclose(fp); } int main (int argc, char *argv[]) { int i; seqs *seqlist=NULL; unsigned long numseq=0,maxlenseq=0,dimseq=0; if (argc<2) { fprintf(stderr,"Usage: ck2phylip [....]\n"); fprintf(stderr,"\nTakes SEQUOIA/CKWHENCE files and generates "); fprintf(stderr,"non-interlaced PHYLIP files.\n"); fprintf(stderr,"SEQUOIA: http://www.scripps.edu/~bruns/sequoia.html\n"); fprintf(stderr,"PHYLIP: http://evolution.genetics.washington.edu/phylip.html\n"); fprintf(stderr,"\nVersion 1.0, February 2000\n\n"); exit(EXIT_FAILURE); } for (i=1; i