8 #define BLOCK_SIZE_NT 4 /* one block holds 4 nucleotides. */
9 #define BITS_IN_NT 2 /* two bits holds 1 nucleotide. */
10 #define BITS_IN_BYTE 8 /* number of bits in one byte. */
11 #define BLOCK_SPACE_MAX 64 /* maximum space between two blocks. */
12 #define BLOCK_MASK ( ( BLOCK_SPACE_MAX << 1 ) - 1 ) /* mask for printing block space. */
13 #define COUNT_ARRAY_NMEMB ( 1 << 30 ) /* number of objects in the unsigned int count array. */
14 #define CUTOFF 10000 /* minimum number of motifs in output. */
16 #define add_A( c ) /* add 00 to the rightmost two bits of bin (i.e. do nothing). */
17 #define add_T( c ) ( c |= 3 ) /* add 11 on the rightmost two bits of c. */
18 #define add_C( c ) ( c |= 1 ) /* add 01 on the rightmost two bits of c. */
19 #define add_G( c ) ( c |= 2 ) /* add 10 on the rightmost two bits of c. */
21 /* Structure that will hold one tetra nucleotide block. */
24 uchar bin; /* Tetra nucleotide binary encoded. */
25 bool hasN; /* Flag indicating any N's in the block. */
28 typedef struct _bitblock bitblock;
30 /* Byte array for fast convertion of binary blocks back to DNA. */
31 char *bin2dna[256] = {
32 "AAAA", "AAAC", "AAAG", "AAAT", "AACA", "AACC", "AACG", "AACT",
33 "AAGA", "AAGC", "AAGG", "AAGT", "AATA", "AATC", "AATG", "AATT",
34 "ACAA", "ACAC", "ACAG", "ACAT", "ACCA", "ACCC", "ACCG", "ACCT",
35 "ACGA", "ACGC", "ACGG", "ACGT", "ACTA", "ACTC", "ACTG", "ACTT",
36 "AGAA", "AGAC", "AGAG", "AGAT", "AGCA", "AGCC", "AGCG", "AGCT",
37 "AGGA", "AGGC", "AGGG", "AGGT", "AGTA", "AGTC", "AGTG", "AGTT",
38 "ATAA", "ATAC", "ATAG", "ATAT", "ATCA", "ATCC", "ATCG", "ATCT",
39 "ATGA", "ATGC", "ATGG", "ATGT", "ATTA", "ATTC", "ATTG", "ATTT",
40 "CAAA", "CAAC", "CAAG", "CAAT", "CACA", "CACC", "CACG", "CACT",
41 "CAGA", "CAGC", "CAGG", "CAGT", "CATA", "CATC", "CATG", "CATT",
42 "CCAA", "CCAC", "CCAG", "CCAT", "CCCA", "CCCC", "CCCG", "CCCT",
43 "CCGA", "CCGC", "CCGG", "CCGT", "CCTA", "CCTC", "CCTG", "CCTT",
44 "CGAA", "CGAC", "CGAG", "CGAT", "CGCA", "CGCC", "CGCG", "CGCT",
45 "CGGA", "CGGC", "CGGG", "CGGT", "CGTA", "CGTC", "CGTG", "CGTT",
46 "CTAA", "CTAC", "CTAG", "CTAT", "CTCA", "CTCC", "CTCG", "CTCT",
47 "CTGA", "CTGC", "CTGG", "CTGT", "CTTA", "CTTC", "CTTG", "CTTT",
48 "GAAA", "GAAC", "GAAG", "GAAT", "GACA", "GACC", "GACG", "GACT",
49 "GAGA", "GAGC", "GAGG", "GAGT", "GATA", "GATC", "GATG", "GATT",
50 "GCAA", "GCAC", "GCAG", "GCAT", "GCCA", "GCCC", "GCCG", "GCCT",
51 "GCGA", "GCGC", "GCGG", "GCGT", "GCTA", "GCTC", "GCTG", "GCTT",
52 "GGAA", "GGAC", "GGAG", "GGAT", "GGCA", "GGCC", "GGCG", "GGCT",
53 "GGGA", "GGGC", "GGGG", "GGGT", "GGTA", "GGTC", "GGTG", "GGTT",
54 "GTAA", "GTAC", "GTAG", "GTAT", "GTCA", "GTCC", "GTCG", "GTCT",
55 "GTGA", "GTGC", "GTGG", "GTGT", "GTTA", "GTTC", "GTTG", "GTTT",
56 "TAAA", "TAAC", "TAAG", "TAAT", "TACA", "TACC", "TACG", "TACT",
57 "TAGA", "TAGC", "TAGG", "TAGT", "TATA", "TATC", "TATG", "TATT",
58 "TCAA", "TCAC", "TCAG", "TCAT", "TCCA", "TCCC", "TCCG", "TCCT",
59 "TCGA", "TCGC", "TCGG", "TCGT", "TCTA", "TCTC", "TCTG", "TCTT",
60 "TGAA", "TGAC", "TGAG", "TGAT", "TGCA", "TGCC", "TGCG", "TGCT",
61 "TGGA", "TGGC", "TGGG", "TGGT", "TGTA", "TGTC", "TGTG", "TGTT",
62 "TTAA", "TTAC", "TTAG", "TTAT", "TTCA", "TTCC", "TTCG", "TTCT",
63 "TTGA", "TTGC", "TTGG", "TTGT", "TTTA", "TTTC", "TTTG", "TTTT"
66 /* Function declarations. */
67 void run_scan( int argc, char *argv[] );
69 void scan_file( char *file, seq_entry *entry, uint *count_array );
70 uint *count_array_new( size_t nmemb );
71 void scan_seq( char *seq, size_t seq_len, uint *count_array );
72 void scan_list( list_sl *list, uint *count_array );
73 bitblock *bitblock_new();
74 uint blocks2motif( uchar bin1, uchar bin2, ushort dist );
75 void count_array_print( uint *count_array, size_t nmemb, size_t cutoff );
76 void motif_print( uint motif, uint count );
77 void bitblock_list_print( list_sl *list );
78 void bitblock_print( bitblock *out );
80 /* Unit test declarations. */
81 static void run_tests();
82 static void test_count_array_new();
83 static void test_bitblock_new();
84 static void test_bitblock_print();
85 static void test_bitblock_list_print();
86 static void test_scan_seq();
87 static void test_blocks2motif();
90 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MAIN <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
93 int main( int argc, char *argv[] )
101 run_scan( argc, argv );
107 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FUNCTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
112 /* Martin A. Hansen, September 2008 */
114 /* Print usage and exit if no files in argument. */
117 "Usage: bipartite_scam <FASTA file(s)> > result.csv\n"
120 exit( EXIT_SUCCESS );
124 void run_scan( int argc, char *argv[] )
126 /* Martin A. Hansen, September 2008 */
128 /* For each file in argv scan the file for */
129 /* bipartite motifs and output the motifs */
130 /* and their count. */
134 seq_entry *entry = NULL;
135 uint *count_array = NULL;
136 // size_t new_nmemb = 0;
138 count_array = count_array_new( COUNT_ARRAY_NMEMB );
140 entry = seq_new( MAX_SEQ_NAME, MAX_SEQ );
142 for ( i = 1; i < argc; i++ )
146 fprintf( stderr, "Scanning file: %s\n", file );
147 scan_file( file, entry, count_array );
148 fprintf( stderr, "done.\n" );
151 fprintf( stderr, "Printing motifs: ... " );
152 count_array_print( count_array, COUNT_ARRAY_NMEMB, CUTOFF );
153 fprintf( stderr, "done.\n" );
155 seq_destroy( entry );
157 mem_free( &count_array );
161 uint *count_array_new( size_t nmemb )
163 /* Martin A. Hansen, September 2008 */
165 /* Initialize a new zeroed uint array of nmemb objects. */
171 array = mem_get_zero( nmemb * sizeof( uint ) );
177 void scan_file( char *file, seq_entry *entry, uint *count_array )
179 /* Martin A. Hansen, September 2008 */
181 /* Scan all FASTA entries of a file in both */
182 /* sense and anti-sense directions. */
184 FILE *fp = read_open( file );
186 while ( fasta_get_entry( fp, &entry ) == TRUE )
188 fprintf( stderr, " Scanning: %s (%zu nt) ... ", entry->seq_name, entry->seq_len );
190 scan_seq( entry->seq, entry->seq_len, count_array );
192 fprintf( stderr, "done.\n" );
199 void scan_seq( char *seq, size_t seq_len, uint *count_array )
201 /* Martin A. Hansen, September 2008 */
203 /* Run a sliding window over a given sequence. The window */
204 /* consists of a list where new blocks of 4 nucleotides */
205 /* are pushed onto one end while at the same time old */
206 /* blocks are popped from the other end. The number of */
207 /* in the list is determined by the maximum seperator. */
208 /* Everytime we have a full window, the window is scanned */
211 bitblock *block = NULL;
216 bool first_node = TRUE;
217 node_sl *new_node = NULL;
218 node_sl *old_node = NULL;
219 list_sl *list = list_sl_new();
221 for ( i = 0; seq[ i ]; i++ )
227 case 'A': case 'a': add_A( bin ); break;
228 case 'T': case 't': add_T( bin ); break;
229 case 'C': case 'c': add_C( bin ); break;
230 case 'G': case 'g': add_G( bin ); break;
231 default: n_count = BLOCK_SIZE_NT; break;
234 if ( i > BLOCK_SIZE_NT - 2 )
238 block = bitblock_new();
247 new_node = node_sl_new();
248 new_node->val = block;
251 list_sl_add_beg( &list, &new_node );
253 list_sl_add_after( &old_node, &new_node );
260 if ( b_count > BLOCK_SPACE_MAX + BLOCK_SIZE_NT )
262 // bitblock_list_print( list ); /* DEBUG */
264 scan_list( list, count_array );
266 list_sl_remove_beg( &list );
271 /* if the list is shorter than BLOCK_SPACE_MAX + BLOCK_SIZE_NT */
272 if ( b_count <= BLOCK_SPACE_MAX + BLOCK_SIZE_NT )
274 // bitblock_list_print( list ); /* DEBUG */
276 scan_list( list, count_array );
279 list_sl_destroy( &list );
283 void scan_list( list_sl *list, uint *count_array )
285 /* Martin A. Hansen, September 2008 */
287 /* Scan a list of blocks for biparite motifs by creating */
288 /* a binary motif consisting of two blocks of 4 nucleotides */
289 /* along with the distance separating them. Motifs containing */
290 /* N's are skipped. */
292 node_sl *first_node = NULL;
293 node_sl *next_node = NULL;
294 bitblock *block1 = NULL;
295 bitblock *block2 = NULL;
300 // bitblock_list_print( list );
302 first_node = list->first;
304 block1 = ( bitblock * ) first_node->val;
306 if ( ! block1->hasN )
308 next_node = first_node->next;
310 for ( i = 0; i < BLOCK_SIZE_NT - 1; i++ ) {
311 next_node = next_node->next;
314 for ( next_node = next_node; next_node != NULL; next_node = next_node->next )
316 block2 = ( bitblock * ) next_node->val;
318 // printf( "block1: %s block2: %s dist: %d\n", bin2dna[ block1->bin ], bin2dna[ block2->bin ], dist ); /* DEBUG */
320 if ( ! block2->hasN )
322 motif_bin = blocks2motif( block1->bin, block2->bin, dist );
324 // motif_print( motif_bin, 0 ); /* DEBUG */
325 // bitblock_list_print( list ); /* DEBUG */
327 count_array[ motif_bin ]++;
336 bitblock *bitblock_new()
338 /* Martin A. Hansen, September 2008 */
340 /* Initializes a new empty bitblock. */
342 bitblock *new_block = NULL;
344 new_block = mem_get( sizeof( bitblock ) );
347 new_block->hasN = FALSE;
353 uint blocks2motif( uchar bin1, uchar bin2, ushort dist )
355 /* Martin A. Hansen, September 2008 */
357 /* Given two binary encoded tetra nuceotide blocks, */
358 /* and the distance separating this, create a binary */
359 /* bipartite motif. */
365 motif <<= sizeof( uchar ) * BITS_IN_BYTE;
369 motif <<= sizeof( uchar ) * BITS_IN_BYTE;
373 // motif_print( motif, 0 ); /* DEBUG */
379 void count_array_print( uint *count_array, size_t nmemb, size_t cutoff )
381 /* Martin A. Hansen, Seqptember 2008. */
383 /* Print all bipartite motifs in count_array as */
384 /* tabular output. */
390 for ( i = 0; i < nmemb; i++ )
393 count = count_array[ i ];
395 if ( count >= cutoff ) {
396 motif_print( motif, count );
402 void motif_print( uint motif, uint count )
404 /* Martin A. Hansen, September 2008 */
406 /* Converts a binary encoded bipartite motif */
407 /* into DNA and output the motif, distance and */
408 /* count seperated by tabs: */
409 /* BLOCK1 \t BLOCK2 \t DIST \t COUNT */
415 // printf( "%d\t", motif ); /* DEBUG */
417 dist = ( ushort ) motif & BLOCK_MASK;
419 motif >>= sizeof( uchar ) * BITS_IN_BYTE;
421 bin2 = ( uchar ) motif;
423 motif >>= sizeof( uchar ) * BITS_IN_BYTE;
425 bin1 = ( uchar ) motif;
427 printf( "%s\t%s\t%d\t%d\n", bin2dna[ bin1 ], bin2dna[ bin2 ], dist, count );
431 void bitblock_list_print( list_sl *list )
433 /* Martin A. Hansen, September 2008 */
435 /* Debug function to print all blocks in a list. */
437 node_sl *node = NULL;
439 printf( "\nbitblock_list_print:\n" );
441 for ( node = list->first; node != NULL; node = node->next ) {
442 bitblock_print( ( bitblock * ) node->val );
447 void bitblock_print( bitblock *out )
449 /* Martin A. Hansen, September 2008 */
451 /* Debug function to print a given block. */
453 printf( "bin: %d dna: %s hasN: %d\n", out->bin, bin2dna[ ( int ) out->bin ], out->hasN );
457 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UNIT TESTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
462 fprintf( stderr, "Running tests\n" );
464 test_count_array_new();
466 test_bitblock_print();
467 test_bitblock_list_print();
471 fprintf( stderr, "All tests OK\n" );
475 void test_count_array_new()
477 fprintf( stderr, " Running test_count_array_new ... " );
483 array = count_array_new( nmemb );
485 for ( i = 0; i < nmemb; i++ ) {
486 assert( array[ i ] == 0 );
491 fprintf( stderr, "done.\n" );
495 void test_bitblock_new()
497 fprintf( stderr, " Running test_bitblock_new ... " );
499 bitblock *new_block = bitblock_new();
501 assert( new_block->bin == 0 );
502 assert( new_block->hasN == FALSE );
504 fprintf( stderr, "done.\n" );
508 void test_bitblock_print()
510 fprintf( stderr, " Running test_bitblock_print ... " );
512 bitblock *new_block = bitblock_new();
515 new_block->hasN = TRUE;
517 // bitblock_print( new_block );
519 fprintf( stderr, "done.\n");
523 void test_bitblock_list_print()
525 fprintf( stderr, " Running test_bitblock_list_print ... " );
527 list_sl *list = list_sl_new();
528 node_sl *node1 = node_sl_new();
529 node_sl *node2 = node_sl_new();
530 node_sl *node3 = node_sl_new();
531 bitblock *block1 = bitblock_new();
532 bitblock *block2 = bitblock_new();
533 bitblock *block3 = bitblock_new();
546 list_sl_add_beg( &list, &node1 );
547 list_sl_add_beg( &list, &node2 );
548 list_sl_add_beg( &list, &node3 );
550 // bitblock_list_print( list );
552 fprintf( stderr, "done.\n" );
558 fprintf( stderr, " Running test_scan_seq ... " );
560 //char *seq = "AAAANTCGGCTNGGGG";
561 //char *seq = "AAAATCGGCTGGGG";
562 char *seq = "AAAAAAAAAAAAAAG";
563 size_t seq_len = strlen( seq );
564 size_t nmemb = 1 << 5;
566 uint *count_array = count_array_new( nmemb );
568 scan_seq( seq, seq_len, count_array );
570 // count_array_print( count_array, nmemb, 1 ); /* DEBUG */
572 fprintf( stderr, "done.\n" );
576 static void test_blocks2motif()
578 fprintf( stderr, " Running test_blocks2motif ... " );
585 motif = blocks2motif( bin1, bin2, dist );
587 // printf( "motif: %d\n", motif );
589 fprintf( stderr, "done.\n");
593 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */