-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgene_core.h
178 lines (144 loc) · 7.76 KB
/
gene_core.h
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
#ifndef _CORE
#define _CORE
#include <stdio.h>
#include <time.h>
// For interactive applications where it is inappropriate to simply exit with an error
// message to standard error, define the constant INTERACTIVE. If set, then error
// messages are put in the global variable Ebuffer and the caller of a core routine
// can decide how to deal with the error.
#ifdef INTERACTIVE
#define EPRINTF sprintf
#define EPLACE Ebuffer
#define EXIT(x) return (x)
extern char Ebuffer[];
#else // BATCH
#define EPRINTF fprintf
#define EPLACE stderr
#define EXIT(x) exit (1)
#endif
/*******************************************************************************************
*
* MY STANDARD TYPE DECLARATIONS
*
********************************************************************************************/
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
typedef unsigned long long uint64;
typedef signed char int8;
typedef signed short int16;
typedef signed int int32;
typedef signed long long int64;
typedef float float32;
typedef double float64;
/*******************************************************************************************
*
* MACROS TO HELP PARSE COMMAND LINE
*
********************************************************************************************/
extern char *Prog_Name; // Name of program, available everywhere
extern char *Command_Line; // Name of program, available everywhere
#define ARG_INIT(name) \
{ int n, i; \
char *c; \
\
n = 0; \
for (i = 0; i < argc; i++) \
n += strlen(argv[i])+1; \
\
Command_Line = Malloc(n+1,"Allocating command string"); \
if (Command_Line == NULL) \
exit (1); \
\
c = Command_Line; \
if (argc >= 1) \
{ c += sprintf(c,"%s",argv[0]); \
for (i = 1; i < argc; i++) \
c += sprintf(c," %s",argv[i]); \
} \
*c = '\0'; \
} \
\
Prog_Name = Strdup(name,""); \
for (i = 0; i < 128; i++) \
flags[i] = 0;
#define ARG_FLAGS(set) \
for (k = 1; argv[i][k] != '\0'; k++) \
{ if (index(set,argv[i][k]) == NULL) \
{ fprintf(stderr,"%s: -%c is an illegal option\n",Prog_Name,argv[i][k]); \
exit (1); \
} \
flags[(int) argv[i][k]] = 1; \
}
#define ARG_POSITIVE(var,name) \
var = strtol(argv[i]+2,&eptr,10); \
if (*eptr != '\0' || argv[i][2] == '\0') \
{ fprintf(stderr,"%s: -%c '%s' argument is not an integer\n", \
Prog_Name,argv[i][1],argv[i]+2); \
exit (1); \
} \
if (var <= 0) \
{ fprintf(stderr,"%s: %s must be positive (%d)\n",Prog_Name,name,var); \
exit (1); \
}
#define ARG_NON_NEGATIVE(var,name) \
var = strtol(argv[i]+2,&eptr,10); \
if (*eptr != '\0' || argv[i][2] == '\0') \
{ fprintf(stderr,"%s: -%c '%s' argument is not an integer\n", \
Prog_Name,argv[i][1],argv[i]+2); \
exit (1); \
} \
if (var < 0) \
{ fprintf(stderr,"%s: %s must be non-negative (%d)\n",Prog_Name,name,var); \
exit (1); \
}
#define ARG_REAL(var) \
var = strtod(argv[i]+2,&eptr); \
if (*eptr != '\0' || argv[i][2] == '\0') \
{ fprintf(stderr,"%s: -%c '%s' argument is not a real number\n", \
Prog_Name,argv[i][1],argv[i]+2); \
exit (1); \
}
/*******************************************************************************************
*
* MEMORY ALLOCATION,FILE HANDLING, AND PRETTY PRINTING UTILITIES
*
********************************************************************************************/
// The following general utilities return NULL if any of their input pointers are NULL, or if they
// could not perform their function (in which case they also print an error to stderr).
void *Malloc(int64 size, char *mesg); // Guarded versions of malloc, realloc
void *Realloc(void *object, int64 size, char *mesg); // and strdup, that output "mesg" to
char *Strdup(char *string, char *mesg); // stderr if out of memory
char *Strndup(char *string, int len, char *mesg); // stderr if out of memory
FILE *Fopen(char *path, char *mode); // Open file path for "mode"
char *PathTo(char *path); // Return path portion of file name "path"
char *Root(char *path, char *suffix); // Return the root name, excluding suffix, of "path"
// Catenate returns concatenation of path.sep.root.suffix in a *temporary* buffer
// Numbered_Suffix returns concatenation of left.<num>.right in a *temporary* buffer
char *Catenate(char *path, char *sep, char *root, char *suffix);
char *Numbered_Suffix(char *left, int num, char *right);
void Print_Number(int64 num, int width, FILE *out); // Print readable big integer
int Number_Digits(int64 num); // Return # of digits in printed number
/*******************************************************************************************
*
* ROUTINES FOR HANDLING DNA AND ARROW STRINGS
*
********************************************************************************************/
#define COMPRESSED_LEN(len) (((len)+3) >> 2)
void Compress_Read(int len, char *s); // Compress read in-place into 2-bit form
void Uncompress_Read(int len, char *s); // Uncompress read in-place into numeric form
void Print_Read(char *s, int width);
void Lower_Read(char *s); // Convert read from numbers to lowercase letters (0-3 to acgt)
void Upper_Read(char *s); // Convert read from numbers to uppercase letters (0-3 to ACGT)
void Number_Read(char *s); // Convert read from letters to numbers
void Change_Read(char *s); // Convert read from one case to the other
void Letter_Arrow(char *s); // Convert arrow pw's from numbers to uppercase letters (0-3 to 1234)
void Number_Arrow(char *s); // Convert arrow pw string from letters to numbers
/*******************************************************************************************
*
* RESOURCE UTILITY
*
********************************************************************************************/
void StartTime();
void TimeTo(FILE *f, int all);
#endif // _CORE