/////////////////////////////////////////////////////////////////////// // Function: // // Author: $author$ // Date: 2/21/2012 /////////////////////////////////////////////////////////////////////// virtual EvError CopyFile2File (const char* chars, const char* chars2) { EvError error = EV_ERROR_FAILED; bool complete = false; bool modified = false; const char* fileDir; CEvCharFile file, file2; LONG length, count; DBT("(const char* chars=\"%s\", const chars2=\"%s\")...\n", chars, chars2); if (0 < (length = m_filePath.Assign(chars2))) if ((fileDir = m_filePath.GetFileDirectory(length))) if (!(error = m_fileSystem.MakeDirectory(fileDir))) if (!(error = file.Open(chars, EVFILE_MODE_READ_BINARY))) { #if defined(WIN32) if (!(m_targetEntry.TruncateExisting(chars2))) if (EvFileSystemEntry::EV_TYPE_FILE & (m_targetEntry.Exists(chars2))) if (0 < ((m_targetEntry.GetSize()))) unlink(chars2); #else // defined(WIN32) #endif // defined(WIN32) if (!(error = file2.Open(chars2, EVFILE_MODE_WRITE_BINARY))) { complete = true; modified = false; do { if (0 <= (length = file.Read (m_blockChars, m_blockSize))) if (length <= (count = Write2File (modified, file2, m_blockChars, length))) continue; complete = false; break; } while((LONG)m_blockSize <= length); file2.Flush(); file2.Close(); } file.Close(); } if (modified) DBI("() CRs removed from \"%s\"\n", chars2); else if (complete) error = m_targetEntry. SetExistingTimes(m_sourceEntry, chars2); return error; }
/* * 0 - no edits made * 1 - edits made */ int editsMade(EdStruct *xx) { return _editsMade(DBI(xx)); }
/* * Produce a consensus trace from a specific region of this contig. */ Read *cons_trace(EdStruct *xx, int start, int end, int strand, int match, int exception) { int *seqList, i, j, count, next; Read *r; int max_points = 10000; char *con = NULL; diff_cons_seq *rlist = NULL; char fileName[256]; char t_type[5]; int form; int offset = 0, w; /* Get the consensus sequence */ if (NULL == (con = (char *)xmalloc(end - start + 2))) goto error; DBcalcConsensus(xx, start, end - start + 1, con, NULL, BOTH_STRANDS); /* Allocate a list of read pointers and positions */ if (NULL == (rlist = (diff_cons_seq *)xcalloc(DBI_gelCount(xx), sizeof(*rlist)))) goto error; /* Allocate a read structure */ if (NULL == (r = read_allocate(max_points, end - start + 1))) goto error; /* Derive the initial list of sequences covering the start point */ count = 0; seqList = DBI_list(xx); for (i = 1; i <= DBI_gelCount(xx) && DB_RelPos(xx, DBI_order(xx)[i]) <= start; i++) { int seq = DBI_order(xx)[i]; DBgetSeq(DBI(xx), seq); if (DB_RelPos(xx, seq) + DB_Length(xx, seq) > start && strand_matches(xx, seq, strand) && seq != exception) { if (get_trace_path(xx, seq, fileName, t_type) == 0) { form = trace_type_str2int(t_type); rlist[count].r = read_reading(fileName, form); if (rlist[count].r) { rlist[count].seq = DBgetSeq(DBI(xx), seq); rlist[count].opos = get_trace_pos(rlist[count].r, xx, seq, 0, DB_Start(xx, seq), DB_Start(xx, seq) + DB_Length(xx, seq), DB_Seq(xx, seq), 0); seqList[count++] = seq; } } } } if (i <= DBI_gelCount(xx)) next = i; else next = 0; /* * Loop along the sequence updating seqList as we go. * At each point we know how many sequences there are so we can * produce the consensus from these sequences. */ for (i = start; i <= end; i++) { w = do_cons_base(xx, con, i, start, count, seqList, rlist, r, offset, match, &max_points); if (w == -1) goto error; offset += w; /* Update seqList for the next position */ if (i < end) { /* Remove sequences */ for (j = 0; j < count; j++) { int seq = seqList[j]; if (DB_RelPos(xx, seq) + DB_Length(xx, seq) - 1 <= i) { read_deallocate(rlist[j].r); xfree(rlist[j].opos); memmove(&seqList[j], &seqList[j+1], (count-1-j) * sizeof(*seqList)); memmove(&rlist[j], &rlist[j+1], (count-1-j) * sizeof(*rlist)); count--; j--; } } /* Add sequences */ while (next && DB_RelPos(xx, next) <= i+1) { /* printf("next=%d %d %d\n", next, DB_RelPos(xx, next), i+1); */ DBgetSeq(DBI(xx), next); if (strand_matches(xx, next, strand) && get_trace_path(xx, next, fileName, t_type) == 0) { form = trace_type_str2int(t_type); rlist[count].r = read_reading(fileName, form); if (rlist[count].r) { rlist[count].seq = DBgetSeq(DBI(xx), next); rlist[count].opos = get_trace_pos(rlist[count].r, xx, next, 0, DB_Start(xx, next), DB_Start(xx,next)+DB_Length(xx,next), DB_Seq(xx, next), 0); seqList[count++] = next; } } if (++next > DBI_gelCount(xx)) next = 0; } } } for (i = 0; i < count; i++) { read_deallocate(rlist[i].r); xfree(rlist[i].opos); } tidy_up(r, end-start + 1, offset); xfree(con); xfree(rlist); return r; error: if (con) xfree(con); if (rlist) xfree(rlist); return NULL; }
/* * Align the two contig editor windows * Returns: * 0 - aligned ok * 1 - not ok */ int alignOverlap(EdStruct *xx[2]) { int left0,right0; int left1/*,right1*/; int length0,length1; int offset = editorLockedPos(xx, 1/*force recalculation*/); int overlapLength; int len0,len1; int ret; int xx0_dp, xx1_dp; if (! inJoinMode(xx[0])) return 1; /* Compute overlap position and sizes */ if (offset < 0) { left0 = 1-offset; left1 = 1; } else { left0 = 1; left1 = 1+offset; } length0 = DB_Length(xx[0],0); length1 = DB_Length(xx[1],0); if (offset+length0 < length1) { right0 = length0; } else { right0 = length1-offset; } overlapLength = right0 - left0+1; if (overlapLength <= 0) return 1; len0 = len1 = overlapLength; /* Add on extra data either end to allow for padding */ #define XTRA_PERC 0.30 left0 -= (int)(overlapLength * XTRA_PERC); left1 -= (int)(overlapLength * XTRA_PERC); len0 += (int)(overlapLength * XTRA_PERC * 2); len1 += (int)(overlapLength * XTRA_PERC * 2); xx0_dp = xx[0]->displayPos; xx1_dp = xx[1]->displayPos; if (left0 < 1 && left1 < 1) { xx[0]->displayPos += MAX(left0, left1)-1; xx[1]->displayPos += MAX(left0, left1)-1; } if (left0 < 1) { len0 -= 1-left0; xx[0]->displayPos += 1-left0; left0 = 1; } if (left1 < 1) { len1 -= 1-left1; xx[1]->displayPos += 1-left1; left1 = 1; } if (len0 > length0 - left0 + 1) { len0 = length0 - left0 + 1; } if (len1 > length1 - left1 + 1) { len1 = length1 - left1 + 1; } xx[0]->link->lockOffset = xx[1]->displayPos - xx[0]->displayPos; openUndo(DBI(xx[0])); openUndo(DBI(xx[1])); /* Do the actual alignment */ ret = align(xx[0], left0, len0, xx[1], left1, len1); if (ret) { /* Alignment failed - put back display positions before returning */ xx[0]->displayPos = xx0_dp; xx[1]->displayPos = xx1_dp; } else { /* * If displayPos has changed, put it back to the original position and * adjust it once more using U_adjust_display. This will cause the undo * information to be stored correctly. */ if (xx0_dp != xx[0]->displayPos) { int tmp = xx[0]->displayPos - xx0_dp; xx[0]->displayPos = xx0_dp; U_adjust_display(xx[0], tmp); } if (xx1_dp != xx[1]->displayPos) { int tmp = xx[1]->displayPos - xx1_dp; xx[1]->displayPos = xx1_dp; U_adjust_display(xx[1], tmp); } } closeUndo(xx[1], DBI(xx[1])); closeUndo(xx[0], DBI(xx[0])); return ret; }
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); }
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); }