]> git.donarmstrong.com Git - biopieces.git/blob - code_c/Maasha/src/lib/ucsc.c
bdb722a85b3ce98d713adb85d8ad97ca0e1ec9b2
[biopieces.git] / code_c / Maasha / src / lib / ucsc.c
1 /* Martin Asser Hansen (mail@maasha.dk) Copyright (C) 2008 - All right reserved */
2
3
4 #include "common.h"
5 #include "mem.h"
6 #include "filesys.h"
7 #include "list.h"
8 #include "strings.h"
9 #include "ucsc.h"
10
11
12 bed_entry *bed_entry_new( const int cols )
13 {
14     bed_entry *entry = mem_get( sizeof( bed_entry ) );
15
16     entry->cols    = cols;
17     entry->chr     = mem_get( BED_CHR_MAX );
18     entry->chr_beg = 0;
19     entry->chr_end = 0;
20
21     if ( cols == 3 ) {
22         return entry;
23     }
24
25     entry->q_id = mem_get( BED_QID_MAX );
26
27     if ( cols == 4 ) {
28         return entry;
29     }
30
31     entry->score = 0;
32
33     if ( cols == 5 ) {
34         return entry;
35     }
36
37     entry->strand = 0; 
38
39     if ( cols == 6 ) {
40         return entry;
41     }
42
43     entry->thick_beg  = 0;
44     entry->thick_end  = 0;
45     entry->itemrgb    = mem_get( BED_ITEMRGB_MAX );
46     entry->blockcount = 0;;
47     entry->blocksizes = mem_get( BED_BLOCKSIZES_MAX );
48     entry->q_begs     = mem_get( BED_QBEGS_MAX );
49
50     return entry;
51 }
52
53
54 bed_entry *bed_entry_get( FILE *fp, int cols )
55 {
56     bed_entry *entry = bed_entry_new( cols );
57     char       buffer[ BED_BUFFER ];
58
59     assert( cols == 0 || cols == 3 || cols == 4 || cols == 5 || cols == 6 || cols == 12 );
60
61     if ( fgets( buffer, sizeof( buffer ), fp ) != NULL )
62     {
63         if ( ! cols )
64         {
65             cols = 1 + strchr_total( buffer, '\t' );
66             entry->cols = cols;
67         }
68
69         if ( cols == 3 )
70         {
71             sscanf(
72                     buffer,
73                     "%s\t%u\t%u",
74                     entry->chr, 
75                     &entry->chr_beg, 
76                     &entry->chr_end
77             );
78
79             return entry;
80         }
81
82         if ( cols == 4 )
83         {
84             sscanf(
85                     buffer,
86                     "%s\t%u\t%u\t%s",
87                     entry->chr, 
88                     &entry->chr_beg, 
89                     &entry->chr_end,
90                     entry->q_id
91             );
92
93             return entry;
94         }
95
96         if ( cols == 5 )
97         {
98             sscanf(
99                     buffer,
100                     "%s\t%u\t%u\t%s\t%i",
101                     entry->chr, 
102                     &entry->chr_beg, 
103                     &entry->chr_end,
104                     entry->q_id, 
105                     &entry->score
106             );
107
108             return entry;
109         }
110
111         if ( cols == 6 )
112         {
113             sscanf(
114                     buffer,
115                     "%s\t%u\t%u\t%s\t%i\t%c",
116                     entry->chr, 
117                     &entry->chr_beg, 
118                     &entry->chr_end,
119                     entry->q_id, 
120                     &entry->score,
121                     &entry->strand
122             );
123
124             return entry;
125         }
126
127         if ( cols == 12 )
128         {
129             sscanf(
130                     buffer,
131                     "%s\t%u\t%u\t%s\t%i\t%c\t%u\t%u\t%s\t%u\t%s\t%s",
132                     entry->chr, 
133                     &entry->chr_beg, 
134                     &entry->chr_end,
135                     entry->q_id, 
136                     &entry->score,
137                     &entry->strand,
138                     &entry->thick_beg,
139                     &entry->thick_end,
140                     entry->itemrgb,
141                     &entry->blockcount,
142                     entry->blocksizes,
143                     entry->q_begs
144             );
145
146             return entry;
147         }
148     }
149
150     return NULL;
151 }
152
153
154 list_sl *bed_entries_get( char *path, const int cols )
155 {
156     list_sl   *list     = list_sl_new();
157     node_sl   *node     = node_sl_new();
158     node_sl   *old_node = NULL;
159     bed_entry *entry    = NULL;
160     FILE      *fp       = NULL;
161     
162     fp = read_open( path );
163
164     if ( ( entry = bed_entry_get( fp, cols ) ) != NULL )
165     {
166         node->val = entry;
167     
168         list_sl_add_beg( &list, &node );
169
170         old_node = node;
171     }
172
173     while ( ( entry = bed_entry_get( fp, cols ) ) != NULL )
174     {
175         node = node_sl_new();
176
177         node->val = entry;
178
179         list_sl_add_after( &old_node, &node );
180
181         old_node = node;
182     }
183
184     close_stream( fp );
185
186     return list;
187 }
188
189  
190 void bed_entry_put( bed_entry *entry, int cols )
191 {
192     if ( ! cols ) {
193         cols = entry->cols;
194     } 
195
196     if ( cols == 3 )
197     {
198         printf(
199             "%s\t%u\t%u\n",
200             entry->chr,
201             entry->chr_beg,
202             entry->chr_end
203         );
204     }
205     else if ( cols == 4 )
206     {
207         printf(
208             "%s\t%u\t%u\t%s\n",
209             entry->chr,
210             entry->chr_beg,
211             entry->chr_end,
212             entry->q_id 
213         );
214     }
215     else if ( cols == 5 )
216     {
217         printf(
218             "%s\t%u\t%u\t%s\t%i\n",
219             entry->chr,
220             entry->chr_beg,
221             entry->chr_end,
222             entry->q_id,
223             entry->score
224         );
225     }
226     else if ( cols == 6 )
227     {
228         printf(
229             "%s\t%u\t%u\t%s\t%i\t%c\n",
230             entry->chr,
231             entry->chr_beg,
232             entry->chr_end,
233             entry->q_id,
234             entry->score,
235             entry->strand
236         );
237     }
238     else if ( cols == 12 )
239     {
240         printf(
241             "%s\t%u\t%u\t%s\t%i\t%c\t%u\t%u\t%s\t%u\t%s\t%s\n",
242             entry->chr,
243             entry->chr_beg,
244             entry->chr_end,
245             entry->q_id,
246             entry->score,
247             entry->strand,
248             entry->thick_beg,
249             entry->thick_end,
250             entry->itemrgb,
251             entry->blockcount,
252             entry->blocksizes,
253             entry->q_begs
254         );
255     }
256     else
257     {
258         fprintf( stderr, "ERROR: Wrong number of columns in bed_entry_put: %d\n", cols );
259
260         abort();
261     }
262 }
263
264
265 void bed_entries_put( list_sl *entries, int cols )
266 {
267     node_sl *node = NULL;
268
269     for ( node = entries->first; node != NULL; node = node->next ) {
270         bed_entry_put( ( bed_entry * ) node->val, cols );
271     }
272 }
273
274
275 int cmp_bed_sort_beg( const void *a, const void *b )
276 {
277     node_sl *a_node = *( ( node_sl ** ) a );
278     node_sl *b_node = *( ( node_sl ** ) b );
279
280     bed_entry *a_entry = ( bed_entry * ) a_node->val;
281     bed_entry *b_entry = ( bed_entry * ) b_node->val;
282
283     if ( a_entry->chr_beg < b_entry->chr_beg ) {
284         return -1;
285     } else if ( a_entry->chr_beg > b_entry->chr_beg ) {
286         return 1;
287     } else {
288         return 0;
289     }
290 }
291
292
293 int cmp_bed_sort_chr_beg( const void *a, const void *b )
294 {
295     node_sl *a_node = *( ( node_sl ** ) a );
296     node_sl *b_node = *( ( node_sl ** ) b );
297
298     bed_entry *a_entry = ( bed_entry * ) a_node->val;
299     bed_entry *b_entry = ( bed_entry * ) b_node->val;
300
301     int diff = 0;
302
303     diff = strcmp( a_entry->chr, b_entry->chr );
304
305     if ( diff < 0 ) {
306         return -1;
307     } else if ( diff > 0 ) {
308         return 1;
309     } else if ( a_entry->chr_beg < b_entry->chr_beg ) {
310         return -1;
311     } else if ( a_entry->chr_beg > b_entry->chr_beg ) {
312         return 1;
313     } else {
314         return 0;
315     }
316 }
317
318
319 int cmp_bed_sort_chr_strand_beg( const void *a, const void *b )
320 {
321     node_sl *a_node = *( ( node_sl ** ) a );
322     node_sl *b_node = *( ( node_sl ** ) b );
323
324     bed_entry *a_entry = ( bed_entry * ) a_node->val;
325     bed_entry *b_entry = ( bed_entry * ) b_node->val;
326
327     int diff = 0;
328
329     diff = strcmp( a_entry->chr, b_entry->chr );
330
331     if ( diff < 0 ) {
332         return -1;
333     } else if ( diff > 0 ) {
334         return 1;
335     } else if ( a_entry->strand < b_entry->strand ) {
336         return -1;
337     } else if ( a_entry->strand > b_entry->strand ) {
338         return 1;
339     } else if ( a_entry->chr_beg < b_entry->chr_beg ) {
340         return -1;
341     } else if ( a_entry->chr_beg > b_entry->chr_beg ) {
342         return 1;
343     } else {
344         return 0;
345     }
346 }
347