From: martinahansen Date: Mon, 8 Sep 2008 01:50:55 +0000 (+0000) Subject: added sort to bipartite_scan.c X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=b322af368f991c964580ced2846e6aed85f673ff;p=biopieces.git added sort to bipartite_scan.c git-svn-id: http://biopieces.googlecode.com/svn/trunk@248 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_c/Maasha/src/bipartite_scan.c b/code_c/Maasha/src/bipartite_scan.c index c317445..59076fb 100644 --- a/code_c/Maasha/src/bipartite_scan.c +++ b/code_c/Maasha/src/bipartite_scan.c @@ -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 ... " ); diff --git a/code_c/Maasha/src/inc/common.h b/code_c/Maasha/src/inc/common.h index 453e80d..6be9b1d 100644 --- a/code_c/Maasha/src/inc/common.h +++ b/code_c/Maasha/src/inc/common.h @@ -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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ diff --git a/code_c/Maasha/src/lib/common.c b/code_c/Maasha/src/lib/common.c index 48b71e5..b93220d 100644 --- a/code_c/Maasha/src/lib/common.c +++ b/code_c/Maasha/src/lib/common.c @@ -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 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<*/ diff --git a/code_c/Maasha/src/testall.pl b/code_c/Maasha/src/testall.pl index 4c9d2f1..09b1a1b 100755 --- a/code_c/Maasha/src/testall.pl +++ b/code_c/Maasha/src/testall.pl @@ -13,6 +13,8 @@ $test_dir = "test"; test_filesys test_list test_mem + test_seq + test_sort test_strings );