Beispiel #1
0
/*
 * create a random sequence from either dna or protein input sequence
 * add sequence to sequence manager
 */
int ScrambleSeq(Tcl_Interp *interp,
		int seq_num)
{
    char *seq1 = GetSeqSequence(seq_num);
    int length = GetSeqLength(seq_num);
    int seq_id = GetSeqId(seq_num);
    char *seq2;
    char *name;
    time_t tim;
    int seed;
    char *parental_name, *child_name;
    int new_seq_num, start, end;
    static int num = 0;


    if (NULL == (seq2 = (char *)xmalloc((length+1) * sizeof(char))))
	return -1;

    memcpy(seq2, seq1, length);

    tim = time(NULL);
    seed = (int) tim;

    scramble_seq(seq2, length, seed);
    seq2[length] = '\0';

    parental_name = GetParentalSeqName(seq_num);
    child_name = GetSeqName(seq_num);

    if (NULL == (name = (char *)xmalloc((strlen(parental_name)+13) * 
					sizeof(char))))
	return -1;
    sprintf(name, "%s_x%d", parental_name, num);
    if (-1 == (new_seq_num = AddSequence(interp, -1, GetSeqLibrary(seq_num), 
					 name, seq2, GetSeqStructure(seq_num), 
					 GetSeqType(seq_num), NULL , " ")))
	return -1;
    xfree(name);

    if (strcmp(parental_name, child_name) != 0) {
	/* sub-sequence */
	/* 
	 * need to get seq num from seq_id instead of using seq_num incase
	 * AddSequence has deleted duplicate names
	 */
	start = GetSubSeqStart(GetSeqNum(seq_id));
	end = GetSubSeqEnd(GetSeqNum(seq_id));

	if (NULL == (name = (char *)xmalloc((strlen(child_name)+13) * 
					    sizeof(char))))
	    return -1;

	sprintf(name, "%s_x%d", child_name, num);
	if (-1 == (AddSubSequence(interp, GetSeqId(new_seq_num), start, end, name)))
	    return -1;
    }
    num++;
    return 0;
}
Beispiel #2
0
/*
 * complement sequence
 * add new sequence name to sequence manager
 */
int ComplementSeq(Tcl_Interp *interp,
		  int seq_num)
{
    char *seq1 = GetSeqSequence(seq_num);
    char *seq2;
    char *name;
    int length = GetSeqLength(seq_num);
    int seq_id = GetSeqId(seq_num);
    int new_seq_num, start, end;
    char *parental_name, *child_name;

    if (NULL == (seq2 = (char *)xmalloc((length+1) * sizeof(char))))
	return -1;

    memcpy(seq2, seq1, length);
    (void) complement_seq(seq2, length);
    seq2[length] = '\0';

    parental_name = GetParentalSeqName(seq_num);
    child_name = GetSeqName(seq_num);

    if (NULL == (name = (char *)xmalloc((strlen(parental_name)+3) * 
					sizeof(char))))
	return -1;
    sprintf(name, "%s_c", parental_name);
    if (-1 == (new_seq_num = AddSequence(interp, -1, GetSeqLibrary(seq_num), 
					 name, seq2, GetSeqStructure(seq_num), 
					 GetSeqType(seq_num), NULL, " ")))
	return -1;

    xfree(name);

    if (strcmp(parental_name, child_name) != 0) {
	/* sub-sequence */
	/* 
	 * need to get seq num from seq_id instead of using seq_num incase
	 * AddSequence has deleted duplicate names
	 */
	start = GetSubSeqStart(GetSeqNum(seq_id));
	end = GetSubSeqEnd(GetSeqNum(seq_id));

	if (NULL == (name = (char *)xmalloc((strlen(child_name)+3) * 
					    sizeof(char))))
	    return -1;

	sprintf(name, "%s_c", child_name);
	if (-1 == (AddSubSequence(interp, GetSeqId(new_seq_num), 
				  length - end + 1, 
				  length - start + 1, name)))
	    return -1;
    }
    return 0;
}
int init_emboss_graph_create(Tcl_Interp *interp, 
			     int seq_id, 
			     int start, 
			     int end,
			     char *filename,
			     Tcl_Obj **graph_obj,
			     int *id)
{
    int seq_num, seq_len;
    in_emboss *input;
    Tcl_DString input_params;
    text_emboss *text_data;

    e_graph *data = NULL;

    seq_num = GetSeqNum(seq_id);
    seq_len = GetSeqLength(seq_num);

    /* if the end has not been defined, set it to be the sequence length */
    if (end == -1) {
	end = seq_len;
    }
    seq_len = end - start + 1;

    if (NULL == (input = (in_emboss *)xmalloc (sizeof(in_emboss))))
	return -1;

    read_emboss_data_file(filename, &data, graph_obj, &text_data);
    if (!data) {
	verror(ERR_FATAL,"emboss", "error in reading results\n");
	return -1;
    }

    /* create inputs parameters */
    Tcl_DStringInit(&input_params);
    vTcl_DStringAppend(&input_params, "sequence %s: from %d to %d\n",
		       GetSeqName(seq_num), start, end);

    vfuncparams("%s", Tcl_DStringValue(&input_params));
    input->params = strdup(Tcl_DStringValue(&input_params)); 
    Tcl_DStringFree(&input_params);

    if (-1 == (*id = store_emboss_graph(seq_num, start, end, data, input, 
					text_data, graph_obj))) {
	verror(ERR_FATAL,"emboss", "error in saving results\n");
	return -1;
    }
    xfree(data);
    return 0;
}
Beispiel #4
0
ALPHA SeqVect::GuessAlpha() const
	{
// If at least MIN_NUCLEO_PCT of the first CHAR_COUNT non-gap
// letters belong to the nucleotide alphabet, guess nucleo.
// Otherwise amino.
	const unsigned CHAR_COUNT = 100;
	const unsigned MIN_NUCLEO_PCT = 95;

	const unsigned uSeqCount = GetSeqCount();
	if (0 == uSeqCount)
		return ALPHA_Amino;

	unsigned uSeqIndex = 0;
	unsigned uPos = 0;
	unsigned uSeqLength = GetSeqLength(0);
	unsigned uDNACount = 0;
	unsigned uRNACount = 0;
	unsigned uTotal = 0;
	const Seq *ptrSeq = &GetSeq(0);
	for (;;)
		{
		while (uPos >= uSeqLength)
			{
			++uSeqIndex;
			if (uSeqIndex >= uSeqCount)
				break;
			ptrSeq = &GetSeq(uSeqIndex);
			uSeqLength = ptrSeq->Length();
			uPos = 0;
			}
		if (uSeqIndex >= uSeqCount)
			break;
		char c = ptrSeq->at(uPos++);
		if (IsGapChar(c))
			continue;
		if (IsDNA(c))
			++uDNACount;
		if (IsRNA(c))
			++uRNACount;
		++uTotal;
		if (uTotal >= CHAR_COUNT)
			break;
		}
	if (uTotal != 0 && ((uDNACount*100)/uTotal) >= MIN_NUCLEO_PCT)
		return ALPHA_DNA;
	if (uTotal != 0 && ((uRNACount*100)/uTotal) >= MIN_NUCLEO_PCT)
		return ALPHA_RNA;
	return ALPHA_Amino;
	}
Beispiel #5
0
int init_sip_similar_spans_create(Tcl_Interp *interp, 
				  int seq_id_h,
				  int seq_id_v, 
				  int start_h,
				  int end_h, 
				  int start_v,
				  int end_v, 
				  int win_len,
				  int min_match, 
				  int *id)
{
    in_comp_spans *input = NULL;
    int *seq1_match = NULL;
    int *seq2_match = NULL;
    int *match_score = NULL;
    int n_matches;
    char *seq1, *seq2;
    int seq1_len, seq2_len;
    int same_seq;
    int max_matches = get_max_matches();
    int seq1_num, seq2_num;
    int seq1_type, seq2_type;
    int sub1_len, sub2_len;
    Tcl_DString input_params;
   
    vfuncheader("find similar spans");
    
    if (NULL == (seq1_match = (int *)xmalloc(max_matches * sizeof(int))))
	goto error;
    if (NULL == (seq2_match = (int *)xmalloc(max_matches * sizeof(int))))
	goto error;
    if (NULL == (match_score = (int *)xmalloc(max_matches * sizeof(int))))
	goto error;
    if (NULL == (input = (in_comp_spans *)xmalloc(sizeof(in_comp_spans))))
	goto error;
    
    /* get first and second sequence saved using extract_sequence */
    seq1_num = GetSeqNum(seq_id_h);
    seq2_num = GetSeqNum(seq_id_v);
    
    if (seq1_num == -1) {
	verror(ERR_WARN, "find similar spans", "horizontal sequence undefined");
	goto error;
    } else if (seq2_num == -1) {
	verror(ERR_WARN, "find similar spans", "vertical sequence undefined");
	goto error;
    }

    seq1 = GetSeqSequence(seq1_num);
    seq2 = GetSeqSequence(seq2_num);
    seq1_len = GetSeqLength(seq1_num);
    seq2_len = GetSeqLength(seq2_num);
    seq1_type = GetSeqType(seq1_num);
    seq2_type = GetSeqType(seq2_num);

    if (end_h == -1)
	end_h = seq1_len;

    if (end_v == -1)
	end_v = seq2_len;

    if (seq1_type != seq2_type) {
	verror(ERR_WARN, "find similar spans", "sequences must both be either DNA or protein");
	return TCL_OK;
    } else if (seq1_type == PROTEIN) {
	set_char_set(PROTEIN);
        set_score_matrix(get_matrix_file(PROTEIN));
    } else if (seq1_type == DNA) {
	set_char_set(DNA);
        set_score_matrix(get_matrix_file(DNA));
    }

    /* 
     * first check if seq lengths are equal, if not the seqs cannot be the
     * same
     */

    /*
     * Should check length of sub sequences only. These lengths are not
     * stored, so have to calculate them here. Not storing them in
     * seq1_len and seq2_len as I'm unsure whether subsequent functions
     * expect the length of the whole sequence. Anyway, the compare_spans
     * function recalculates the lengths of the sub sequences before doing
     * the comparison.
     */

    sub1_len = end_h - start_h + 1;
    sub2_len = end_v - start_v + 1;

    if (sub1_len == sub2_len) {
	if (strncmp(seq1 + start_h - 1, seq2 + start_v - 1, sub1_len) == 0) {
	    same_seq = 1;
	} else {
	    same_seq = 0;
	}
    } else {
	same_seq = 0;
    }
    if (!get_remove_dup() && same_seq)
	same_seq = 0;

    Compare_Spans(seq1, seq2, seq1_len, seq2_len, start_h, end_h, 
		  start_v, end_v, max_matches, same_seq, 
		  win_len, min_match, 1, 0,
		  &seq1_match, &seq2_match, &match_score, &n_matches);

    /* n_matches == -1 if malloc problem or -2 if too many matches */
    if (n_matches == -2) {
	verror(ERR_WARN, "find similar spans", "too many matches");
	goto error;
    } else if (n_matches == -1) {
	goto error;
    } else if (n_matches == 0) {
	verror(ERR_WARN, "Find similar spans", "no matches found\n"); 
	if (seq1_match)
	    xfree (seq1_match);
	if (seq2_match)
	    xfree (seq2_match);
	if (match_score)
	    xfree(match_score);
	if (input)
	    xfree(input);
	return -1;
    }

    /* create inputs parameters */
    Tcl_DStringInit(&input_params);
    vTcl_DStringAppend(&input_params, "horizontal %s: %s \nvertical %s: %s\n"
	    "window length %d min match %d number of matches %d", 
	    GetSeqLibraryName(seq1_num), 
	    GetSeqName(seq1_num), 
	    GetSeqLibraryName(seq2_num), 
	    GetSeqName(seq2_num), 
	    win_len, min_match, n_matches);
    vfuncparams("%s", Tcl_DStringValue(&input_params));
    input->params = strdup(Tcl_DStringValue(&input_params)); 
    Tcl_DStringFree(&input_params);

    if (-1 == (*id = store_sip_similar_spans(seq1_num, seq2_num, win_len,
					     min_match, start_h, end_h, 
					     start_v, end_v,
					     seq1_match, seq2_match, 
					     match_score, n_matches,
					     input))) {
	goto error;
    }
    
    if (seq1_match)
	xfree (seq1_match);
    if (seq2_match)
	xfree (seq2_match);
    if (match_score)
	xfree(match_score);
    return 0;
    
 error:
    verror(ERR_WARN, "find similar spans", "failure in find similar spans");
    if (seq1_match)
	xfree (seq1_match);
    if (seq2_match)
	xfree (seq2_match);
    if (match_score)
	xfree(match_score);
    if (input)
      xfree(input);
    return -1;
}
Beispiel #6
0
void similar_spans_text_func(void *obj)
{
    seq_result *result = (seq_result *) obj;
    d_plot *data = result->data;
    int n_pts = data->n_pts;
    int i;
    char *seq1, *seq2;
    int seq1_len, seq2_len;
    int max_str;
    char *r_seq1, *r_seq2;
    int r;
    int num_spaces;
    int seq_num_h, seq_num_v;
    int seq1_type;

    seq_num_h = GetSeqNum(result->seq_id[HORIZONTAL]);
    seq_num_v = GetSeqNum(result->seq_id[VERTICAL]);

    seq1 = GetSeqSequence(seq_num_h);
    seq1_len = GetSeqLength(seq_num_h);
    seq2 = GetSeqSequence(seq_num_v);
    seq2_len = GetSeqLength(seq_num_v);

    if (seq1_len > seq2_len)
	max_str = seq1_len;
    else
	max_str = seq2_len;

    if (seq1_len >= data->win_len) {
	if (NULL == (r_seq1 = (char *)xmalloc(seq1_len + 1)))
	    return;
    } else {
	if (NULL == (r_seq1 = (char *)xmalloc(data->win_len + 1)))
	    return;
    }
    if (seq2_len >= data->win_len) {
	if (NULL == (r_seq2 = (char *)xmalloc(seq2_len + 1)))
	    return;
    } else {
	if (NULL == (r_seq2 = (char *)xmalloc(data->win_len + 1)))
	    return;
    }

    for (i = 0; i < n_pts; i++) {
	UpdateTextOutput();
	vmessage("Positions %10d h %10d v and score %10d\n", 
	       data->p_array[i].x, (int)data->p_array[i].y, 
		 data->p_array[i].score);	
	if (data->p_array[i].x <= 0) {
	    num_spaces = abs(data->p_array[i].x) + 1;
	    memset(r_seq1, ' ', num_spaces);
	    r_seq1[num_spaces] = '\0';
	    strncat(r_seq1, seq1, data->win_len - num_spaces);
	} 
	if (data->p_array[i].y <= 0) {
	    num_spaces = abs(data->p_array[i].y) + 1;
	    memset(r_seq2, ' ', num_spaces);
	    r_seq2[num_spaces] = '\0';
	    strncat(r_seq2, seq2, data->win_len - num_spaces);
	} 
	if (data->p_array[i].x > 0) {
	    strncpy(r_seq1, &seq1[data->p_array[i].x-1], data->win_len);
	}
	if (data->p_array[i].y > 0) {
	    strncpy(r_seq2, &seq2[(int)data->p_array[i].y-1], data->win_len);
	}
	r_seq1[data->win_len] = '\0';
	r_seq2[data->win_len] = '\0';
	/*yy*/
	seq1_type = GetSeqType(seq_num_h);
	r = spin_list_alignment(r_seq1, r_seq2, "H", "V", data->p_array[i].x, 
			   data->p_array[i].y, "", seq1_type);
#ifdef DEBUG
	printf("i %d \n%s\n%s\n", i, r_seq1, r_seq2);
#endif
	r_seq1[0] = '\0';
	r_seq2[0] = '\0';
    }
    xfree(r_seq1);
    xfree(r_seq2);
}
int init_nip_string_search_create(char *strand_sym,
				  float match,
				  char *string,
				  int use_iub_code,
				  int start,
				  int end,
				  int seq_id,
				  int *id)
{
    in_string_search *input;
    char *seq;
    int seq_len;
    int seq_num;
    int string_length;
    int max_matches, min_match;
    int n_matches;
    int *pos;
    int *score;
    Tcl_DString input_params;
    char strand[8];

    vfuncheader("string search");

    if (NULL == (input = (in_string_search *)xmalloc
		 (sizeof(in_string_search))))
	return -1;

    seq_num = GetSeqNum(seq_id);
    seq = GetSeqSequence(seq_num);
    seq_len = GetSeqLength(seq_num);

    /* if the end has not been defined, set it to be the sequence length */
    if (end == -1) {
	end = seq_len;
    }

    seq_len = end - start + 1;

    max_matches = seq_len;
    string_length = strlen(string);

    if (NULL == (pos = (int *)xmalloc((max_matches + 1) * sizeof(int))))
	return -1;
    
    if (NULL == (score = (int *)xmalloc((max_matches + 1) * sizeof(int))))
	return -1;

    /* convert percentage mis-matches into min matches */
    min_match = (int) (ceil(string_length * match / 100)); 

    /* complement */
    if (strcmp(strand_sym, "-") == 0) {
	complement_seq(string, string_length);
    }
    
#ifdef DEBUG
    printf("min_match %d match %f string %d\n", min_match, match, string_length);
#endif

    n_matches = iubc_inexact_match(&seq[start-1], seq_len, string, 
				   string_length, min_match, use_iub_code, 
				   pos, score, max_matches);

    if (n_matches <= 0) {
	vmessage("String search: no matches found\n");
	xfree(input);
	xfree(pos);
	xfree(score);
	return -1;
    }

    input->string = strdup(string);
    /* create inputs parameters */
    Tcl_DStringInit(&input_params);
    if (strcmp(strand_sym, "+") == 0) {
        strcpy(strand, "forward");
    } else {
        strcpy(strand, "reverse");
    }

    {
      char tmp[10];
      if (use_iub_code)
	strcpy(tmp, "iub");
      else
	strcpy(tmp, "literal");
      vTcl_DStringAppend(&input_params, "sequence %s: from %d to %d\n"
			 "strand %s\nuse %s code\nminimum percent match %f\nstring %s\n",
			 GetSeqName(seq_num), start, end,
			 strand, tmp, match, string);
    }
    vfuncparams("%s", Tcl_DStringValue(&input_params));
    input->params = strdup(Tcl_DStringValue(&input_params)); 
    Tcl_DStringFree(&input_params);

    if (-1 == (*id = store_string_search(seq_num, input, start, end, pos, 
					 score, n_matches, string_length))){
	verror(ERR_FATAL,"string search", "error in saving matches\n");
	return -1;
    }

    xfree(pos);
    xfree(score);
    return 0;
}
int init_emboss_dot_create(Tcl_Interp *interp, 
			   int seq_id_h, 
			   int start_h, 
			   int end_h,
			   int seq_id_v, 
			   int start_v, 
			   int end_v,
			   char *filename,
			   Tcl_Obj **graph_obj,
			   int *id)
{
    int seq_num_h, seq_len_h, seq_num_v, seq_len_v;
    in_emboss *input;
    Tcl_DString input_params;
    int *seq1_match = NULL;
    int *seq2_match = NULL; 
    int *len_match = NULL;
    e_graph *data = NULL;
    text_emboss *text_data;

    seq_num_h = GetSeqNum(seq_id_h);
    seq_num_v = GetSeqNum(seq_id_v);

    seq_len_h = GetSeqLength(seq_num_h);
    seq_len_v = GetSeqLength(seq_num_v);

    /* if the end has not been defined, set it to be the sequence length */
   if (end_h == -1)
	end_h = seq_len_h;

    if (end_v == -1)
	end_v = seq_len_v;

    seq_len_h = end_h - start_h + 1;
    seq_len_v = end_v - start_v + 1;

    read_emboss_data_file(filename, &data, graph_obj, &text_data);
    if (!data) {
	verror(ERR_FATAL,"emboss", "error in reading results\n");
	return -1;
    }

    if (NULL == (seq1_match = (int *)xmalloc((data->n_data_obj+1) * sizeof(int))))
	return -1;
    if (NULL == (seq2_match = (int *)xmalloc((data->n_data_obj+1) * sizeof(int))))
	return -1;
    if (NULL == (len_match = (int *)xmalloc((data->n_data_obj+1) * sizeof(int))))
	return -1;

    if (NULL == (input = (in_emboss *)xmalloc (sizeof(in_emboss))))
	return -1;

    /* create inputs parameters */
    Tcl_DStringInit(&input_params);
    vTcl_DStringAppend(&input_params, "sequence %s: from %d to %d\nsequence %s: from %d to %d\n",
		       GetSeqName(seq_num_h), start_h, end_h,
		       GetSeqName(seq_num_v), start_v, end_v);

    vfuncparams("%s", Tcl_DStringValue(&input_params));
    input->params = strdup(Tcl_DStringValue(&input_params)); 
    Tcl_DStringFree(&input_params);

    if (-1 == (*id = store_emboss_dot(seq_num_h, start_h, end_h,
				      seq_num_v, start_v, end_v, 
				      data, input, text_data, graph_obj))) {
	verror(ERR_FATAL,"emboss", "error in saving results\n");
	return -1;
    }

    xfree(seq1_match);
    xfree(seq2_match);
    xfree(len_match);
    xfree(data);
    return 0;
}
int init_emboss_stick_create(Tcl_Interp *interp, 
			     int seq_id, 
			     int start, 
			     int end,
			     char *filename,
			     int *id)
{
    FILE *fp;
    int i;
    int *pos = NULL;
    int *res = NULL;
    int seq_num, seq_len;
    in_emboss *input;
    Tcl_DString input_params;
    char dummy1[100];
    char dummy2;

    seq_num = GetSeqNum(seq_id);
    seq_len = GetSeqLength(seq_num);

    /* if the end has not been defined, set it to be the sequence length */
    if (end == -1) {
	end = seq_len;
    }
    seq_len = end - start + 1;

    if (NULL == (pos = (int *)xmalloc((seq_len+1) * sizeof(int))))
	return -1;
    if (NULL == (res = (int *)xmalloc((seq_len+1) * sizeof(res))))
	return -1;
    if (NULL == (input = (in_emboss *)xmalloc (sizeof(in_emboss))))
	return -1;

    if (NULL == (fp = fopen(filename, "r"))) {
	printf("unable to open file\n");
	return -1;
    }

    if (fgetc(fp) == 'P') {
	printf("first char\n");
	fgets(dummy1, 100, fp);
	fgets(dummy1, 100, fp);
	fgets(dummy1, 100, fp);
    } else {
	rewind(fp);
    }

    i = 0;
    while (fscanf(fp, "%d %c %d\n", &pos[i], &dummy2, &res[i]) != EOF) { 
#ifdef DEBUG
	printf("pos %d result %d\n", pos[i], res[i]);
#endif
	i++;
    }
    fclose (fp);

    printf("num points %d\n", i);

    /* create inputs parameters */
    Tcl_DStringInit(&input_params);
    vTcl_DStringAppend(&input_params, "sequence %s: from %d to %d\n",
		       GetSeqName(seq_num), start, end);

    vfuncparams("%s", Tcl_DStringValue(&input_params));
    input->params = strdup(Tcl_DStringValue(&input_params)); 
    Tcl_DStringFree(&input_params);

#ifdef TODO
    if (-1 == (*id = store_emboss_graph(seq_num, start, end, pos, score, 
					i, input))) {
	verror(ERR_FATAL,"emboss", "error in saving results\n");
	return -1;
    }
#endif
    if (pos)
	xfree(pos);
    if (res)
	xfree(res);

    return 0;
}
void sip_rescan_matches(Tcl_Interp *interp,
			seq_result *s_result,
			int id,
			int min_score) 
{
    int i, j;
    double coords[2];
    int index;
    char *seq1, *seq2;
    int seq1_len, seq2_len;
    int score;
    int start;
    int x, y;
    int index1, index2;
    double wx0, wy0, wx1, wy1;
    Tcl_Obj *graph_obj = (Tcl_Obj *) s_result->data;
    element *e = s_result->e;
    int result_id = s_result->id;
    in_sim_spans *input = s_result->input;
    double invert_y;
    char cmd[1024];
    plot_data *result;
    Graph *graph;
    double m, c, pos;
    d_box bbox;

    result = find_plot_data(e, result_id);

    graph = Tcl_GetGraphFromObj(graph_obj);

    if (result->hidden) {
	return;
    }

    index1 = GetSeqNum(s_result->seq_id[HORIZONTAL]);
    index2 = GetSeqNum(s_result->seq_id[VERTICAL]);

    /* if either index is -1; the corresponding seq must have been deleted */
    if ((index1 == -1) || (index2 == -1)) {
	return;
    }
    seq1 = GetSeqSequence(index1);
    seq2 = GetSeqSequence(index2);
    seq1_len = GetSeqLength(index1);
    seq2_len = GetSeqLength(index2);
    
#if 0
    Tcl_GetCommandInfo(interp, output->raster_win, &info);
    raster = (Tk_Raster*)info.clientData;
    opts[0] = "-fg";
    opts[1] = "purple";
    opts[2] = NULL;
    index = CreateDrawEnviron(interp, raster, 2, opts);

    SetDrawEnviron(output->interp, raster, index);

    RasterGetWorldScroll(raster, &wx0, &wy0, &wx1, &wy1);
#endif

    start = (int)(input->win_length / 2);

    /* printing */
    for (i = 0; i < graph->p_arrays[0].n_pts; i++) {

	x = graph->p_arrays[0].p_array[i].x - start;
	y = graph->p_arrays[0].p_array[i].y - start;

	for (j = 0; j < input->win_length; j++, x++, y++) {
	    
	    if ((x < 1) || (y < 1) || (x > seq1_len) || (y > seq2_len)) {
		continue;
	    }
	    score = score_matrix[char_lookup[seq1[x - 1]]]
	      [char_lookup[seq2[y - 1]]];

	    if (score >= min_score) {
		/* printf("j %d x %d y %d score %d \n", j, x, y, score); */

		printf("x %d y %d %d %d\n", x, y, score, min_score);

		/*
		coords[0] =  x;
		coords[1] =  (int)wy1 - y + wy0;
		*/
		invert_y = graph->dim.y1 - y + graph->dim.y0;

		sprintf(cmd, "%s create line %d %f %d %f -fill purple -width %d -tag rescan", e->win, x, invert_y,
			x+1, invert_y+1, result->line_width);

		if (TCL_ERROR == Tcl_Eval(interp, cmd))
		    printf("ERROR %s\n", interp->result);

		/* RasterDrawPoints(raster, coords, 1); */
		
	    }
	}
    }
    /*    tk_RasterRefresh(raster); */

    printf("dim %f %f\n", graph->dim.y1, graph->dim.y0);

    sprintf(cmd, "%s create line %d %f %d %f -fill pink -width 10 -tag rescan", e->win, 500, graph->dim.y1 - 500+ graph->dim.y0, 500, graph->dim.y1 - 500+ graph->dim.y0);
    Tcl_Eval(interp, cmd);
 
    bbox = scale_box(e);

    /* use the data min and max so all graphs are to their own scale */
    if (e->orientation & HORIZONTAL) {
	m = (e->world->visible->y2 - e->world->visible->y1) /
	    (e->world->total->y2 - e->world->total->y1);
	
	c = e->world->visible->y2 - (m * e->world->total->y2);
	
	bbox.y1 = m * graph->dim.y0 + c;
	bbox.y2 = m * graph->dim.y1 + c;
    } 
    if (e->orientation & VERTICAL) {
	m = (e->world->visible->x2 - e->world->visible->x1) /
	    (e->world->total->x2 - e->world->total->x1);
	
	c = e->world->visible->x2 - (m * e->world->total->x2);
	
	/* note that graph->dim never changes orientation */
	if (!(e->orientation & HORIZONTAL)) {
	    /* vertical only */
	    bbox.x1 = m * graph->dim.y0 + c;
	    bbox.x2 = m * graph->dim.y1 + c;
	} else {
	    /* dot plot */
	    bbox.x1 = m * graph->dim.x0 + c;
	    bbox.x2 = m * graph->dim.x1 + c;
	}
    }

    {
    double x_origin, y_origin;
    double sf_x, sf_y;
    int p_x1, p_y1, p_x2, p_y2;
    int i;
    double w_x1, w_x2, w_y1, w_y2;

    /* initialise world coords */
    w_x1 = bbox.x1;
    w_x2 = bbox.x2;
    w_y1 = bbox.y1;
    w_y2 = bbox.y2;

    p_x1 = e->pixel->x;
    p_x2 = e->pixel->x + e->pixel->width;
    p_y1 = (double)e->pixel->y;
    p_y2 = (double)e->pixel->y + e->pixel->height;

    if (e->orientation & HORIZONTAL) {
	p_x1 = e->c->column[e->column_index]->pixel->x;
	p_x2 = e->c->column[e->column_index]->pixel->x + e->c->column[e->column_index]->pixel->width;
    }

    if (e->orientation & VERTICAL) {
	p_y1 = e->c->row[e->row_index]->pixel->y;
	p_y2 = e->c->row[e->row_index]->pixel->y + e->c->row[e->row_index]->pixel->height;
    }
     
    x_origin = calc_zoom_origin(w_x1, p_x1, w_x2, p_x2);
    sf_x = calc_zoom_sf((double)p_x1, w_x1, p_x2, w_x2);
    y_origin = calc_zoom_origin(w_y1, p_y1, w_y2, p_y2);
    sf_y = calc_zoom_sf(p_y1, w_y1, p_y2, w_y2);
    sprintf(cmd, "%s scale rescan %f %f %f %f \n", 
	    e->win, x_origin, y_origin, sf_x, sf_y);	

    printf("scale %s\n", cmd);
    Tcl_Eval(interp, cmd);
    }
 
    /*
    e->scrollregion_func(interp, e, e->world->total, 
			 e->c->column[e->column_index]->pixel,
			 e->c->row[e->row_index]->pixel);    
    */
}
void SipRescanMatches(Tcl_Interp *interp,
		      seq_result *s_result,
		      int id,
		      int min_score) 
{
    int i, j;
    double coords[2];
    int index;
    char *seq1, *seq2;
    int seq1_len, seq2_len;
    int score;
    int start;
    int x, y;
    int index1, index2;
    double wx0, wy0, wx1, wy1;

    if (output->hidden) {
	return;
    }

    index1 = GetSeqNum(s_result->seq_id[HORIZONTAL]);
    index2 = GetSeqNum(s_result->seq_id[VERTICAL]);

    /* if either index is -1; the corresponding seq must have been deleted */
    if ((index1 == -1) || (index2 == -1)) {
	return;
    }
    seq1 = GetSeqSequence(index1);
    seq2 = GetSeqSequence(index2);
    seq1_len = GetSeqLength(index1);
    seq2_len = GetSeqLength(index2);
    

    


    Tcl_GetCommandInfo(interp, output->raster_win, &info);
    raster = (Tk_Raster*)info.clientData;
    opts[0] = "-fg";
    opts[1] = "purple";
    opts[2] = NULL;
    index = CreateDrawEnviron(interp, raster, 2, opts);

    SetDrawEnviron(output->interp, raster, index);

    RasterGetWorldScroll(raster, &wx0, &wy0, &wx1, &wy1);

    start = (int)(data->win_len / 2);

    /* printing */
    for (i = 0; i < num_pts; i++) {

	x = data->p_array[i].x - start;
	y = data->p_array[i].y - start;

	for (j = 0; j < data->win_len; j++, x++, y++) {
	    
	    if ((x < 1) || (y < 1) || (x > seq1_len) || (y > seq2_len)) {
		continue;
	    }
	    score = score_matrix[char_lookup[seq1[x - 1]]]
	      [char_lookup[seq2[y - 1]]];
    
	    if (score >= min_score) {
		/* printf("j %d x %d y %d score %d \n", j, x, y, score); */
		coords[0] =  x;
		coords[1] =  (int)wy1 - y + wy0;
		RasterDrawPoints(raster, coords, 1);
	    }
	}
    }
    tk_RasterRefresh(raster);
}
int init_splice_search_create(int seq_id,
			      int start,
			      int end,
			      char *donor,
			      char *acceptor,
			      int *id) /* out */
{
    in_splice *input1;
    in_splice *input2;
    in_splice *input3;
    char *seq;
    int seq_len;
    int seq_num;
    SpliceResults *splice_result;
    Tcl_DString input_params;
    int irs;

    vfuncheader("splice search");
    set_char_set(DNA);

    if (NULL == (input1 = (in_splice *)xmalloc (sizeof(in_splice))))
	return -1;

    if (NULL == (input2 = (in_splice *)xmalloc (sizeof(in_splice))))
	return -1;

    if (NULL == (input3 = (in_splice *)xmalloc (sizeof(in_splice))))
	return -1;

    if (NULL == (splice_result = (SpliceResults *)xmalloc 
		 (sizeof(SpliceResults))))
	return -1;

    seq_num = GetSeqNum(seq_id);
    seq = GetSeqSequence(seq_num);
    seq_len = GetSeqLength(seq_num);

    /* if the end has not been defined, set it to be the sequence length */
    if (end == -1) {
	end = seq_len;
    }

    irs = splice_search(seq, seq_len, start, end, donor, acceptor, 
			splice_result);
    
    if (irs == -1) {
      xfree(splice_result);
      xfree(input1);
      xfree(input2);
      xfree(input3);
      verror(ERR_WARN, "splice search",
	     "error in splice search (maybe none found)");
      return -1;
    }

    if (splice_result->ied_f1->number_of_res == 0 &&
	splice_result->ied_f2->number_of_res == 0 &&
	splice_result->ied_f3->number_of_res == 0 &&
	splice_result->eia_f1->number_of_res == 0 &&
	splice_result->eia_f2->number_of_res == 0 &&
	splice_result->eia_f3->number_of_res == 0) {
	verror(ERR_WARN, "splice search", "no matches found");
	xfree(splice_result);
	xfree(input1);
	xfree(input2);
	xfree(input3);
	return -1;
    }

    /* create inputs parameters */
    Tcl_DStringInit(&input_params);
    vTcl_DStringAppend(&input_params, "sequence %s: from %d to %d\n"
		       "donor weight matrix %s\nacceptor weight matrix %s\n",
		       GetSeqName(seq_num), start, end, donor, acceptor);

    vfuncparams("%s", Tcl_DStringValue(&input_params));
    input1->params = strdup(Tcl_DStringValue(&input_params)); 
    input2->params = strdup(Tcl_DStringValue(&input_params)); 
    input3->params = strdup(Tcl_DStringValue(&input_params)); 
    Tcl_DStringFree(&input_params);

    if (-1 == (id[0] = StoreSpliceSearch(seq_num, splice_result->ied_f1,
					 splice_result->eia_f1, input1, 
					 start, end, 1))){
	verror(ERR_FATAL,"nip splice search", "error in saving matches\n");
	return -1;
    }

    if (-1 == (id[1] = StoreSpliceSearch(seq_num, splice_result->ied_f2, 
					 splice_result->eia_f2, input2, 
					 start, end, 2))){
	verror(ERR_FATAL,"nip splice search", "error in saving matches\n");
	return -1;
    }

    if (-1 == (id[2] = StoreSpliceSearch(seq_num, splice_result->ied_f3,
					 splice_result->eia_f3, input3, 
					 start, end, 3))){
	verror(ERR_FATAL,"nip splice search", "error in saving matches\n");
	return -1;
    }
    xfree(splice_result);
    return 0;
}
Beispiel #13
0
/*
 * display the expected and observed scores
 */
int tcl_sip_find_probs(ClientData clientData, 
		       Tcl_Interp *interp, 
		       int argc, 
		       char *argv[]) 
{    
    find_prob_arg args;
    char *seq1;
    char *seq2;
    int seq1_len, seq2_len;
    int seq1_type, seq2_type;
    int seq1_num, seq2_num;

    cli_args a[] = {
	{"-win_len",  ARG_INT, 1, NULL, offsetof(find_prob_arg, win_len)},
	{"-seq_id_h", ARG_INT, 1, NULL, offsetof(find_prob_arg, seq_id_h)},
	{"-seq_id_v", ARG_INT, 1, NULL, offsetof(find_prob_arg, seq_id_v)},
	{"-start_h",  ARG_INT, 1, "1",  offsetof(find_prob_arg, start_h)},
	{"-end_h",    ARG_INT, 1, NULL, offsetof(find_prob_arg, end_h)},
	{"-start_v",  ARG_INT, 1, "1",  offsetof(find_prob_arg, start_v)},
	{"-end_v",    ARG_INT, 1, NULL, offsetof(find_prob_arg, end_v)},
	{"-type_h",   ARG_INT, 1, "-1", offsetof(find_prob_arg, type_h)},
	{"-type_v",   ARG_INT, 1, "-1", offsetof(find_prob_arg, type_v)},
	{"-use_av_comp", ARG_INT, 1, "0", offsetof(find_prob_arg, use_av_comp)},
	{NULL,       0,       0, NULL, 0}
    };
    if (-1 == parse_args(a, &args, argc, argv))
	return TCL_ERROR;

    /* get first and second sequence saved using extract_sequence */
    seq1_num = GetSeqNum(args.seq_id_h);
    seq2_num = GetSeqNum(args.seq_id_v);

    if (seq1_num == -1) {
	verror(ERR_WARN, "find probabilities", "horizontal sequence undefined");
	return TCL_OK;
    } else if (seq2_num == -1) {
	verror(ERR_WARN, "find probabilities", "vertical sequence undefined");
	return TCL_OK;
    }

    seq1 = GetSeqSequence(seq1_num);
    seq1_len = GetSeqLength(seq1_num);
    seq2 = GetSeqSequence(seq2_num);
    seq2_len = GetSeqLength(seq2_num);

    if (args.type_h == -1) 
	seq1_type = GetSeqType(seq1_num);
    else 
	seq1_type = args.type_h;

    if (args.type_v == -1) 
	seq2_type = GetSeqType(seq2_num);
    else
	seq2_type = args.type_v;

    if (args.use_av_comp) {
	seq1_type = PROTEIN;
	seq2_type = PROTEIN;
    }

    if (seq1_type != seq2_type) {
	verror(ERR_WARN, "find score", "sequences must both be either DNA or protein");
	return TCL_OK;
    } else if (seq1_type == PROTEIN) {
	set_char_set(PROTEIN);
        set_score_matrix(get_matrix_file(PROTEIN));
    } else if (seq1_type == DNA) {
	set_char_set(DNA);
        set_score_matrix(get_matrix_file(DNA));
    }

    FindProbs(seq1, seq2, args.start_h, args.end_h, args.start_v, args.end_v, 
	      args.win_len, seq1_type, args.use_av_comp);
   
    return TCL_OK;
}
Beispiel #14
0
/*
 * finds the expected score to give num_matches for a given window length
 */
int tcl_sip_find_score(ClientData clientData, 
		       Tcl_Interp *interp, 
		       int argc, 
		       char *argv[]) 
{    
    find_score_arg args;
    char *seq1;
    char *seq2;
    int seq1_len, seq2_len;
    int seq1_type, seq2_type;
    int seq1_num, seq2_num;
    int score;

    cli_args a[] = {
	{"-win_len",     ARG_INT, 1, NULL, offsetof(find_score_arg, win_len)},
	{"-num_matches", ARG_INT, 1, NULL, 
	     offsetof(find_score_arg, num_matches)},
	{"-seq_id_h", ARG_INT, 1, NULL, offsetof(find_score_arg, seq_id_h)},
	{"-seq_id_v", ARG_INT, 1, NULL, offsetof(find_score_arg, seq_id_v)},
	{"-start_h",  ARG_INT, 1, "1",  offsetof(find_score_arg, start_h)},
	{"-end_h",    ARG_INT, 1, NULL, offsetof(find_score_arg, end_h)},
	{"-start_v",  ARG_INT, 1, "1",  offsetof(find_score_arg, start_v)},
	{"-end_v",    ARG_INT, 1, NULL, offsetof(find_score_arg, end_v)},
	{"-use_av_comp", ARG_INT, 1, "0", offsetof(find_score_arg, use_av_comp)},
	{NULL,       0,       0, NULL, 0}
    };
    if (-1 == parse_args(a, &args, argc, argv))
	return TCL_ERROR;

    seq1_num = GetSeqNum(args.seq_id_h);
    seq2_num = GetSeqNum(args.seq_id_v);

    seq1 = GetSeqSequence(seq1_num);
    seq2 = GetSeqSequence(seq2_num);
    seq1_type = GetSeqType(seq1_num);
    seq2_type = GetSeqType(seq2_num);
    seq1_len = GetSeqLength(seq1_num);
    seq2_len = GetSeqLength(seq2_num);

    if (args.start_h < 1)
	args.start_h = 1;

    if (args.end_h > seq1_len)
	args.end_h = seq1_len;

    if (args.start_v < 1)
	args.start_v = 1;

    if (args.end_v > seq2_len)
	args.end_v = seq2_len;

    seq1_len = args.end_h - args.start_h + 1;
    seq2_len = args.end_v - args.start_v + 1;

    if (args.use_av_comp) {
	seq1_len = seq1_len / 3;
	seq2_len = seq2_len / 3;
	seq1_type = PROTEIN;
	seq2_type = PROTEIN;
    }

    if (seq1_type != seq2_type) {
	verror(ERR_WARN, "find score", "sequences must both be either DNA or protein");
	return TCL_OK;
    } else if (seq1_type == PROTEIN) {
	set_char_set(PROTEIN);
        set_score_matrix(get_matrix_file(PROTEIN));
    } else if (seq1_type == DNA) {
	set_char_set(DNA);
        set_score_matrix(get_matrix_file(DNA));
    }

    score = FindScore(seq1_len, seq2_len, args.win_len, args.num_matches);
    vTcl_SetResult(interp, "%d", score);
   
    return TCL_OK;
}
int init_nip_start_codons_create(int seq_id,
				 int start,
				 int end,
				 int strand_sym,
				 Tcl_Obj **graph_obj,
				 int *id)
{
    in_s_codon *input1;
    in_s_codon *input2;
    in_s_codon *input3;
    char *seq;
    int seq_len;
    int sequence_type;
    int seq_num;
    s_codon_res *start_codon;
    int type = START_CODON;
    Tcl_DString input_params;
    char strand[8];

    vfuncheader("plot start codons");

    if (NULL == (input1 = (in_s_codon *)xmalloc
		 (sizeof(in_s_codon))))
	return -1;
    if (NULL == (input2 = (in_s_codon *)xmalloc
		 (sizeof(in_s_codon))))
	return -1;
    if (NULL == (input3 = (in_s_codon *)xmalloc
		 (sizeof(in_s_codon))))
	return -1;

    if (NULL == (start_codon = (s_codon_res *)xmalloc(sizeof(s_codon_res))))
	return -1;

    seq_num = GetSeqNum(seq_id);
    seq = GetSeqSequence(seq_num);
    seq_len = GetSeqLength(seq_num);
    sequence_type = GetSeqStructure(seq_num);

    /* if the end has not been defined, set it to be the sequence length */
    if (end == -1) {
	end = seq_len;
    }
    nip_start_codons(seq, sequence_type, start, end, strand_sym, start_codon);

    /* create inputs parameters */
    Tcl_DStringInit(&input_params);

    if (strand_sym & TOP_S) {
        strcpy(strand, "forward");
    } else if (strand_sym & BOTTOM_S) {
        strcpy(strand, "reverse");
    } else {
        strcpy(strand, "both");
    }
    vTcl_DStringAppend(&input_params, "sequence %s: from %d to %d\n"
		       "strand %s\n",
		       GetSeqName(seq_num), start, end, strand);

    vfuncparams("%s", Tcl_DStringValue(&input_params));
    input1->params = strdup(Tcl_DStringValue(&input_params)); 
    input2->params = strdup(Tcl_DStringValue(&input_params)); 
    input3->params = strdup(Tcl_DStringValue(&input_params)); 
    Tcl_DStringFree(&input_params);

    if (start_codon->n_match1 > 0) {
	if (-1 == (id[0] = store_stop_codons(seq_num, input1, start, end,
					     start_codon->match1, 
					     start_codon->n_match1,
					     1, type, &graph_obj[0]))){
	    verror(ERR_FATAL,"nip start codons", "error in saving matches\n");
	    return -1;
	}
    } else {
	id[0] = -1;
	free(input1->params);
	xfree(input1);
	xfree(start_codon->match1);
	verror(ERR_WARN, "nip start codons", "no matches found for frame 1");
    }
    if (start_codon->n_match2 > 0) {
	if (-1 == (id[1] = store_stop_codons(seq_num, input2, start, end,
					     start_codon->match2, 
					     start_codon->n_match2,
					     2, type, &graph_obj[1]))){
	    verror(ERR_FATAL,"nip start codons", "error in saving matches\n");
	    return -1;
	}
    } else {
	id[1] = -1;
	free(input2->params);
	xfree(input2);
	xfree(start_codon->match2);
	verror(ERR_WARN, "nip start codons", "no matches found for frame 2");
    }
    if (start_codon->n_match3 > 0) {
	if (-1 == (id[2] = store_stop_codons(seq_num, input3, start, end,
					     start_codon->match3, 
					     start_codon->n_match3,
					     3, type, &graph_obj[2]))){
	    verror(ERR_FATAL,"nip start codons", "error in saving matches\n");
	    return -1;
	}
    } else {
	id[2] = -1;
	free(input3->params);
	xfree(input3);
	xfree(start_codon->match3);
	verror(ERR_WARN, "nip start codons", "no matches found for frame 3");
    }
    xfree(start_codon);
    return 0;
}