void read_tree(Node **head, char **buffer) {
    char ch;
    Node *x = create_node(0,0);
    *head = x;
    ch = (*buffer)[0];
    *buffer += 1;
    if(ch == 'N') {
        x->ch = ALPHABET;
        read_tree(&x->left, buffer);
        read_tree(&x->right, buffer);
    } else {
        x->ch = (*buffer)[0];
        *buffer += 1;
   }
}
Exemple #2
0
static void		pipe_right(t_node *tree, int *pipe_fd, t_data *data)
{
	char	*line;
	off_t	offset;

	close(pipe_fd[1]);
	if (data->tmp_pipeout == -1)
		get_tmpfd(&data->tmp_pipeout, "/.temp_pipeout");
	else
	{
		remove_tmp("/.temp_pipeout");
		get_tmpfd(&data->tmp_pipeout, "/.temp_pipeout");
	}
	while (get_next_line(pipe_fd[0], &line) > 0)
	{
		ft_putendl_fd(line, data->tmp_pipeout);
		ft_strdel(&line);
	}
	offset = lseek(data->tmp_pipeout, 0, SEEK_CUR);
	lseek(data->tmp_pipeout, 0, SEEK_SET);
	update_offset(&data, offset);
	read_tree(tree->right);
	remove_tmp("/.temp_pipeout");
	if (g_pid.built == 0 || WEXITSTATUS(g_pid.id) != 0)
		exit(EXIT_FAILURE);
	else
		exit(EXIT_SUCCESS);
}
Exemple #3
0
static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size)
{
	const struct index_extension *source;
	struct index_extension dest;
	size_t total_size;

	source = (const struct index_extension *)(buffer);

	memcpy(dest.signature, source->signature, 4);
	dest.extension_size = ntohl(source->extension_size);

	total_size = dest.extension_size + sizeof(struct index_extension);

	if (buffer_size - total_size < INDEX_FOOTER_SIZE)
		return 0;

	/* optional extension */
	if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
		/* tree cache */
		if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {

			if (read_tree(index, buffer + 8, dest.extension_size) < 0)
				return 0;
		}
	} else {
		/* we cannot handle non-ignorable extensions;
		 * in fact they aren't even defined in the standard */
		return 0;
	}

	return total_size;
}
int main(int argc, char **argv) {
    unordered_map<int, TreeNode *> map;
    TreeNode *root = read_tree(argc - 3, argv + 1);
    add_to_map(map, root);
    cout << Solution().lowestCommonAncestor(root,
        map[atoi(argv[argc - 2])], map[atoi(argv[argc - 1])])->val << endl;
    return 0;
}
int main(int argc, char **argv) {
    BSTIterator i(read_tree(argc - 1, argv + 1));
    while (i.hasNext()) {
        cout << i.next() << " ";
    }
    cout << endl;
    return 0;
}
Exemple #6
0
int main( void ) {
  tree *tp = read_tree();
  if ((balanced(tp)) == 1) printf("BALANCED\n");
  else printf("UNSTABLE\n");



  return 0;
}
Exemple #7
0
softmax_layer parse_softmax(list *options, size_params params)
{
    int groups = option_find_int_quiet(options, "groups",1);
    softmax_layer layer = make_softmax_layer(params.batch, params.inputs, groups);
    layer.temperature = option_find_float_quiet(options, "temperature", 1);
    char *tree_file = option_find_str(options, "tree", 0);
    if (tree_file) layer.softmax_tree = read_tree(tree_file);
    return layer;
}
Exemple #8
0
void huffman_decoder::read_tree(size_t node_offset) {
    code_node* node;
    bool is_leaf = inbs->read_bit();

    if (is_leaf) {
        node = new code_node(chars.front());
        chars.pop();
    } else {
        size_t l = used_nodes_count++;
        read_tree(l);
        
        size_t r = used_nodes_count++;
        read_tree(r);
        node = new code_node(nodes[l], nodes[r]);
    }

    nodes[node_offset] = node;
}
Exemple #9
0
void BenchTree()
{
        
        printf("\n************ ROOT/TTree I/O  ************ \n");       
        const char *fname2 = "testio.root";
        Double_t wbytes;        
        wbytes = write_tree(fname2,100000000,0); 
        read_tree(fname2,wbytes); 
}
Exemple #10
0
static int unpack_tree(unsigned char *sha1)
{
	void *buffer;
	unsigned long size;

	buffer = read_object_with_reference(sha1, "tree", &size, 0);
	if (!buffer)
		return -1;
	return read_tree(buffer, size, stage);
}
Exemple #11
0
/* Build the Huffman tree recursively.
 */
void read_tree(file *f, huf_node *node, bool isRoot){
  int bit = bitfile_get_bit(f);
  if(bit==EOF){
    if(isRoot) return;
    else die_format();
  }
  if(!bit){ // internal node
    huf_node *left = (huf_node *)malloc_or_die(2*sizeof(huf_node));
    huf_node *right = left+1;
    node->left = left;
    node->right = right;
    read_tree(f, left, false);
    read_tree(f, right, false);
  }else{ // leaf node
    int s = bitfile_get_symbol(f, SYMBOL_LENGTH);
    if(s==EOF) die_format();
    node->symbol = s;
    node->left = NULL;
    node->right = NULL;
  }
}
Exemple #12
0
/*
 * Read the tree specified with --with-tree option
 * (typically, HEAD) into stage #1 and then
 * squash them down to stage #0.  This is used for
 * --error-unmatch to list and check the path patterns
 * that were given from the command line.  We are not
 * going to write this index out.
 */
void overlay_tree_on_cache(const char *tree_name, const char *prefix)
{
	struct tree *tree;
	unsigned char sha1[20];
	const char **match;
	struct cache_entry *last_stage0 = NULL;
	int i;

	if (get_sha1(tree_name, sha1))
		die("tree-ish %s not found.", tree_name);
	tree = parse_tree_indirect(sha1);
	if (!tree)
		die("bad tree-ish %s", tree_name);

	/* Hoist the unmerged entries up to stage #3 to make room */
	for (i = 0; i < active_nr; i++) {
		struct cache_entry *ce = active_cache[i];
		if (!ce_stage(ce))
			continue;
		ce->ce_flags |= CE_STAGEMASK;
	}

	if (prefix) {
		static const char *(matchbuf[2]);
		matchbuf[0] = prefix;
		matchbuf[1] = NULL;
		match = matchbuf;
	} else
		match = NULL;
	if (read_tree(tree, 1, match))
		die("unable to read tree entries %s", tree_name);

	for (i = 0; i < active_nr; i++) {
		struct cache_entry *ce = active_cache[i];
		switch (ce_stage(ce)) {
		case 0:
			last_stage0 = ce;
			/* fallthru */
		default:
			continue;
		case 1:
			/*
			 * If there is stage #0 entry for this, we do not
			 * need to show it.  We use CE_UPDATE bit to mark
			 * such an entry.
			 */
			if (last_stage0 &&
			    !strcmp(last_stage0->name, ce->name))
				ce->ce_flags |= CE_UPDATE;
		}
	}
}
Exemple #13
0
void huffman_decoder::read_tree(std::istream & ins) {
    nodes_count = nodes_count_by_chars_count(chars_count);
    chars = std::queue< code_char_t >();
    
    for (size_t i = 0; i < chars_count; i++) {
        chars.push(ins.get());
    }

    nodes = new code_node*[nodes_count];
    
    used_nodes_count = 1;
    read_tree(0);
}
Exemple #14
0
layer parse_region(list *options, size_params params)
{
    int coords = option_find_int(options, "coords", 4);
    int classes = option_find_int(options, "classes", 20);
    int num = option_find_int(options, "num", 1);

    layer l = make_region_layer(params.batch, params.w, params.h, num, classes, coords);
    assert(l.outputs == params.inputs);

    l.log = option_find_int_quiet(options, "log", 0);
    l.sqrt = option_find_int_quiet(options, "sqrt", 0);

    l.softmax = option_find_int(options, "softmax", 0);
    l.background = option_find_int_quiet(options, "background", 0);
    l.max_boxes = option_find_int_quiet(options, "max",30);
    l.jitter = option_find_float(options, "jitter", .2);
    l.rescore = option_find_int_quiet(options, "rescore",0);

    l.thresh = option_find_float(options, "thresh", .5);
    l.classfix = option_find_int_quiet(options, "classfix", 0);
    l.absolute = option_find_int_quiet(options, "absolute", 0);
    l.random = option_find_int_quiet(options, "random", 0);

    l.coord_scale = option_find_float(options, "coord_scale", 1);
    l.object_scale = option_find_float(options, "object_scale", 1);
    l.noobject_scale = option_find_float(options, "noobject_scale", 1);
    l.mask_scale = option_find_float(options, "mask_scale", 1);
    l.class_scale = option_find_float(options, "class_scale", 1);
    l.bias_match = option_find_int_quiet(options, "bias_match",0);

    char *tree_file = option_find_str(options, "tree", 0);
    if (tree_file) l.softmax_tree = read_tree(tree_file);
    char *map_file = option_find_str(options, "map", 0);
    if (map_file) l.map = read_map(map_file);

    char *a = option_find_str(options, "anchors", 0);
    if(a){
        int len = strlen(a);
        int n = 1;
        int i;
        for(i = 0; i < len; ++i){
            if (a[i] == ',') ++n;
        }
        for(i = 0; i < n; ++i){
            float bias = atof(a);
            l.biases[i] = bias;
            a = strchr(a, ',')+1;
        }
    }
    return l;
}
Exemple #15
0
void huffman_decoder::decode_file(std::istream & ins, std::ostream & outs) {
    inbs = new ibstream(ins);
    
    chars_count = (((size_t)ins.get()) << 8) | ((size_t)ins.get());
    if (chars_count == 0) {
        return;
    }

    read_tree(ins);
    
    decode_and_write(outs);   
    
    free_resources();
}
Exemple #16
0
std::vector<parse_tree> extract_trees(std::istream& stream)
{
    std::vector<parse_tree> results;

    read_whitespace(stream);

    while (stream)
    {
        results.emplace_back(read_tree(stream));
        read_whitespace(stream);
    }

    return results;
}
Exemple #17
0
layer parse_softmax(list *options, size_params params)
{
    int groups = option_find_int_quiet(options, "groups",1);
    layer l = make_softmax_layer(params.batch, params.inputs, groups);
    l.temperature = option_find_float_quiet(options, "temperature", 1);
    char *tree_file = option_find_str(options, "tree", 0);
    if (tree_file) l.softmax_tree = read_tree(tree_file);
    l.w = params.w;
    l.h = params.h;
    l.c = params.c;
    l.spatial = option_find_float_quiet(options, "spatial", 0);
    l.noloss =  option_find_int_quiet(options, "noloss", 0);
    return l;
}
Exemple #18
0
static vl_bool read_tree(VlHIKMNode*& node, vl_uint32 height, const char* strSavePath, const char* strInd)
{
	node = (VlHIKMNode*)malloc (sizeof(VlHIKMNode)) ;

	node-> filter   =  new CIKMTree(); 
	node-> children =  NULL ; 


	//CString strFileClusterName = strSavePath + "\\" + strInd + ".txt";
	char strFileClusterName[MAX_PATH];
	strcpy( strFileClusterName, strSavePath );
	strcat( strFileClusterName, "\\" );
	strcat( strFileClusterName, strInd );
	strcat( strFileClusterName, ".txt" );
	if(!node->filter->vl_ikm_read_tree( strFileClusterName )){
		return FALSE;
	}

	if (height > 1) {
		vl_uint32 K = node->filter->vl_ikm_get_k();
		node-> children = (VlHIKMNode**)malloc(sizeof(VlHIKMNode*) * K) ;

		for ( vl_uint32 k = 0; k < K; k++ ) {
			//CString strIndChild;
			//strIndChild.Format( _T("%3d"), k );
			//strIndChild.Replace( _T(" "), _T("0") );
			//strIndChild = strInd + strIndChild;
			char strIndChild[MAX_PATH];
			sprintf( strIndChild, "%s%4d", strInd, k );
			for ( int c = 0; c < strlen(strIndChild); c++ )
			{
				if ( strIndChild[c] == ' ')
				{
					strIndChild[c] = '0';
				}
			}

			if(!read_tree(node->children[k], height-1, strSavePath, strIndChild)){
				return FALSE;
			}
		}
	}
	return TRUE;
}
int main(int argc, char **argv) {
  int64_t i;
  if (argc<3) {
    printf("Usage: %s box_size tree.dat ...\n", argv[0]);
    exit(1);
  }

  box_size = atof(argv[1]);
  init_time_table(0.27, 0.7);

  printf("#Scale Id Mvir_acc(Host) Rvir_acc(Host) Mnow Vnow Mlastmm Vlastmm Rlastmm Rlastmm/Rvir Vacc Mistracked? RlastMajM Rlastmillim a_acc a_lastmm a_lastmajm a_lastmillim\n");

  for (i=2; i<argc; i++) {
    read_tree(argv[i]);
    process_tree();
    delete_tree();
  }
  return 0;
}
Exemple #20
0
TREE_T* read_tree_from_file
  (char* filename) 
{

    TREE_T* tree;
    FILE* tree_file = NULL;

    if (open_file(filename, "r", 1, "tree", "tree", &tree_file) == 0) {
      die("Couldn't open the file %s.\n", filename);
    }
    read_tree(tree_file, &tree);
    (void) fclose(tree_file);
    if (verbosity >= HIGH_VERBOSE) {
      fprintf(stderr, "Read tree: ");
      write_tree(tree, stderr);
    }

    return tree;
}
Exemple #21
0
static void		pipe_left(t_node *tree, int *pipe_fd, t_data *data)
{
	char	*line;

	close(pipe_fd[0]);
	get_tmpfd(&data->tmp_pipein, "/.temp_pipein");
	read_tree(tree->left);
	lseek(data->tmp_pipein, 0, SEEK_SET);
	while (get_next_line(data->tmp_pipein, &line) > 0)
	{
		ft_putendl_fd(line, pipe_fd[1]);
		ft_strdel(&line);
	}
	remove_tmp("/.temp_pipein");
	if (g_pid.built == 0 || WEXITSTATUS(g_pid.id) != 0)
		exit(EXIT_FAILURE);
	else
		exit(EXIT_SUCCESS);
}
Exemple #22
0
void			redirr_proc(t_node *tree)
{
	char	*file;
	int		fd;
	t_data	*data;

	file = ft_strtrim(tree->left->word);
	data = init_data(0);
	if (data->tmp_fdout == -1)
		get_tmpfd(&data->tmp_fdout, "/.temp_out");
	if (data->tmp_fdout != -1 && (fd = check_file(file)) != -1)
	{
		add_outfildes(&data->outfildes, file, fd);
		read_tree(tree->right);
	}
	else
	{
		update_data(&data);
		g_pid.built = 0;
	}
	ft_strdel(&file);
}
int read_archive(FILE *archive, FILE *output, Header *header) {
    unsigned long long i;
    Node *head = create_node(ALPHABET, 0);
    char byte;

    read_tree(&head, &header->tree);

    output = fopen(header->filename, "wb");

    if(output == NULL) {
        return 1;
    }

    Node *current = head;
    for(i = 0; i < header->filesize; i++) {
        if(i % 8 == 0) {
            fread(&byte, sizeof(char), 1, archive);
        }
        if(byte & TAB[7 - (i % 8)]) {
            current = current->right;
        } else {
            current = current->left;
        }
        if(current == NULL) {
            fprintf(stderr, "Damaged tree\n");
            return 1;
        }

        if(current->ch < ALPHABET) {
            fwrite(&current->ch, sizeof(char), 1, output);
            current = head;
        }
        show_progress(i, header->filesize);
    }
    fprintf(stderr, "\n");
    
    return 0;
}
Exemple #24
0
vl_bool CHIKMTree::vl_hikm_read_tree(const char* strSavePath)
{
	//CString strFileName = strSavePath + "\\cluster.txt";
	char strFileName[MAX_PATH];
	strcpy( strFileName, strSavePath );
	strcat( strFileName, "\\cluster.txt" );
	FILE * file = fopen( strFileName,"rb");
	if (!file) {
		return FALSE;
	}

	vl_hikm_new_VlHIKMTree();

	if(fscanf(file,"%u %u %u %d %d\n", &(m_VlHIKMTree->M), &(m_VlHIKMTree->K), &(m_VlHIKMTree->depth), &(m_VlHIKMTree->max_niters), &(m_VlHIKMTree->verb)) != 5){
		return FALSE;
	}
	fclose(file);

	if(!read_tree(m_VlHIKMTree->root, m_VlHIKMTree->depth, strSavePath, "0")){
		return FALSE;
	}

	return TRUE;
}
Exemple #25
0
static size_t read_extension(git_index *index, const char *buffer, size_t buffer_size)
{
	const struct index_extension *source;
	struct index_extension dest;
	size_t total_size;

	source = (const struct index_extension *)(buffer);

	memcpy(dest.signature, source->signature, 4);
	dest.extension_size = ntohl(source->extension_size);

	total_size = dest.extension_size + sizeof(struct index_extension);

	if (buffer_size - total_size < INDEX_FOOTER_SIZE)
		return 0;

	/* optional extension */
	if (dest.signature[0] >= 'A' && dest.signature[0] <= 'Z') {
		/* tree cache */
		if (memcmp(dest.signature, INDEX_EXT_TREECACHE_SIG, 4) == 0) {
			if (read_tree(index, buffer + 8, dest.extension_size) < GIT_SUCCESS)
				return 0;
		} else if (memcmp(dest.signature, INDEX_EXT_UNMERGED_SIG, 4) == 0) {
			if (read_unmerged(index, buffer + 8, dest.extension_size) < GIT_SUCCESS)
				return 0;
		}
		/* else, unsupported extension. We cannot parse this, but we can skip
		 * it by returning `total_size */
	} else {
		/* we cannot handle non-ignorable extensions;
		 * in fact they aren't even defined in the standard */
		return 0;
	}

	return total_size;
}
Exemple #26
0
gboolean
dfvm_apply(dfilter_t *df, proto_tree *tree)
{
	int		id, length;
	gboolean	accum = TRUE;
	dfvm_insn_t	*insn;
	dfvm_value_t	*arg1;
	dfvm_value_t	*arg2;
	dfvm_value_t	*arg3 = NULL;
	dfvm_value_t	*arg4 = NULL;
	header_field_info	*hfinfo;
	GList		*param1;
	GList		*param2;

	g_assert(tree);

	length = df->insns->len;

	for (id = 0; id < length; id++) {

	  AGAIN:
		insn = g_ptr_array_index(df->insns, id);
		arg1 = insn->arg1;
		arg2 = insn->arg2;

		switch (insn->op) {
			case CHECK_EXISTS:
				hfinfo = arg1->value.hfinfo;
				while(hfinfo) {
					accum = proto_check_for_protocol_or_field(tree,
							hfinfo->id);
					if (accum) {
						break;
					}
					else {
						hfinfo = hfinfo->same_name_next;
					}
				}
				break;

			case READ_TREE:
				accum = read_tree(df, tree,
						arg1->value.hfinfo, arg2->value.numeric);
				break;

			case CALL_FUNCTION:
				arg3 = insn->arg3;
				arg4 = insn->arg4;
				param1 = NULL;
				param2 = NULL;
				if (arg3) {
					param1 = df->registers[arg3->value.numeric];
				}
				if (arg4) {
					param2 = df->registers[arg4->value.numeric];
				}
				accum = arg1->value.funcdef->function(param1, param2,
						&df->registers[arg2->value.numeric]);
				break;

			case MK_RANGE:
				arg3 = insn->arg3;
				mk_range(df,
						arg1->value.numeric, arg2->value.numeric,
						arg3->value.drange);
				break;

			case ANY_EQ:
				accum = any_test(df, fvalue_eq,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case ANY_NE:
				accum = any_test(df, fvalue_ne,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case ANY_GT:
				accum = any_test(df, fvalue_gt,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case ANY_GE:
				accum = any_test(df, fvalue_ge,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case ANY_LT:
				accum = any_test(df, fvalue_lt,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case ANY_LE:
				accum = any_test(df, fvalue_le,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case ANY_BITWISE_AND:
				accum = any_test(df, fvalue_bitwise_and,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case ANY_CONTAINS:
				accum = any_test(df, fvalue_contains,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case ANY_MATCHES:
				accum = any_test(df, fvalue_matches,
						arg1->value.numeric, arg2->value.numeric);
				break;

			case NOT:
				accum = !accum;
				break;

			case RETURN:
				free_register_overhead(df);
				return accum;

			case IF_TRUE_GOTO:
				if (accum) {
					id = arg1->value.numeric;
					goto AGAIN;
				}
				break;

			case IF_FALSE_GOTO:
				if (!accum) {
					id = arg1->value.numeric;
					goto AGAIN;
				}
				break;

			case PUT_FVALUE:
#if 0
				accum = put_fvalue(df,
						arg1->value.fvalue, arg2->value.numeric);
				break;
#endif

			default:
				g_assert_not_reached();
				break;
		}
	}

	g_assert_not_reached();
	return FALSE; /* to appease the compiler */
}
Exemple #27
0
int main(int argc,char **argv)
{
	sint i,j,k,n,s;
	sint status;
	sint result_type;
	char c;
	char infile[FILENAMELEN+1];
	char treefile[FILENAMELEN+1];
	float meanid;
	FILE *tree;
	sint maxres,*gapptr=NULL;
	IN_TREEPTR itree;
	double dscore;
	double meanscore;
	double **tmat;
	sint window;
	sint block_cutoff;
	OPT opt;

	if(argc!=2) {
		fprintf(stdout,"Usage: %s input_aln\n",argv[0]);
		exit(1);
	}

	strcpy(infile,argv[1]);

        init_options(&opt);

/* read in the sequences */
	seq_input(infile,opt.explicit_type,FALSE,&mult_aln);
	if(mult_aln.nseqs<=0) {
		error("No sequences in %s\n",infile);
		exit(1);
	}


	window=8;

/* count pairwise residue percent identities */
        tmat = (double **) ckalloc( (mult_aln.nseqs+1) * sizeof (double *) );
        for(i=0;i<mult_aln.nseqs;i++)
                tmat[i] = (double *)ckalloc( (mult_aln.nseqs+1) * sizeof (double) );

	meanscore=0;
        for (i=0,n=0;i<mult_aln.nseqs;i++) {
                for (j=i+1;j<mult_aln.nseqs;j++) {
                        dscore = countid(mult_aln.seqs[i],mult_aln.seqs[j]);
                        tmat[j][i] = tmat[i][j] = (100.0 - dscore)/100.0;
			n++;
			meanscore+=dscore;
                }
        }
	meanscore/=(float)n;

	/*if(mult_aln.nseqs<100) block_cutoff=8;
	else if(mult_aln.nseqs<250) block_cutoff=6;
	else*/ block_cutoff=5;

	if(meanscore>50) block_cutoff=50;

/* make a tree from the percent identities (used for sequence weighting) */
	strcpy(treefile,infile);
	strcat(treefile,".ph");
        if((tree = open_explicit_file(treefile))==NULL) exit(1);

        guide_tree(tree,mult_aln.seqs,mult_aln.nseqs, tmat, QUICKNJ);
        itree=(IN_TREEPTR)ckalloc(sizeof(IN_TREE));

        status = read_tree(treefile, mult_aln.seqs, 0, mult_aln.nseqs,itree);
        for(i=0;i<mult_aln.nseqs;i++)
                ckfree(tmat[i]);
        ckfree(tmat);

        if (status < 0) exit(1);


        seq_weight = calc_seq_weights(0,mult_aln.nseqs,itree,FALSE);
        free_tree(itree);
        remove(treefile);


/* find the start and end positions of each sequence */

	is = (sint *)ckalloc((mult_aln.nseqs+1) * sizeof(sint));
	ie = (sint *)ckalloc((mult_aln.nseqs+1) * sizeof(sint));
	for(s=0;s<mult_aln.nseqs;s++) {
		is[s]=0;
                ie[s] = mult_aln.seqs[s].len;
                for (i=0; i<mult_aln.seqs[s].len; i++) {
                        c = mult_aln.seqs[s].data[i];
                        if (!isalpha(c))
                                is[s]++;
                        else
                                break;
                }
                for (i=mult_aln.seqs[s].len-1; i>=0; i--) {
                        c = mult_aln.seqs[s].data[i];
                        if (!isalpha(c))
                                ie[s]--;
                        else
                                break;
                }
	}

	matrix.format=0;
	maxres = get_cl_matrix(FALSE, gon250mt, gapptr, TRUE, 100, &matrix);

	all_blocks(infile,window,block_cutoff);
}
Exemple #28
0
void	scolon_proc(t_node *tree, int fd_in, int fd_out)
{
	read_tree(tree->left, fd_in, fd_out);
	read_tree(tree->right, fd_in, fd_out);
}
Exemple #29
0
int main(int argc,char **argv)
{
  unsigned int outstrlen=0;
  int i,j,k,n;
  unsigned int offset,wdim=1,alldim;
  unsigned int has_future,*future,**useries,h;
  unsigned long count;
  long *segment;
  char *outstring,*infile,*wcol,*use_array;
  double **series,*sermin,*serinter,**dfuture;
  ptree *root;
  FILE *fout;

  if (scan_help(argc,argv))
    show_options(argv[0]);

  for (i=0;i<argc;i++) {
    outstrlen += strlen(argv[i]);
    outstrlen++;
  }
  check_alloc(outstring=(char*)malloc(sizeof(char)*(outstrlen+8)));
  sprintf(outstring,"#Prog: ");
  offset=7;
  for (i=0;i<argc;i++) {
    sprintf(outstring+offset,"%s ",argv[i]);
    offset += (strlen(argv[i])+1);
  }

  scan_options(argc,argv);
 
  if (!compdimset) 
    compdim=pars.DIM;
  else
    if (compdim > pars.DIM) {
      fprintf(stderr,"-C value larger -m Value.\n");
      exit(LANGEVIN_MAIN_C_TOO_LARGE);
    }

  infile=search_datafile(argc,argv,NULL,VERBOSITY);
  if (infile == NULL) {
    fprintf(stderr,"No input datafile found.\n");
    exit(LANGEVIN_MAIN_NO_INPUTFILE);
  }
  
  if (OUTFILE == NULL) {
    check_alloc(OUTFILE=(char*)calloc(strlen(infile)+5,(size_t)1));
    sprintf(OUTFILE,"%s.pru",infile);
  }
  OUTFILE=test_outfile(OUTFILE);

  if (COLUMN == NULL)
    series=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				      &(pars.DIM),"",dimset,VERBOSITY);
  else
    series=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				      &(pars.DIM),COLUMN,dimset,VERBOSITY);

  check_alloc(sermin=malloc(sizeof(double)*pars.DIM));
  check_alloc(serinter=malloc(sizeof(double)*pars.DIM));
  rescale_data(series,pars,sermin,serinter);

  alldim=pars.DIM;
  pars.DIM=compdim;

  check_alloc(useries=(unsigned int**)malloc(sizeof(int*)*pars.DIM));
  check_alloc(PART=(unsigned int*)malloc(sizeof(int)*pars.DIM));
  if (SPART == NULL)
    for (i=0;i<pars.DIM;i++)
      PART[i]=2;
  else
    set_part(PART,SPART,pars);

  for (i=0;i<pars.DIM;i++) {
    check_alloc(useries[i]=(unsigned int*)malloc(sizeof(int)*pars.LENGTH));
    for (j=0;j<pars.LENGTH;j++) {
      h=(unsigned int)((series[i][j]*(double)PART[i])/serinter[i]);
      useries[i][j]=((h<PART[i])?h:(PART[i]-1));
    }
  }

  check_alloc(future=(unsigned int*)malloc(sizeof(int)*pars.LENGTH));
  if (WHICHFUTURE>0) {
    check_alloc(wcol=(char*)calloc((size_t)10,(size_t)1));
    sprintf(wcol,"%u",WHICHFUTURE);
    dfuture=(double**)get_multi_series(infile,&(pars.LENGTH),EXCLUDE,
				       &wdim,wcol,1,0L);
    for (i=0;i<pars.LENGTH-1;i++) {
      future[i] = (dfuture[0][i]>0.0);
    }
    future[pars.LENGTH-1]=1;
    free(dfuture);
  }
  else {
    for (i=0;i<pars.LENGTH;i++)
      future[i]=1;
  }
  root=make_ptree(PART[0]);

  count=0;
  check_alloc(segment=(long*)malloc(sizeof(long)*pars.LENGTH));
  for (i=0;i<pars.LENGTH;i += ODEPTH) {
    has_future=1;
    for (j=0;j<ODEPTH;j++)
      has_future &= future[i+j];
    if (has_future) {
      fill_tree(root,useries,pars,i,0,DEPTH,PART);
    }
    segment[count++]=i;
  }

  if (scrambleset)
    lscramble(segment,count);

  check_alloc(use_array=(char*)malloc(sizeof(char)*pars.LENGTH));
  for (i=0;i<count;i++) {
    n=segment[i];
    has_future=1;
    for (j=0;j<ODEPTH;j++)
      has_future &= future[n+j];
    if (has_future) {
      h=read_tree(root,useries,pars,n,0,DEPTH);
      if (h <= MAXOUT)
	use_array[i]=1;
      else 
	use_array[i]=0;
    }
    else 
      use_array[i]=2;
  }
  pars.DIM=alldim;

  fout=fopen(OUTFILE,"w");
  fprintf(fout,"%s\n",outstring);
  fprintf(fout,"#Content: ");
  for (i=0;i<pars.DIM;i++)
    fprintf(fout,"x%d ",i+1);
  fprintf(fout,"future time_index\n");
  fflush(fout);

  for (i=0;i<count;i++) {
    n=segment[i];
    if (use_array[i] == 1) {
      for (j=0;j<(ODEPTH-1);j++) {
	for (k=0;k<pars.DIM;k++)
	  fprintf(fout,"%f ",series[k][n+j]+sermin[k]);
	fprintf(fout,"1 %u\n",n+j);
      }
      for (k=0;k<pars.DIM;k++)
	fprintf(fout,"%f ",series[k][n+ODEPTH-1]+sermin[k]);
      if (i<(count-1)) {
	if (segment[i+1] == (n+ODEPTH) && use_array[i+1])
	  fprintf(fout,"1 %u\n",n+ODEPTH-1);
	else 
	  fprintf(fout,"0 %u\n",n+ODEPTH-1);
      }
      else 
	fprintf(fout,"0 %u\n",n+ODEPTH-1);
    }
  }

  fclose(fout);
}
void run(std::string tree_filename, std::string fasta_filename, std::string model_name) {
  Model Mod;                 // The model
  Counts data;               // the counts
  Parameters Par;            // the parameters
  std::vector<double> br;    // branch lengths
  double eps = 1e-8;         // The threshold for the EM algorithm.

  Parameters Parsim;         // used for simulating data.
  std::vector<double> brsim; // branch lengths of simulated data.

  std::vector<std::vector<double> > Cov;  // Covariance matrix
  std::vector<double> variances;          // The variances


  bool simulate;
  bool nonident;
  std::string parameters_filename;
  std::string covariances_filename;

  // initialize random number generator with time(0).
  random_initialize();

  parameters_filename = strip_extension(fasta_filename) + ".dat";
  covariances_filename = strip_extension(fasta_filename) + ".cov";

  // Creates the pointers to the model-specific functions.
  Mod = create_model(model_name);
  std::cout << "Model: " << Mod.name << std::endl;

  // Reads the tree.
  Tree T = read_tree(tree_filename);

  // Prints the Tree
  std::cout << "Tree:" << std::endl;
  print_tree(T);

  // Check for possible nonidentifiability issues.
  nonident = nonident_warning(T);

  // Initialize the parameters for simulation of K81 data for testing
  Parsim = create_parameters(T);

  if (fasta_filename == ":test") {      // if fasta file is :test generate random data.
    simulate = true;

    // Warn
    std::cout << "WARNING: Using simulated data " << std::endl << std::endl;

    // Generate random parameters
    random_parameters_length(T, Mod, Parsim);

    // Simulate the data
    data = random_fake_counts(T, 1000, Parsim);

    // Prints branch-lengths for future check.
    branch_lengths(Parsim, brsim);
    std::cout << "Simulated branch lengths:" << std::endl;
    print_vector(brsim);

  } else {                                  // otherwise read the data
    simulate = false;

    // Read the counts.
    std::cout << "Reading fasta file:" << std::endl;
    read_counts(T, data, fasta_filename);
    add_pseudocounts(0.01, data);
    std::cout << std::endl;
  }

  // Check whether the data and the tree match.
  if (T.nalpha != data.nalpha || T.nleaves != data.nspecies) {
    throw std::invalid_argument("The order of the sequences or their number and the phylogenetic tree do not match.");
  }

  //Par = create_parameters(T);
  //print_parameters(Par);
  //print_vector(Par.r);

  //clock_t
  long start_time, end_time;

  // Runs the EM algorithm. Par is used as initial parameters.
  // After execution, Par contains the MLE computed by the algorithm.

 // for local max over multiple iterations
  Parameters Parmax = Par;
  Model Modmax = Mod;

  float likelL = 0.0;
  float likelMax = -1000000.0;
  float timerec;
  float timemax;

  int outfiles; //whether to save output
  std::cout << "Starting the EM algorithm: " << std::endl;

  int s;
  int S = 0; //count of cases with neg branches

  int iter;
  int iterMax;

  for (int it_runs = 0; it_runs < 10; it_runs++) {
      Par = create_parameters(T);
      Mod = create_model(model_name);
      std::cout << it_runs << ", " ;

      start_time = clock();

      std::tie(likelL, iter) = EMalgorithm(T, Mod, Par, data, eps);

      end_time = clock();
      //print_parameters(Par);

      // Choses the best permutation.
      guess_permutation(T, Mod, Par);

      branch_lengths(Par, br);

      //print_vector(br);
      s = find_negative(br);
      S +=s;
      timerec = ((float)end_time - start_time) / CLOCKS_PER_SEC;

      //assign the 1st iter time value, inc ase it's the best
      if (it_runs == 0){
        timemax = timerec;
        iterMax = iter;
      }

      if (likelL > likelMax){
        Parmax = Par;
        Modmax = Mod;
        timemax = timerec;
        likelMax = likelL;
        iterMax = iter;
      }

  }


  // If parameters are not identifiable, the computation of the covariance matrix will
  // fail as the Fisher info matrix will not be invertible.
  if (!nonident) {
    // Compute the covariance matrix using observed Fisher.
    full_MLE_observed_covariance_matrix(T, Modmax, Parmax, data, Cov);
    variances.resize(Cov.size());
    for(unsigned int i=0; i < Cov.size(); i++) {
      variances[i] = Cov[i][i];
    }

    // OUTPUT Save the sigmas into a file
    //save_sigmas_to(covariances_filename, Cov);
  }

  std::cout << std::endl;
  std::cout << "Finished." << std::endl;
  std::cout << "Likelihood: " << log_likelihood(T, Parmax, data) << std::endl ;
  std::cout << "Time: " << timemax << std::endl << std::endl;
  std::cout << "negative branches: "  << S << std::endl;
  std::cout << "Iter: "  << iterMax << std::endl;

  //std::cout << "Branch lengths: " << std::endl;
  //print_vector(br);
  outfiles = 0;
  if (!nonident && outfiles) {
    std::cout << "Parameter variances: " << std::endl;
    print_vector(variances);
  }

  std::cout << "Newick Tree:" << std::endl;
  print_newick_tree(T, br);

  // if is a simulation, print the L2 distance !
  if (simulate) {
    std::cout << "L2 distance:   " << parameters_distance(Par, Parsim) << std::endl;
    std::cout << "KL divergence: " << KL_divergence(T, Par, Parsim) << std::endl;
    std::cout << std::endl;
  }

  // if it is not a simulation, store the parameters in a file !
  if (!simulate && outfiles) {
    std::fstream st;
    st.precision(15);
    st.setf(std::ios::fixed,std::ios::floatfield);
    st.open(parameters_filename.c_str(), std::ios::out);
    print_parameters(Par, st);
  }
}