/** * Pretty formatting of a binary tree to the output stream * * Parameters: * fp FILE * to print to. Just use 'stdout' if you are unsure. * root Root of Huffman Coding Tree that we're printing * level Control how wide you want the tree to sparse (eg, level 1 has * the minimum space between nodes, while level 2 has a larger * space between nodes) * indent Change this to add some indent space to the left (eg, * indent of 0 means the lowest level of the left node will * stick to the left margin) */ static void HuffNode_printPretty_Worker(FILE * fp, HuffNode * root, int level, int indent) { int r, i; // for-loop indices int h = HuffNode_height(root); // eq of the length of branch for each node of each level int branch_sz = 2*((1 << h) - 1) - (3-level)*(1 << (h-1)); // distance between left neighbor node's right arm and right neighbor // node's left arm int node_spacing_sz = 2 + (level+1)*(1 << h); // starting space to the first node to print of each level // (for the left most node of each level only) int start_sz = branch_sz + (3-level) + indent; Queue * Q = Queue_create(); Queue_pushBack(Q, root); int curr_tree_width = 1; for (r = 1; r < h; r++) { print_branches(fp, Q, branch_sz, node_spacing_sz, start_sz, curr_tree_width); branch_sz = branch_sz / 2 - 1; node_spacing_sz = node_spacing_sz / 2 + 1; start_sz = branch_sz + (3-level) + indent; print_nodes(fp, Q, branch_sz, node_spacing_sz, start_sz, curr_tree_width); for (i = 0; i < curr_tree_width; i++) { HuffNode * currNode = Queue_popFront(Q); if(currNode) { Queue_pushBack(Q, currNode->left); Queue_pushBack(Q, currNode->right); } else { Queue_pushBack(Q, NULL); Queue_pushBack(Q, NULL); } } curr_tree_width *= 2; } print_branches(fp, Q, branch_sz, node_spacing_sz, start_sz, curr_tree_width); print_leaves(fp, Q, indent, level, curr_tree_width); Queue_destroy(Q); }
int output_specs_for_refseq ( Options * optin, Protein * protein, Alignment * alignment, int * almt2protein, Node * leaf, double ** score, double cutoff , int ** is_precedent) { int node_id, pos, no_seqs = alignment->number_of_seqs; int ctr,number_of_nodes_above_cutoff ; int ctr1, ctr2, node_id_1, node_id_2; int node_ctr; int node_pos_ctr, ** node_related_position; Node * node_ptr, * first_inner_node; int * above_cutoff = NULL; char filename[BUFFLEN]; FILE * fptr; int print_leaves (FILE * fptr, Node * node); if ( ! ( above_cutoff = emalloc (no_seqs*sizeof(int) ) ) ) exit (1); if ( ! ( node_related_position = intmatrix ( no_seqs, alignment->length+1) ) ) exit (1); sprintf (filename, "%s.qry_specs", options->outname); fptr = efopen (filename, "w"); if ( !fptr) return 1; for ( pos=0; pos < alignment->length; pos++) { number_of_nodes_above_cutoff = 0; ctr = 0; for ( node_id= 1; node_id < no_seqs; node_id++ ) { if ( score[pos][node_id] > cutoff ){ above_cutoff [ctr] = node_id; ctr ++; } } if ( !ctr ) continue; above_cutoff [ctr] = -1; number_of_nodes_above_cutoff = ctr; for (ctr1=0; above_cutoff [ctr1] >= 0; ctr1++ ) { node_id_1 = above_cutoff [ctr1]; if ( ! node_id_1 ) continue; for (ctr2 = ctr1+1; above_cutoff [ctr2] >= 0 ; ctr2++ ) { node_id_2 = above_cutoff [ctr2]; if ( ! node_id_2 ) continue; /* go for the highest z */ /* if ( is_precedent[node_id_1][node_id_2] || is_precedent[node_id_2][node_id_1] ) { number_of_nodes_above_cutoff --; if ( score[pos][node_id_2] < score[pos][node_id_1] ) { above_cutoff [ctr2] = 0; } else { above_cutoff [ctr1] = 0; break; } }*/ /* alternatively, go for the ancestor */ if ( is_precedent[node_id_1][node_id_2] ) { number_of_nodes_above_cutoff --; above_cutoff [ctr2] = 0; } else if ( is_precedent[node_id_2][node_id_1] ) { number_of_nodes_above_cutoff --; above_cutoff [ctr1] = 0; break; } } } if ( number_of_nodes_above_cutoff <= 0) { printf (" %d foul\n", pos); for (ctr1=0; above_cutoff [ctr1] >= 0; ctr1++ ) { printf (" ** %d \n", above_cutoff [ctr1] ); } exit (1); } if ( number_of_nodes_above_cutoff > 1 ) { fprintf (fptr, "%4d %5s:", pos + 1, "discr" ); } else { fprintf (fptr, "%4d %5s:", pos + 1, "det" ); } for (ctr1=0; above_cutoff [ctr1] >= 0; ctr1++ ) { // printf (" ** %d \n", above_cutoff [ctr1] ); node_id_1 = above_cutoff [ctr1]; if ( ! node_id_1 ) continue; fprintf (fptr, " %4d", node_id_1); node_ctr = 2*no_seqs-1 - node_id_1; (leaf+node_ctr) -> marked = 1; node_related_position[node_id_1][0] ++; node_pos_ctr = node_related_position[node_id_1][0]; /* use the zeroth pos as a counter */ node_related_position[node_id_1][ node_pos_ctr ] = pos + 1; } fprintf (fptr, "\n"); } fprintf ( fptr, "\n\n"); fprintf ( fptr, "nodes:\n=============\n\n"); first_inner_node = leaf + no_seqs; for ( node_ctr = 0; node_ctr < no_seqs-1; node_ctr ++ ) { node_ptr = first_inner_node + node_ctr; if ( node_ptr->marked ) { fprintf ( fptr, "\n\t node %4d\n", node_ptr->id ); fprintf ( fptr, "\t positions:"); for ( node_pos_ctr=1; node_pos_ctr<= node_related_position[node_ptr->id][0]; node_pos_ctr++ ) { fprintf ( fptr, " %4d", node_related_position [node_ptr->id][ node_pos_ctr ]); } fprintf (fptr, "\n"); fprintf ( fptr, "\t sequences\n"); print_leaves ( fptr, node_ptr); } } fprintf (fptr, "\n"); free (above_cutoff); free_matrix ( (void*) node_related_position); //fclose (fptr); return 0; }
int main( int argc, char ** argv ) { char * input_file; FILE * fp; node * root; int input, range2; char instruction; char license_part; root = NULL; verbose_output = false; /* if (argc > 1) { order = atoi(argv[1]); if (order < MIN_ORDER || order > MAX_ORDER) { fprintf(stderr, "Invalid order: %d .\n\n", order); usage_3(); exit(EXIT_FAILURE); } } license_notice(); usage_1(); usage_2(); */ if (argc > 2) { switch (argc[1]) { case 's'; } /*input_file = argv[2]; fp = fopen(input_file, "r"); if (fp == NULL) { perror("Failure to open input file."); exit(EXIT_FAILURE); } while (!feof(fp)) { fscanf(fp, "%d\n", &input); root = insert(root, input, input); } fclose(fp); print_tree(root);*/ } printf("> "); while (scanf("%c", &instruction) != EOF) { switch (instruction) { case 'd': scanf("%d", &input); root = delete_key(root, input); print_tree(root); break; case 'i': scanf("%d", &input); root = insert(root, input, input); print_tree(root); break; case 'f': case 'p': scanf("%d", &input); find_and_print(root, input, instruction == 'p'); break; case 'r': scanf("%d %d", &input, &range2); if (input > range2) { int tmp = range2; range2 = input; input = tmp; } find_and_print_range(root, input, range2, instruction == 'p'); break; case 'l': print_leaves(root); break; case 'q': while (getchar() != (int)'\n'); return EXIT_SUCCESS; case 's': if (scanf("how %c", &license_part) == 0) { usage_2(); break; } switch(license_part) { case 'w': print_license(LICENSE_WARRANTEE); break; case 'c': print_license(LICENSE_CONDITIONS); break; default: usage_2(); break; } break; case 't': print_tree(root); break; case 'v': verbose_output = !verbose_output; break; case 'x': if (root) root = destroy_tree(root); print_tree(root); break; default: usage_2(); break; } while (getchar() != (int)'\n'); printf("> "); } printf("\n"); return EXIT_SUCCESS; }
int output_specs_score ( Options *options, Protein * protein, Alignment * alignment, int *almt2prot, Node * leaf, double ** score, double ** complement_score, double ** p_value, double **probability, double **overlap, double cutoff, int ** is_precedent) { # if 0 char *base_filename = options ->outname; int node_id, pos, no_seqs = alignment->number_of_seqs; int node_id_2; int node_ctr,* print, first; int ** related; int int_max_gaps = MAX_GAPS*no_seqs; Node *root; int refseq_number, refseq_2_number = -1; char filename[BUFFLEN]; char in_qry2; FILE * fptr; int print_leaves (FILE * fptr, Node * node); int recursive_cleanup (Node *node, int * related, double * probability); if ( ! ( related = intmatrix (alignment->length+1, no_seqs ) ) ) exit (1); if ( ! ( print = emalloc (no_seqs*sizeof(int) ) ) ) exit (1); printf ("in output\n"); #if 1 sprintf (filename, "%s.specs", base_filename); fptr = efopen (filename, "w"); if ( !fptr) return 1; # else fptr = stdout; # endif if ( ! options->refseq ) { fprintf ( stderr, "Please define refseq.\n"); exit (0); } /* locate the refseq */ refseq_number = find_refseq (alignment, options->refseq); if ( options->qry2[0]) refseq_2_number = find_refseq (alignment, options->qry2); /* associate nodes and positions */ for ( node_id = 2; node_id < no_seqs; node_id++ ) { node_ctr = 2*no_seqs -1 - node_id; if ( (leaf+node_ctr)-> number_of_leaves < MIN_NO_LEAVES) continue; for ( pos=0; pos < alignment->length; pos++) { if ( alignment->column_gaps[pos] > int_max_gaps) continue; if ( overlap[pos][node_id] < MAX_OVERLAP && probability[pos][node_id] <= MAX_PROB) { related[pos][node_id] = 1; } } } /* recursive cleanup (so parents and children do not appear for the same reason */ for ( pos=0; pos < alignment->length; pos++) { if ( alignment->column_gaps[pos] > int_max_gaps ) continue; recursive_cleanup ( root = leaf+2*no_seqs-1 - 1, related[pos], probability[pos] ) ; } /* which nodes should be printed? */ for ( node_id = 2; node_id < no_seqs; node_id++ ) { for ( pos=0; pos < alignment->length; pos++) { if ( alignment->column_gaps[pos] > int_max_gaps ) continue; if ( related[pos][node_id] ) { print[node_id] = 1; break; } } } for ( node_id = 2; node_id < no_seqs; node_id++ ) { node_ctr = 2*no_seqs-1 - node_id; if ( ! print[node_id] ) continue; fprintf ( fptr, "\n\nnode %d\n=============\n", node_id); fprintf ( fptr, " %4s %4s %4s %4s %5s %5s %5s %5s other nodes\n", "pos", "pdbid", "qry1", "qry2", "entr", "c_entr", "prob", "ovlp"); for ( pos=0; pos < alignment->length; pos++) { if ( ! related[pos][node_id] ) continue; in_qry2 = (options->qry2[0]) ? alignment->sequence[refseq_2_number][pos]: '-'; fprintf ( fptr, " %4d %4s %1c %1c %5.1lf %5.1lf %5.1le %5.2lf ", pos+1, protein->sequence[ almt2prot[pos]].pdb_id, alignment->sequence[refseq_number][pos], in_qry2, score[pos][node_id], complement_score[pos][node_id], probability[pos][node_id], overlap[pos][node_id] ); /* other nodes which have this same pos as discriminant */ first = 1; for ( node_id_2 = 2; node_id_2 < no_seqs; node_id_2 ++ ) { if ( node_id_2 == node_id ) continue; if ( related[pos][node_id_2] ) { if ( ! first ) { fprintf ( fptr, ","); } fprintf ( fptr, "%d",node_id_2); first = 0; } } fprintf ( fptr, "\n"); } } fclose (fptr); /* print out leaves */ sprintf (filename, "%s.leaves", base_filename); fptr = efopen (filename, "w"); if ( !fptr) return 1; for ( node_id = 2; node_id < no_seqs; node_id++ ) { if ( ! print[node_id] ) continue; node_ctr = 2*no_seqs -1 - node_id; fprintf ( fptr, "\n\nnode %d\n=============\n", node_id); print_leaves ( fptr, leaf+node_ctr); fprintf (fptr, "\n"); } fclose (fptr); # endif return 0; }