Example #1
0
/*! \brief Expand the data structures for L and U during the factorization.
 * 
 * <pre>
 * Return value:   0 - successful return
 *               > 0 - number of bytes allocated when run out of space
 * </pre>
 */
int
cLUMemXpand(int jcol,
	   int next,          /* number of elements currently in the factors */
	   MemType mem_type,  /* which type of memory to expand  */
	   int *maxlen,       /* modified - maximum length of a data structure */
	   GlobalLU_t *Glu    /* modified - global LU data structures */
	   )
{
    void   *new_mem;
    
#ifdef DEBUG    
    printf("cLUMemXpand(): jcol %d, next %d, maxlen %d, MemType %d\n",
	   jcol, next, *maxlen, mem_type);
#endif    

    if (mem_type == USUB) 
    	new_mem = cexpand(maxlen, mem_type, next, 1, Glu);
    else
	new_mem = cexpand(maxlen, mem_type, next, 0, Glu);
    
    if ( !new_mem ) {
	int    nzlmax  = Glu->nzlmax;
	int    nzumax  = Glu->nzumax;
	int    nzlumax = Glu->nzlumax;
    	fprintf(stderr, "Can't expand MemType %d: jcol %d\n", mem_type, jcol);
    	return (cmemory_usage(nzlmax, nzumax, nzlumax, Glu->n) + Glu->n);
    }

    switch ( mem_type ) {
      case LUSUP:
	Glu->lusup   = (complex *) new_mem;
	Glu->nzlumax = *maxlen;
	break;
      case UCOL:
	Glu->ucol   = (complex *) new_mem;
	Glu->nzumax = *maxlen;
	break;
      case LSUB:
	Glu->lsub   = (int *) new_mem;
	Glu->nzlmax = *maxlen;
	break;
      case USUB:
	Glu->usub   = (int *) new_mem;
	Glu->nzumax = *maxlen;
	break;
    }
    
    return 0;
    
}
Example #2
0
/*
 *  Print out a string onto a stream, changing unprintables into
 *  termcap printables.
 */
int
cpr(FILE *stream, char *string)
{
	register char *ret;
	if (string != NULL) {
		ret = cexpand(string);
		(void) fprintf(stream, "%s", ret);
		return (strlen(ret));
	} else
		return (0);
}
Example #3
0
/*
 * Produces an alignment buffer between sequence 'si' (in 'dir' direction)
 * and the contig at position 'pos', tolerance 'tol' and allowing for maximum
 * padding of 'maxpads'.
 *
 * Returns NULL for failure, otherwise an alloced buffer containing
 * the alignment edits.
 * 'ierr' is set to contain an error code. 0 means OK, 1 means fatal error,
 * 2 means failed mismatch score, 3 means no overlap.
 */
align_info *assemble_align(GapIO *io, SeqInfo *si, consen_info *ci,
			   int contig, int *pos, int dir, int tol,
			   int display, int maxpads, double max_mism,
			   int *ierr) {
    char *seq = NULL;
    int start, end, len, orig_start;
    int s_start, s_end, s_length;
    align_info *ai = NULL;
    double mism;
    int orig_pos = *pos;

    end = 0;
    *ierr = 0;

    if (NULL == (ai = (align_info *)xmalloc(sizeof(align_info)))) {
	*ierr = 1;
	goto error;
    }
    ai->res = NULL;

    s_length = si->length;
    s_start = si->start;
    s_end = si->end;
    seq = (char *)xmalloc(s_length * sizeof(*seq));
    strncpy(seq, exp_get_entry(si->e, EFLT_SQ), s_length);

    /* Complement if requested */
    if (dir == GAP_STRAND_REVERSE)
	io_complement_seq(&s_length, &s_start, &s_end, seq, NULL, NULL);

    orig_start = s_start;

    /* Calculate ranges to align */    
    start = *pos-1 - tol;

    if (start < 0) {
	if (-*pos - tol > 0) {
	    s_start += -*pos - tol;
	}
	start = 0;
	end = -1; /* flag */
    }

    if (s_start < 0)
	s_start = 0;

    if ((len = s_end - s_start - 1) < 1) {
#ifdef DEBUG
	printf("len %d s_end %d s_start %d\n", len, s_end, s_start);
#endif
	*ierr = 3;
	goto error;
    }

    /* KFB 14.09.01 FIXME - check with James
     * this is wrong if start is set to 0
     *    end   = *pos-1 + tol + maxpads + len;
     */
    if (end == 0) {
	end = *pos-1 + tol + maxpads + len;
    } else {
	end = tol + maxpads + len;
    }
#ifdef DEBUG
    printf("len %d end %d pos %d tol %d maxpads %d\n", len, end, *pos, tol, maxpads);
#endif

    if (end >= io_clength(io, contig)) {
	/* KFB 06.09.01 found end was one out */
	/* end = io_clength(io, contig)-1; */
	end = io_clength(io, contig);
    }

    if (end <= start) {
#ifdef DEBUG
	printf("end %d start %d\n", end, start);
#endif
	*ierr = 3;
	goto error;
    }


    /* Perform the alignment */
    if (NULL == (ai->res = (align_int *)xcalloc((len + end - start + 1),
						sizeof(align_int)))) {
	*ierr = 1;
	goto error;
    }

    if (-1 == calign(&seq[s_start], &ci->con_item[contig-1][start], /* seqs */
		     len, end - start,       /* lengths */
		     NULL, NULL, NULL, NULL, /* returned sequences */
		     0, 0,		     /* bands */
		     gopenval, gextendval,   /* penalties */
		     3, 0,		     /* job, protein */
		     ai->res)) {             /* result */
	*ierr = 1;
	goto error;
    }

    ai->start1 = s_start;
    ai->start2 = start;
    ai->len1 = len;
    ai->len2 = end - start;
    
    /*
     * Clip ends and derive score
     */
    mism = clip_score(&seq[ai->start1], &ci->con_item[contig-1][ai->start2],
 		      &ai->len1, &ai->len2, ai->res,
		      pos, &ai->start1, &ai->start2, 3 /* clip both ends */);

    if (display) {
	char *seq1, *seq2;
	int seq1l, seq2l;
	if (NULL == (seq1 = (char *)xmalloc(2 * s_length * sizeof(*seq))))
	    goto error;

	if (NULL == (seq2 = (char *)xmalloc(2 * s_length * sizeof(*seq)))) {
	    xfree(seq1);
	    goto error;
	}

	cexpand(&seq[ai->start1], &ci->con_item[contig-1][ai->start2],
		ai->len1, ai->len2,
		seq1, seq2, &seq1l, &seq2l,
		3 | ALIGN_J_PADS, ai->res);
	
	/* KFB: out by one error
	 * list_alignment(seq1, seq2, "Reading", "Consensus",
	 *       ai->start1 - orig_start + 1, ai->start2, "");
	 */
	list_alignment(seq1, seq2, "Reading", "Consensus",
		       ai->start1 - orig_start + 1, ai->start2+1, "");

	xfree(seq1);
	xfree(seq2);
    }

    if (ABS(orig_pos - (*pos - (s_start - orig_start))) > tol) {
#ifdef DEBUG
	printf("TOL %d %d orig_pos %d pos %d s_start %d orig_start %d\n",
	       ABS(orig_pos - (*pos - (s_start - orig_start))), tol, 
	       orig_pos, *pos, s_start, orig_start);
#endif
	*ierr = 4;
	goto error;
    }

    if (max_mism >= 0 && mism > max_mism) {
	*ierr = 2;
	goto error;
    }

    /* was *pos -= s_start - orig_start; */
    *pos = ai->start2 - (ai->start1 - orig_start) + 1;

    xfree(seq);
    return ai;

 error:
    if (ai->res)
	xfree(ai->res);
    if (ai)
	xfree(ai);
    if (seq)
	xfree(seq);

    return NULL;
}
Example #4
0
int init_sip_local_align_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 num_align,
				float score_align,
				float match,
				float transition,
				float transversion,
				float start_gap,
				float cont_gap,
				int *id)
{
    char *seq1, *seq2;
    int seq1_len, seq2_len;
    int r_len1, r_len2;
    char *r_seq1, *r_seq2;
    char *name1, *name2;
    int i;
    int seq1_type, seq2_type;
    int seq1_num;
    int seq2_num;
    in_sim *input;
    align_int **res;
    long *start1, *start2, *end1, *end2;
    int cnt = 0;
    int max_align;
    Tcl_DString input_params;
    int num_elements;
    d_plot *data;

#define NUM_SCORES 100
    
    vfuncheader("local alignment");

    if (-1 == (seq1_num = GetSeqNum(seq_id_h))) {
	verror(ERR_WARN, "local alignment", 
	       "horizontal sequence undefined");
	goto error;
    }
     
    if (-1 == (seq2_num = GetSeqNum(seq_id_v))) {
	verror(ERR_WARN, "local alignment", 
	       "vertical sequence undefined");
	goto error;
    }

    /* only align dna or protein */
    seq1_type = GetSeqType(seq1_num);
    seq2_type = GetSeqType(seq2_num);

    if (seq1_type != seq2_type) {
	verror(ERR_FATAL, "sim alignment", "sequences must both be either DNA or protein");
	goto error;
    }

    seq1 = GetSeqSequence(seq1_num);
    if ((seq1_len = end_h - start_h + 1) < 1) {
	verror(ERR_WARN, "align sequences", "negative length");
	goto error;
    }
    seq2 = GetSeqSequence(seq2_num);
    if ((seq2_len = end_v - start_v + 1) < 1) {
	verror(ERR_WARN, "align sequences", "negative length");
	goto error;
    }

    if (NULL == (input = (in_sim *)xmalloc(sizeof(in_sim))))
	goto error;

     Tcl_DStringInit(&input_params);
     vTcl_DStringAppend(&input_params, "horizontal %s: %s from %d to %d\n"
	    "vertical %s: %s from %d to %d\n", 
	    GetSeqLibraryName(seq1_num), 
	    GetSeqName(seq1_num), 
	    start_h, end_h, 
	    GetSeqLibraryName(seq2_num), 
	    GetSeqName(seq2_num), 
	    start_v, end_v);

     if (score_align == -1) {
	 vTcl_DStringAppend(&input_params, "number of alignments %d \n",
			    num_align);
     } else {
	 vTcl_DStringAppend(&input_params, "alignments above score %g\n",
			    score_align);
     }
     
     if (GetSeqType(seq1_num) == DNA) {
	 vTcl_DStringAppend(&input_params, "score for match %g\n"
		  "score for transition %g\n"
		  "score for transversion %g\n",
		  match, transition, transversion);
     }
     vTcl_DStringAppend(&input_params, "penalty for starting gap %g\n"
			"penalty for each residue in gap %g\n",
			start_gap, cont_gap);

    vfuncparams("%s", Tcl_DStringValue(&input_params));
    input->params = strdup(Tcl_DStringValue(&input_params)); 
    Tcl_DStringFree(&input_params);
     
    if (NULL == (r_seq1 = (char *)xcalloc(((seq1_len+seq2_len)*2+1),
					  sizeof(char)))) {
	goto error;
    }
    if (NULL == (r_seq2 = (char *)xcalloc(((seq1_len+seq2_len)*2+1),
					  sizeof(char)))) {
	goto error;
    }
    
    if (score_align != -1) {
	num_align = NUM_SCORES;
    }
    max_align = num_align;

    if (NULL == (start1 = (long *)xmalloc(max_align * sizeof(long)))) {
      goto error;
    }
    if (NULL == (start2 = (long *)xmalloc(max_align * sizeof(long)))) {
	goto error;
    }
    if (NULL == (end1 = (long *)xmalloc(max_align * sizeof(long)))) {
	goto error;
    }
    if (NULL == (end2 = (long *)xmalloc(max_align * sizeof(long)))) {
	goto error;
    }
    if (NULL == (res = (align_int **)xmalloc(max_align *sizeof(align_int*)))) {
	goto error;
    }

    for (i = 0; i < max_align; i++) {
	if (NULL == (res[i] = (align_int *)xcalloc((seq1_len+seq2_len+1),
						   sizeof(align_int)))) {
	    goto error;
	}
    }
    
    /* 
     * if finding all alignments above a certain score, the return value of
     * num_align is the number of alignments found
     */
    sim_align(&seq1[start_h-1], &seq2[start_v-1], seq1_len,
	      seq2_len, seq1_type, &num_align, score_align,
	      match, transition, transversion, start_gap, cont_gap, res, 
	      start1, start2, end1, end2);

    if (num_align <= 0) {
	verror(ERR_WARN, "local alignment", "no matches found\n");
	goto error;
    }
    name1 = GetSeqBaseName(seq1_num);
    name2 = GetSeqBaseName(seq2_num);

    num_elements = (seq1_len + seq2_len + 1) * num_align;
    if (NULL == (data = (d_plot *)xmalloc(sizeof(d_plot))))
	goto error;
    
    if (NULL == (data->p_array = (pt_score *)xmalloc(sizeof(pt_score) * 
						     num_elements)))
	goto error;
    
    for (i = 0; i < num_align; i++) {
	store_sim1(&seq1[start_h+start1[i]-2], 
		  &seq2[start_v+start2[i]-2], seq1_len, 
		  seq2_len, end1[i]-start1[i]+1, end2[i]-start2[i]+1,
		  res[i], start_h+start1[i]-1, start_v+start2[i]-1,
		  data->p_array, &cnt);

	cexpand(&seq1[start_h+start1[i]-2], 
		&seq2[start_v+start2[i]-2], end1[i]-start1[i]+1,
		end2[i]-start2[i]+1, r_seq1, r_seq2, &r_len1, &r_len2, 
		ALIGN_J_SSH | ALIGN_J_PADS, res[i]);
	
	spin_list_alignment(r_seq1, r_seq2, name1, name2, start_h+start1[i]-1, 
		       start_v+start2[i]-1, "", seq1_type);
	
    }
    *id = store_sim2(seq1_num, seq2_num, start_h, end_h, start_v, end_v, 
		     input, data, cnt);

    xfree(r_seq1);
    xfree(r_seq2);
    xfree(start1);
    xfree(start2);
    xfree(end1);
    xfree(end2);
    for (i = 0; i < max_align; i++) {
	xfree(res[i]);
    }
    xfree(res);
    
    return 0;

 error:
    return -1;
}
Example #5
0
/*! \brief Allocate storage for the data structures common to all factor routines.
 *
 * <pre>
 * For those unpredictable size, estimate as fill_ratio * nnz(A).
 * Return value:
 *     If lwork = -1, return the estimated amount of space required, plus n;
 *     otherwise, return the amount of space actually allocated when
 *     memory allocation failure occurred.
 * </pre> 
 */
int
cLUMemInit(fact_t fact, void *work, int lwork, int m, int n, int annz,
	  int panel_size, float fill_ratio, SuperMatrix *L, SuperMatrix *U,
          GlobalLU_t *Glu, int **iwork, complex **dwork)
{
    int      info, iword, dword;
    SCformat *Lstore;
    NCformat *Ustore;
    int      *xsup, *supno;
    int      *lsub, *xlsub;
    complex   *lusup;
    int      *xlusup;
    complex   *ucol;
    int      *usub, *xusub;
    int      nzlmax, nzumax, nzlumax;
    
    iword     = sizeof(int);
    dword     = sizeof(complex);
    Glu->n    = n;
    Glu->num_expansions = 0;

    if ( !Glu->expanders )	
        Glu->expanders = (ExpHeader*)SUPERLU_MALLOC( NO_MEMTYPE *
                                                     sizeof(ExpHeader) );
    if ( !Glu->expanders ) ABORT("SUPERLU_MALLOC fails for expanders");
    
    if ( fact != SamePattern_SameRowPerm ) {
	/* Guess for L\U factors */
	nzumax = nzlumax = fill_ratio * annz;
	nzlmax = SUPERLU_MAX(1, fill_ratio/4.) * annz;

	if ( lwork == -1 ) {
	    return ( GluIntArray(n) * iword + TempSpace(m, panel_size)
		    + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n );
        } else {
	    cSetupSpace(work, lwork, Glu);
	}
	
#if ( PRNTlevel >= 1 )
	printf("cLUMemInit() called: fill_ratio %.0f, nzlmax %ld, nzumax %ld\n", 
	       fill_ratio, nzlmax, nzumax);
	fflush(stdout);
#endif	
	
	/* Integer pointers for L\U factors */
	if ( Glu->MemModel == SYSTEM ) {
	    xsup   = intMalloc(n+1);
	    supno  = intMalloc(n+1);
	    xlsub  = intMalloc(n+1);
	    xlusup = intMalloc(n+1);
	    xusub  = intMalloc(n+1);
	} else {
	    xsup   = (int *)cuser_malloc((n+1) * iword, HEAD, Glu);
	    supno  = (int *)cuser_malloc((n+1) * iword, HEAD, Glu);
	    xlsub  = (int *)cuser_malloc((n+1) * iword, HEAD, Glu);
	    xlusup = (int *)cuser_malloc((n+1) * iword, HEAD, Glu);
	    xusub  = (int *)cuser_malloc((n+1) * iword, HEAD, Glu);
	}

	lusup = (complex *) cexpand( &nzlumax, LUSUP, 0, 0, Glu );
	ucol  = (complex *) cexpand( &nzumax, UCOL, 0, 0, Glu );
	lsub  = (int *)    cexpand( &nzlmax, LSUB, 0, 0, Glu );
	usub  = (int *)    cexpand( &nzumax, USUB, 0, 1, Glu );

	while ( !lusup || !ucol || !lsub || !usub ) {
	    if ( Glu->MemModel == SYSTEM ) {
		SUPERLU_FREE(lusup); 
		SUPERLU_FREE(ucol); 
		SUPERLU_FREE(lsub); 
		SUPERLU_FREE(usub);
	    } else {
		cuser_free((nzlumax+nzumax)*dword+(nzlmax+nzumax)*iword,
                            HEAD, Glu);
	    }
	    nzlumax /= 2;
	    nzumax /= 2;
	    nzlmax /= 2;
	    if ( nzlumax < annz ) {
		printf("Not enough memory to perform factorization.\n");
		return (cmemory_usage(nzlmax, nzumax, nzlumax, n) + n);
	    }
#if ( PRNTlevel >= 1)
	    printf("cLUMemInit() reduce size: nzlmax %ld, nzumax %ld\n", 
		   nzlmax, nzumax);
	    fflush(stdout);
#endif
	    lusup = (complex *) cexpand( &nzlumax, LUSUP, 0, 0, Glu );
	    ucol  = (complex *) cexpand( &nzumax, UCOL, 0, 0, Glu );
	    lsub  = (int *)    cexpand( &nzlmax, LSUB, 0, 0, Glu );
	    usub  = (int *)    cexpand( &nzumax, USUB, 0, 1, Glu );
	}
	
    } else {
	/* fact == SamePattern_SameRowPerm */
	Lstore   = L->Store;
	Ustore   = U->Store;
	xsup     = Lstore->sup_to_col;
	supno    = Lstore->col_to_sup;
	xlsub    = Lstore->rowind_colptr;
	xlusup   = Lstore->nzval_colptr;
	xusub    = Ustore->colptr;
	nzlmax   = Glu->nzlmax;    /* max from previous factorization */
	nzumax   = Glu->nzumax;
	nzlumax  = Glu->nzlumax;
	
	if ( lwork == -1 ) {
	    return ( GluIntArray(n) * iword + TempSpace(m, panel_size)
		    + (nzlmax+nzumax)*iword + (nzlumax+nzumax)*dword + n );
        } else if ( lwork == 0 ) {
	    Glu->MemModel = SYSTEM;
	} else {
	    Glu->MemModel = USER;
	    Glu->stack.top2 = (lwork/4)*4; /* must be word-addressable */
	    Glu->stack.size = Glu->stack.top2;
	}
	
	lsub  = Glu->expanders[LSUB].mem  = Lstore->rowind;
	lusup = Glu->expanders[LUSUP].mem = Lstore->nzval;
	usub  = Glu->expanders[USUB].mem  = Ustore->rowind;
	ucol  = Glu->expanders[UCOL].mem  = Ustore->nzval;;
	Glu->expanders[LSUB].size         = nzlmax;
	Glu->expanders[LUSUP].size        = nzlumax;
	Glu->expanders[USUB].size         = nzumax;
	Glu->expanders[UCOL].size         = nzumax;	
    }

    Glu->xsup    = xsup;
    Glu->supno   = supno;
    Glu->lsub    = lsub;
    Glu->xlsub   = xlsub;
    Glu->lusup   = lusup;
    Glu->xlusup  = xlusup;
    Glu->ucol    = ucol;
    Glu->usub    = usub;
    Glu->xusub   = xusub;
    Glu->nzlmax  = nzlmax;
    Glu->nzumax  = nzumax;
    Glu->nzlumax = nzlumax;
    
    info = cLUWorkInit(m, n, panel_size, iwork, dwork, Glu);
    if ( info )
	return ( info + cmemory_usage(nzlmax, nzumax, nzlumax, n) + n);
    
    ++Glu->num_expansions;
    return 0;
    
} /* cLUMemInit */
Example #6
0
static int align_old(EdStruct *xx0, int pos0, int len0,
		 EdStruct *xx1, int pos1, int len1)
{

    char *ol0,*ol1;
    int  *depad_to_pad0_m, *depad_to_pad1_m;
    int  *depad_to_pad0,   *depad_to_pad1;
    align_int *res, *S;
    int old_def_conf0 = xx0->default_conf_n;
    int old_def_conf1 = xx1->default_conf_n;
    int off0 = 0, off1 = 0;
    int left0 = 0, left1 = 0;

    vfuncheader("Align contigs (join editor)");

    /* Memory allocation */
    ol0 = (char *) xmalloc(len0+1);
    ol1 = (char *) xmalloc(len1+1);
    depad_to_pad0 = depad_to_pad0_m = (int *)xmalloc((len0+1) * sizeof(int));
    depad_to_pad1 = depad_to_pad1_m = (int *)xmalloc((len1+1) * sizeof(int));
    S = res = (align_int *) xmalloc((len0+len1+1)*sizeof(align_int));

    /* Compute the consensus */
    DBcalcConsensus(xx0,pos0,len0,ol0,NULL,BOTH_STRANDS);
    DBcalcConsensus(xx1,pos1,len1,ol1,NULL,BOTH_STRANDS);

    /* Strip the pads from the consensus */
    depad_seq(ol0, &len0, depad_to_pad0);
    depad_seq(ol1, &len1, depad_to_pad1);

    /* Do the actual alignment */
    (void)calign(ol0, ol1, len0, len1,
		 NULL, NULL, NULL, NULL,
		 0, 0, gopenval, gextendval, 3, 0, res);

    /* Clip left end */
    if (*S != 0) {
	/* Pad at start, so shift contigs */
	if (*S < 0) {
	    left0 = -*S; /* used for display only */
	    depad_to_pad0 += -*S;
	    off0 = depad_to_pad0[0];
	    xx1->displayPos -= off0;
	    pos0 += off0;
	    len0 -= off0;
	} else {
	    left1 = *S; /* used for display only */
	    depad_to_pad1 += *S;
	    off1 = depad_to_pad1[0];
	    xx0->displayPos -= off1;
	    pos1 += off1;
	    len1 -= off1;
	}
	S++;
	xx0->link->lockOffset = xx1->displayPos - xx0->displayPos;
    }

    /* Clip right end */
    {
	int i = 0, j = 0, op;
	align_int *S2 = S;

	while (i < len0 && j < len1) {
	    if ((op = *S2++) == 0)
		i++, j++;
	    else if (op > 0)
		j += op;
	    else
		i -= op;
	}
	
	len0 = i;
	len1 = j;
    }

    /* Display the alignment. */
    {
	char *exp0, *exp1;
	int exp_len0, exp_len1;
	char name0[100];
	char name1[100];

	exp0 = (char *) xmalloc(len0+len1+1);
	exp1 = (char *) xmalloc(len0+len1+1);

	sprintf(name0, "%d", xx0->DBi->DB_contigNum);
	sprintf(name1, "%d", xx1->DBi->DB_contigNum);
	cexpand(ol0+left0, ol1+left1, len0, len1,
		exp0, exp1, &exp_len0, &exp_len1, 
		ALIGN_J_SSH | ALIGN_J_PADS, S);
	list_alignment(exp0, exp1, name0, name1, pos0, pos1, "");

	xfree(exp0);
	xfree(exp1);
    }


    /*************************************************************************/
    /* Now actually make the edits, keeping track of old and new pads. */
    openUndo(DBI(xx0));
    openUndo(DBI(xx1));

    xx0->default_conf_n = -1;
    xx1->default_conf_n = -1;
    {
	int depad_pos0 = 0, depad_pos1 = 0;
	int curr_pad0;  /* Current padded position in seq 0 */
	int curr_pad1;  /* Current padded position in seq 1 */
	int extra_pads; /* Difference between padded positions */
	int last_pad0 = -1;
	int last_pad1 = -1;
	int inserted_bases0 = 0;
	int inserted_bases1 = 0;

	while (depad_pos0 < len0 || depad_pos1 < len1) {
	    if (*S < 0) {
		depad_pos0 -= *S;
	    } else if (*S > 0) {
		depad_pos1 += *S;
	    }

	    curr_pad0 = depad_to_pad0[depad_pos0]-off0;
	    curr_pad1 = depad_to_pad1[depad_pos1]-off1;
	    extra_pads = (curr_pad1 - last_pad1) - (curr_pad0 - last_pad0);

	    if (extra_pads < 0) { /* Add to seq 0 */
		add_pads(xx1, pos1 + curr_pad1 + inserted_bases1, -extra_pads);
		inserted_bases1 -= extra_pads;
	    } else if (extra_pads > 0) { /* Add to seq 1 */
		add_pads(xx0, pos0 + curr_pad0 + inserted_bases0,  extra_pads);
		inserted_bases0 += extra_pads;
	    }
	    
	    last_pad0 = curr_pad0;
	    last_pad1 = curr_pad1;

	    if (*S == 0) {
		depad_pos0++;
		depad_pos1++;
	    }

	    S++;
	}
    }
    xx0->default_conf_n = old_def_conf0;
    xx1->default_conf_n = old_def_conf1;
    /*************************************************************************/

    closeUndo(xx1, DBI(xx1));
    closeUndo(xx0, DBI(xx0));

    xfree(res);
    xfree(ol0);
    xfree(ol1);
    xfree(depad_to_pad0_m);
    xfree(depad_to_pad1_m);

    return(0);
}
Example #7
0
static int align(EdStruct *xx0, int pos0, int len0,
		 EdStruct *xx1, int pos1, int len1)
{

    char *ol0,*ol1, *cons0, *cons1;
    int old_def_conf0 = xx0->default_conf_n;
    int old_def_conf1 = xx1->default_conf_n;
    OVERLAP *overlap;
    int ierr;
    char PAD_SYM = '.';
    int  *depad_to_pad0, *dp0, *depad_to_pad1, *dp1;
    int *S, *res;
    int off0 = 0, off1 = 0;
    int left0 = 0, left1 = 0;

    vfuncheader("Align contigs (join editor)");

    /* Memory allocation */
    ol0 = (char *) xmalloc(len0+1);
    ol1 = (char *) xmalloc(len1+1);
    cons0 = (char *) xmalloc(len0+1);
    cons1 = (char *) xmalloc(len1+1);
    dp0 = depad_to_pad0 = (int *)xmalloc((len0+1) * sizeof(int));
    dp1 = depad_to_pad1 = (int *)xmalloc((len1+1) * sizeof(int));

    /* Compute the consensus */
    DBcalcConsensus(xx0,pos0,len0,ol0,NULL,BOTH_STRANDS);
    DBcalcConsensus(xx1,pos1,len1,ol1,NULL,BOTH_STRANDS);

    memcpy(cons0, ol0, len0+1);
    memcpy(cons1, ol1, len1+1);

    /* Strip the pads from the consensus */
    depad_seq(ol0, &len0, depad_to_pad0);
    depad_seq(ol1, &len1, depad_to_pad1);

    if (NULL == (overlap = create_overlap())) return -1;
    init_overlap (overlap, ol0, ol1, len0, len1);

    if(-1 == (ierr =  align_contigs (overlap))) {
	xfree(ol0);
	xfree(ol1);
	destroy_overlap(overlap);
	return -1;
    }

    /*
    overlap->seq1_out[overlap->right+1] = 0;
    overlap->seq2_out[overlap->right+1] = 0;
    */

    S = res = rsalign2myers(overlap->seq1_out, strlen(overlap->seq1_out),
			    overlap->seq2_out, strlen(overlap->seq2_out),
			    PAD_SYM);

    /* Clip left end */
    if (*S != 0) {
	/* Pad at start, so shift contigs */
	if (*S < 0) {
	    left0 = -*S; /* used for display only */
	    depad_to_pad0 += -*S;
	    off0 = depad_to_pad0[0];
	    xx1->displayPos -= off0;
	    pos0 += off0;
	    len0 -= -*S;
	} else {
	    left1 = *S; /* used for display only */
	    depad_to_pad1 += *S;
	    off1 = depad_to_pad1[0];
	    xx0->displayPos -= off1;
	    pos1 += off1;
	    len1 -= *S;
	}
	S++;
	xx0->link->lockOffset = xx1->displayPos - xx0->displayPos;
    }

    /* Clip right end */
    {
	int pos0 = 0, pos1 = 0;
	int *s = S;

	while (pos0 < len0 && pos1 < len1) {
	    if (*s < 0) {
		pos0 -= *s;
	    } else if (*s > 0) {
		pos0 += *s;
	    } else {
		pos0++;
		pos1++;
	    }

	    s++;
	}

	if (*s < 0)
	    len0 += *s;
	else if (*s > 0)
	    len1 -= *s;
    }

    /* Display the alignment. */
    {
	char *exp0, *exp1;
	int exp_len0, exp_len1;
	char name0[100];
	char name1[100];

	exp0 = (char *) xmalloc(len0+len1+1);
	exp1 = (char *) xmalloc(len0+len1+1);

	sprintf(name0, "%d", xx0->DBi->DB_contigNum);
	sprintf(name1, "%d", xx1->DBi->DB_contigNum);
	cexpand(ol0+left0, ol1+left1, len0, len1,
		exp0, exp1, &exp_len0, &exp_len1, 
		ALIGN_J_SSH | ALIGN_J_PADS, S);
	list_alignment(exp0, exp1, name0, name1, pos0, pos1, "");

	xfree(exp0);
	xfree(exp1);
    }


    /*************************************************************************/
    /* Now actually make the edits, keeping track of old and new pads. */
    openUndo(DBI(xx0));
    openUndo(DBI(xx1));

    xx0->default_conf_n = -1;
    xx1->default_conf_n = -1;
    {
	int depad_pos0 = 0, depad_pos1 = 0;
	int curr_pad0;  /* Current padded position in seq 0 */
	int curr_pad1;  /* Current padded position in seq 1 */
	int extra_pads; /* Difference between padded positions */
	int last_pad0 = -1;
	int last_pad1 = -1;
	int inserted_bases0 = 0;
	int inserted_bases1 = 0;


	while (depad_pos0 < len0 && depad_pos1 < len1) {
	    if (*S < 0) {
		depad_pos0 -= *S;
	    } else if (*S > 0) {
		depad_pos1 += *S;
	    }

	    if (depad_pos0 >= len0 || depad_pos1 >= len1)
		break;

	    curr_pad0 = depad_to_pad0[depad_pos0]-off0;
	    curr_pad1 = depad_to_pad1[depad_pos1]-off1;

	    extra_pads = (curr_pad1 - last_pad1) - (curr_pad0 - last_pad0);

	    if (extra_pads < 0) { /* Add to seq 0 */
		add_pads(xx1, pos1 + curr_pad1 + inserted_bases1, -extra_pads);
		inserted_bases1 -= extra_pads;
	    } else if (extra_pads > 0) { /* Add to seq 1 */
		add_pads(xx0, pos0 + curr_pad0 + inserted_bases0,  extra_pads);
		inserted_bases0 += extra_pads;
	    }
	    
	    last_pad0 = curr_pad0;
	    last_pad1 = curr_pad1;

	    if (*S == 0) {
		depad_pos0++;
		depad_pos1++;
	    }

	    S++;
	}
    }
    xx0->default_conf_n = old_def_conf0;
    xx1->default_conf_n = old_def_conf1;
    /*************************************************************************/

    closeUndo(xx1, DBI(xx1));
    closeUndo(xx0, DBI(xx0));

    xfree(res);

    xx0->default_conf_n = old_def_conf0;
    xx1->default_conf_n = old_def_conf1;

    xfree(ol0);
    xfree(ol1);
    xfree(dp0);
    xfree(dp1);
    destroy_overlap(overlap);

    return(0);
}