Mercurial > repos > dawe > srf2fastq
comparison srf2fastq/io_lib-1.12.2/io_lib/misc_scf.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 Title: misc_scf.c | |
17 | |
18 Purpose: misc handling of Standard Chromatogram Format sequences | |
19 Last update: August 18 1994 | |
20 | |
21 Change log: | |
22 18 Aug 1994 Creation from bits of {read,write}scf.c and new code. | |
23 | |
24 */ | |
25 | |
26 #include <stdio.h> | |
27 #include <string.h> | |
28 | |
29 #include "io_lib/scf.h" | |
30 #include "io_lib/mach-io.h" | |
31 #include "io_lib/xalloc.h" | |
32 | |
33 #include "io_lib/stdio_hack.h" | |
34 | |
35 float scf_version_str2float(char version[]) | |
36 { | |
37 char v[5]; | |
38 strncpy(v,version,4);v[4]='\0'; | |
39 if (strspn(v,"0123456789. ")!=4) return 0.0; | |
40 return (float)atof(v); | |
41 } | |
42 | |
43 char *scf_version_float2str(float f) | |
44 { | |
45 static char v[5]; | |
46 | |
47 sprintf(v, "%1.2f", f); | |
48 return v; | |
49 } | |
50 | |
51 | |
52 /* | |
53 * Allocates memory for the scf elements based upon arguments passed. | |
54 * Returns; | |
55 * Scf * - Success. The scf structure and it's samples, bases, | |
56 * and comments fields have been allocated. | |
57 * NULL - Failure. | |
58 */ | |
59 Scf *scf_allocate(int num_samples, int sample_size, int num_bases, | |
60 int comment_size, int private_size) { | |
61 Scf *scf; | |
62 | |
63 scf = (Scf *)xcalloc(1, sizeof(Scf)); | |
64 if (NULL == scf) | |
65 return NULL; | |
66 | |
67 /* bases - +1 as a safety guard for when num_bases==0 */ | |
68 scf->bases = (Bases *)xcalloc(sizeof(Bases), num_bases+1); | |
69 if (NULL == scf->bases) | |
70 return NULL; | |
71 | |
72 /* samples */ | |
73 scf->header.sample_size = sample_size; | |
74 if (scf->header.sample_size == 1) { | |
75 scf->samples.samples1 = (Samples1 *)xmalloc(num_samples * | |
76 sizeof(Samples1) + 1); | |
77 } else { | |
78 scf->samples.samples2 = (Samples2 *)xmalloc(num_samples * | |
79 sizeof(Samples2) + 1); | |
80 } | |
81 if (NULL == scf->samples.samples1) { | |
82 xfree(scf->bases); | |
83 xfree(scf); | |
84 return NULL; | |
85 } | |
86 | |
87 /* comments */ | |
88 if (comment_size) { | |
89 scf->comments = (Comments *)xmalloc(sizeof(Comments) * | |
90 (comment_size + 1)); | |
91 if (NULL == scf->comments) { | |
92 xfree(scf->bases); | |
93 xfree(scf->samples.samples1); | |
94 xfree(scf); | |
95 return NULL; | |
96 } | |
97 } else | |
98 scf->comments = NULL; | |
99 | |
100 /* private data */ | |
101 if (private_size) { | |
102 scf->private_data = (char *)xmalloc(private_size); | |
103 if (NULL == scf->private_data) { | |
104 xfree(scf->bases); | |
105 xfree(scf->samples.samples1); | |
106 if (scf->comments) xfree(scf->comments); | |
107 xfree(scf); | |
108 return NULL; | |
109 } | |
110 } else | |
111 scf->private_data = NULL; | |
112 | |
113 return scf; | |
114 } | |
115 | |
116 void scf_deallocate(Scf *scf) { | |
117 xfree(scf->bases); | |
118 xfree(scf->samples.samples1); | |
119 if (scf->comments) | |
120 xfree(scf->comments); | |
121 if (scf->private_data) | |
122 xfree(scf->private_data); | |
123 xfree(scf); | |
124 } | |
125 | |
126 | |
127 int is_scf(char *fn) | |
128 /* | |
129 * Check to see if file with name `fn' is in SCF format | |
130 * | |
131 * Returns: | |
132 * 1 - is SCF format | |
133 * 0 - not SCF format | |
134 *-1 - failure | |
135 */ | |
136 { | |
137 FILE *fp; | |
138 uint_4 magic; | |
139 int ok; | |
140 | |
141 if ( (fp=fopen(fn,"rb")) == NULL) { | |
142 ok = -1; | |
143 } else { | |
144 if ( be_read_int_4(fp, &magic) != 1 ) { | |
145 ok = 0; | |
146 } else { | |
147 ok = (magic==SCF_MAGIC); | |
148 } | |
149 fclose(fp); | |
150 } | |
151 | |
152 return ok; | |
153 } | |
154 | |
155 void scf_delta_samples1 ( int1 samples[], int num_samples, int job) { | |
156 | |
157 /* If job == DELTA_IT: | |
158 change a series of sample points to a series of delta delta values: | |
159 ie change them first: delta = current_value - previous_value | |
160 then delta_delta = delta - previous_delta | |
161 | |
162 else | |
163 do the reverse | |
164 */ | |
165 | |
166 int i; | |
167 | |
168 if ( DELTA_IT == job ) { | |
169 #ifdef CLEAR_BUT_SLOW | |
170 int1 p_delta, p_sample; | |
171 | |
172 p_delta = 0; | |
173 for (i=0;i<num_samples;i++) { | |
174 p_sample = samples[i]; | |
175 samples[i] = samples[i] - p_delta; | |
176 p_delta = p_sample; | |
177 } | |
178 p_delta = 0; | |
179 for (i=0;i<num_samples;i++) { | |
180 p_sample = samples[i]; | |
181 samples[i] = samples[i] - p_delta; | |
182 p_delta = p_sample; | |
183 } | |
184 #else | |
185 for (i = num_samples-1 ; i > 1; i--) { | |
186 samples[i] = samples[i] - 2*samples[i-1] + samples[i-2]; | |
187 } | |
188 samples[1] = samples[1] - 2*samples[0]; | |
189 #endif | |
190 | |
191 } else { | |
192 | |
193 #ifdef CLEAR_BUT_SLOW | |
194 int1 p_sample; | |
195 | |
196 p_sample = 0; | |
197 for (i=0;i<num_samples;i++) { | |
198 samples[i] = samples[i] + p_sample; | |
199 p_sample = samples[i]; | |
200 } | |
201 p_sample = 0; | |
202 for (i=0;i<num_samples;i++) { | |
203 samples[i] = samples[i] + p_sample; | |
204 p_sample = samples[i]; | |
205 } | |
206 #else | |
207 int1 p_sample1, p_sample2; | |
208 | |
209 p_sample1 = p_sample2 = 0; | |
210 for (i = 0; i < num_samples; i++) { | |
211 p_sample1 = p_sample1 + samples[i]; | |
212 samples[i] = p_sample1 + p_sample2; | |
213 p_sample2 = samples[i]; | |
214 } | |
215 #endif | |
216 } | |
217 } | |
218 | |
219 void scf_delta_samples2 ( uint_2 samples[], int num_samples, int job) { | |
220 | |
221 /* If job == DELTA_IT: | |
222 change a series of sample points to a series of delta delta values: | |
223 ie change them first: delta = current_value - previous_value | |
224 then delta_delta = delta - previous_delta | |
225 | |
226 else | |
227 do the reverse | |
228 */ | |
229 | |
230 register int i; | |
231 | |
232 if ( DELTA_IT == job ) { | |
233 #ifdef CLEAR_BUT_SLOW | |
234 register uint_2 p_delta, p_sample; | |
235 | |
236 p_delta = 0; | |
237 for (i=0;i<num_samples;i++) { | |
238 p_sample = samples[i]; | |
239 samples[i] = samples[i] - p_delta; | |
240 p_delta = p_sample; | |
241 } | |
242 p_delta = 0; | |
243 for (i=0;i<num_samples;i++) { | |
244 p_sample = samples[i]; | |
245 samples[i] = samples[i] - p_delta; | |
246 p_delta = p_sample; | |
247 } | |
248 #else | |
249 for (i = num_samples-1 ; i > 1; i--) { | |
250 samples[i] = samples[i] - 2*samples[i-1] + samples[i-2]; | |
251 } | |
252 samples[1] = samples[1] - 2*samples[0]; | |
253 #endif | |
254 | |
255 } else { | |
256 | |
257 #ifdef CLEAR_BUT_SLOW | |
258 register uint_2 p_sample; | |
259 | |
260 p_sample = 0; | |
261 for (i=0;i<num_samples;i++) { | |
262 samples[i] = samples[i] + p_sample; | |
263 p_sample = samples[i]; | |
264 } | |
265 p_sample = 0; | |
266 for (i=0;i<num_samples;i++) { | |
267 samples[i] = samples[i] + p_sample; | |
268 p_sample = samples[i]; | |
269 } | |
270 #else | |
271 uint_2 p_sample1, p_sample2; | |
272 | |
273 p_sample1 = p_sample2 = 0; | |
274 for (i = 0; i < num_samples; i++) { | |
275 p_sample1 = p_sample1 + samples[i]; | |
276 samples[i] = p_sample1 + p_sample2; | |
277 p_sample2 = samples[i]; | |
278 } | |
279 #endif | |
280 } | |
281 } | |
282 |