/*
Copyright (C) 1997 by Warren R. Gish.  All Rights Reserved.
*/
#ifndef _NRDB_
#define _NRDB_

#ifndef EXTERN
#define EXTERN	extern
#else
#define INIT
#endif

#include <errno.h>

#include "util.h"

#define HASHLEN	4

typedef struct nonredundant_file {
			char	*filename;
			int		dbnamelen;
			char	*dbname;
			FILE	*fp;
			unsigned long	numseqs, nummatches, totres, dupres, lencnt, lenres, numnull;
			int		filenum;
		} NRFile, *NRFilePtr;


typedef struct seqname {
			struct seqname	*chain;
			NRFilePtr	nrfp;
			size_t	namelen;
			char	*name;
		} SeqName, *SeqNamePtr;

typedef struct seqstr {
			struct seqstr	*chain;
			size_t		seqlen;
			size_t		clen; /* compressed length (0 if not compressed) */
			int			compression;
			unsigned long	id;
			char		*seq; /* the sequence string */
			SeqNamePtr	name1;
		} SeqStr, *SeqStrPtr;

/* Keep all hash values and sequence pointers in the same pages of memory */
typedef struct {
			SeqStrPtr	sp;
			unsigned long	hashval;
			unsigned long	nextid;
		} HashRay, *HashRayPtr;

/* SeqName_New -- allocate a new SeqName area */
SeqNamePtr	SeqName_New PROTO((void));
/* SeqName_Dup -- return pointer to a duplicate of a sequence name */
SeqNamePtr	SeqName_Dup PROTO((SeqNamePtr name));
/* SeqName_Destruct -- deallocate a SeqName area */
void		SeqName_Destruct PROTO((SeqNamePtr));
/* SeqStr_Name -- returns the name of the given sequence */
SeqNamePtr	SeqStr_Name PROTO((SeqStrPtr seq));

/* SeqStr_AlreadyFound -- returns pointer to matching sequence (or NULL) */
SeqStrPtr	SeqStr_AlreadyFound PROTO((SeqStrPtr));
/* SeqStr_Cmp -- returns 0 if the two sequences are identical */
int			SeqStr_Cmp PROTO((unsigned long id1, unsigned long id2));
/* SeqStr_Append -- append SeqStr item to the master linked list */
SeqStrPtr	SeqStr_Append PROTO((SeqStrPtr item));
/* SeqStr_AddBase -- add the SeqStr to the baseray */
int			SeqStr_AddBase PROTO((SeqStrPtr));
/* SeqStr_AppendName -- append SeqName to a linked list */
int			SeqStr_AppendName PROTO((SeqStrPtr item, SeqNamePtr name));
/* SeqStr_Dup -- return pointer to a duplicate a SeqStr structure */
SeqStrPtr	SeqStr_Dup PROTO((SeqStrPtr seq));

NRFilePtr	NRFile_Open PROTO((char *filename, char *dbname));
Boolean		NRFile_Isopen PROTO((NRFilePtr nrfp));
int			NRFile_Close PROTO((NRFilePtr nrfp));
void		NRFile_Destruct PROTO((NRFilePtr nrfp));
SeqStrPtr	NRFile_Read PROTO((NRFilePtr nrfp));

EXTERN int	hashlen
#ifdef INIT
		= HASHLEN
#endif
		;

EXTERN Boolean skip1
#ifdef INIT
		= FALSE
#endif
	;

EXTERN char	*module;

EXTERN FILE	*ofp;	/* the output file stream */

EXTERN SeqStrPtr	listhead, listtail;
EXTERN char	plocked;

EXTERN unsigned long	numseqs, totres, dupres, numhits, numtothits, nummatches, numnull;
EXTERN unsigned long	lencnt, lenres;
EXTERN long numhashhits;
EXTERN long	maxnamelen, maxwnamelen, maxseqlen;

EXTERN NRFilePtr	curfp;

EXTERN size_t	seqbufmax;
EXTERN UcharPtr	seqbuf; /* buffer big enough for the longest sequence */
EXTERN long	seqcnt;
EXTERN unsigned long	baseray[1<<16];
EXTERN HashRayPtr	hashray;
EXTERN long	hashraymax;
EXTERN unsigned char	xltab[1<<CHAR_BIT];
EXTERN unsigned char	nt_atob[1<<CHAR_BIT], nt_btoa[1<<CHAR_BIT];
EXTERN int	desc_max;
EXTERN char	delim
#ifdef INIT
	= '\001'
#endif
	;

#endif /* !_NRDB_ */

