source: tags/arb_5.2/ARBDBPP/adt_edit.cxx

Last change on this file was 5968, checked in by westram, 15 years ago
  • new flag -w to aisc_mkpt (add include wrapper)
  • uniform style for several include wrappers
  • removed duplicated includes
  • removed useless nt_concatenate.hxx
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 37.4 KB
Line 
1
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5// #include <malloc.h>
6#include <memory.h>
7#include <arbdb.h>
8#include "adtools.hxx"
9
10/***************************************************************************
11class ADT_COMPLEMENT
12***************************************************************************/
13ADT_COMPLEMENT::ADT_COMPLEMENT() {
14    //      ad_species = NULL;
15    species_name       = NULL;
16    alignment_type     = NULL;
17    alignment_name     = NULL;
18    sequence           = NULL;
19    char_array         = NULL;
20    sequence_buffer    = NULL;
21    index_buffer       = NULL;
22
23    adt_acid            = ADT_ALITYPE_UNDEFINED;
24    complement_seq      = NO;
25    invert_seq          = NO;
26    seq_is_complemented = NO;
27    seq_is_inverted     = NO;
28    take_cursor         = NO;
29    take_borders        = NO;
30    remove_gaps_points  = NO;
31    which_button        = NO_BUTTON;
32
33    alignment_length = max_value; //max_value
34    sequence_length  = 0;
35    left_border      = 0;
36    right_border     = 0;
37
38}
39
40// *******************
41ADT_COMPLEMENT::~ADT_COMPLEMENT() {
42    // delete       char_array;
43}
44
45
46
47// *******************
48char * ADT_COMPLEMENT::make_char_array()
49{
50    char    *local_char_array = new char[256];
51
52    for( int i=0 ; i <= 255 ; i++ ) {
53        local_char_array[i] = (char) i;
54    }
55    return local_char_array;
56}
57
58// *******************
59AD_ERR * ADT_COMPLEMENT::complement_compile(void) {  //Veraendert Zeichensatz
60
61    delete char_array;
62    char_array = make_char_array();
63
64    char_array[(int)'C'] = 'G';
65    char_array[(int)'c'] = 'g';
66    char_array[(int)'G'] = 'C';
67    char_array[(int)'g'] = 'c';
68    char_array[(int)'U'] = 'A';
69    char_array[(int)'u'] = 'a';
70    char_array[(int)'T'] = 'A';
71    char_array[(int)'t'] = 'a';
72    char_array[(int)'M'] = 'K';
73    char_array[(int)'m'] = 'k';
74    char_array[(int)'R'] = 'Y';
75    char_array[(int)'r'] = 'y';
76    //      char_array[(int)'W'] = 'W';             //der Vollstaendigkeit halber
77    //      char_array[(int)'w'] = 'w';             //der Vollstaendigkeit halber
78    //      char_array[(int)'S'] = 'S';             //der Vollstaendigkeit halber
79    //      char_array[(int)'s'] = 's';             //der Vollstaendigkeit halber
80    char_array[(int)'Y'] = 'R';
81    char_array[(int)'y'] = 'r';
82    char_array[(int)'K'] = 'M';
83    char_array[(int)'k'] = 'm';
84    char_array[(int)'V'] = 'B';
85    char_array[(int)'v'] = 'b';
86    char_array[(int)'H'] = 'D';
87    char_array[(int)'h'] = 'd';
88    char_array[(int)'D'] = 'H';
89    char_array[(int)'d'] = 'h';
90    char_array[(int)'B'] = 'V';
91    char_array[(int)'b'] = 'v';
92    //      char_array[(int)'N'] = 'N';             //der Vollstaendigkeit halber
93    //      char_array[(int)'n'] = 'n';             //der Vollstaendigkeit halber
94    //      char_array[(int)'X'] = 'X';             //der Vollstaendigkeit halber
95    //      char_array[(int)'x'] = 'x';             //der Vollstaendigkeit halber
96    //      char_array[(int)'.'] = '.';             //der Vollstaendigkeit halber
97
98    switch(adt_acid)  {
99
100        case ADT_ALITYPE_RNA:
101            char_array[(int)'A'] = 'U';
102            char_array[(int)'a'] = 'u';
103            break;
104
105        case ADT_ALITYPE_DNA:
106            char_array[(int)'A'] = 'T';
107            char_array[(int)'a'] = 't';
108            break;
109        default:
110            break;
111    }
112
113    return 0;
114}
115
116// *******************
117AD_ERR * ADT_COMPLEMENT::complement_buffers(void) {  //Puffer fuer Seq + Index
118
119    char            *seq_buff;
120    long            *ind_buff;
121
122    long            buffer_len      = 0;
123
124    //buffer_len = (long)strlen(sequence);
125    buffer_len = sequence_length;
126
127    seq_buff = (char *)calloc( ((int)buffer_len + 1), sizeof(char) );
128    memset(seq_buff, (int)'.', ((int)buffer_len));
129    sequence_buffer = seq_buff;
130
131
132    ind_buff = (long *) calloc( ((int)buffer_len + 1), sizeof(long) );
133    ind_buff[buffer_len] = -1;
134    index_buffer = ind_buff;
135
136    return 0;
137}
138
139
140
141/***************************************************************************
142class ADT_EDIT
143
144***************************************************************************/
145ADT_EDIT::ADT_EDIT() {
146
147    selection               = 0;
148    found_matchp            = 0;
149    actual_cursorpos_editor = -1;
150    replace_overflow        = 0;
151    mistakes_found          = 0;
152    seq_equal_match         = 1;
153
154    db_status       = ADT_DB_CLOSED;
155    border_overflow = NO;
156}
157
158ADT_EDIT::~ADT_EDIT() {
159}
160
161
162/***************************************************************************
163class ADT_SEARCH
164
165***************************************************************************/
166ADT_SEARCH::ADT_SEARCH() {
167
168    seq_anfang               = NULL;
169    save_sequence            = NULL;
170    seq_index_start          = NULL;
171    matchpattern_buffer      = NULL;
172    search_array             = NULL;
173    replace_string           = NULL;
174    mistakes_allowed         = 0;
175    gaps                     = -1; //gaps werden nicht ignoriert
176    upper_eq_lower           = 0; //gross/KLEIN-Schreibung,
177    t_equal_u                = 0; //t und u gleichbehandeln,
178    search_start_cursor_pos  = 0;
179    replace_start_cursor_pos = 0;
180    search_direction         = ADT_SEARCH_FORWARD; //Vorwaertssuche
181    replace_option           = ADT_NO_REPLACE; //Kein REPLACE
182    replace_loop_sequence    = ADT_DONT_STOPP_REPLACE;
183
184    found_cursor_pos = 0;
185    string_replace   = 0;
186}
187
188ADT_SEARCH::~ADT_SEARCH() {
189    delete search_array;
190    delete matchpattern_buffer;
191}
192
193// *************************************
194char * ADT_SEARCH::show_search_array()
195{
196    search_array = new char[256];
197
198    for( int i=0 ; i <= 255 ; i++ ) {
199        search_array[i] = (char) i;
200    }
201    return search_array;
202}
203
204// ***********************************
205//Veraendert Zeichensatz, Liefert Matchpattern ohne Gaps
206AD_ERR * ADT_SEARCH::compile(void) {
207    delete search_array;
208    search_array = show_search_array();
209
210    if(t_equal_u) {
211        search_array[(int)'U'] = 'T';
212        search_array[(int)'u'] = 't';
213    }
214
215    if(upper_eq_lower) {
216        int char_pos;
217        for( char_pos = 'a'; char_pos <= 'z' ; char_pos++) {
218            search_array[char_pos] -= ( 'a' - 'A' );
219        }
220    }
221
222    ///////////////////////////////////Matchpattern WITHOUT gaps.
223        if((gaps == 1) || (gaps == 2)) {
224            delete matchpattern_buffer;
225            matchpattern_buffer=(char *)calloc(strlen(matchpattern)+1,sizeof(char));
226
227            if(matchpattern_buffer != NULL) {
228                char    *matchp_copy_loop;
229                char    *matchp_buffer_start, *matchp_buffer_loop;
230
231                matchp_buffer_start = matchp_buffer_loop =
232                    matchpattern_buffer;
233
234                for( matchp_copy_loop = matchpattern ;
235                     *matchp_copy_loop != '\0' ;
236                     matchp_copy_loop++ ) {
237                    if(*matchp_copy_loop != '-') {
238                        *matchp_buffer_loop  = *matchp_copy_loop;
239                        matchp_buffer_loop++;
240                    }
241                }
242                *matchp_buffer_loop     = '\0';
243                matchpattern = matchp_buffer_start;
244            }
245        }
246        return 0;
247}
248
249
250// **************************************************************************
251//      Funktion liefert die Sequenz entsprechend OHNE Gaps !!!!
252//      (Sequenz wird ohne Gaps in ein Puffer-Array geschrieben,
253//      => Pointer auf Sequenz-Puffer)
254//
255// **************************************************************************
256AD_ERR * ADT_SEQUENCE::make_sequence_buffer(ADT_SEARCH
257                                            *ptr_adt_search, ADT_EDIT *ptr_adt_edit) {
258
259    //-------------------------------------------------start sequencebuffer
260    char    *seq_anfang, *seq_copy_loop;
261    char    *seq_buffer_start, *seq_buffer_loop;    //buffer
262    long    *seq_index_start, *seq_index_loop;      //index buffer
263
264    seq_anfang = ptr_adt_search->seq_anfang;
265
266    char source;
267
268    //------------------------------------------memory for sequence-buffer
269    seq_buffer_start = (char *)calloc(strlen(seq_anfang)+1,sizeof(char));
270
271    if (!seq_buffer_start) {
272        //              printf("\n**** ERROR sequence-buffer no memory allocated");
273    }
274    seq_buffer_loop = seq_buffer_start;
275    //------------------------------------------------------end seq_buffer
276
277    //---------------------------------------------memory for index-buffer
278    seq_index_start = (long *) calloc(strlen(seq_anfang)+1,sizeof(long));
279
280    if (!seq_index_start) {
281        //              printf("\n**** ERROR index-buffer no memory allocated");
282    }
283    seq_index_loop = seq_index_start;
284    //----------------------------------------------------end index_buffer
285
286    for( seq_copy_loop = seq_anfang ;
287         (source = *seq_copy_loop) ;
288         seq_copy_loop++ ) {
289        if(*seq_copy_loop != '-') {
290            *(seq_buffer_loop++) = source;  //kopiert Zeichen
291            *(seq_index_loop++)  = seq_copy_loop - seq_anfang;
292            //Index des Zeichens
293        }
294    }
295    *seq_buffer_loop        = '\0';
296    *seq_index_loop         = -1;
297    ptr_adt_search->seq_anfang = seq_buffer_start; //SEQUENCE START
298    //--------------------------------------------------end sequencebuffer
299
300    // -------------------------------------neu search/replace cursorstart
301    long    var_cursorpos_editor = ptr_adt_edit->actual_cursorpos_editor;
302    //========================!!
303    // Fuer die Neuberechnung der Startpos.
304    long    buffer_index = 0;               //zaehlvariable fuer
305    //Cursorberechnung
306    for(  buffer_index = 0 ;
307          (seq_index_start[buffer_index] <
308           var_cursorpos_editor) &&
309              (seq_index_start[buffer_index] != -1 ) ;
310          buffer_index++ ) {
311    }
312    //   "buffer_index"
313    //somit steht hier der Index, unter dessen
314    //Adresse die Cursorposition steht, die
315    //entweder groesser (Cursor stand auf einem GAP)
316    //oder gleich (Cursor stand auf einem Zeichen)
317    //der Cursorpos im Editor ist, vielmehr war.
318
319    if( ptr_adt_search->search_direction == ADT_SEARCH_FORWARD) {
320        if( (seq_index_start[buffer_index] == var_cursorpos_editor) &&
321            (var_cursorpos_editor > 0) ) {
322            ptr_adt_search->search_start_cursor_pos =
323                buffer_index + 1;
324        }
325        else {
326            ptr_adt_search->search_start_cursor_pos =
327                buffer_index;
328        }
329    } // end if
330    else {
331        ptr_adt_search->search_start_cursor_pos = buffer_index - 1;
332    } // end else
333    delete ptr_adt_search->seq_index_start;
334    ptr_adt_search->seq_index_start  = seq_index_start; //indexarray
335    ptr_adt_edit->actual_cursorpos_editor = buffer_index;
336    //----------------------------------------------------end cursorstart
337
338    return 0;
339}
340// **************************************************************************
341
342
343
344// **************************************************************************
345//
346// **************************************************************************
347AD_ERR * ADT_SEQUENCE::rewrite_from_sequence_buffer(ADT_SEARCH
348                                                    *ptr_adt_search, ADT_EDIT  *ptr_adt_edit) {
349
350    char    *seq_anfang;            //Sequenz
351    char    *seq_buffer_start;      //Sequenz ohne GAPS
352    long    *seq_index_start;       //index buffer, siehe unten!
353    long    found_cursor_pos_buffer = ptr_adt_search->found_cursor_pos;
354
355    //--------------------------------found_cursor_pos nach Suche ohne Gaps
356    if (( ptr_adt_edit->found_matchp == 1 ) &&
357        ( ptr_adt_search->replace_option == ADT_NO_REPLACE )) {
358        ptr_adt_search->found_cursor_pos =
359            ptr_adt_search->seq_index_start[found_cursor_pos_buffer];
360    }
361    //-------------------------------------------------END found_cursor_pos
362
363    //---------------------------------------rewrite Seq-Puffer auf Sequenz
364    if( ptr_adt_search->replace_option != ADT_NO_REPLACE )  {
365        seq_buffer_start = ptr_adt_search->seq_anfang;
366        seq_index_start  = ptr_adt_search->seq_index_start;
367        seq_anfang = get();
368
369        long buffer_index = 0; //zaehlvariable
370        char source;
371
372        for( ; (source = seq_buffer_start[buffer_index]) ; buffer_index++ ) {
373            *(seq_anfang + seq_index_start[buffer_index]) = source;
374        }
375        ptr_adt_search->seq_anfang = seq_anfang;
376        //-------------------------------------------------end rewrite
377
378        char *replace_anfang       = ptr_adt_search->replace_string;
379        long  var_start_cursor_pos = ptr_adt_search->replace_start_cursor_pos;
380
381        //--------------------------------------------cursorpos_editor
382        long  buffer_end_replace = var_start_cursor_pos + strlen(replace_anfang);
383
384        ptr_adt_edit->actual_cursorpos_editor =
385            ptr_adt_search->seq_index_start[buffer_end_replace-1];
386        //  nur bei Suchrichtung
387        //  "Vorwaerts" !!!
388        //----------------------------------------end cursorpos_editor
389
390        free(seq_buffer_start);
391        ptr_adt_search->seq_anfang = NULL;
392        free((char *)seq_index_start);
393        ptr_adt_search->seq_index_start = NULL;
394
395    }
396    //--------------------------------------------------------END rewrite
397
398    return 0;
399}
400//-----------------------------------------END rewrite_from_sequence_buffer()
401// **************************************************************************
402
403
404
405
406// **************************************************************************
407//
408// **************************************************************************
409AD_ERR * ADT_SEQUENCE::show_edit_seq_search(ADT_SEARCH *ptr_adt_search,
410                                            ADT_EDIT *ptr_adt_edit) {
411    AD_ERR  *ad_err;
412
413    long    replace_loop = ADT_DONT_STOPP_REPLACE;  //default
414    // ADT_DONT_STOPP_REPLACE == 1 !!
415
416    ptr_adt_search->seq_anfang      =  get();
417
418
419    // Sicherheitsabfrage fuer die
420    // Rueckwaertssuche !!
421    if(ptr_adt_search->search_start_cursor_pos > len()) {
422        //Cursorpos > Sequenzlaenge?
423        ptr_adt_search->search_start_cursor_pos = len() - 1;
424    }
425
426    //-----------------------------------------------------Sequenz puffern
427    if( (ptr_adt_search->gaps == 0) ||  (ptr_adt_search->gaps == 1) ) {
428        make_sequence_buffer(ptr_adt_search, ptr_adt_edit);
429        ///////////////////////////////////////////////////
430            } //end if,  >>Sequenz<< OHNE Gaps.
431    //---------------------------------------------------- end seq puffern
432
433    //---------------------------------Sicherungskopie der Seq fuer REPLACE
434    if( ptr_adt_search->replace_option != ADT_NO_REPLACE ) {
435        ptr_adt_search->save_sequence = strdup(ptr_adt_search->seq_anfang);
436    }
437    //--------------------------------------------------END Sicherungskopie
438
439    //---------------------------------------korrigiert die Start Cursorpos
440    // bis Ende.., da erst search(), dann replace()
441    if( ( (ptr_adt_search->replace_option == ADT_REPLACE_ONLY)         ||
442          (ptr_adt_search->replace_option == ADT_REPLACE_AND_SEARCH_NEXT)||
443          (ptr_adt_search->replace_option == ADT_REPLACE_REST_SEQUENCE)||
444          (ptr_adt_search->replace_option == ADT_REPLACE_REST_EDITOR)  ) &&
445        (ptr_adt_search->search_start_cursor_pos > 0)                  ) {
446
447        ptr_adt_search->search_start_cursor_pos--;
448    }
449    //----------------------------------------------------END start_cursor
450
451    //====================================================================
452    //=================================================while(replace_loop)
453    long max_loop = this->seq_len;
454    do {                                             // END Sequenz/Editor
455
456        //------------------------------------------------------SEARCH
457        ptr_adt_edit->found_matchp = 0;
458
459        show_edit_search(ptr_adt_search, ptr_adt_edit);
460        //////////////////////////////////////////////
461        //------------------------------------------------- END SEARCH
462
463        if( ((ptr_adt_search->replace_option == ADT_REPLACE_ONLY) ||
464             (ptr_adt_search->replace_option == ADT_REPLACE_AND_SEARCH_NEXT)) &&
465            (ptr_adt_edit->found_matchp == 0) )   {
466            ptr_adt_edit->seq_equal_match = 0;
467            ptr_adt_edit->found_matchp = 1;
468            return 0;
469        } // end if ( ... == ADT_REPLACE_ONLY)
470
471        else {
472            ptr_adt_search->replace_start_cursor_pos =
473                ptr_adt_search->found_cursor_pos;
474        }
475
476        //---------------------------------------------------- REPLACE
477        if( (ptr_adt_edit->found_matchp == 1) &&
478            (ptr_adt_search->replace_option != ADT_NO_REPLACE) ) {
479
480            ad_err = show_edit_replace(ptr_adt_search,ptr_adt_edit);
481            //////////////////////////////////////////////
482            if(ad_err)      return ad_err;
483        }
484        //-------------------------------------------------END REPLACE
485
486        //------------------------------------------repl and find next
487        if( (ptr_adt_search->replace_option == ADT_REPLACE_AND_SEARCH_NEXT) &&
488            (ptr_adt_search->string_replace == 1) )                 {
489            ptr_adt_search->string_replace = 0;
490            ptr_adt_search->search_start_cursor_pos =
491                ptr_adt_edit->actual_cursorpos_editor + 1;
492            ptr_adt_search->replace_option = ADT_NO_REPLACE;
493        }
494        //---------end repl/find next
495        else  {
496            if( (ptr_adt_search->replace_option ==  ADT_REPLACE_REST_SEQUENCE) ||
497                (ptr_adt_search->replace_option ==  ADT_REPLACE_REST_EDITOR) )  {
498
499                replace_loop = ptr_adt_search->replace_loop_sequence;
500                ptr_adt_search->search_start_cursor_pos =
501                    ( ptr_adt_edit->actual_cursorpos_editor + 1 );
502
503                // ABBRECHEN, sonst naechste Sequenz
504                if( replace_loop == ADT_STOPP_REPLACE ) {
505                    ptr_adt_edit->found_matchp = 1;
506                }
507                if (max_loop-- <0) replace_loop = ADT_STOPP_REPLACE;
508            } else {
509                replace_loop = ADT_STOPP_REPLACE;
510            }
511        }
512
513    } while(replace_loop);
514    //============================================================end loop
515    //====================================================================
516
517    delete ptr_adt_search->save_sequence;   //Sicherungskopie loeschen
518
519    //-----------------------------------------Zurueckschreiben aus Puffer
520    if( (ptr_adt_search->gaps == 0) ||  (ptr_adt_search->gaps == 1) ) {
521        rewrite_from_sequence_buffer(ptr_adt_search, ptr_adt_edit);
522        /////////////////////////////////////////////
523            } //end if,  >>Sequenz<< OHNE Gaps.
524    //---------------------------------------------------------end rewrite
525
526    //----------------------------------- zurueckschreiben in die Datenbank
527    if( (ptr_adt_search->string_replace == 1)  ||
528        (ptr_adt_edit->db_status == ADT_DB_OPEN) ) {
529        ad_err = put();
530        if(ad_err) {
531            //                      printf("**** ERROR  ADT_SEARCH  show_edit_replace(), put() \n");
532            return  ad_err;
533        }
534
535        show_update();
536    }
537    //---------------------------------------------------end write into DB
538
539    if( (ptr_adt_edit->found_matchp == 1) &&
540        (ptr_adt_search->string_replace == 0) ) {
541        ptr_adt_edit->actual_cursorpos_editor =
542            ptr_adt_search->found_cursor_pos;
543    }
544
545    //   nichts gefunden,
546    //---------------------------------- cursorpos in next sequence
547    if( (ptr_adt_edit->found_matchp == 0)  ||
548        (ptr_adt_search->replace_option == ADT_REPLACE_REST_EDITOR) ) {
549        if( ptr_adt_search->search_direction == ADT_SEARCH_FORWARD ) {
550            ptr_adt_edit->actual_cursorpos_editor   = 0;
551            ptr_adt_search->search_start_cursor_pos = 0;
552            //startet naechste Seq bei Cursorpos = 0
553        }
554        if( ptr_adt_search->search_direction == ADT_SEARCH_BACKWARD ) {
555            ptr_adt_edit->actual_cursorpos_editor   = max_value;
556            ptr_adt_search->search_start_cursor_pos = max_value;
557        }
558    }
559    //-------------------------------end cursorpos in next sequence
560
561    return 0;
562} // end func show_edit_seq_search()
563
564// **************************************************************************
565
566
567
568
569// **************************************************************************
570//
571// **************************************************************************
572AD_ERR * ADT_SEQUENCE::show_edit_search(ADT_SEARCH *ptr_adt_search,
573                                        ADT_EDIT *ptr_adt_edit) {
574
575    char    *seq_anfang, *seq_loop_start, *seq_loop_ptr;    // Suche Sequenz
576    char    *matchp_anfang, *matchp_loop_ptr;               // Suche
577    // Matchpattern
578
579    long    matchp_len;
580    long    vorgabefehler, realfehler=0, equal_chars;
581    long    seq_empty_var;
582    long    search_var;
583
584    search_var      =  ptr_adt_search->search_direction;
585
586    matchp_anfang   =  ptr_adt_search->matchpattern;
587    matchp_len      =  strlen(matchp_anfang);
588
589    seq_empty_var=0;
590    vorgabefehler = ptr_adt_search->mistakes_allowed;
591
592    seq_anfang      =  ptr_adt_search->seq_anfang;
593    long    var_start_cursor_pos = ptr_adt_search->search_start_cursor_pos;
594
595    seq_loop_start  =  seq_anfang + var_start_cursor_pos;
596
597    //---------------------------------------------------------start search
598    for(  ; (*seq_loop_start != '\0') &&
599              (seq_loop_start >= seq_anfang) ;
600          seq_loop_start+=search_var )     {
601        realfehler  = 0;
602        equal_chars = 0;
603        seq_loop_ptr=seq_loop_start;
604
605        for( matchp_loop_ptr=matchp_anfang ; *matchp_loop_ptr != '\0' ;
606             matchp_loop_ptr++ ) {
607
608            unsigned char index_seq = *(unsigned char *)seq_loop_ptr;
609            unsigned char index_mat=*(unsigned char*)matchp_loop_ptr;
610            char i_s = ptr_adt_search->search_array[(int)index_seq];
611            char i_m = ptr_adt_search->search_array[(int)index_mat];
612            if( i_m == '?' ) {      // Wilde Karten
613                equal_chars++;
614            }
615
616            else if( i_s != i_m ) {
617                realfehler++;
618            }
619            else {
620                equal_chars++;
621            }
622            //   mehr Fehler als erlaubt?
623            if(realfehler > vorgabefehler) {
624                ptr_adt_edit->found_matchp = 0;
625                break;
626            }
627            //gefunden? (noetig, da Restsequenz
628            //kuerzer
629            //als Suchstringlaenge sein kann.)
630            //Fehler + Uebereinstimmungen = Laenge
631            //des Matchpatterns
632            if(realfehler+equal_chars == matchp_len) {
633                ptr_adt_edit->found_matchp = 1;
634                ptr_adt_edit->mistakes_found = realfehler;
635                break;
636            }
637
638            seq_loop_ptr++;         //  Ende der Sequenz erreicht?
639            if( *seq_loop_ptr == '\0') {            //
640                seq_empty_var = 1;
641                break;
642            }
643
644        } // end for()
645
646        if( *(seq_loop_ptr+1) == '\0') {
647            ptr_adt_search->replace_loop_sequence =
648                ADT_STOPP_REPLACE;
649        }
650        //----------------------------------------search end found y/n
651
652        //---------------------------------found cursorpos in sequence
653        if( ptr_adt_edit->found_matchp == 1)  {
654            ptr_adt_search->found_cursor_pos =
655                seq_loop_start - seq_anfang;
656            break;
657        }
658        //-----------------------------------end cursorpos in sequence
659
660        if( (ptr_adt_search->replace_option == ADT_REPLACE_ONLY) ||
661            (ptr_adt_search->replace_option ==
662             ADT_REPLACE_AND_SEARCH_NEXT)) {
663            break;           //for schleife nur einmal ausfuehren.
664        }
665    } // end for()
666    //----------------------------------------------------------end search
667
668    return 0;
669}
670// **************************************************************************
671
672
673
674
675// **************************************************************************
676//
677//      Ueberprueft zuerst, ob die Sequenz ab der Cursorposition mit dem
678//      Matchpattern uebereinstimmt. Wenn ja, dann erfolgt die Ersetzung.
679//      Mit GAPS: durch remove(), und insert() der AD_SEQ-Klasse;
680//      Ohne Gaps: direktes ueberschreiben im Sequenzpuffer, danach
681//              ueberschreiben der Sequenzkopie im Cache.
682//      Beide Varianten schreiben die geaenderte Sequenz mittels put() in
683//      die Datenbank zurueck. (Security-Level nicht vergessen!!)
684//
685// **************************************************************************
686AD_ERR * ADT_SEQUENCE::show_edit_replace(ADT_SEARCH *ptr_adt_search,
687                                         ADT_EDIT *ptr_adt_edit)
688
689{
690    char    *seq_anfang, *seq_loop_start, *seq_loop_ptr;    // Sequenz
691    char    *matchp_anfang;
692    char    *replace_anfang, *replace_loop_ptr;     // Replace-String
693    AD_ERR  *ad_err;
694
695    matchp_anfang                   =  ptr_adt_search->matchpattern;
696    replace_anfang                  =  ptr_adt_search->replace_string;
697    seq_anfang                      =  ptr_adt_search->seq_anfang;
698
699    ptr_adt_search->string_replace = 0;
700    long    var_start_cursor_pos    =
701        ptr_adt_search->replace_start_cursor_pos;
702
703    seq_loop_start = seq_anfang + var_start_cursor_pos;
704    seq_loop_ptr   = seq_loop_start;
705
706    //============================================================REPLACE
707
708    long    match_len       = strlen(ptr_adt_search->matchpattern);
709    long    replace_len     = strlen(ptr_adt_search->replace_string);
710
711    if( (match_len == replace_len)  ||
712        (ptr_adt_search->gaps == 0) || (ptr_adt_search->gaps == 1) ) {
713
714        //----------------------------------------------------replace
715        seq_loop_ptr = seq_loop_start;
716        for( replace_loop_ptr = replace_anfang ;
717             *replace_loop_ptr != '\0' ; replace_loop_ptr++ ) {
718            *seq_loop_ptr = *replace_loop_ptr;
719            seq_loop_ptr++;
720        } // end for()
721    } // end if(match_len == replace_len)
722
723    else {
724        //-----------------------------------------------------remove
725        ad_err = remove( strlen(matchp_anfang),
726                         (int)var_start_cursor_pos,1  );
727        if (ad_err) {   // Fehler bei remove
728            replace(ptr_adt_search->save_sequence,0,1);
729            //                      printf("**** ERROR  REPLACE     remove() with GAPS\n");
730            free(ptr_adt_search->save_sequence);
731            return ad_err;
732        }
733
734        //-----------------------------------------------------insert
735        else if( strlen(replace_anfang) != 0 ){
736            ad_err = insert( replace_anfang,
737                             (int)var_start_cursor_pos ,1 );
738
739            if (ad_err) {   // Fehler bei replace
740                replace(ptr_adt_search->save_sequence,0,1);
741                //                              printf("**** ERROR  REPLACE     insert() with GAPS\n");
742                free(ptr_adt_search->save_sequence);
743                return ad_err;
744            }
745            ptr_adt_search->seq_anfang = get();
746        }
747    }
748    //========================================================END REPLACE
749    //-------------------------------------------------- Cursorpos Editor
750    ptr_adt_edit->actual_cursorpos_editor =
751        ptr_adt_search->replace_start_cursor_pos +
752        strlen(replace_anfang) - 1;
753    //-----------------------------------------------------END Cursorpos
754
755    ptr_adt_search->string_replace = 1;             //wir haben replaced!
756    return 0;
757
758}  // end fkt show_edit_replace()
759// *************************************************************************
760
761
762
763
764// *************************************************************************
765//      Invert/Complement - Haelt und arbeitet auf der Sequenz
766// *************************************************************************
767AD_ERR * ADT_SEQUENCE::show_edit_seq_compl(
768                                           ADT_COMPLEMENT *ptr_adt_complement, ADT_EDIT *ptr_adt_edit) {
769
770    AD_ERR  *ad_err;
771
772    ptr_adt_complement->sequence = get();
773
774    ptr_adt_complement->sequence_length =
775        (long)strlen(ptr_adt_complement->sequence);
776
777    //-------------------------------------------------------- border check
778    if( ptr_adt_complement->take_borders == YES ) {
779        if( (ptr_adt_complement->sequence_length - 1) <
780            ptr_adt_complement->right_border )  {
781            ptr_adt_complement->right_border =
782                ptr_adt_complement->sequence_length - 1;
783        }
784        if( (ptr_adt_complement->sequence_length - 1) <
785            ptr_adt_complement->left_border )  {
786            return 0;
787        }
788    }
789    //---------------------------------------------------- end border check
790
791    //-----------------------------------------------------fct complement()
792    if( ptr_adt_complement->complement_seq == YES )  {
793        show_edit_complement(ptr_adt_complement, ptr_adt_edit);
794        // *****************************************************
795    }
796    //-------------------------------------------------end fct complement()
797
798    //---------------------------------------------------------fct invert()
799    if( ptr_adt_complement->invert_seq == YES )  {
800        show_edit_invert(ptr_adt_complement, ptr_adt_edit);
801        // *****************************************************
802    }
803    //-----------------------------------------------------end fct invert()
804
805    if(     (ptr_adt_complement->seq_is_complemented == YES)  ||
806            (ptr_adt_complement->seq_is_inverted == YES)                    ) {
807        ptr_adt_edit->actual_cursorpos_editor = 0;
808    }
809
810    //----------------------------------- zurueckschreiben in die Datenbank
811    if( ptr_adt_edit->db_status == ADT_DB_OPEN ) {
812        ad_err = put();
813        if(ad_err) {
814            //                      printf("**** ERROR  ADT_COMPLEMENT, no rewrite into DB\n");
815            return  ad_err;
816        }
817
818        show_update();
819    }
820    //---------------------------------------------------end write into DB
821
822    //----------------------------------------------------------------------------
823    //testen
824#if 0
825    char* seq_loop   = ptr_adt_complement->sequence;
826    printf("\n================================================================\n");
827    printf("Species-name:           %s\n", ptr_adt_complement->species_name );
828    printf("Alignment-type:         %s == %d\n", ptr_adt_complement->alignment_type,
829           ptr_adt_complement->adt_acid);
830    printf("Alignment-name:         %s\n", ptr_adt_complement->alignment_name );
831    printf("SEQUENCE:\n");
832    //printf("SEQUENCE:   (ab Cursorpos 200)\n");
833    //seq_loop+=200;
834    for(int k=0; (k<100) && (*seq_loop); k++) {
835        printf("%c", *seq_loop);
836        seq_loop++;
837    }
838    printf("\n");
839    printf("alignment_length:       %d\n", ptr_adt_complement->alignment_length );
840    printf("Borders [left..right]:  [%d  ..  %d]\n",
841           ptr_adt_complement->left_border, ptr_adt_complement->right_border );
842    printf("seq_is_complemented:    %d\n", ptr_adt_complement->seq_is_complemented );
843    printf("seq_is_inverted:        %d\n", ptr_adt_complement->seq_is_inverted );
844    printf("which_button:   %d\n", ptr_adt_complement->which_button );
845#endif
846    //end test
847    //---------------------------------------------------------------------------
848    return 0;
849
850} // end show_edit_seq_compl()
851// *************************************************************************
852
853
854
855
856// *************************************************************************
857//      Fkt bildet das Complement
858// *************************************************************************
859
860AD_ERR * ADT_SEQUENCE::show_edit_complement(ADT_COMPLEMENT *ptr_adt_complement,
861                                            ADT_EDIT *ptr_adt_edit) {
862    char            *seq_start, *right_border;
863    char            *compl_start, *compl_loop;
864
865    seq_start    =  ptr_adt_complement->sequence;
866    compl_start  =  ptr_adt_complement->sequence;           //vorbelegung
867
868    right_border     = seq_start + max_value;
869    //--------------------------------------------------------ab Cursorpos
870    if(ptr_adt_complement->take_cursor == YES)  {
871        compl_start  = seq_start + ptr_adt_edit->actual_cursorpos_editor;
872    }
873    //-------------------------------------------------------end Cursorpos
874
875    //-----------------------------------------------------------Intervall
876    if(ptr_adt_complement->take_borders == YES)  {
877        compl_start  = seq_start + ptr_adt_complement->left_border;
878        right_border = seq_start + ptr_adt_complement->right_border;
879    }
880    //-------------------------------------------------------end Intervall
881
882    //-------------------------------------------------erledigt complement
883    for( compl_loop = compl_start;
884         (*compl_loop) && (compl_loop <= right_border) ; compl_loop++ )  {
885        *compl_loop = ptr_adt_complement->char_array[(int)*compl_loop];
886    }
887    ptr_adt_complement->seq_is_complemented = YES;
888    //------------------------------------------------------end complement
889
890    return 0;
891
892} // end fct show_edit_complement()
893// *************************************************************************
894
895
896
897// *************************************************************************
898//      Fkt fuehrt Invertierung durch  ( = REVERT )
899// *************************************************************************
900
901AD_ERR * ADT_SEQUENCE::show_edit_invert(ADT_COMPLEMENT *ptr_adt_complement,
902                                        ADT_EDIT *ptr_adt_edit) {
903    char  source;
904
905    char            *right_border;
906    char            *sequence_loop_start, *sequence_loop, *sequence;
907    char            *seq_buffer, *seq_buffer_loop_start, *seq_buffer_loop;
908    long            *index_buffer, *index_buffer_loop_start, *index_buffer_loop;
909
910    long            char_counter    = 0;
911
912    sequence                = ptr_adt_complement->sequence;
913    right_border    = sequence + max_value;
914
915    //------------------------------------------------------ make Seq/Puffer
916    ptr_adt_complement->complement_buffers();
917    //-------------------------------------------------- end make Seq/Puffer
918
919    seq_buffer              = ptr_adt_complement->sequence_buffer;
920    index_buffer            = ptr_adt_complement->index_buffer;
921
922    sequence_loop_start             = sequence;
923    seq_buffer_loop_start   = seq_buffer;
924    index_buffer_loop_start = index_buffer;
925
926    //--------------------------------------------------------ab Cursorpos
927    if( ptr_adt_complement->take_cursor == YES ) {
928        sequence_loop_start             += ptr_adt_edit->actual_cursorpos_editor;
929        seq_buffer_loop_start   += ptr_adt_edit->actual_cursorpos_editor;
930        index_buffer_loop_start += ptr_adt_edit->actual_cursorpos_editor;
931    }
932    //-------------------------------------------------------end Cursorpos
933
934    //------------------------------------------------------- take Borders
935    if( ptr_adt_complement->take_borders == YES ) {
936        sequence_loop_start             += ptr_adt_complement->left_border;
937        seq_buffer_loop_start   += ptr_adt_complement->left_border;
938        index_buffer_loop_start += ptr_adt_complement->left_border;
939        right_border            = sequence + ptr_adt_complement->right_border;
940    }
941    //-------------------------------------------------------- end Borders
942
943    seq_buffer_loop = seq_buffer_loop_start;
944    index_buffer_loop       = index_buffer_loop_start;
945
946    //========================================== kopieren der Seq in Puffer
947    for( sequence_loop = sequence_loop_start;
948         ( source = *sequence_loop) && (sequence_loop <= right_border) ;
949         sequence_loop++ ) {
950        if( !((source == '-') || (source == '.')) )
951        {       *( seq_buffer_loop++ )  = source;
952            *( index_buffer_loop++ )        = sequence_loop - sequence;
953            char_counter++;
954        }
955    }
956    //=============================================== end kopieren in Puffer
957
958    //========================================================== invertieren
959    char            *first, *last;
960    first   = seq_buffer_loop_start;
961    last            = seq_buffer + char_counter - 1;
962
963    for( ; first < last ; first++, last-- )
964    {       source  = *first;
965        *first  = *last;
966        *last   = source;
967    }
968    //====================================================== end invertieren
969
970    //================================================ueberschreiben der Seq
971
972    long            buffer_index = 0;
973
974    //---------------------------------------------------------- take cursor
975    if( ptr_adt_complement->take_cursor == YES ) {
976        buffer_index    = ptr_adt_edit->actual_cursorpos_editor;
977    }
978    //----------------------------------------------------------- end cursor
979
980    //--------------------------------------------------------- take borders
981    if(ptr_adt_complement->take_borders == YES)  {
982        buffer_index    = ptr_adt_complement->left_border;
983        right_border    = seq_buffer + ptr_adt_complement->right_border;
984    }
985
986    //---------------------------------------------------------- end borders
987
988    sequence_loop = sequence_loop_start;
989
990    if(ptr_adt_complement->remove_gaps_points == YES) {
991        for( seq_buffer_loop = seq_buffer_loop_start ;
992             (source = *seq_buffer_loop) && (seq_buffer_loop <= right_border);
993             seq_buffer_loop++ ) {
994            *(sequence_loop++) = source;
995        }
996    }
997    else {          //////////// ptr_adt_complement->remove_gaps_points == NO
998        for(    ; (source = seq_buffer[buffer_index]) &&
999                    (seq_buffer[buffer_index] != '.');
1000                buffer_index++) {
1001            *(sequence + index_buffer[buffer_index]) = source;
1002
1003        }
1004    }
1005    //=========================================== end ueberschreiben der Seq
1006    ptr_adt_complement->seq_is_inverted = YES;
1007
1008    free(ptr_adt_complement->sequence_buffer);
1009    free((char *)ptr_adt_complement->index_buffer);
1010    ptr_adt_complement->sequence_buffer     = NULL;
1011    ptr_adt_complement->index_buffer                = NULL;
1012
1013    return 0;
1014
1015} // end fct show_edit_invert()
1016// *************************************************************************
Note: See TracBrowser for help on using the repository browser.