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 COUNT_ARRAY_SIZE ( sizeof( uint ) * ( 1 << 30 ) ) /* size of the unsigned int count array. */
14 #define add_A( c ) /* add 00 to the rightmost two bits of bin (i.e. do nothing). */
15 #define add_T( c ) ( c |= 3 ) /* add 11 on the rightmost two bits of c. */
16 #define add_C( c ) ( c |= 1 ) /* add 01 on the rightmost two bits of c. */
17 #define add_G( c ) ( c |= 2 ) /* add 10 on the rightmost two bits of c. */
19 /* Structure that will hold one tetra nucleotide block. */
22 uchar bin; /* Tetra nucleotide encoded binary. */
23 bool hasN; /* Flag indicating any N's in the block. */
26 typedef struct _bitblock bitblock;
28 /* Byte array for fast convertion of binary blocks back to DNA. */
29 char *bin2dna[256] = {
30 "AAAA", "AAAC", "AAAG", "AAAT", "AACA", "AACC", "AACG", "AACT",
31 "AAGA", "AAGC", "AAGG", "AAGT", "AATA", "AATC", "AATG", "AATT",
32 "ACAA", "ACAC", "ACAG", "ACAT", "ACCA", "ACCC", "ACCG", "ACCT",
33 "ACGA", "ACGC", "ACGG", "ACGT", "ACTA", "ACTC", "ACTG", "ACTT",
34 "AGAA", "AGAC", "AGAG", "AGAT", "AGCA", "AGCC", "AGCG", "AGCT",
35 "AGGA", "AGGC", "AGGG", "AGGT", "AGTA", "AGTC", "AGTG", "AGTT",
36 "ATAA", "ATAC", "ATAG", "ATAT", "ATCA", "ATCC", "ATCG", "ATCT",
37 "ATGA", "ATGC", "ATGG", "ATGT", "ATTA", "ATTC", "ATTG", "ATTT",
38 "CAAA", "CAAC", "CAAG", "CAAT", "CACA", "CACC", "CACG", "CACT",
39 "CAGA", "CAGC", "CAGG", "CAGT", "CATA", "CATC", "CATG", "CATT",
40 "CCAA", "CCAC", "CCAG", "CCAT", "CCCA", "CCCC", "CCCG", "CCCT",
41 "CCGA", "CCGC", "CCGG", "CCGT", "CCTA", "CCTC", "CCTG", "CCTT",
42 "CGAA", "CGAC", "CGAG", "CGAT", "CGCA", "CGCC", "CGCG", "CGCT",
43 "CGGA", "CGGC", "CGGG", "CGGT", "CGTA", "CGTC", "CGTG", "CGTT",
44 "CTAA", "CTAC", "CTAG", "CTAT", "CTCA", "CTCC", "CTCG", "CTCT",
45 "CTGA", "CTGC", "CTGG", "CTGT", "CTTA", "CTTC", "CTTG", "CTTT",
46 "GAAA", "GAAC", "GAAG", "GAAT", "GACA", "GACC", "GACG", "GACT",
47 "GAGA", "GAGC", "GAGG", "GAGT", "GATA", "GATC", "GATG", "GATT",
48 "GCAA", "GCAC", "GCAG", "GCAT", "GCCA", "GCCC", "GCCG", "GCCT",
49 "GCGA", "GCGC", "GCGG", "GCGT", "GCTA", "GCTC", "GCTG", "GCTT",
50 "GGAA", "GGAC", "GGAG", "GGAT", "GGCA", "GGCC", "GGCG", "GGCT",
51 "GGGA", "GGGC", "GGGG", "GGGT", "GGTA", "GGTC", "GGTG", "GGTT",
52 "GTAA", "GTAC", "GTAG", "GTAT", "GTCA", "GTCC", "GTCG", "GTCT",
53 "GTGA", "GTGC", "GTGG", "GTGT", "GTTA", "GTTC", "GTTG", "GTTT",
54 "TAAA", "TAAC", "TAAG", "TAAT", "TACA", "TACC", "TACG", "TACT",
55 "TAGA", "TAGC", "TAGG", "TAGT", "TATA", "TATC", "TATG", "TATT",
56 "TCAA", "TCAC", "TCAG", "TCAT", "TCCA", "TCCC", "TCCG", "TCCT",
57 "TCGA", "TCGC", "TCGG", "TCGT", "TCTA", "TCTC", "TCTG", "TCTT",
58 "TGAA", "TGAC", "TGAG", "TGAT", "TGCA", "TGCC", "TGCG", "TGCT",
59 "TGGA", "TGGC", "TGGG", "TGGT", "TGTA", "TGTC", "TGTG", "TGTT",
60 "TTAA", "TTAC", "TTAG", "TTAT", "TTCA", "TTCC", "TTCG", "TTCT",
61 "TTGA", "TTGC", "TTGG", "TTGT", "TTTA", "TTTC", "TTTG", "TTTT"
64 /* Function declarations. */
65 void run_scan( int argc, char *argv[] );
67 void scan_file( char *file, seq_entry *entry, uint *count_array );
68 uint *count_array_new( size_t size );
69 void scan_seq( char *seq, size_t seq_len, uint *count_array );
70 void scan_list( list_sl *list, uint *count_array );
71 bitblock *bitblock_new();
72 uint blocks2motif( uchar bin1, uchar bin2, ushort dist );
73 void count_array_print( uint *count_array );
74 void motif_print( uint motif, uint count );
75 void bitblock_list_print( list_sl *list );
76 void bitblock_print( bitblock *out );
78 /* Unit test declarations. */
79 static void run_tests();
80 static void test_count_array_new();
81 static void test_bitblock_new();
82 static void test_bitblock_print();
83 static void test_bitblock_list_print();
84 static void test_scan_seq();
85 static void test_blocks2motif();
88 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> MAIN <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
91 int main( int argc, char *argv[] )
98 run_scan( argc, argv );
104 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> FUNCTIONS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
109 /* Martin A. Hansen, September 2008 */
111 /* Print usage and exit if no files in argument. */
114 "Usage: bipartite_scam <FASTA file(s)> > result.csv\n"
117 exit( EXIT_SUCCESS );
121 void run_scan( int argc, char *argv[] )
123 /* Martin A. Hansen, September 2008 */
125 /* For each file in argv scan the file for */
126 /* bipartite motifs and output the motifs */
127 /* and their count. */
131 seq_entry *entry = NULL;
132 uint *count_array = NULL;
134 count_array = count_array_new( COUNT_ARRAY_SIZE );
136 entry = seq_new( MAX_SEQ_NAME, MAX_SEQ );
138 for ( i = 1; i < argc; i++ )
142 fprintf( stderr, "Scanning file: %s\n", file );
144 scan_file( file, entry, count_array );
146 fprintf( stderr, "done.\n" );
149 fprintf( stderr, "Printing motifs: ... " );
151 count_array_print( count_array );
153 fprintf( stderr, "done.\n" );
155 seq_destroy( entry );
157 mem_free( &count_array );
161 uint *count_array_new( size_t size )
163 /* Martin A. Hansen, September 2008 */
165 /* Initialize a new zeroed uint array of a given byte size. */
169 array = mem_get_zero( size );
175 void scan_file( char *file, seq_entry *entry, uint *count_array )
177 /* Martin A. Hansen, September 2008 */
179 /* Scan all FASTA entries of a file in both */
180 /* sense and anti-sense directions. */
182 FILE *fp = read_open( file );
184 while ( fasta_get_entry( fp, &entry ) == TRUE )
186 fprintf( stderr, " Scanning: %s (%zu nt) ... ", entry->seq_name, entry->seq_len );
188 scan_seq( entry->seq, entry->seq_len, count_array );
190 fprintf( stderr, "done.\n" );
197 void scan_seq( char *seq, size_t seq_len, uint *count_array )
199 /* Martin A. Hansen, September 2008 */
201 /* Run a sliding window over a given sequence. The window */
202 /* consists of a list where new blocks of 4 nucleotides */
203 /* are pushed onto one end while at the same time old */
204 /* blocks are popped from the other end. The number of */
205 /* in the list is determined by the maximum seperator. */
206 /* Everytime we have a full window, the window is scanned */
209 bitblock *block = NULL;
214 bool first_node = TRUE;
215 node_sl *new_node = NULL;
216 node_sl *old_node = NULL;
217 list_sl *list = list_sl_new();
219 for ( i = 0; seq[ i ]; i++ )
225 case 'A': case 'a': add_A( bin ); break;
226 case 'T': case 't': add_T( bin ); break;
227 case 'C': case 'c': add_C( bin ); break;
228 case 'G': case 'g': add_G( bin ); break;
229 default: n_count = BLOCK_SIZE_NT; break;
232 if ( i > BLOCK_SIZE_NT - 2 )
236 block = bitblock_new();
245 new_node = node_sl_new();
246 new_node->val = block;
249 list_sl_add_beg( &list, &new_node );
251 list_sl_add_after( &old_node, &new_node );
258 if ( b_count > BLOCK_SPACE_MAX + BLOCK_SIZE_NT )
260 // bitblock_list_print( list );
262 scan_list( list, count_array );
264 list_sl_remove_beg( &list );
269 list_sl_destroy( &list );
273 void scan_list( list_sl *list, uint *count_array )
275 /* Martin A. Hansen, September 2008 */
277 /* Scan a list of blocks for biparite motifs by creating */
278 /* a binary motif consisting of two blocks of 4 nucleotides */
279 /* along with the distance separating them. Motifs containing */
280 /* N's are skipped. */
282 node_sl *first_node = NULL;
283 node_sl *next_node = NULL;
284 bitblock *block1 = NULL;
285 bitblock *block2 = NULL;
290 // bitblock_list_print( list );
292 first_node = list->first;
294 block1 = ( bitblock * ) first_node->val;
296 if ( ! block1->hasN )
298 next_node = first_node->next;
300 for ( i = 0; i < BLOCK_SIZE_NT - 1; i++ ) {
301 next_node = next_node->next;
304 for ( next_node = next_node; next_node != NULL; next_node = next_node->next )
306 block2 = ( bitblock * ) next_node->val;
308 // printf( "block1: %s block2: %s dist: %d\n", bin2dna[ block1->bin ], bin2dna[ block2->bin ], dist ); /* DEBUG */
310 if ( ! block2->hasN )
312 motif_bin = blocks2motif( block1->bin, block2->bin, dist );
314 // motif_print( motif_bin, 0 ); /* DEBUG */
316 count_array[ motif_bin ]++;
325 bitblock *bitblock_new()
327 /* Martin A. Hansen, September 2008 */
329 /* Initializes a new empty bitblock. */
331 bitblock *new_block = NULL;
333 new_block = mem_get( sizeof( bitblock ) );
336 new_block->hasN = FALSE;
342 uint blocks2motif( uchar bin1, uchar bin2, ushort dist )
344 /* Martin A. Hansen, September 2008 */
346 /* Given two binary encoded tetra nuceotide blocks, */
347 /* and the distance separating this, create a binary */
348 /* bipartite motif. */
354 motif <<= sizeof( uchar ) * BITS_IN_BYTE;
358 motif <<= sizeof( uchar ) * BITS_IN_BYTE;
366 void count_array_print( uint *count_array )
368 /* Martin A. Hansen, Seqptember 2008. */
370 /* Print all bipartite motifs in count_array as */
371 /* tabular output. */
377 for ( i = 0; i < COUNT_ARRAY_SIZE / 4; i++ )
380 count = count_array[ i ];
383 motif_print( motif, count );
389 void motif_print( uint motif, uint count )
391 /* Martin A. Hansen, September 2008 */
393 /* Converts a binary encoded bipartite motif */
394 /* into DNA and output the motif, distance and */
395 /* count seperated by tabs: */
396 /* BLOCK1 \t BLOCK2 \t DIST \t COUNT */
402 dist = ( ushort ) motif & 255;
404 motif >>= sizeof( uchar ) * BITS_IN_BYTE;
406 bin2 = ( uchar ) motif;
408 motif >>= sizeof( uchar ) * BITS_IN_BYTE;
410 bin1 = ( uchar ) motif;
412 printf( "%s\t%s\t%d\t%d\n", bin2dna[ bin1 ], bin2dna[ bin2 ], dist, count );
416 void bitblock_list_print( list_sl *list )
418 /* Martin A. Hansen, September 2008 */
420 /* Debug function to print all blocks in a list. */
422 node_sl *node = NULL;
424 printf( "\nbitblock_list_print:\n" );
426 for ( node = list->first; node != NULL; node = node->next ) {
427 bitblock_print( ( bitblock * ) node->val );
432 void bitblock_print( bitblock *out )
434 /* Martin A. Hansen, September 2008 */
436 /* Debug function to print a given block. */
438 printf( "bin: %d dna: %s hasN: %d\n", out->bin, bin2dna[ ( int ) out->bin ], out->hasN );
442 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> UNIT TESTS <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
447 fprintf( stderr, "Running tests\n" );
449 test_count_array_new();
451 test_bitblock_print();
452 test_bitblock_list_print();
456 fprintf( stderr, "All tests OK\n" );
460 void test_count_array_new()
462 fprintf( stderr, " Running test_count_array_new ... " );
468 array = count_array_new( 4 * size );
470 for ( i = 0; i < size; i++ ) {
471 assert( array[ i ] == 0 );
476 fprintf( stderr, "done.\n" );
480 void test_bitblock_new()
482 fprintf( stderr, " Running test_bitblock_new ... " );
484 bitblock *new_block = bitblock_new();
486 assert( new_block->bin == 0 );
487 assert( new_block->hasN == FALSE );
489 fprintf( stderr, "done.\n" );
493 void test_bitblock_print()
495 fprintf( stderr, " Running test_bitblock_print ... " );
497 bitblock *new_block = bitblock_new();
500 new_block->hasN = TRUE;
502 // bitblock_print( new_block );
504 fprintf( stderr, "done.\n");
508 void test_bitblock_list_print()
510 fprintf( stderr, " Running test_bitblock_list_print ... " );
512 list_sl *list = list_sl_new();
513 node_sl *node1 = node_sl_new();
514 node_sl *node2 = node_sl_new();
515 node_sl *node3 = node_sl_new();
516 bitblock *block1 = bitblock_new();
517 bitblock *block2 = bitblock_new();
518 bitblock *block3 = bitblock_new();
531 list_sl_add_beg( &list, &node1 );
532 list_sl_add_beg( &list, &node2 );
533 list_sl_add_beg( &list, &node3 );
535 // bitblock_list_print( list );
537 fprintf( stderr, "done.\n" );
543 fprintf( stderr, " Running test_scan_seq ... " );
545 //char *seq = "AAAANTCGGCTNGGGG";
546 //char *seq = "AAAATCGGCTGGGG";
547 char *seq = "AAAAAAAAAAAAAAA";
548 size_t seq_len = strlen( seq );
549 uint *count_array = mem_get_zero( sizeof( uint ) * ( 1 << 5 ) );
551 scan_seq( seq, seq_len, count_array );
553 fprintf( stderr, "done.\n" );
557 static void test_blocks2motif()
559 fprintf( stderr, " Running test_blocks2motif ... " );
566 motif = blocks2motif( bin1, bin2, dist );
568 // printf( "motif: %d\n", motif );
570 fprintf( stderr, "done.\n");
574 /* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */