]> git.donarmstrong.com Git - biopieces.git/commitdiff
added sort to bipartite_scan.c
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Mon, 8 Sep 2008 01:50:55 +0000 (01:50 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Mon, 8 Sep 2008 01:50:55 +0000 (01:50 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@248 74ccb610-7750-0410-82ae-013aeee3265d

code_c/Maasha/src/bipartite_scan.c
code_c/Maasha/src/inc/common.h
code_c/Maasha/src/lib/common.c
code_c/Maasha/src/testall.pl

index c3174452c79c891a619e02887dbc5cfd84a55e8d..59076fb7accea85c9a1890681df64898b8c23507 100644 (file)
@@ -19,7 +19,7 @@
 /* Structure that will hold one tetra nucleotide block. */
 struct _bitblock
 {
-    uchar bin;   /* Tetra nucleotide encoded binary. */
+    uchar bin;   /* Tetra nucleotide binary encoded. */
     bool hasN;   /* Flag indicating any N's in the block. */
 };
 
@@ -70,6 +70,7 @@ void      scan_seq( char *seq, size_t seq_len, uint *count_array );
 void      scan_list( list_sl *list, uint *count_array );
 bitblock *bitblock_new();
 uint      blocks2motif( uchar bin1, uchar bin2, ushort dist );
+int       cmp_uint_desc( const void *a, const void *b );
 void      count_array_print( uint *count_array );
 void      motif_print( uint motif, uint count );
 void      bitblock_list_print( list_sl *list );
@@ -82,6 +83,7 @@ static void test_bitblock_new();
 static void test_bitblock_print();
 static void test_bitblock_list_print();
 static void test_scan_seq();
+static void test_array_sort();
 static void test_blocks2motif();
 
 
@@ -90,11 +92,12 @@ static void test_blocks2motif();
 
 int main( int argc, char *argv[] )
 {
+    run_tests();
+
     if ( argc == 1 ) {
         print_usage();
     }
 
-    run_tests();
     run_scan( argc, argv );
 
     return EXIT_SUCCESS;
@@ -146,6 +149,12 @@ void run_scan( int argc, char *argv[] )
         fprintf( stderr, "done.\n" );
     }
 
+    fprintf( stderr, "Sorting motifs: ... " );
+
+    qsort( count_array, COUNT_ARRAY_SIZE, sizeof( uint ), cmp_uint_desc );
+
+    fprintf( stderr, "done.\n" );
+
     fprintf( stderr, "Printing motifs: ... " );
 
     count_array_print( count_array );
@@ -363,6 +372,20 @@ uint blocks2motif( uchar bin1, uchar bin2, ushort dist )
 }
 
 
+int cmp_uint_desc( const void *a, const void *b )
+{
+    /* Martin A. Hansen, September 2008 */
+
+    /* Compare function for qsort of an array of unsigned ints */
+    /* to be sorted in descending order. */
+
+    const uint *uint_a = ( const uint *) a;
+    const uint *uint_b = ( const uint *) b;
+
+    return *uint_b - *uint_a; 
+}
+
+
 void count_array_print( uint *count_array )
 {
     /* Martin A. Hansen, Seqptember 2008. */
@@ -451,6 +474,7 @@ void run_tests()
     test_bitblock_print();
     test_bitblock_list_print();
     test_scan_seq();
+    test_array_sort();
     test_blocks2motif();
 
     fprintf( stderr, "All tests OK\n" );
@@ -554,6 +578,31 @@ void test_scan_seq()
 }
 
 
+void test_array_sort()
+{
+    fprintf( stderr, "   Running test_array_sort ... " );
+
+    uint array[]    = { 4, 234, 23, 43, 12, 23, 1, 34 };
+    int  array_size = sizeof( array ) / sizeof( uint );
+    int  i          = 0;
+
+    for ( i = 0; i < array_size; i ++ ) {
+//        printf( "elem: %i\n", array[ i ] );
+    }
+
+    qsort( array, array_size, sizeof( uint ), cmp_uint_desc );
+
+    for ( i = 0; i < array_size; i ++ ) {
+//        printf( "elem: %i\n", array[ i ] );
+    }
+
+    assert( array[ 0 ] == 234 );     /* 234 first in sorted array */
+    assert( array[ array_size - 1 ] == 1 );     /* 1 last in sorted array */
+
+    fprintf( stderr, "done.\n" );
+}
+
+
 static void test_blocks2motif()
 {
     fprintf( stderr, "   Running test_blocks2motif ... " );
index 453e80d718677f4b524b74ef07f50965fae0011b..6be9b1deed16abff59b45ce5cd2bfcbf798ae66f 100644 (file)
@@ -11,6 +11,7 @@
 
 typedef unsigned char uchar;
 typedef unsigned short ushort;
+typedef long long llong;
 typedef char bool;
 
 #define TRUE 1
@@ -29,13 +30,6 @@ typedef char bool;
 #define die assert( DEBUG_EXIT )
 
 
-/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
-
-
-/* Binary search an array of integers for an integer value. */
-bool binary_search_array( int *array, int array_size, int val );
-
-
 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
 
 
index 48b71e570b6063d8a0d4bb4297ae03f78af56e99..b93220dca6b257ea11d750f18866b61336c0b878 100644 (file)
@@ -3,39 +3,6 @@
 #include "mem.h"
 
 
-/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> ARRAYS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
-
-
-bool binary_search_array( int *array, int array_size, int val )
-{
-    /* Martin A. Hansen, June 2008 */
-
-    /* Binary search an array of integers for an integer value. */
-
-    int high;
-    int low;
-    int try;
-
-    high = array_size; 
-    low  = 0;                                                                                                                   
-
-    while ( low < high )                                                                                                        
-    {                                                                                                                           
-        try = ( ( high + low ) / 2 );                                                                                           
-
-        if ( val < array[ try ] ) {
-            high = try;
-        } else if ( val > array[ try ] ) {
-            low = try + 1;
-        } else {
-            return TRUE;
-        }   
-    }
-
-    return FALSE;
-}
-
-
 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MISC <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/
 
 
index 4c9d2f1a1f1b55ce36ff5cd220a2262603da200c..09b1a1b74325eebba457b169e3f0801d46601290 100755 (executable)
@@ -13,6 +13,8 @@ $test_dir = "test";
     test_filesys
     test_list
     test_mem
+    test_seq
+    test_sort
     test_strings
 );