comparison srf2fastq/io_lib-1.12.2/io_lib/read_alloc.c @ 0:d901c9f41a6a default tip

Migrated tool version 1.0.1 from old tool shed archive to new tool shed repository
author dawe
date Tue, 07 Jun 2011 17:48:05 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d901c9f41a6a
1 /*
2 * Copyright (c) Medical Research Council 1994. All rights reserved.
3 *
4 * Permission to use, copy, modify and distribute this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * this copyright and notice appears in all copies.
7 *
8 * This file was written by James Bonfield, Simon Dear, Rodger Staden,
9 * as part of the Staden Package at the MRC Laboratory of Molecular
10 * Biology, Hills Road, Cambridge, CB2 2QH, United Kingdom.
11 *
12 * MRC disclaims all warranties with regard to this software.
13 */
14
15 /*
16 * File: read_alloc.c
17 * Purpose: Performs the allocation/freeing of Read structures
18 * Last update: 01/09/94
19 */
20
21
22 /*
23 The Read data type is designed so that it can hold a varying degree
24 of information about sequences, yet have a single set of calls
25 to access the data.
26
27 There are plenty of assumptions around that both the number of
28 bases and the number of points will fit into an int_2, a short.
29
30 */
31
32 /* ---- Includes ---- */
33
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <assert.h>
38
39 #include "io_lib/misc.h"
40 #include "io_lib/Read.h"
41
42 /*
43 * Allocate a new sequence, with the given sizes.
44 * Returns:
45 * "Read *" for success
46 * "NULLRead" for failure
47 */
48 Read *read_allocate(int num_points, int num_bases) {
49 Read *seq = NULLRead;
50
51 int sections = read_sections(0);
52
53 /* Allocate the body of the sequence */
54 if ((seq = (Read *)xmalloc(sizeof(Read))) == NULL)
55 return(NULLRead);
56
57 seq->NPoints = num_points;
58 seq->NBases = num_bases;
59
60 /*
61 * Initialise the body, all pointers are set to NULL so we can
62 * happily call `read_deallocate()`.
63 */
64 seq->leftCutoff = 0;
65 seq->rightCutoff = 0;
66 seq->maxTraceVal = 0;
67 seq->baseline = 0;
68
69 seq->traceC = NULL;
70 seq->traceA = NULL;
71 seq->traceG = NULL;
72 seq->traceT = NULL;
73
74 seq->base = NULL;
75 seq->basePos = NULL;
76
77 seq->info = NULL;
78 seq->format = TT_ANY;
79 seq->trace_name = NULL;
80
81 seq->prob_A = NULL;
82 seq->prob_C = NULL;
83 seq->prob_G = NULL;
84 seq->prob_T = NULL;
85
86 seq->orig_trace_format = TT_ANY;
87 seq->orig_trace = NULL;
88 seq->orig_trace_free = NULL;
89
90 seq->ident = NULL;
91
92 /* Allocate space for the bases - 1 extra for the ->base field so
93 * that we can treat it as a NULL terminated string.
94 */
95 if (sections & READ_BASES &&
96 (((seq->base = (char *)xcalloc(num_bases+1,1)) == NULL) ||
97 ((seq->basePos = (uint_2 *)xcalloc(num_bases+1,2)) == NULL) ||
98 ((seq->prob_A = (char *)xcalloc(num_bases+1,1)) == NULL) ||
99 ((seq->prob_C = (char *)xcalloc(num_bases+1,1)) == NULL) ||
100 ((seq->prob_G = (char *)xcalloc(num_bases+1,1)) == NULL) ||
101 ((seq->prob_T = (char *)xcalloc(num_bases+1,1)) == NULL))
102 )
103 {
104 read_deallocate(seq);
105 return NULLRead;
106 }
107
108 if (sections & READ_SAMPLES &&
109 (((seq->traceC =(TRACE *)xcalloc(num_points+1, 2)) == NULL)||
110 ((seq->traceA =(TRACE *)xcalloc(num_points+1, 2)) == NULL)||
111 ((seq->traceG =(TRACE *)xcalloc(num_points+1, 2)) == NULL)||
112 ((seq->traceT =(TRACE *)xcalloc(num_points+1, 2)) == NULL))
113 )
114 {
115 read_deallocate(seq);
116 return NULLRead;
117 }
118
119 seq->nflows = 0;
120 seq->flow_order = NULL;
121 seq->flow = NULL;
122 seq->flow_raw = NULL;
123
124 seq->private_data = NULL;
125 seq->private_size = 0;
126
127 return seq;
128 }
129
130
131 /*
132 * Free memory allocated to a sequence by read_allocate().
133 */
134 void read_deallocate(Read *read)
135 {
136 if (read == NULLRead)
137 return;
138
139 if (read->traceC != NULL) xfree(read->traceC);
140 if (read->traceA != NULL) xfree(read->traceA);
141 if (read->traceG != NULL) xfree(read->traceG);
142 if (read->traceT != NULL) xfree(read->traceT);
143
144 if (read->base != NULL) xfree(read->base);
145 if (read->basePos != NULL) xfree(read->basePos);
146
147 if (read->info != NULL) xfree(read->info);
148
149 if (read->prob_A != NULL) xfree(read->prob_A);
150 if (read->prob_C != NULL) xfree(read->prob_C);
151 if (read->prob_G != NULL) xfree(read->prob_G);
152 if (read->prob_T != NULL) xfree(read->prob_T);
153
154 if (read->trace_name != NULL) xfree(read->trace_name);
155
156 if (read->orig_trace != NULL) {
157 if (read->orig_trace_free)
158 read->orig_trace_free(read->orig_trace);
159 else
160 xfree(read->orig_trace);
161 }
162
163 if (read->ident != NULL)
164 xfree(read->ident);
165
166 if (read->flow_order)
167 xfree(read->flow_order);
168 if (read->flow)
169 xfree(read->flow);
170 if (read->flow_raw)
171 xfree(read->flow_raw);
172
173 if (read->private_data)
174 xfree(read->private_data);
175
176 xfree(read);
177 }
178
179
180
181
182 /*
183 * Duplicates the read structure and optionally gives it a new filename.
184 * The following fields are not duplicated:
185 *
186 * int orig_trace_format;
187 * void (*orig_trace_free)(void *ptr);
188 * void *orig_trace;
189 * char *ident;
190 *
191 * Returns:
192 * "Read *" for success
193 * "NULLRead" for failure
194 */
195 Read* read_dup( Read* src, const char* new_name )
196 {
197 int n;
198 Read* dst;
199 assert(src);
200
201 /* Allocate storage and initialise */
202 dst = read_allocate( src->NPoints, src->NBases );
203 if( dst == NULLRead )
204 return 0;
205 dst->info = 0;
206 dst->trace_name = 0;
207
208
209 /* Copy over possibly new name */
210 if( new_name )
211 n = strlen(new_name);
212 else if( src->trace_name )
213 n = strlen(src->trace_name);
214 else
215 n = 0;
216 if( n > 0 ) {
217 dst->trace_name = (char*) xmalloc(n+1);
218 if( !dst->trace_name )
219 goto error;
220
221 if(new_name)
222 strcpy( dst->trace_name, new_name );
223 else
224 strcpy( dst->trace_name, src->trace_name );
225 }
226
227
228 /* Copy over info */
229 if( src->info ) {
230 dst->info = strdup(src->info);
231 }
232
233
234 /* Copy single fields */
235 dst->format = src->format;
236 dst->maxTraceVal = src->maxTraceVal;
237 dst->leftCutoff = src->leftCutoff;
238 dst->rightCutoff = src->rightCutoff;
239 dst->baseline = src->baseline;
240
241
242 /* Copy NPoints fields if they exist */
243 if( src->traceA )
244 {
245 for( n=0; n<src->NPoints; n++ )
246 {
247 dst->traceA[n] = src->traceA[n];
248 dst->traceC[n] = src->traceC[n];
249 dst->traceG[n] = src->traceG[n];
250 dst->traceT[n] = src->traceT[n];
251 }
252 }
253
254
255 /* Copy NBases fields if they exist */
256 if( src->base && src->base[0] )
257 {
258 for( n=0; n<src->NBases; n++ )
259 {
260 dst->base[n] = src->base[n];
261 dst->basePos[n] = src->basePos[n];
262 if( src->prob_A )
263 {
264 dst->prob_A[n] = src->prob_A[n];
265 dst->prob_C[n] = src->prob_C[n];
266 dst->prob_G[n] = src->prob_G[n];
267 dst->prob_T[n] = src->prob_T[n];
268 }
269 }
270 }
271
272
273 /* Success */
274 return dst;
275
276 error:
277 /* Failure */
278 read_deallocate(dst);
279 return NULLRead;
280 }