]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_c/Maasha/src/bed2fixedstep.c
added bed2fixedstep.c
[biopieces.git] / code_c / Maasha / src / bed2fixedstep.c
diff --git a/code_c/Maasha/src/bed2fixedstep.c b/code_c/Maasha/src/bed2fixedstep.c
new file mode 100644 (file)
index 0000000..a060437
--- /dev/null
@@ -0,0 +1,100 @@
+#include "common.h"
+#include "mem.h"
+#include "filesys.h"
+#include "list.h"
+#include "ucsc.h"
+#include "hash.h"
+#include "barray.h"
+
+#define BED_COLS    5
+#define HASH_SIZE   8
+#define BARRAY_SIZE ( 1 << 16 )
+
+
+long get_score( char *str )
+{
+    /* Martin A. Hansen, December 2008. */
+
+    /* Extract the last decimal number after _ 
+     * in a string and return that. If no number
+     * was found return 1. */
+
+    char *c;
+    long  score = 1;
+
+    if ( ( c = strrchr( str, '_' ) ) != NULL ) {
+        score = strtol( &c[ 1 ], NULL , 10 );
+    }
+
+    return score;
+}
+
+
+int main( int argc, char *argv[] )
+{
+    char      *file     = NULL;
+    FILE      *fp       = NULL;
+    bed_entry *entry    = NULL;
+    hash      *chr_hash = NULL;
+    hash_elem *bucket   = NULL;
+    barray    *ba       = NULL;
+    ushort     score    = 0;
+    size_t     i        = 0;
+    size_t     j        = 0;
+    char      *chr      = NULL;
+    size_t     beg      = 0;
+    size_t     end      = 0;
+    size_t     pos      = 0;
+
+    entry    = bed_entry_new( BED_COLS );
+    chr_hash = hash_new( HASH_SIZE );
+
+    file = argv[ argc - 1 ];
+    fp   = read_open( file );
+
+    while ( ( bed_entry_get( fp, &entry ) ) )
+    {
+//        bed_entry_put( entry, entry->cols );
+
+        ba = ( barray * ) hash_get( chr_hash, entry->chr );
+
+        if ( ba == NULL )
+        {
+            ba = barray_new( BARRAY_SIZE );
+
+            hash_add( chr_hash, entry->chr, ba );
+        }
+
+        score = ( ushort ) get_score( entry->q_id );
+
+        barray_interval_inc( ba, entry->chr_beg, entry->chr_end - 1, score );
+    }
+
+    close_stream( fp );
+
+//    barray_print( ba );
+
+    for ( i = 0; i < chr_hash->table_size; i++ )
+    {
+        for ( bucket = chr_hash->table[ i ]; bucket != NULL; bucket = bucket->next )
+        {
+            chr = bucket->key;
+            ba  = ( barray * ) bucket->val;
+        
+            pos = 0;
+
+            while ( barray_interval_scan( ba, &pos, &beg, &end ) )
+            {
+//                printf( "chr: %s   pos: %zu   beg: %zu   end: %zu\n", chr, pos, beg, end );
+
+                printf( "fixedStep chrom=%s start=%zu step=1\n", chr, beg );
+
+                for ( j = beg; j <= end; j++ ) {
+                    printf( "%hd\n", ba->array[ j ] );
+                }
+            }
+        }
+    }
+
+    return EXIT_SUCCESS;
+}