Mercurial > repos > rliterman > csp2
comparison CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/opt/mummer-3.23/src/tigr/tigrinc.cc @ 69:33d812a61356
planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author | jpayne |
---|---|
date | Tue, 18 Mar 2025 17:55:14 -0400 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
67:0e9998148a16 | 69:33d812a61356 |
---|---|
1 #include "tigrinc.hh" | |
2 | |
3 | |
4 FILE * File_Open (const char * Filename, const char * Mode) | |
5 | |
6 /* Open Filename in Mode and return a pointer to its control | |
7 * block. If fail, print a message and exit. */ | |
8 | |
9 { | |
10 FILE * fp; | |
11 | |
12 fp = fopen (Filename, Mode); | |
13 if (fp == NULL) | |
14 { | |
15 fprintf (stderr, "ERROR: Could not open file %s \n", Filename); | |
16 exit (EXIT_FAILURE); | |
17 } | |
18 | |
19 return fp; | |
20 } | |
21 | |
22 | |
23 | |
24 void * Safe_calloc (size_t N, size_t Len) | |
25 | |
26 /* Allocate and return a pointer to an array of N elements of | |
27 * Len bytes each. All set to 0. Exit if fail. */ | |
28 | |
29 { | |
30 void * P; | |
31 | |
32 P = calloc (N, Len); | |
33 if (P == NULL) | |
34 { | |
35 fprintf (stderr, | |
36 "ERROR: calloc failed, there is not enough memory\n"); | |
37 exit (EXIT_FAILURE); | |
38 } | |
39 | |
40 return P; | |
41 } | |
42 | |
43 | |
44 | |
45 void * Safe_malloc (size_t Len) | |
46 | |
47 /* Allocate and return a pointer to Len bytes of memory. | |
48 * Exit if fail. */ | |
49 | |
50 { | |
51 void * P; | |
52 | |
53 P = malloc (Len); | |
54 if (P == NULL) | |
55 { | |
56 fprintf (stderr, | |
57 "ERROR: malloc failed, there is not enough memory\n"); | |
58 exit (EXIT_FAILURE); | |
59 } | |
60 | |
61 return P; | |
62 } | |
63 | |
64 | |
65 | |
66 void * Safe_realloc (void * Q, size_t Len) | |
67 | |
68 /* Reallocate memory for Q to Len bytes and return a | |
69 * pointer to the new memory. Exit if fail. */ | |
70 | |
71 { | |
72 void * P; | |
73 | |
74 P = realloc (Q, Len); | |
75 if (P == NULL) | |
76 { | |
77 fprintf (stderr, | |
78 "ERROR: realloc failed, there is not enough memory\n"); | |
79 exit (EXIT_FAILURE); | |
80 } | |
81 | |
82 return P; | |
83 } | |
84 | |
85 | |
86 | |
87 char Complement (char Ch) | |
88 | |
89 /* Returns the DNA complement of Ch . */ | |
90 | |
91 { | |
92 switch (tolower (Ch)) | |
93 { | |
94 case 'a' : | |
95 return 't'; | |
96 case 'c' : | |
97 return 'g'; | |
98 case 'g' : | |
99 return 'c'; | |
100 case 't' : | |
101 return 'a'; | |
102 case 'r' : // a or g | |
103 return 'y'; | |
104 case 'y' : // c or t | |
105 return 'r'; | |
106 case 's' : // c or g | |
107 return 's'; | |
108 case 'w' : // a or t | |
109 return 'w'; | |
110 case 'm' : // a or c | |
111 return 'k'; | |
112 case 'k' : // g or t | |
113 return 'm'; | |
114 case 'b' : // c, g or t | |
115 return 'v'; | |
116 case 'd' : // a, g or t | |
117 return 'h'; | |
118 case 'h' : // a, c or t | |
119 return 'd'; | |
120 case 'v' : // a, c or g | |
121 return 'b'; | |
122 default : // anything | |
123 return tolower (Ch); | |
124 } | |
125 } | |
126 | |
127 | |
128 bool CompareIUPAC (char x, char y) | |
129 { | |
130 x = tolower(x); | |
131 y = tolower(y); | |
132 | |
133 if ( x == 'n' || x == 'x' || y == 'n' || y == 'x' ) | |
134 return true; | |
135 | |
136 switch ( x ) | |
137 { | |
138 case 'a' : | |
139 switch ( y ) | |
140 { | |
141 case 'a': | |
142 case 'r': | |
143 case 'w': | |
144 case 'm': | |
145 case 'd': | |
146 case 'h': | |
147 case 'v': | |
148 return true; | |
149 } | |
150 return false; | |
151 | |
152 case 'c' : | |
153 switch ( y ) | |
154 { | |
155 case 'c': | |
156 case 'y': | |
157 case 's': | |
158 case 'm': | |
159 case 'b': | |
160 case 'h': | |
161 case 'v': | |
162 return true; | |
163 } | |
164 return false; | |
165 | |
166 case 'g' : | |
167 switch ( y ) | |
168 { | |
169 case 'g': | |
170 case 'r': | |
171 case 's': | |
172 case 'k': | |
173 case 'b': | |
174 case 'd': | |
175 case 'v': | |
176 return true; | |
177 } | |
178 return false; | |
179 | |
180 case 't' : | |
181 switch ( y ) | |
182 { | |
183 case 't': | |
184 case 'y': | |
185 case 'w': | |
186 case 'k': | |
187 case 'b': | |
188 case 'd': | |
189 case 'h': | |
190 return true; | |
191 } | |
192 return false; | |
193 | |
194 case 'r' : // a or g | |
195 switch ( y ) | |
196 { | |
197 case 'r': | |
198 case 'a': | |
199 case 'g': | |
200 case 'd': | |
201 case 'v': | |
202 return true; | |
203 } | |
204 return false; | |
205 | |
206 case 'y' : // c or t | |
207 switch ( y ) | |
208 { | |
209 case 'y': | |
210 case 'c': | |
211 case 't': | |
212 case 'b': | |
213 case 'h': | |
214 return true; | |
215 } | |
216 return false; | |
217 | |
218 case 's' : // c or g | |
219 switch ( y ) | |
220 { | |
221 case 's': | |
222 case 'c': | |
223 case 'g': | |
224 case 'b': | |
225 case 'v': | |
226 return true; | |
227 } | |
228 return false; | |
229 | |
230 case 'w' : // a or t | |
231 switch ( y ) | |
232 { | |
233 case 'w': | |
234 case 'a': | |
235 case 't': | |
236 case 'd': | |
237 case 'h': | |
238 return true; | |
239 } | |
240 return false; | |
241 | |
242 case 'm' : // a or c | |
243 switch ( y ) | |
244 { | |
245 case 'm': | |
246 case 'a': | |
247 case 'c': | |
248 case 'h': | |
249 case 'v': | |
250 return true; | |
251 } | |
252 return false; | |
253 | |
254 case 'k' : // g or t | |
255 switch ( y ) | |
256 { | |
257 case 'k': | |
258 case 'g': | |
259 case 't': | |
260 case 'b': | |
261 case 'd': | |
262 return true; | |
263 } | |
264 return false; | |
265 | |
266 case 'b' : // c, g or t | |
267 switch ( y ) | |
268 { | |
269 case 'b': | |
270 case 'c': | |
271 case 'g': | |
272 case 't': | |
273 return true; | |
274 } | |
275 return false; | |
276 | |
277 case 'd' : // a, g or t | |
278 switch ( y ) | |
279 { | |
280 case 'd': | |
281 case 'a': | |
282 case 'g': | |
283 case 't': | |
284 return true; | |
285 } | |
286 return false; | |
287 | |
288 case 'h' : // a, c or t | |
289 switch ( y ) | |
290 { | |
291 case 'h': | |
292 case 'a': | |
293 case 'c': | |
294 case 't': | |
295 return true; | |
296 } | |
297 return false; | |
298 | |
299 case 'v' : // a, c or g | |
300 switch ( y ) | |
301 { | |
302 case 'v': | |
303 case 'a': | |
304 case 'c': | |
305 case 'g': | |
306 return true; | |
307 } | |
308 return false; | |
309 } | |
310 return false; | |
311 } | |
312 | |
313 | |
314 int Read_String (FILE * fp, char * & T, long int & Size, char Name [], | |
315 int Partial) | |
316 | |
317 /* Read next string from fp (assuming FASTA format) into T [1 ..] | |
318 * which has Size characters. Allocate extra memory if needed | |
319 * and adjust Size accordingly. Return TRUE if successful, FALSE | |
320 * otherwise (e.g., EOF). Partial indicates if first line has | |
321 * numbers indicating a subrange of characters to read. | |
322 */ | |
323 | |
324 { | |
325 char * P, Line [MAX_LINE]; | |
326 long int Len, Lo, Hi; | |
327 int Ch, Ct; | |
328 | |
329 while ((Ch = fgetc (fp)) != EOF && Ch != '>') | |
330 ; | |
331 | |
332 if (Ch == EOF) | |
333 return FALSE; | |
334 | |
335 fgets (Line, MAX_LINE, fp); | |
336 Len = strlen (Line); | |
337 assert (Len > 0 && Line [Len - 1] == '\n'); | |
338 P = strtok (Line, " \t\n"); | |
339 if (P != NULL) | |
340 strcpy (Name, P); | |
341 else | |
342 Name [0] = '\0'; | |
343 Lo = 0; Hi = LONG_MAX; | |
344 if (Partial) | |
345 { | |
346 P = strtok (NULL, " \t\n"); | |
347 if (P != NULL) | |
348 { | |
349 Lo = strtol (P, NULL, 10); | |
350 P = strtok (NULL, " \t\n"); | |
351 if (P != NULL) | |
352 Hi = strtol (P, NULL, 10); | |
353 } | |
354 assert (Lo <= Hi); | |
355 } | |
356 | |
357 Ct = 0; | |
358 T [0] = '\0'; | |
359 Len = 1; | |
360 while ((Ch = fgetc (fp)) != EOF && Ch != '>') | |
361 { | |
362 if (isspace (Ch)) | |
363 continue; | |
364 | |
365 Ct ++; | |
366 if (Ct < Lo || Ct > Hi) | |
367 continue; | |
368 | |
369 if (Len >= Size) | |
370 { | |
371 Size += INCR_SIZE; | |
372 T = (char *) Safe_realloc (T, Size); | |
373 } | |
374 Ch = tolower (Ch); | |
375 | |
376 if (! isalpha (Ch) && Ch != '*') | |
377 { | |
378 fprintf (stderr, "Unexpected character `%c\' in string %s\n", | |
379 Ch, Name); | |
380 Ch = 'x'; | |
381 } | |
382 | |
383 T [Len ++] = Ch; | |
384 } | |
385 | |
386 T [Len] = '\0'; | |
387 if (Ch == '>') | |
388 ungetc (Ch, fp); | |
389 | |
390 return TRUE; | |
391 } | |
392 | |
393 | |
394 | |
395 void Reverse_Complement | |
396 (char S [], long int Lo, long int Hi) | |
397 | |
398 // Convert substring S [Lo .. Hi] to its Watson-Crick reverse | |
399 // complement | |
400 | |
401 { | |
402 char Ch; | |
403 | |
404 while (Lo <= Hi) | |
405 { | |
406 Ch = S [Hi]; | |
407 S [Hi] = Complement (S [Lo]); | |
408 S [Lo] = Complement (Ch); | |
409 Lo ++; | |
410 Hi --; | |
411 } | |
412 | |
413 return; | |
414 } | |
415 |