1 /*   salutil.c
2 * ===========================================================================
3 *
4 *                            PUBLIC DOMAIN NOTICE
5 *            National Center for Biotechnology Information (NCBI)
6 *
7 *  This software/database is a "United States Government Work" under the
8 *  terms of the United States Copyright Act.  It was written as part of
9 *  the author's official duties as a United States Government employee and
10 *  thus cannot be copyrighted.  This software/database is freely available
11 *  to the public for use. The National Library of Medicine and the U.S.
12 *  Government do not place any restriction on its use or reproduction.
13 *  We would, however, appreciate having the NCBI and the author cited in
14 *  any work or product based on this material
15 *
16 *  Although all reasonable efforts have been taken to ensure the accuracy
17 *  and reliability of the software and data, the NLM and the U.S.
18 *  Government do not and cannot warrant the performance or results that
19 *  may be obtained by using this software or data. The NLM and the U.S.
20 *  Government disclaim all warranties, express or implied, including
21 *  warranties of performance, merchantability or fitness for any particular
22 *  purpose.
23 *
24 * ===========================================================================
25 *
26 * File Name:  salutil.c
27 *
28 * Author:  Colombe Chappey , Hugues Sicotte
29 *
30 * Version Creation Date:   1/27/96
31 *
32 * $Revision: 6.17 $
33 *
34 * File Description:
35 *
36 * Modifications:
37 * --------------------------------------------------------------------------
38 * Date     Name        Description of modification
39 * -------  ----------  -----------------------------------------------------
40 *
41 *
42 * ==========================================================================
43 */
44 
45 #include <ncbi.h>
46 #include <salutil.h>
47 #include <salstruc.h>
48 #include <salsap.h>
49 #include <salpacc.h>
50 #include <salpedit.h>
51 #include <edutil.h>
52 #include <sequtil.h>
53 #include <sqnutils.h>
54 
55 #ifdef SALSA_DEBUG
56 #include <simutil.h>
57 #include <toasn3.h>
58 #include <utilpub.h>
59 #include <tfuns.h>
60 #endif
61 
62 #define OBJ_VIRT 254
63 
64 #define BAND_LIMIT 100
65 
OBJ_(Uint2 feattype)66 static Uint2 OBJ_ (Uint2 feattype)
67 {
68   if ( feattype == FEATDEF_BAD ) return OBJ_BIOSEQ;
69   return OBJ_SEQFEAT;
70 }
71 
72 /****************************************************
73 ***   Read SeqPort.bsp from SeqPort.start to stop
74 ***   in : spp, from + to in seq coordinates
75 ***   out: length of buffer + buffer
76 ***        compiled with -D SAP
77 *****************************************************/
ReadBufferFromSep(SeqPortPtr spp,CharPtr buffer,Int4 from,Int4 to,Int4 buffsegstart)78 NLM_EXTERN Int4 ReadBufferFromSep (SeqPortPtr spp, CharPtr buffer, Int4 from, Int4 to, Int4 buffsegstart)
79 {
80   Uint1    residue;
81   Int4     k;
82   Int4     pos;
83 
84   SeqPortSeek (spp, from, SEEK_SET);
85   k = buffsegstart;
86   pos = from;
87   residue = SeqPortGetResidue(spp);
88   while (pos < to && residue != SEQPORT_EOF)
89   {
90     if ( ! IS_residue(residue)) {
91       /*
92       switch (residue)
93       {
94            case SEQPORT_VIRT:
95               Message(MSG_OK,"SEQPORT_VIRT [%d=%c] at %ld\n", (int)residue, (char)residue, (long)pos);
96               break;
97            case SEQPORT_EOS:
98               Message(MSG_OK,"[EOS]\n");
99               break;
100            default:
101               Message(MSG_OK,"unknown char\n");
102               break;
103       }
104       */
105       pos++;
106     } else {
107       buffer[k] = (Char) residue;
108       k++;
109       pos++;
110     }
111     residue = SeqPortGetResidue(spp);
112   }
113   buffer[k] = '\0';
114   return k;
115 }
116 
117 
118 /*****************************************************************************
119 ***  ReadBufferFromSap
120 ******************************************************************************/
ReadBufferFromSap(CharPtr str,CharPtr buffer,SeqAlignPtr salp,SeqIdPtr sip,Int4 from,Int4 to,Int4 * ncar)121 NLM_EXTERN CharPtr ReadBufferFromSap (CharPtr str, CharPtr buffer, SeqAlignPtr salp, SeqIdPtr sip, Int4 from, Int4 to, Int4 *ncar)
122 {
123   CompSegPtr  dsp;
124   BoolPtr     dspstart;
125   Int4Ptr     dsplens;
126   Int4        dspfrom;
127   Int4        sumstart;
128   Int4        bufflen, buffstart;
129   Int4        pre_from;
130   Int4        bsplens;
131   Int4        ibuff;
132   Int4        istr;
133   Int4        j = 0;
134   Int4        k = 0;
135   Int2        dim;
136   Int2        numseg;
137   Int2        index;
138   Boolean     seen = FALSE;
139   Boolean     nogap;
140   Boolean     ok;
141 
142   *ncar = 0;
143   if (buffer == NULL || salp == NULL) {
144          ErrPostEx (SEV_ERROR, 0, 0, "fail in ReadBufferFromSap [1]\n");
145          return NULL;
146   }
147   if ( salp->segtype != COMPSEG ) {
148          ErrPostEx (SEV_ERROR, 0, 0, "fatal fail in ReadBufferFromSap");
149          return NULL;
150   }
151   if ( (dsp = (CompSegPtr) salp->segs ) == NULL) {
152          ErrPostEx (SEV_ERROR, 0, 0, "fail in ReadBufferFromSap [3-2]\n");
153          return NULL;
154   }
155   if (dsp->from == NULL || dsp->starts == NULL || dsp->lens == NULL) {
156          ErrPostEx (SEV_ERROR, 0, 0, "fail in ReadBufferFromSap [3-3]\n");
157          return NULL;
158   }
159   index = SeqIdOrderInBioseqIdList (sip, dsp->ids);
160   if (index < 1) {
161          ErrPostEx (SEV_ERROR, 0, 0, "fail in ReadBufferFromSap [3-4]");
162          return NULL;
163   }
164   index -=1;
165   dim = dsp->dim;
166   dspfrom = *(dsp->from + index);
167   dspstart = dsp->starts + index;
168   dsplens = dsp->lens;
169   seen = LocateInSeqAlign (from, dim, dsp->numseg, &dspstart, &dsplens, &numseg,
170                            &pre_from, &bsplens);
171   if (!seen) {
172          ErrPostEx (SEV_ERROR, 0, 0, "fail in ReadBufferFromSap [4]\n");
173          return NULL;
174   }
175   ibuff = 0;
176   istr = 0;
177   ok = (ibuff < to - from);
178   if (!ok) {
179         ErrPostEx (SEV_ERROR, 0, 0, "fail in ReadBufferFromSap [6] %ld %ld %ld\n", (long) ibuff, (long) to, (long) from);
180         return NULL;
181   }
182   nogap = (Boolean)( *dspstart );
183   if ( nogap )
184     buffstart= (Int4)(dspfrom +bsplens +pre_from);
185   else  buffstart = 0;
186   bufflen = (Int4)MIN((Int4)(*dsplens -pre_from),(Int4)(to-from +1));
187   sumstart = 0;
188   while ( ok )
189   {
190      if ( nogap )
191      {
192         for (j = 0; j < bufflen; j++, ibuff++, istr++) {
193            buffer[ibuff] = str [istr];
194            k++;
195         }
196         buffer[ibuff] = '\0';
197         bsplens += *dsplens;
198      }
199      else
200      {
201         for (j = 0; j < bufflen; j++, ibuff++) {
202            buffer[ibuff] = '-';
203         }
204         buffer[ibuff] = '\0';
205      }
206      numseg++;
207      if (numseg > dsp->numseg) break;
208      dspstart += dim;
209      dsplens++;
210      nogap = (Boolean)(*dspstart);
211      if ( nogap )
212         buffstart = (Int4) (dspfrom +bsplens +pre_from);
213      else buffstart = 0;
214      bufflen = (Int4) MIN ((Int4)(*dsplens), (Int4) (to -from -ibuff +1));
215   }
216   *ncar = k;
217   return buffer;
218 }
219 
220 
221 /*------------------------------------------
222 --  String Functions
223 --------------------------------------------*/
CCStrToInt(CharPtr str,Int2Ptr intval)224 NLM_EXTERN Boolean CCStrToInt (CharPtr str, Int2Ptr intval)
225 {
226   Char     ch;
227   Int2     i;
228   Int2     len;
229   Char     local [64];
230   Boolean  nodigits;
231   Boolean  rsult;
232   int      val;
233 
234   rsult = FALSE;
235   if (intval != NULL) {
236     *intval = (Int2) 0;
237   }
238   len = (Int2) StringLen (str);
239   if (len != 0) {
240     rsult = TRUE;
241     nodigits = TRUE;
242     for (i = 0; i < len; i++) {
243       ch = str [i];
244       if (ch == ' ' || ch == '+' || ch == '-') {
245         rsult = rsult;
246       } else if (ch < '0' || ch > '9') {
247         rsult = FALSE;
248       } else {
249         nodigits = FALSE;
250       }
251     }
252     if (nodigits) {
253       rsult = FALSE;
254     }
255     if (rsult && intval != NULL) {
256       StringNCpy_0 (local, str, sizeof (local));
257       if (sscanf (local, "%d", &val) == 1) {
258         *intval = (Int2) val;
259       }
260     }
261   }
262   return rsult;
263 }
264 
CCStrToLong(CharPtr str,Int4Ptr longval)265 NLM_EXTERN Boolean CCStrToLong (CharPtr str, Int4Ptr longval)
266 {
267   Char     ch;
268   Int2     i;
269   Int2     len;
270   Char     local [64];
271   Boolean  nodigits;
272   Boolean  rsult;
273   long     val;
274 
275   rsult = FALSE;
276   if (longval != NULL) {
277     *longval = (Int4) 0;
278   }
279   len = (Int2) StringLen (str);
280   if (len != 0) {
281     rsult = TRUE;
282     nodigits = TRUE;
283     for (i = 0; i < len; i++) {
284       ch = str [i];
285       if (ch == ' ' || ch == '+' || ch == '-') {
286         rsult = rsult;
287       } else if (ch < '0' || ch > '9') {
288         rsult = FALSE;
289       } else {
290         nodigits = FALSE;
291       }
292     }
293     if (nodigits) {
294       rsult = FALSE;
295     }
296     if (rsult && longval != NULL) {
297       StringNCpy_0 (local, str, sizeof (local));
298       if (sscanf (local, "%ld", &val) == 1) {
299         *longval = (Int4) val;
300       }
301     }
302   }
303   return rsult;
304 }
305 
dashedstring(CharPtr buffer,Int4 maxbufflength)306 NLM_EXTERN CharPtr dashedstring (CharPtr buffer, Int4 maxbufflength)
307 {
308   Int4 j;
309 
310   for (j = 0; j < maxbufflength; j++)
311      buffer[j] = '-';
312   buffer[0] = '\0';
313   return buffer;
314 }
315 
emptystring(CharPtr buffer,Int4 maxbufflength)316 NLM_EXTERN CharPtr emptystring (CharPtr buffer, Int4 maxbufflength)
317 {
318   Int4 j;
319 
320   if (buffer == NULL)
321      return NULL;
322   for (j = 0; j < maxbufflength; j++) buffer[j] = ' ';
323   buffer[0] = '\0';
324   return buffer;
325 }
326 
not_empty_string(CharPtr str,Int4 lens)327 NLM_EXTERN Boolean not_empty_string (CharPtr str, Int4 lens)
328 {
329   CharPtr  strtmp;
330   Int4     j = 0;
331 
332   if (str == NULL) return FALSE;
333   strtmp = str;
334   for (; *strtmp != '\0' && *strtmp == ' ' && j < lens; j++, strtmp++ )
335          continue;
336   if ( j == lens || *strtmp == '\0' ) return FALSE;
337   return TRUE;
338 }
339 
stringhasnochar(CharPtr str,Int4 from,Int4 to)340 NLM_EXTERN Boolean stringhasnochar (CharPtr str, Int4 from, Int4 to)
341 {
342   Char  ch;
343 
344   if (str != NULL) {
345      if (from <= -1)
346         from = 0;
347      if (to <= (Int4)-1 || to > (Int4)StringLen(str))
348         to = StringLen(str);
349      if (from < to && from < (Int4)StringLen(str)) {
350         ch = TO_UPPER(str[from]);
351         while (ch != '\0' && from < to)
352         {
353            if (ch >= 'A' && ch <= 'Z') {
354               return FALSE;
355            }
356            from++;
357            ch = TO_UPPER(str[from]);
358         }
359      }
360   }
361   return TRUE;
362 }
363 
getgapsfromstring(CharPtr str,Int4 from,Int4 to,BoolPtr * gapline)364 NLM_EXTERN Int1 getgapsfromstring (CharPtr str, Int4 from, Int4 to, BoolPtr *gapline)
365 {
366   Char    ch;
367   BoolPtr boolgap;
368   Int4    nchar = 0;
369   Int4    len;
370 
371   if (str != NULL) {
372      boolgap = *gapline;
373      if (from <= -1)
374         from = 0;
375      if (to <= (Int4)-1 || to > (Int4)StringLen(str))
376         to = StringLen(str);
377      else
378         to = MIN (to, (Int4)StringLen(str));
379      len = (to-from);
380      if (from < to) {
381         ch = TO_UPPER(str[from]);
382         while (ch != '\0' && from < to)
383         {
384            if (ch >= 'A' && ch <= 'Z') {
385               nchar++;
386               *boolgap = FALSE;
387            }
388            else
389               *boolgap = TRUE;
390            from++;
391            ch = TO_UPPER(str[from]);
392            boolgap++;
393         }
394         if (nchar == 0)
395            return LINE_ONLYGAP;
396         if (nchar < len)
397            return LINE_WITHGAP;
398         return LINE_NOGAP;
399      }
400   }
401   return LINE_ONLYGAP;
402 }
403 
404 
stringhasnocharplus(CharPtr str)405 NLM_EXTERN Boolean stringhasnocharplus (CharPtr str)
406 {
407   Char  ch;
408 
409   if (str != NULL) {
410     ch = TO_UPPER(*str);
411     while (ch != '\0') {
412       if ((ch >= 'A' && ch <= 'Z') || ch == '-') {
413         return FALSE;
414       }
415       str++;
416       ch = TO_UPPER(*str);
417     }
418   }
419   return TRUE;
420 }
421 
purge_string(CharPtr * st)422 NLM_EXTERN CharPtr purge_string (CharPtr *st)
423 {
424   CharPtr str;
425   Int4    i, j, k, n;
426   Int4    lens;
427 
428   str = *st;
429   if (str==NULL)
430      return NULL;
431   lens = StringLen (str);
432   for (i =0; i <lens; i++) {
433          if (str[i] == ' ')
434          {
435                 j =1;
436                 while (str[i+j] == ' ') j++;
437                 lens--;
438                 for (k =j, n =0; k <lens; k++, n++) {
439                        str[i+n] = str[k];
440                 }
441          }
442   }
443   str[lens] = '\0';
444   return str;
445 }
446 
reverse_string(CharPtr str)447 NLM_EXTERN CharPtr reverse_string (CharPtr str)
448 {
449   Char    car;
450   Int4    length;
451   Int4    j;
452 
453   if (str==NULL)
454      return NULL;
455   length = StringLen (str);
456   for (j = 0; j < length / 2; j++)
457   {
458      car = str[j]; str[j] = str[length-j-1]; str[length-j-1] = car;
459   }
460   return str;
461 }
462 
to_lower(CharPtr str)463 NLM_EXTERN CharPtr to_lower (CharPtr str)
464 {
465   CharPtr tmp;
466 
467   if (str==NULL)
468      return NULL;
469   tmp = str;
470   while (*tmp!='\n' && *tmp!='\0') {
471      *tmp = TO_LOWER(*tmp);
472      tmp++;
473   }
474   return str;
475 }
476 
477 /******************************************************************
478 ****      complement_string
479 ****
480 *******************************************************************/
complement_string(CharPtr str)481 NLM_EXTERN CharPtr complement_string (CharPtr str)
482 {
483   CharPtr strp;
484 
485   for (strp = str; *strp != '\0'; strp++) {
486          if (*strp == 'a') *strp = 't';
487          else if (*strp == 't') *strp = 'a';
488          else if (*strp == 'c') *strp = 'g';
489          else if (*strp == 'g') *strp = 'c';
490          else if (*strp == 'A') *strp = 'T';
491          else if (*strp == 'T') *strp = 'A';
492          else if (*strp == 'C') *strp = 'G';
493          else if (*strp == 'G') *strp = 'C';
494   }
495   *strp = '\0';
496   return str;
497 }
498 
499 /*************************************************************
500 ***
501 *** compare_string
502 ***    compare 2 string by sliding in ONE direction only
503 ***    str2 slides along str1
504 ***    returns the position where the PERCENT of matches and
505 ***    sequence length compared are greater
506 ***
507 ***************************************************************/
compare_string(CharPtr str1,CharPtr str2,Int4 * bestscorep)508 NLM_EXTERN Int4 compare_string (CharPtr str1, CharPtr str2, Int4 *bestscorep)
509 {
510   Int4  len1, len2;
511   Int4  length = 0;
512   Int4  score = 0;
513   Int4  ratio;
514   Int4  longer = 0;
515   Int4  best_ratio = 0;
516   Int4  best_pos = -1;
517   Int4  j, k;
518 
519   if (str1 == NULL || str2 == NULL)
520      return -1;
521   len1 = StringLen (str1);
522   len2 = StringLen (str2);
523   for (j=0; j<len1; j++) {
524      length = 0;
525      score = 0;
526      for (k=0; k<MIN(len2, len1-j); k++) {
527         length ++;
528         if (str1[j + k] == str2[k])
529            score ++;
530      }
531      ratio = (Int4) (100*score/length);
532      if (ratio > best_ratio || (ratio == best_ratio && length > longer))
533      {
534         best_ratio = ratio;
535         longer = length;
536         best_pos = j;
537      }
538   }
539   if (bestscorep != NULL) {
540      *bestscorep = best_ratio;
541   }
542   return best_pos;
543 }
544 
545 /*************************************************************
546 ***
547 *** load_seq_data
548 ***    loads bioseq sequence into a string
549 ***    sip: SeqId of the bioseq
550 ***    from, to: included bondaries
551 ***    returns the length of the string (lenp)
552 ***
553 ***************************************************************/
load_seq_data(SeqIdPtr sip,Int4 from,Int4 to,Boolean is_prot,Int4 * lenp)554 NLM_EXTERN CharPtr load_seq_data (SeqIdPtr sip, Int4 from, Int4 to, Boolean is_prot, Int4 *lenp)
555 {
556   BioseqPtr        bsp;
557   SeqLocPtr        slp;
558   SeqPortPtr       spp;
559   CharPtr          str = NULL;
560   Int4             lens;
561 
562   if (from > -1 && to > -1 && from >= to)
563      return NULL;
564   bsp = BioseqLockById (sip);
565   if (bsp != NULL) {
566      if (from < 0 || from > bsp->length -1)
567         from = 0;
568      if (to < 0 || to > bsp->length -1)
569         to = bsp->length -1;
570      BioseqUnlock (bsp);
571      if (from < to)
572         slp = SeqLocIntNew (from, to, Seq_strand_plus, sip);
573      else
574         slp = SeqLocIntNew (to, from, Seq_strand_minus, sip);
575      if (is_prot)
576         spp = SeqPortNewByLoc (slp, Seq_code_ncbieaa);
577      else
578         spp = SeqPortNewByLoc (slp, Seq_code_iupacna);
579      if (spp != NULL) {
580         str = (CharPtr)MemNew ((to-from+4) * sizeof(Char));
581         lens = ReadBufferFromSep (spp, str, 0, to -from +1, 0);
582         SeqPortFree (spp);
583         if (lenp != NULL)
584            *lenp = lens;
585      }
586      SeqLocFree (slp);
587   }
588   return str;
589 }
590 
591 
IS_protSeqLoc(SeqLocPtr slp)592 NLM_EXTERN Boolean IS_protSeqLoc (SeqLocPtr slp)
593 {
594   CharPtr seq;
595   Int4 len;
596 
597   seq = load_seq_data (SeqLocId(slp), SeqLocStart(slp), SeqLocStop(slp), TRUE, &len);
598   return FALSE;
599 }
600 
601 /*************************************************************
602 ***
603 ***  StringToSeqEntry :
604 ***    in:  sequence (CharPtr) + name + length of the alignment
605 ***    out: SeqEntryPtr
606 ***
607 ***************************************************************/
StringToSeqEntry(CharPtr str,SeqIdPtr sip,Int4 length_align,Uint1 mol_type)608 NLM_EXTERN SeqEntryPtr StringToSeqEntry (CharPtr str, SeqIdPtr sip, Int4 length_align, Uint1 mol_type)
609 {
610   SeqEntryPtr  sep;
611   BioseqPtr    bsp;
612   ByteStorePtr bs;
613   Char         ch;
614 
615   if (str == NULL || sip == NULL) return NULL;
616   sep = SeqEntryNew ();
617   if (sep == NULL) return NULL;
618   bsp = BioseqNew ();
619   if (bsp == NULL) {
620          ValNodeFree (sep);
621          return NULL;
622   }
623   sep->choice = 1;
624   sep->data.ptrvalue = (Pointer) bsp;
625   bsp->id = SeqIdDup (sip);
626   bsp->id->next = NULL;
627   SeqMgrAddToBioseqIndex (bsp);
628   bsp->repr = Seq_repr_raw;
629   if ( ISA_na (mol_type) ) {
630          bsp->mol = Seq_mol_na;
631          bsp->seq_data_type = Seq_code_iupacna;
632   }
633   else {
634          bsp->mol = Seq_mol_aa;
635          bsp->seq_data_type = Seq_code_ncbieaa;
636   }
637   bsp->length = 0;
638   bs = BSNew (length_align);
639   bsp->seq_data = (SeqDataPtr) bs;
640   while ((ch = *str) != '\0' && ch != ';' && bsp->length < length_align) {
641          ch = TO_UPPER (ch);
642          if ( ISA_na (mol_type) ) {
643                 if (ch == 'U') ch = 'T';
644                 if (ch == 'X') ch = 'N';
645                 if ( StringChr ("EFIJLOPQXZ-.*", ch) == NULL ) {
646                        BSPutByte ( bs, (Int2) ch );
647                        bsp->length++;
648                 }
649          }
650          else {
651                 if ( StringChr("JO-.", ch) == NULL ) {
652                        BSPutByte ( bs, (Int2) ch );
653                        bsp->length++;
654                 }
655          }
656          str++;
657   }
658   if ( bsp->length == 0 ) {
659          BioseqFree (bsp);
660          ValNodeFree (sep);
661          return NULL;
662   }
663   return sep;
664 }
665 
666 /*******************************************
667 ***
668 ***
669 ********************************************/
ValNodeFind(ValNodePtr head,Int2 start,Int2 index)670 NLM_EXTERN ValNodePtr ValNodeFind (ValNodePtr head, Int2 start, Int2 index)
671 {
672   Int2       j;
673 
674   if ( head == NULL ) return NULL;
675   for (j = start; j < index && head != NULL; j++, head = head->next)
676           continue;
677   return head;
678 }
679 
ValNodeFreeType(ValNodePtr * head,Int2 seqtype)680 NLM_EXTERN ValNodePtr ValNodeFreeType (ValNodePtr *head, Int2 seqtype)
681 {
682   ValNodePtr   vnp;
683 
684   if ( ( vnp = *head ) == NULL ) return NULL;
685   for ( ; vnp != NULL; vnp = vnp->next ) {
686          switch ( seqtype ) {
687            case TypeEmpty: { }
688                break;
689            case TypeSeqInt :
690                SeqIntFree ((SeqIntPtr) vnp->data.ptrvalue);
691                break;
692            case TypeSeqId :
693                SeqIdFree ((SeqIdPtr) vnp->data.ptrvalue);
694                break;
695            case TypeSeqLoc :
696                SeqLocFree ((SeqLocPtr) vnp->data.ptrvalue);
697                break;
698            case TypeSelStruct :
699                SelStructDel ((SelStructPtr) vnp->data.ptrvalue);
700                break;
701            case TypeSelEdStruct :
702                SelEdStructDel ((SelEdStructPtr) vnp->data.ptrvalue);
703                break;
704            default:    break;
705          }
706          vnp->data.ptrvalue = NULL;
707   }
708   ValNodeFree (vnp);
709   vnp = NULL;
710   return NULL;
711 }
712 
seqloc2fuzzloc(SeqLocPtr slp,Boolean is_from,Boolean is_to)713 NLM_EXTERN SeqLocPtr seqloc2fuzzloc(SeqLocPtr slp, Boolean is_from, Boolean is_to)
714 {
715    IntFuzzPtr fuzz;
716    SeqIntPtr sint;
717 
718 	   sint = (SeqIntPtr)slp->data.ptrvalue;
719 	   if(is_from){
720 	   	sint->if_from = IntFuzzNew();
721 	   	fuzz = sint->if_from;
722 	   	fuzz->choice = 4;
723 	   	fuzz->a =2;
724 	   }
725 	   if(is_to){
726 	     	sint->if_to = IntFuzzNew();
727 	     	fuzz = sint->if_to;
728 	     	fuzz->choice =4;
729 	     	fuzz->a =1;
730 	   }
731 	   return slp;
732 }
733 
TextAlignBufFind(ValNodePtr anpvnp,Uint2 entityID,Uint4 itemID,Uint2 itemtype)734 NLM_EXTERN TextAlignBufPtr TextAlignBufFind (ValNodePtr anpvnp, Uint2 entityID, Uint4 itemID, Uint2 itemtype)
735 {
736   ValNodePtr      vnp;
737   TextAlignBufPtr tap;
738   Uint2           tentityID;
739 
740   if ( anpvnp == NULL ) return NULL;
741   for (vnp = anpvnp; vnp != NULL; vnp = vnp->next)
742   {
743          tap = (TextAlignBufPtr) vnp->data.ptrvalue;
744          if ( tap != NULL)
745          {
746             if (OBJ_(tap->feattype) == OBJ_BIOSEQ) tentityID = tap->seqEntityID;
747             else tentityID = tap->entityID;
748             if (tentityID == entityID && tap->itemID == itemID && OBJ_(tap->feattype) == itemtype)
749                break;
750          }
751   }
752   if (vnp==NULL) return NULL;
753   return tap;
754 }
755 
buf2array(ValNodePtr list,Int2 seq1,Int2 seq2)756 NLM_EXTERN CharPtr PNTR buf2array (ValNodePtr list, Int2 seq1, Int2 seq2)
757 {
758   CharPtr  PNTR   tabp = NULL;
759   ValNodePtr      listmp;
760   TextAlignBufPtr tdp;
761   Int2            nrows, j;
762 
763   nrows = seq2-seq1+1;
764   if (nrows > 1 )  {
765      tabp = (CharPtr PNTR) MemNew ((size_t)((nrows+4)*sizeof(CharPtr)));
766      if (tabp != NULL)  {
767         for (j=0; j<nrows+4; j++)
768            tabp[j] = NULL;
769         j = 0;
770         for (listmp=list; listmp!=NULL; listmp=listmp->next)
771         {
772            tdp = (TextAlignBufPtr) listmp->data.ptrvalue;
773            if (tdp!=NULL) {
774               if (OBJ_(tdp->feattype) == OBJ_BIOSEQ) {
775                  if (tdp->buf != NULL && (j>=seq1 && j<=seq2)) {
776                     tabp[j-seq1] = tdp->buf;
777                     j++;
778                  }
779               }
780            }
781         }
782      }
783   }
784   return tabp;
785 }
786 
AlignNodeFind(ValNodePtr anpvnp,Uint2 entityID,Uint4 itemID,Uint2 itemtype)787 NLM_EXTERN AlignNodePtr AlignNodeFind (ValNodePtr anpvnp, Uint2 entityID, Uint4 itemID, Uint2 itemtype)
788 {
789   ValNodePtr   vnp;
790   AlignNodePtr anp;
791 
792   if ( itemtype != OBJ_BIOSEQ ) return NULL;
793   if ( anpvnp == NULL ) return NULL;
794   for (vnp = anpvnp; vnp != NULL; vnp = vnp->next) {
795          if ( (anp = (AlignNodePtr) vnp->data.ptrvalue) != NULL)
796          {
797               if ( anp->seq_entityID == entityID && anp->bsp_itemID == itemID )
798                    break;
799          }
800   }
801   if ( vnp == NULL ) return NULL;
802   return anp;
803 }
804 
805 
AlignNodeIndex(ValNodePtr anpvnp,Uint2 entityID,Uint4 itemID,Uint2 itemtype)806 NLM_EXTERN Int2 AlignNodeIndex (ValNodePtr anpvnp, Uint2 entityID, Uint4 itemID, Uint2 itemtype)
807 {
808   ValNodePtr   vnp;
809   AlignNodePtr anp;
810   Int2         index = 0;
811 
812   if ( itemtype != OBJ_BIOSEQ ) return 0;
813   if ( anpvnp == NULL ) return 0;
814   for (vnp = anpvnp; vnp != NULL; vnp = vnp->next, index++) {
815          if ( (anp = (AlignNodePtr) vnp->data.ptrvalue) != NULL)
816          {
817               if ( anp->seq_entityID == entityID && anp->bsp_itemID == itemID )
818                    break;
819          }
820   }
821   if ( vnp == NULL ) return 0;
822   return index;
823 }
824 
825 /******************************************************************/
OrderFeatProc(ValNodePtr vnpanp)826 NLM_EXTERN void OrderFeatProc (ValNodePtr vnpanp)
827 {
828 /*
829   ValNodePtr   vnp;
830   AlignNodePtr anp;
831   AlignSegPtr  asp;
832 
833   for (vnp = vnpanp; vnp != NULL; vnp = vnp->next)
834   {
835      anp = (AlignNodePtr) vnp->data.ptrvalue;
836      for(asp = anp->segs; asp !=NULL; asp = asp->next)
837      {
838         if (asp->cnp != NULL)
839            OrderCdsmRNA (&(asp->cnp));
840      }
841   }
842 */
843   return;
844 }
845 
846 /******************************************************************/
SeqfeatlistFree(ValNodePtr feathead)847 NLM_EXTERN ValNodePtr SeqfeatlistFree (ValNodePtr feathead)
848 {
849   ValNodePtr     vnp, next;
850   SelEdStructPtr sesp;
851 
852   if (feathead==NULL)
853      return NULL;
854   vnp = feathead;
855   while (vnp != NULL)
856   {
857      next = vnp->next;
858      if (vnp->data.ptrvalue != NULL) {
859         sesp = (SelEdStructPtr) vnp->data.ptrvalue;
860         vnp->data.ptrvalue = SelEdStructListDel (sesp);
861      }
862      vnp = next;
863   }
864   ValNodeFree (vnp);
865   return NULL;
866 }
867 
868 /******************************************************************/
SeqfeatlistFree_fromID(ValNodePtr feathead,Uint2 entityID)869 NLM_EXTERN ValNodePtr SeqfeatlistFree_fromID (ValNodePtr feathead, Uint2 entityID)
870 {
871   ValNodePtr     vnp, pre, next;
872   SelEdStructPtr sesp;
873 
874   vnp = feathead;
875   pre = NULL;
876   while (vnp != NULL)
877   {
878      next = vnp->next;
879      if (vnp->data.ptrvalue != NULL) {
880         sesp = (SelEdStructPtr) vnp->data.ptrvalue;
881         if (sesp->entityID == entityID) {
882            vnp->data.ptrvalue = SelEdStructListDel (sesp);
883            vnp->next = NULL;
884            if (pre != NULL) pre->next = next;
885            else feathead = next;
886            ValNodeFree (vnp);
887         }
888         else pre = vnp;
889      }
890      vnp = next;
891   }
892   return feathead;
893 }
894 
895 /******************************************************************/
get_feat_fromid(ValNodePtr feat_vnp,Uint2 feattype,Uint2 ei,Uint4 ii,Int4 pos,SelEdStructPtr * prec)896 NLM_EXTERN SelEdStructPtr get_feat_fromid (ValNodePtr feat_vnp, Uint2 feattype, Uint2 ei, Uint4 ii, Int4 pos, SelEdStructPtr *prec)
897 {
898   ValNodePtr     vnp;
899   SelEdStructPtr sesp = NULL;
900   SeqLocPtr      slp;
901   Boolean        precedent;
902 
903   if (feat_vnp == NULL )
904          return NULL;
905   if (prec != NULL) precedent = TRUE;
906   else precedent = FALSE;
907   for (vnp = feat_vnp; vnp != NULL; vnp = vnp->next)
908   {
909          if (vnp->choice == feattype || feattype == 255) {
910             sesp = (SelEdStructPtr) vnp->data.ptrvalue;
911             if (sesp->entityID == ei && sesp->itemID == ii)
912             {
913                if (pos < 0)
914                   return sesp;
915                else {
916                   if (precedent) *prec = NULL;
917                   for (; sesp != NULL; sesp = sesp->next) {
918                      if (sesp->regiontype == OM_REGION_SEQLOC && sesp->region != NULL)
919                      {
920                         slp = (SeqLocPtr) sesp->region;
921                         if (pos >= SeqLocStart(slp) && pos <= SeqLocStop(slp))
922                            return sesp;
923                      }
924                      if (precedent) *prec = sesp;
925                   }
926                }
927             }
928          }
929   }
930   return NULL;
931 }
932 
933 /******************************************************************/
CollectSeqLocFromAlignNode(AlignNodePtr anp)934 NLM_EXTERN SeqLocPtr CollectSeqLocFromAlignNode (AlignNodePtr anp)
935 {
936   AlignSegPtr asp;
937   SeqLocPtr slp = NULL;
938   Int4 start, stop;
939   Uint1 strand;
940   Int4 current_pos;
941   Int4 seglen;
942 
943   current_pos = anp->seqpos;
944   if(anp->seqpos < 0)
945          strand = Seq_strand_minus;
946   else
947          strand = Seq_strand_plus;
948   for(asp = anp->segs; asp !=NULL; asp = asp->next)
949   {
950          seglen = 0;
951          if(asp->type == REG_SEG || asp->type == DIAG_SEG)
952          {
953                 seglen = asp->gr.right - asp->gr.left +1;
954                 if(strand == Seq_strand_minus)
955                 {
956                        stop = -current_pos;
957                        start = stop - (seglen-1);
958                 }
959                 else
960                 {
961                        start = current_pos;
962                        stop = start + (seglen -1);
963                 }
964                 if(slp == NULL)
965                        slp = SeqLocIntNew (start, stop, strand, anp->sip);
966                 else
967                        expand_seq_loc (start, stop, strand, slp);
968          }
969          current_pos += seglen;
970   }
971   return slp;
972 }
973 
GetAlignLengthFromAlignNode(AlignNodePtr anp)974 NLM_EXTERN Int4 GetAlignLengthFromAlignNode (AlignNodePtr anp)
975 {
976   AlignSegPtr asp;
977   Int4 lens;
978 
979   lens = 0;
980   for (asp = anp->segs; asp !=NULL; asp = asp->next)
981   {
982          if (asp->type == INS_SEG)
983                 lens += asp->gr.right;
984          else
985                 lens += asp->gr.right - asp->gr.left +1;
986   }
987   return lens;
988 }
989 
990 /******************************************************************/
SeqIdFromAlignNode(ValNodePtr anp_lst,Uint2 entityID,Uint4 itemID,Uint2 itemtype)991 NLM_EXTERN SeqIdPtr SeqIdFromAlignNode (ValNodePtr anp_lst, Uint2 entityID, Uint4 itemID, Uint2 itemtype)
992 {
993   AlignNodePtr anp;
994 
995   anp = (AlignNodePtr) AlignNodeFind (anp_lst, entityID, itemID, itemtype);
996   if (anp == NULL)
997      return NULL;
998   return anp->sip;
999 }
1000 
StrandFromAlignNode(ValNodePtr anp_lst,Uint2 entityID,Uint4 itemID,Uint2 itemtype)1001 NLM_EXTERN Uint1 StrandFromAlignNode (ValNodePtr anp_lst, Uint2 entityID, Uint4 itemID, Uint2 itemtype)
1002 {
1003   AlignNodePtr anp;
1004 
1005   anp = (AlignNodePtr) AlignNodeFind (anp_lst, entityID, itemID, itemtype);
1006   if (anp == NULL)
1007      return Seq_strand_unknown;
1008   return anp->extremes.strand;
1009 }
1010 
1011 /*********************************************************
1012 ***
1013 ***  SeqIdPtr procedures
1014 **********************************************************/
matching_seqid(SeqIdPtr sip1)1015 NLM_EXTERN CharPtr matching_seqid (SeqIdPtr sip1)
1016 {
1017   SeqIdPtr siptmp1, siptmp2;
1018   Boolean  first = TRUE;
1019   Char     strLog[120];
1020   CharPtr  str;
1021 
1022   for (siptmp1 = sip1; siptmp1!=NULL; siptmp1=siptmp1->next) {
1023     first = TRUE;
1024     for (siptmp2 = sip1; siptmp2!=NULL; siptmp2=siptmp2->next)
1025     {
1026        if (SeqIdForSameBioseq(siptmp1, siptmp2))  {
1027           if (first)
1028              first = FALSE;
1029           else {
1030              SeqIdWrite(siptmp1, strLog, PRINTID_FASTA_LONG, 120);
1031              str = StringSave(strLog);
1032              return str;
1033           }
1034        }
1035     }
1036   }
1037   return NULL;
1038 }
1039 
ValNodeSeqIdListDup(ValNodePtr id_list)1040 NLM_EXTERN SeqIdPtr ValNodeSeqIdListDup (ValNodePtr id_list)
1041 {
1042   ValNodePtr   vnp=NULL;
1043   SeqIdPtr     sip = NULL;
1044 
1045   for (vnp = id_list; vnp != NULL; vnp = vnp->next)
1046   {
1047          sip = AddSeqId (&sip, (SeqIdPtr) vnp->data.ptrvalue);
1048   }
1049   return sip;
1050 }
1051 /*******************************************************
1052 ***
1053 ***   SeqIdListToCharArray
1054 ***
1055 *******************************************************/
SeqIdListToCharArray(SeqIdPtr id_list,Int2 n)1056 NLM_EXTERN CharPtr PNTR SeqIdListToCharArray (SeqIdPtr id_list, Int2 n)
1057 {
1058   CharPtr PNTR  idarray;
1059   SeqIdPtr      idtmp;
1060   Int2          k;
1061 
1062   idarray = (CharPtr PNTR)MemNew ((size_t) ((n+1) * sizeof(CharPtr)));
1063   idtmp = id_list;
1064   for (k = 0; k < n && idtmp!=NULL; k++, idtmp =idtmp->next) {
1065          idarray [k] = (CharPtr) MemNew ((size_t) ((52) * sizeof(Char)));
1066          SeqIdWrite (idtmp, idarray [k], PRINTID_FASTA_SHORT, 50);
1067   }
1068   return idarray;
1069 }
1070 
1071 /******************************************************************/
SeqIdReplaceID(SeqIdPtr head,SeqIdPtr pre,SeqIdPtr sip,SeqIdPtr next)1072 NLM_EXTERN SeqIdPtr SeqIdReplaceID (SeqIdPtr head, SeqIdPtr pre, SeqIdPtr sip, SeqIdPtr next)
1073 {
1074   SeqIdPtr tmp;
1075 
1076   if (pre == NULL)
1077   {
1078      head = SeqIdDup(sip);
1079      head->next = next;
1080      return head;
1081   }
1082   tmp = pre->next;
1083   pre->next = NULL;
1084   tmp->next = NULL;
1085   SeqIdFree (tmp);
1086   pre->next = SeqIdDup(sip);
1087   pre->next->next = next;
1088   return head;
1089 }
1090 
1091 typedef struct ccid {
1092   SeqIdPtr    sip;
1093   SeqEntryPtr sep;
1094   BioseqPtr   bsp;
1095 } CcId, PNTR CcIdPtr;
1096 
1097 
ReplaceSeqIdWithSeqIdInFeat(SeqIdPtr old_id,SeqIdPtr new_id,SeqFeatPtr sfp)1098 NLM_EXTERN void ReplaceSeqIdWithSeqIdInFeat (SeqIdPtr old_id, SeqIdPtr new_id, SeqFeatPtr sfp)
1099 {
1100   SeqIdPtr      current_sip;
1101   RnaRefPtr     rrp;
1102   tRNAPtr       trp;
1103   CodeBreakPtr  cbp;
1104   CdRegionPtr   crp;
1105 
1106   if (sfp == NULL || old_id == NULL || new_id == NULL)
1107   {
1108     return;
1109   }
1110 
1111   current_sip = SeqLocId (sfp->location);
1112   if (SeqIdIn (current_sip, old_id))
1113   {
1114     sfp->location = SeqLocReplaceID (sfp->location, new_id);
1115   }
1116   current_sip = SeqLocId (sfp->product);
1117   if (SeqIdIn (current_sip, old_id))
1118   {
1119     sfp->product = SeqLocReplaceID (sfp->product, new_id);
1120   }
1121   if (sfp->data.choice == SEQFEAT_RNA
1122       && (rrp = (RnaRefPtr) sfp->data.value.ptrvalue) != NULL
1123       && rrp->ext.choice == 2
1124       && (trp = (tRNAPtr) rrp->ext.value.ptrvalue) != NULL
1125       && trp->anticodon != NULL)
1126   {
1127     current_sip = SeqLocId (trp->anticodon);
1128     if (SeqIdIn (current_sip, old_id))
1129     {
1130       trp->anticodon = SeqLocReplaceID (trp->anticodon, new_id);
1131     }
1132   }
1133   else if (sfp->data.choice == SEQFEAT_CDREGION
1134            && (crp = (CdRegionPtr) sfp->data.value.ptrvalue) != NULL)
1135   {
1136     for (cbp = crp->code_break; cbp != NULL; cbp = cbp->next) {
1137       current_sip = SeqLocId (cbp->loc);
1138       if (SeqIdIn (current_sip, old_id))
1139       {
1140         cbp->loc = SeqLocReplaceID (cbp->loc, new_id);
1141       }
1142     }
1143   }
1144 }
1145 
1146 
1147 typedef struct replaceseqid
1148 {
1149   SeqIdPtr old_id;
1150   SeqIdPtr new_id;
1151 } ReplaceSeqIdData, PNTR ReplaceSeqIdPtr;
1152 
ReplaceSeqIdFeatCallback(SeqFeatPtr sfp,Pointer userdata)1153 static void ReplaceSeqIdFeatCallback (SeqFeatPtr sfp, Pointer userdata)
1154 {
1155   ReplaceSeqIdPtr rsip;
1156 
1157   if (sfp == NULL || userdata == NULL)
1158   {
1159     return;
1160   }
1161   rsip = (ReplaceSeqIdPtr) userdata;
1162   if (rsip->old_id == NULL || rsip->new_id == NULL)
1163   {
1164     return;
1165   }
1166   ReplaceSeqIdWithSeqIdInFeat (rsip->old_id, rsip->new_id, sfp);
1167 }
1168 
1169 
ReplaceSeqIdInGraphCallback(SeqGraphPtr sgp,Pointer userdata)1170 static void ReplaceSeqIdInGraphCallback (SeqGraphPtr sgp, Pointer userdata)
1171 {
1172   ReplaceSeqIdPtr rsip;
1173   SeqIdPtr        current_sip;
1174 
1175   if (sgp == NULL || sgp->loc == NULL || userdata == NULL)
1176   {
1177     return;
1178   }
1179   rsip = (ReplaceSeqIdPtr) userdata;
1180   if (rsip->old_id == NULL || rsip->new_id == NULL)
1181   {
1182     return;
1183   }
1184   current_sip = SeqLocId (sgp->loc);
1185   if (SeqIdIn (current_sip, rsip->old_id))
1186   {
1187     sgp->loc = SeqLocReplaceID (sgp->loc, rsip->new_id);
1188   }
1189 }
1190 
1191 
SeqAnnotReplaceID(SeqAnnotPtr sap,SeqIdPtr newsip)1192 static void SeqAnnotReplaceID (SeqAnnotPtr sap, SeqIdPtr newsip)
1193 {
1194   SeqFeatPtr  sfp;
1195 
1196   while (sap != NULL) {
1197     if (sap->type == 1) {
1198       sfp = (SeqFeatPtr) sap->data;
1199       while (sfp != NULL) {
1200         if (sfp->location != NULL)
1201            sfp->location = SeqLocReplaceID (sfp->location, newsip);
1202         sfp = sfp->next;
1203       }
1204     }
1205     sap = sap->next;
1206   }
1207 }
1208 
BioseqReplaceID(BioseqPtr bsp,SeqIdPtr newsip)1209 NLM_EXTERN BioseqPtr BioseqReplaceID (BioseqPtr bsp, SeqIdPtr newsip)
1210 {
1211   Uint2            entityID;
1212   SeqEntryPtr      sep;
1213   ReplaceSeqIdData rsid;
1214 
1215   if (bsp == NULL || bsp->id == NULL || newsip == NULL)
1216   {
1217     return bsp;
1218   }
1219 
1220   entityID = ObjMgrGetEntityIDForPointer (bsp);
1221   sep = GetTopSeqEntryForEntityID (entityID);
1222   rsid.old_id = bsp->id;
1223   rsid.new_id = newsip;
1224 
1225   VisitFeaturesInSep (sep, &rsid, ReplaceSeqIdFeatCallback);
1226   SeqIdFree (bsp->id);
1227   bsp->id = SeqIdDup (newsip);
1228   SeqMgrReplaceInBioseqIndex (bsp);
1229   return bsp;
1230 }
1231 
1232 
ReplaceSeqIdWithSeqId(SeqIdPtr sip_old,SeqIdPtr sip_new,SeqEntryPtr sep)1233 NLM_EXTERN void ReplaceSeqIdWithSeqId (SeqIdPtr sip_old, SeqIdPtr sip_new, SeqEntryPtr sep)
1234 {
1235   BioseqPtr        bsp;
1236   SeqEntryPtr      oldscope;
1237   ReplaceSeqIdData rsid;
1238   SeqIdPtr         tmp, prev = NULL, sip;
1239 
1240   if (sip_old == NULL || sip_new == NULL || sep == NULL)
1241   {
1242     return;
1243   }
1244 
1245   oldscope = SeqEntrySetScope (sep);
1246   rsid.old_id = sip_old;
1247   rsid.new_id = sip_new;
1248 
1249   bsp = BioseqFind (sip_old);
1250 
1251   VisitFeaturesInSep (sep, &rsid, ReplaceSeqIdFeatCallback);
1252   VisitGraphsInSep (sep, &rsid, ReplaceSeqIdInGraphCallback);
1253   if (bsp != NULL) {
1254     sip = bsp->id;
1255     while (SeqIdComp (sip, sip_old) != SIC_YES) {
1256       prev = sip;
1257       sip = sip->next;
1258     }
1259     if (sip != NULL) {
1260       tmp = SeqIdDup (sip_new);
1261       tmp->next = sip->next;
1262       sip->next = NULL;
1263       SeqIdFree (sip);
1264       if (prev == NULL) {
1265         bsp->id = tmp;
1266       } else {
1267         prev->next = tmp;
1268       }
1269       SeqMgrReplaceInBioseqIndex (bsp);
1270     }
1271   }
1272 }
1273 
1274 
SeqEntryReplaceSeqIDCallBack(SeqEntryPtr sep,Pointer mydata,Int4 index,Int2 indent)1275 static void SeqEntryReplaceSeqIDCallBack (SeqEntryPtr sep, Pointer mydata,
1276                                           Int4 index, Int2 indent)
1277 {
1278   SeqIdPtr PNTR      sipp;
1279   SeqIdPtr           newsip;
1280   BioseqPtr          bsp;
1281   BioseqSetPtr       bssp;
1282 
1283   if (sep != NULL && sep->data.ptrvalue && mydata != NULL) {
1284      sipp = (SeqIdPtr PNTR)mydata;
1285      newsip = *sipp;
1286      if (IS_Bioseq(sep)) {
1287         bsp = (BioseqPtr) sep->data.ptrvalue;
1288         if (ISA_na (bsp->mol)) {
1289            BioseqReplaceID (bsp, newsip);
1290         }
1291      }
1292      else if (IS_Bioseq_set(sep)) {
1293         bssp = (BioseqSetPtr) sep->data.ptrvalue;
1294         if (bssp->annot) {
1295            SeqAnnotReplaceID (bssp->annot, newsip);
1296         }
1297      }
1298   }
1299 }
1300 
1301 
FindSeqEntryForSeqIdCallback(SeqEntryPtr sep,Pointer mydata,Int4 index,Int2 indent)1302 static void FindSeqEntryForSeqIdCallback (SeqEntryPtr sep, Pointer mydata,
1303                                           Int4 index, Int2 indent)
1304 {
1305   BioseqPtr          bsp;
1306   CcIdPtr            cip;
1307 
1308   if (sep != NULL && sep->data.ptrvalue && mydata != NULL) {
1309      cip = (CcIdPtr)mydata;
1310      if (cip->sep==NULL && IS_Bioseq(sep)) {
1311         bsp = (BioseqPtr) sep->data.ptrvalue;
1312         if (bsp!=NULL && ISA_na (bsp->mol))
1313         {
1314            if (SeqIdForSameBioseq(cip->sip, bsp->id)) {
1315               cip->sep = sep;
1316               cip->bsp = bsp;
1317            }
1318         }
1319      }
1320   }
1321 }
1322 
SeqEntryReplaceSeqID(SeqEntryPtr source_sep,SeqIdPtr sip)1323 NLM_EXTERN SeqEntryPtr SeqEntryReplaceSeqID (SeqEntryPtr source_sep, SeqIdPtr sip)
1324 {
1325   SeqEntryPtr sep;
1326   SeqLocPtr   slp;
1327   SeqIdPtr    newsip;
1328   CcId        ci;
1329   Uint2       entityID;
1330 
1331   ci.sip = SeqIdDup (sip);
1332   ci.sep = NULL;
1333   ci.bsp = NULL;
1334   SeqEntryExplore(source_sep,(Pointer)&ci, FindSeqEntryForSeqIdCallback);
1335   if (ci.sep && ci.bsp)
1336   {
1337      entityID = ObjMgrGetEntityIDForPointer (ci.bsp);
1338      sep = GetBestTopParentForData (entityID, ci.bsp);
1339      slp = SeqLocIntNew (0, ci.bsp->length-1, Seq_strand_plus, ci.bsp->id);
1340      newsip = MakeNewProteinSeqId(slp, NULL);
1341      ValNodeFree (slp);
1342      SeqEntryExplore (sep, &newsip, SeqEntryReplaceSeqIDCallBack);
1343   }
1344   SeqIdFree (ci.sip);
1345   return source_sep;
1346 }
1347 
1348 /*********************************************************
1349 ***
1350 ***  ScorePtr procedures
1351 ***
1352 **********************************************************/
ScoreDup(ScorePtr sp)1353 NLM_EXTERN ScorePtr ScoreDup (ScorePtr sp)
1354 
1355 {
1356   ScorePtr sp_copy;
1357 
1358   if(sp == NULL) return NULL;
1359   sp_copy = ScoreNew();
1360   if(sp_copy == NULL) return NULL;
1361   sp_copy->id = (ObjectIdPtr) ObjectIdDup (sp->id);
1362   sp_copy->choice = sp->choice;
1363   sp_copy->value = sp->value;
1364   sp_copy->next = NULL;
1365   return sp_copy;
1366 }
1367 
ScoreDupAdd(ScorePtr * sp_head,ScorePtr sp)1368 NLM_EXTERN ScorePtr ScoreDupAdd (ScorePtr *sp_head, ScorePtr sp)
1369 {
1370   ScorePtr sp_tmp, sp_copy;
1371 
1372   sp_copy = ScoreDup (sp);
1373   if ( (sp_tmp = *sp_head) != NULL ) {
1374          while (sp_tmp->next != NULL) sp_tmp = sp_tmp->next;
1375          sp_tmp->next = sp_copy;
1376   }
1377   else *sp_head = sp_copy;
1378   return *sp_head;
1379 }
1380 
ScoreAdd(ScorePtr * sp_head,ScorePtr sp)1381 NLM_EXTERN ScorePtr ScoreAdd (ScorePtr *sp_head, ScorePtr sp)
1382 {
1383   ScorePtr sp_tmp;
1384 
1385   if ( (sp_tmp = *sp_head) != NULL ) {
1386          while (sp_tmp->next != NULL) sp_tmp = sp_tmp->next;
1387          sp_tmp->next = sp;
1388   }
1389   else *sp_head = sp;
1390   return *sp_head;
1391 }
1392 
1393 
1394 /*********************************************************
1395 ***
1396 ***  SeqLocPtr procedures
1397 ***
1398 **********************************************************/
chkloc(SeqIdPtr sip,Int4 position,ValNodePtr vnp,Int4 * newpos)1399 NLM_EXTERN Int2 chkloc (SeqIdPtr sip, Int4 position, ValNodePtr vnp, Int4 *newpos)
1400 {
1401   ValNodePtr vnptmp;
1402   SeqIdPtr   siptmp;
1403   SeqLocPtr  slp;
1404 
1405   *newpos = 0;
1406   for (vnptmp=vnp; vnptmp!=NULL; vnptmp=vnptmp->next)
1407   {
1408      slp = (SeqLocPtr)vnptmp->data.ptrvalue;
1409      siptmp = SeqLocId (slp);
1410      if (siptmp!=NULL)
1411      {
1412         if (SeqIdForSameBioseq (sip, siptmp)) {
1413            if (position >= SeqLocStart(slp) && position <= SeqLocStop(slp)) {
1414               *newpos = position;
1415               return 0;
1416            }
1417            if (position >= SeqLocStop(slp) && position <= SeqLocStart(slp)) {
1418               *newpos = position;
1419               return 0;
1420            }
1421            if (position==APPEND_RESIDUE || position>=SeqLocStart(slp)+SeqLocLen(slp)) {
1422               *newpos = SeqLocStart(slp) + SeqLocLen(slp);
1423               return APPEND_RESIDUE;
1424            }
1425            if (position < SeqLocStart(slp)) {
1426               *newpos = SeqLocStart(slp);
1427               return GAP_RESIDUE;
1428            }
1429         }
1430      }
1431   }
1432   return NO_RESIDUE;
1433 }
1434 
expand_seq_loc(Int4 start,Int4 stop,Uint1 strand,SeqLocPtr loc)1435 NLM_EXTERN SeqLocPtr expand_seq_loc(Int4 start, Int4 stop, Uint1 strand, SeqLocPtr loc)
1436 {
1437    SeqIntPtr sint;
1438    SeqPntPtr spp;
1439 
1440         if(loc->choice == SEQLOC_INT)
1441         {
1442                 sint = (SeqIntPtr)loc->data.ptrvalue;
1443                 if(start != -1 && start < sint->from)
1444                         sint->from = start;
1445                 if(stop != -1 && stop > sint->to)
1446                         sint->to = stop;
1447                 if(strand != 0 && sint->strand != strand)
1448                         sint->strand = strand;
1449                 loc->data.ptrvalue = sint;
1450         }
1451         else if(loc->choice == SEQLOC_PNT)
1452         {
1453                 spp = (SeqPntPtr)(loc->data.ptrvalue);
1454                 spp->point = start;
1455                 spp->strand = strand;
1456                 loc->data.ptrvalue = spp;
1457         }
1458 
1459         return loc;
1460 }
1461 
MaxLengthSeqLoc(ValNodePtr sqloc_list)1462 NLM_EXTERN Int4 MaxLengthSeqLoc (ValNodePtr sqloc_list)
1463 {
1464   ValNodePtr       vnp;
1465   SeqLocPtr        slp;
1466   Int4             len, maxlen = 0;
1467 
1468   for (vnp = sqloc_list; vnp != NULL; vnp = vnp->next)
1469   {
1470          slp = (SeqLocPtr) vnp->data.ptrvalue;
1471          if ( ( len = SeqLocLen (slp)) > maxlen )
1472                 maxlen = len;
1473   }
1474   return maxlen;
1475 }
1476 
SeqLocListMatch(ValNodePtr vnp1,ValNodePtr vnp2,Boolean * Fp,Boolean * Tp)1477 NLM_EXTERN Boolean SeqLocListMatch (ValNodePtr vnp1, ValNodePtr vnp2, Boolean *Fp, Boolean *Tp)
1478 {
1479   ValNodePtr tmp1, tmp2;
1480   SeqLocPtr  slp1, slp2;
1481   Boolean    p5short = FALSE, p3short=FALSE;
1482 
1483   for (tmp1=vnp1, tmp2=vnp2; tmp1!=NULL && tmp2!=NULL; tmp1=tmp1->next, tmp2=tmp2->next)
1484   {
1485      slp1 = (SeqLocPtr) tmp1->data.ptrvalue;
1486      slp2 = (SeqLocPtr) tmp2->data.ptrvalue;
1487      if (!p5short)
1488             p5short = ( SeqLocStart(slp1) < SeqLocStart(slp2) );
1489      if (!p3short)
1490             p3short  = ( SeqLocStop (slp1) > SeqLocStop (slp2) );
1491      if (p5short && p3short) break;
1492   }
1493   *Fp = p5short;
1494   *Tp = p3short;
1495   if (p5short || p3short) return FALSE;
1496   return TRUE;
1497 }
1498 
1499 
1500 /***********************************************************************
1501 ***
1502 ***    SeqEntryToSeqLoc
1503 ***      read SeqEntry (1->Bioseq or 2->BioseqSetPtr)
1504 ***      return list of ValNodePtr->SeqLocPtr
1505 ***      The strand of the seqloc is 0, whatever the strand of the bsp
1506 ************************************************************************/
1507 typedef struct ccid3
1508 {
1509   ValNodePtr vnp;
1510   Uint1      bsp_mol;
1511 } CcId3, PNTR CcId3Ptr;
1512 
ListSeqEntryCallback(SeqEntryPtr sep,Pointer mydata,Int4 index,Int2 indent)1513 static void ListSeqEntryCallback (SeqEntryPtr sep, Pointer mydata,
1514                                           Int4 index, Int2 indent)
1515 {
1516   BioseqPtr          bsp;
1517   SeqIdPtr           sip;
1518   SeqLocPtr          slp;
1519   CcId3Ptr           ccp;
1520 
1521   if (sep != NULL && sep->data.ptrvalue && mydata != NULL) {
1522      ccp = (CcId3Ptr)mydata;
1523      if (IS_Bioseq(sep)) {
1524         bsp = (BioseqPtr) sep->data.ptrvalue;
1525         if (bsp!=NULL) {
1526          if (ccp->bsp_mol==0 || ISA_aa(ccp->bsp_mol)==ISA_aa(bsp->mol))
1527          {
1528            sip = SeqIdFindBest(bsp->id, 0);
1529            if (sip!=NULL) {
1530               slp = SeqLocIntNew (0, bsp->length - 1, Seq_strand_plus, sip);
1531               ValNodeAddPointer (&(ccp->vnp), 0, slp);
1532            }
1533          }
1534         }
1535      }
1536   }
1537 }
1538 
SeqEntryToSeqLoc(SeqEntryPtr sep,Int2 * n,Uint1 bsp_mol)1539 NLM_EXTERN ValNodePtr SeqEntryToSeqLoc (SeqEntryPtr sep, Int2 *n, Uint1 bsp_mol)
1540 {
1541   CcId3              cc;
1542   ValNodePtr         vnp = NULL;
1543   Int2               j=0;
1544 
1545   cc.vnp=NULL;
1546   cc.bsp_mol=bsp_mol;
1547   SeqEntryExplore(sep,(Pointer)&cc, ListSeqEntryCallback);
1548   for (vnp = cc.vnp;vnp!=NULL; vnp=vnp->next)
1549   {
1550      j++;
1551   }
1552   *n = j;
1553   return cc.vnp;
1554 }
1555 
1556 
1557 /************************************************************/
SelStructNew(Uint2 ssp_ed,Uint2 ssp_id,Uint2 ssp_it,Int4 from,Int4 to,SeqIdPtr sip,Uint1 strand,Boolean is_fuzz)1558 NLM_EXTERN SelStructPtr SelStructNew (Uint2 ssp_ed, Uint2 ssp_id, Uint2 ssp_it, Int4 from, Int4 to, SeqIdPtr sip, Uint1 strand, Boolean is_fuzz)
1559 {
1560   SelStructPtr   ssp;
1561   SeqLocPtr      slp;
1562 
1563   ssp = (SelStructPtr) MemNew (sizeof (SelStruct));
1564   if (ssp == NULL) return NULL;
1565   ssp->entityID = ssp_ed;
1566   ssp->itemID = ssp_id;
1567   ssp->itemtype = ssp_it;
1568   ssp->regiontype = 0;
1569   ssp->region = NULL;
1570   if (from >= 0 && sip != NULL) {
1571      if (is_fuzz)
1572          slp = fuzz_loc (from, to, strand, sip, TRUE, TRUE);
1573      else
1574          slp = SeqLocIntNew (from, to, strand, sip);
1575      if ( slp != NULL ) {
1576          ssp->regiontype = OM_REGION_SEQLOC;
1577          ssp->region = (Pointer) slp;
1578      }
1579   }
1580   ssp->prev = NULL;
1581   ssp->next = NULL;
1582   return ssp;
1583 }
1584 /******************************************************************/
SelStructCpy(SelStructPtr ssp,SelStructPtr ssp2)1585 NLM_EXTERN SelStructPtr SelStructCpy (SelStructPtr ssp, SelStructPtr ssp2)
1586 {
1587   SeqLocPtr  slp, slp2;
1588   if (ssp == NULL) return NULL;
1589   ssp2->entityID = ssp->entityID;
1590   ssp2->itemID = ssp->itemID;
1591   ssp2->itemtype = ssp->itemtype;
1592   if ( ssp->regiontype == OM_REGION_SEQLOC) {
1593          ssp2->regiontype = OM_REGION_SEQLOC;
1594          if ( ssp->region == NULL ) {
1595                ErrPostEx (SEV_WARNING, 0, 0, "Fail in SelStructCpy [1]");
1596                return NULL;
1597          }
1598          if ( ssp2->region != NULL )
1599                SeqLocFree ((SeqLocPtr)ssp2->region);
1600          slp = (SeqLocPtr) ssp->region;
1601          slp2 = SeqLocIntNew (SeqLocStart(slp), SeqLocStop(slp),
1602                               SeqLocStrand(slp), SeqLocId(slp));
1603          if ( slp2 == NULL ) {
1604                ErrPostEx(SEV_WARNING, 0, 0, "Fail in SelStructCpy [2]");
1605                return NULL;
1606          }
1607          ssp2->region = (Pointer) slp2;
1608   }
1609   else {
1610          ssp2->regiontype = 0;
1611          ssp2->region = NULL;
1612   }
1613   ssp2->next = NULL;
1614   ssp2->prev = NULL;
1615   return ssp2;
1616 }
1617 
1618 /******************************************************************/
SelStructDup(SelStructPtr ssp)1619 NLM_EXTERN SelStructPtr SelStructDup (SelStructPtr ssp)
1620 {
1621   SelStructPtr ssp2;
1622 
1623   if (ssp == NULL) return NULL;
1624   ssp2 = (SelStructPtr) MemNew (sizeof (SelStruct));
1625   ssp2 = SelStructCpy (ssp, ssp2);
1626   return ssp2;
1627 }
1628 
1629 /******************************************************************/
SelStructAdd(SelStructPtr head,SelStructPtr ssp)1630 NLM_EXTERN SelStructPtr SelStructAdd (SelStructPtr head, SelStructPtr ssp)
1631 {
1632   SelStructPtr ssptmp;
1633   if (head == NULL) return ssp;
1634   for (ssptmp = head; ssptmp->next != NULL; ssptmp=ssptmp->next) continue;
1635   ssptmp->next = ssp;
1636   ssp->prev = ssptmp;
1637   return head;
1638 }
1639 
1640 /******************************************************************/
SelStructDel(SelStructPtr ssp)1641 NLM_EXTERN SelStructPtr SelStructDel (SelStructPtr ssp)
1642 {
1643 
1644   if (ssp == NULL) return NULL;
1645   ssp->next = NULL;
1646   if ( ssp->region != NULL )
1647          SeqLocFree ((SeqLocPtr) ssp->region);
1648   MemFree (ssp);
1649   return NULL;
1650 }
1651 
1652 /******************************************************************/
SelStructDelList(SelStructPtr ssp)1653 NLM_EXTERN SelStructPtr SelStructDelList (SelStructPtr ssp)
1654 {
1655   SelStructPtr tmp, next;
1656 
1657   if (ssp!=NULL) {
1658      tmp=ssp;
1659      while (tmp!=NULL) {
1660         next = tmp->next;
1661         tmp->next = NULL;
1662         SelStructDel (tmp);
1663         tmp=next;
1664      }
1665   }
1666   return NULL;
1667 }
1668 
1669 /*****************************************************************/
setposition_tossp(SelStructPtr ssp,Int4 from,Int4 to)1670 NLM_EXTERN void setposition_tossp (SelStructPtr ssp, Int4 from, Int4 to)
1671 {
1672   SeqLocPtr        slp;
1673   SeqIntPtr        sitcaret;
1674   if (ssp == NULL) return;
1675   if (ssp->region == NULL) return;
1676   slp = (SeqLocPtr) ssp->region;
1677   sitcaret = (SeqIntPtr) slp->data.ptrvalue;
1678   sitcaret->from = from;
1679   sitcaret->to = to;
1680 }
1681 
1682 /******************************************************************/
is_samessp(SelStructPtr ssp1,SelStructPtr ssp2)1683 NLM_EXTERN Boolean is_samessp (SelStructPtr ssp1, SelStructPtr ssp2)
1684 {
1685   if (ssp1 == NULL || ssp2 == NULL) return FALSE;
1686   if (ssp1->entityID != ssp2->entityID) return FALSE;
1687   if (ssp1->itemID != ssp2->itemID) return FALSE;
1688   if (ssp1->itemtype != ssp2->itemtype) return FALSE;
1689   return TRUE;
1690 }
1691 
1692 /******************************************************************/
is_sameId(Uint2 sei,Uint4 sii,Uint2 sit,Uint2 sist,Uint2 ei,Uint4 ii,Uint2 it,Uint2 ist)1693 NLM_EXTERN Boolean is_sameId (Uint2 sei, Uint4 sii, Uint2 sit, Uint2 sist, Uint2 ei, Uint4 ii, Uint2 it, Uint2 ist)
1694 {
1695   if ( ei < 255 && sei != ei) return FALSE;
1696   if ( ii < 255 && sii != ii) return FALSE;
1697   if ( it < 255 && sit != it) return FALSE;
1698   if ( ist< 255 && sist!= ist)return FALSE;
1699   return TRUE;
1700 }
1701 
1702 /******************************************************************/
is_samepos(SelStructPtr ssp1,SelStructPtr ssp2)1703 NLM_EXTERN Boolean is_samepos (SelStructPtr ssp1, SelStructPtr ssp2)
1704 {
1705   SeqLocPtr       slp1, slp2;
1706 
1707   if (ssp1->regiontype == 0 || ssp2->regiontype == 0) return FALSE;
1708   if (ssp1->region == NULL || ssp2->region == NULL) return FALSE;
1709   if ( !is_samessp (ssp1, ssp2) ) return FALSE;
1710   slp1 = (SeqLocPtr) ssp1->region;
1711   slp2 = (SeqLocPtr) ssp2->region;
1712   if ( slp1 == NULL || slp2 == NULL) return FALSE;
1713   if ( SeqLocStart(slp1) != SeqLocStart(slp2) ) return FALSE;
1714   if ( SeqLocStop(slp1) != SeqLocStop(slp2) ) return FALSE;
1715   return TRUE;
1716 }
1717 
1718 /******************************************************************/
del_ssp_fromid(ValNodePtr headp,Uint2 itemsubtype,SelEdStructPtr target)1719 NLM_EXTERN ValNodePtr del_ssp_fromid (ValNodePtr headp, Uint2 itemsubtype, SelEdStructPtr target)
1720 {
1721   ValNodePtr     vnphead, pre_vnp = NULL;
1722   SelEdStructPtr ssp,  pressp = NULL;
1723   Boolean        found_ssp = FALSE;
1724 
1725   if (headp == NULL || target == NULL) {
1726          ErrPostEx(SEV_WARNING, 0, 0, "fail in del_ssp_fromid [1]");
1727          return NULL;
1728   }
1729   pre_vnp = NULL;
1730   for  (vnphead = headp; vnphead != NULL; vnphead = vnphead->next)
1731   {
1732      if ( itemsubtype == 255 || vnphead->choice == itemsubtype ) {
1733         ssp = (SelEdStructPtr) vnphead->data.ptrvalue;
1734         if (is_sameses (ssp, target)) {
1735            pressp = NULL;
1736            for (; ssp != NULL; ssp = ssp->next)
1737            {
1738               if (include_ssp ((SeqLocPtr) ssp->region, (SeqLocPtr) target->region))
1739               {
1740                  found_ssp = TRUE;
1741                  break;
1742               }
1743               pressp = ssp;
1744            }
1745            if (found_ssp) break;
1746         }
1747      }
1748      pre_vnp = vnphead;
1749   }
1750   if (vnphead == NULL || ssp == NULL) return headp;
1751   SelEdStructListDel ((SelEdStructPtr) vnphead->data.ptrvalue);
1752   vnphead->data.ptrvalue = NULL;
1753   if (pre_vnp == NULL) {
1754      if (vnphead->next == NULL) {
1755         headp = NULL;
1756      }
1757      else {
1758         headp = vnphead->next;
1759         vnphead->next = NULL;
1760      }
1761   }
1762   else if (vnphead->next == NULL) {
1763      pre_vnp->next = NULL;
1764   }
1765   else {
1766      pre_vnp->next = vnphead->next;
1767      vnphead->next = NULL;
1768   }
1769   ValNodeFree (vnphead);
1770   return headp;
1771 }
1772 
1773 /******************************************************************/
include_ssp(SeqLocPtr slp1,SeqLocPtr slp2)1774 NLM_EXTERN Boolean include_ssp (SeqLocPtr slp1, SeqLocPtr slp2)
1775 {
1776   if ( SeqLocStart(slp1) <= SeqLocStart(slp2)
1777   && SeqLocStop(slp1) >= SeqLocStop(slp2) ) return TRUE;
1778   return FALSE;
1779 }
1780 
1781 /******************************************************************/
overlapp_startssp(SeqLocPtr slp1,SeqLocPtr slp2)1782 NLM_EXTERN Int4 overlapp_startssp (SeqLocPtr slp1, SeqLocPtr slp2)
1783 {
1784   if ( SeqLocStart(slp1) < SeqLocStart(slp2)
1785   &&  SeqLocStop(slp1) < SeqLocStop(slp2) )
1786      return (SeqLocLen(slp1) - (SeqLocStart(slp2) - SeqLocStart(slp1)));
1787   return 0;
1788 }
1789 
1790 /******************************************************************/
overlapp_ssp(SeqLocPtr slp1,SeqLocPtr slp2)1791 NLM_EXTERN Boolean overlapp_ssp (SeqLocPtr slp1, SeqLocPtr slp2)
1792 {
1793   if ( SeqLocStop(slp1) < SeqLocStart(slp2) ) return FALSE;
1794   if ( SeqLocStart(slp1) > SeqLocStop(slp2) ) return FALSE;
1795   return TRUE;
1796 }
1797 
1798 /******************************************************************/
precede_ssp(SeqLocPtr slp1,SeqLocPtr slp2)1799 NLM_EXTERN Boolean precede_ssp (SeqLocPtr slp1, SeqLocPtr slp2)
1800 {
1801   if ( SeqLocStart(slp2) >= SeqLocStop(slp1) ) return TRUE;
1802   return FALSE;
1803 }
1804 
1805 /******************************************************************/
succeed_ssp(SeqLocPtr slp1,SeqLocPtr slp2)1806 NLM_EXTERN Boolean succeed_ssp (SeqLocPtr slp1, SeqLocPtr slp2)
1807 {
1808   if ( SeqLocStop(slp2) == SeqLocStart(slp1) ) return TRUE;
1809   return FALSE;
1810 }
1811 
1812 
1813 /******************************************************************/
new_seledstruct(Uint2 ssp_ed,Uint2 ssp_id,Uint2 ssp_it,Uint2 ssp_sit,Uint2 bspiID,Int4 from,Int4 to,SeqIdPtr sip,Uint1 strand,Boolean is_fuzz,CharPtr label,Pointer data,Int4 offset,Uint1 codonstart)1814 NLM_EXTERN SelEdStructPtr new_seledstruct (Uint2 ssp_ed, Uint2 ssp_id, Uint2 ssp_it, Uint2 ssp_sit, Uint2 bspiID, Int4 from, Int4 to, SeqIdPtr sip, Uint1 strand, Boolean is_fuzz, CharPtr label, Pointer data, Int4 offset, Uint1 codonstart)
1815 {
1816   SelEdStructPtr sesp;
1817   SeqLocPtr      slp;
1818   ValNodePtr     datavnp;
1819 
1820   sesp = (SelEdStructPtr) MemNew (sizeof (SelEdStruct));
1821   if (sesp == NULL) return NULL;
1822   sesp->entityID = ssp_ed;
1823   sesp->itemID = ssp_id;
1824   sesp->itemtype = ssp_it;
1825   sesp->itemsubtype = ssp_sit;
1826   sesp->bsp_itemID = bspiID;
1827   if (sesp->regiontype == OM_REGION_SEQLOC)
1828          sesp->regiontype = 0;
1829   if (from < 0 || sip == NULL) {
1830          sesp->regiontype = 0;
1831          sesp->region = NULL;
1832   }
1833   else {
1834      sesp->region = NULL;
1835      if (is_fuzz)
1836          slp = fuzz_loc (from, to, strand, sip, TRUE, TRUE);
1837      else
1838          slp = SeqLocIntNew (from, to, strand, sip);
1839      if ( slp != NULL ) {
1840          sesp->regiontype = OM_REGION_SEQLOC;
1841          sesp->region = (Pointer) slp;
1842      }
1843      else   sesp->regiontype = 0;
1844   }
1845   if (label != NULL && StringLen (label) > 0)
1846        StringCpy(sesp->label, label);
1847   else sesp->label[0] = '\0';
1848   if (data != NULL)
1849   {
1850      datavnp = ValNodeNew (NULL);
1851      datavnp->choice = 0;
1852      datavnp->data.ptrvalue = data;
1853      sesp->data = datavnp;
1854   }
1855   else sesp->data = NULL;
1856   sesp->codonstart = codonstart;
1857   sesp->offset = offset;
1858   sesp->dirty = TRUE;
1859   sesp->visible = TRUE;
1860   sesp->prev = NULL;
1861   sesp->next = NULL;
1862   return sesp;
1863 }
1864 
new_seledstruct_fromseqloc(Uint2 ssp_ed,Uint2 ssp_id,Uint2 ssp_it,Uint2 ssp_sit,Uint2 bspiID,SeqLocPtr slp,CharPtr label,Pointer data,Int4 offset,Uint1 codonstart)1865 NLM_EXTERN SelEdStructPtr new_seledstruct_fromseqloc (Uint2 ssp_ed, Uint2 ssp_id, Uint2 ssp_it, Uint2 ssp_sit, Uint2 bspiID, SeqLocPtr slp, CharPtr label, Pointer data, Int4 offset, Uint1 codonstart)
1866 {
1867   SelEdStructPtr sesp;
1868   ValNodePtr     datavnp;
1869 
1870   sesp = (SelEdStructPtr) MemNew (sizeof (SelEdStruct));
1871   if (sesp == NULL) return NULL;
1872   sesp->entityID = ssp_ed;
1873   sesp->itemID = ssp_id;
1874   sesp->itemtype = ssp_it;
1875   sesp->itemsubtype = ssp_sit;
1876   sesp->bsp_itemID = bspiID;
1877   if (sesp->regiontype == OM_REGION_SEQLOC)
1878      sesp->regiontype = 0;
1879   if (slp == NULL) {
1880      sesp->regiontype = 0;
1881      sesp->region = NULL;
1882   }
1883   else {
1884      sesp->regiontype = OM_REGION_SEQLOC;
1885      sesp->region = (Pointer) slp;
1886   }
1887   if (label != NULL && StringLen (label) > 0)
1888      StringCpy(sesp->label, label);
1889   else sesp->label[0] = '\0';
1890   if (data != NULL)
1891   {
1892      datavnp = ValNodeNew (NULL);
1893      datavnp->choice = 0;
1894      datavnp->data.ptrvalue = data;
1895      sesp->data = datavnp;
1896   }
1897   else sesp->data = NULL;
1898   sesp->codonstart = codonstart;
1899   sesp->offset = offset;
1900   sesp->dirty = TRUE;
1901   sesp->visible = TRUE;
1902   sesp->prev = NULL;
1903   sesp->next = NULL;
1904   return sesp;
1905 }
1906 
1907 /***************************************************************
1908 *** sesp_to_slp
1909 ***    make seqloc from  ses->region (SelEdStructPtr)
1910 ***    after changing the alignment coordinates into
1911 ***    sequence coordinates
1912 ***
1913 ***    the seqloc is NOT PARTIAL
1914 ***        fuzz_loc (start, stop, strand, sip, TRUE, TRUE);
1915 ***    but COMPLETE:
1916 ***        SeqLocIntNew (start, stop, strand, sip);)
1917 ***************************************************************/
sesp_to_slp(SelEdStructPtr ses,SeqAlignPtr salp,ValNodePtr sqlocs,Boolean partial)1918 NLM_EXTERN SeqLocPtr sesp_to_slp (SelEdStructPtr ses, SeqAlignPtr salp, ValNodePtr sqlocs, Boolean partial)
1919 {
1920   SelEdStructPtr sesp;
1921   SeqLocPtr      slptmp, slpnew,
1922                  slp, slp1;
1923   SeqIdPtr       sip;
1924   Int4           start, stop;
1925   Uint1          strand;
1926 
1927   Int2           j,k;
1928 
1929   if (ses->next == NULL)
1930   {
1931      slptmp = (SeqLocPtr) ses->region;
1932      sip = SeqLocId (slptmp);
1933      start= (Int4)AlignCoordToSeqCoord (SeqLocStart(slptmp), sip, salp, sqlocs,0);
1934      stop = (Int4)AlignCoordToSeqCoord (SeqLocStop(slptmp), sip, salp, sqlocs, 0);
1935      slp = SeqLocIntNew (start, stop, SeqLocStrand (slptmp), sip);
1936      return slp;
1937   }
1938   slp1 = (SeqLocPtr) ValNodeNew (NULL);
1939   slp1->choice = SEQLOC_PACKED_INT;
1940   sesp = ses;
1941   slptmp = (SeqLocPtr) sesp->region;
1942   if (SeqLocStrand (slptmp) == Seq_strand_minus)
1943   {
1944      j=0;
1945      while (sesp->next!=NULL) {
1946         j++; sesp = sesp->next;
1947      }
1948      slptmp = (SeqLocPtr) sesp->region;
1949   }
1950   strand = SeqLocStrand (slptmp);
1951   sip = SeqLocId (slptmp);
1952   start= (Int4) AlignCoordToSeqCoord (SeqLocStart (slptmp), sip, salp, sqlocs, 0);
1953   stop = (Int4) AlignCoordToSeqCoord (SeqLocStop (slptmp), sip, salp, sqlocs, 0);
1954   if (partial)
1955      slpnew = fuzz_loc (start, stop, strand, sip, TRUE, TRUE);
1956   else
1957      slpnew = SeqLocIntNew (start, stop, strand, sip);
1958   slp1->data.ptrvalue = slpnew;
1959   slp = slpnew;
1960   if (strand != Seq_strand_minus)
1961   {
1962      sesp = sesp->next;
1963      for (; sesp != NULL; sesp = sesp->next)
1964      {
1965         slptmp = (SeqLocPtr) sesp->region;
1966         start=(Int4)AlignCoordToSeqCoord(SeqLocStart(slptmp),sip, salp, sqlocs, 0);
1967         stop =(Int4)AlignCoordToSeqCoord(SeqLocStop(slptmp), sip, salp, sqlocs,0);
1968         slpnew = SeqLocIntNew (start, stop, strand, sip);
1969         slp->next = slpnew;
1970         slp = slp->next;
1971      }
1972   }
1973   else {
1974      while (j>0) {
1975         sesp=ses;
1976         for (k=1; k<j; k++) sesp=sesp->next;
1977         slptmp = (SeqLocPtr) sesp->region;
1978         start=(Int4)AlignCoordToSeqCoord(SeqLocStart(slptmp), sip, salp, sqlocs,0);
1979         stop =(Int4)AlignCoordToSeqCoord(SeqLocStop(slptmp), sip, salp, sqlocs, 0);
1980         slpnew = SeqLocIntNew (start, stop, strand, sip);
1981         slp->next = slpnew;
1982         slp = slp->next;
1983         j--;
1984      }
1985   }
1986   return slp1;
1987 }
1988 
1989 
SelEdStructCpy(SelEdStructPtr ssp,SelEdStructPtr ssp2)1990 static SelEdStructPtr SelEdStructCpy (SelEdStructPtr ssp, SelEdStructPtr ssp2)
1991 {
1992   SeqLocPtr  slp, slp2;
1993   if (ssp == NULL) return NULL;
1994   ssp2->entityID = ssp->entityID;
1995   ssp2->itemID = ssp->itemID;
1996   ssp2->itemtype = ssp->itemtype;
1997   if ( ssp->regiontype == OM_REGION_SEQLOC && ssp->region != NULL) {
1998          ssp2->regiontype = OM_REGION_SEQLOC;
1999          if ( ssp2->region != NULL )
2000                SeqLocFree ((SeqLocPtr)ssp2->region);
2001          slp = (SeqLocPtr) ssp->region;
2002          slp2 = SeqLocIntNew (SeqLocStart(slp), SeqLocStop(slp), SeqLocStrand(slp), SeqLocId(slp));
2003          if ( slp2 == NULL ) {
2004                ErrPostEx(SEV_WARNING, 0, 0, "Fail in SelStructCpy [2]");
2005                return NULL;
2006          }
2007          ssp2->region = (Pointer) slp2;
2008   }
2009   else {
2010          ssp2->regiontype = 0;
2011          ssp2->region = NULL;
2012   }
2013   ssp2->data = NULL;
2014   ssp2->next = NULL;
2015   ssp2->prev = NULL;
2016   return ssp2;
2017 }
2018 
2019 /******************************************************************/
SelEdStructDup(SelEdStructPtr ssp)2020 NLM_EXTERN SelEdStructPtr SelEdStructDup (SelEdStructPtr ssp)
2021 {
2022   SelEdStructPtr ssp2;
2023 
2024   if (ssp == NULL) return NULL;
2025   ssp2 = (SelEdStructPtr) MemNew (sizeof (SelEdStruct));
2026   ssp2 = SelEdStructCpy (ssp, ssp2);
2027   return ssp2;
2028 }
2029 
2030 /******************************************************************/
SelEdStructAdd(SelEdStructPtr head,SelEdStructPtr ssp)2031 NLM_EXTERN SelEdStructPtr SelEdStructAdd (SelEdStructPtr head, SelEdStructPtr ssp)
2032 {
2033   SelEdStructPtr ssptmp;
2034   if (head == NULL) return ssp;
2035   for (ssptmp = head; ssptmp->next != NULL; ssptmp=ssptmp->next) continue;
2036   ssptmp->next = ssp;
2037   ssp->prev = ssptmp;
2038   return head;
2039 }
2040 
2041 /******************************************************************/
SelEdStructDel(SelEdStructPtr ssp)2042 NLM_EXTERN SelEdStructPtr SelEdStructDel (SelEdStructPtr ssp)
2043 {
2044   ValNodePtr  vnpdata;
2045 
2046   if (ssp == NULL) return NULL;
2047   if ( ssp->data != NULL )
2048   {
2049            vnpdata = ssp->data;
2050            if ( vnpdata->data.ptrvalue != NULL ) {
2051               if (ssp->prev == NULL)
2052                  vnpdata->data.ptrvalue = MemFree(vnpdata->data.ptrvalue);
2053               else
2054                  vnpdata->data.ptrvalue = NULL;
2055            }
2056            vnpdata = ValNodeFree (vnpdata);
2057   }
2058   if ( ssp->region != NULL )
2059          ssp->region = SeqLocFree ((SeqLocPtr) ssp->region);
2060   MemFree (ssp);
2061   return NULL;
2062 }
2063 
2064 /******************************************************************/
SelEdStructListDel(SelEdStructPtr ssp)2065 NLM_EXTERN SelEdStructPtr SelEdStructListDel (SelEdStructPtr ssp)
2066 {
2067   SelEdStructPtr next;
2068   ValNodePtr     vnpdata;
2069   Boolean        first = TRUE;
2070 
2071   if (ssp == NULL) return NULL;
2072   while (ssp != NULL)
2073   {
2074      next = ssp->next;
2075      if ( ssp->region != NULL )
2076            ssp->region = SeqLocFree ((SeqLocPtr) ssp->region);
2077      if ( ssp->data != NULL )
2078      {
2079            vnpdata = ssp->data;
2080            if ( vnpdata->data.ptrvalue != NULL ) {
2081               if (first)
2082 		  vnpdata->data.ptrvalue = MemFree (vnpdata->data.ptrvalue);
2083 	      else
2084 		  vnpdata->data.ptrvalue = NULL;
2085            }
2086            vnpdata = ValNodeFree (vnpdata);
2087      }
2088      MemFree (ssp);
2089      ssp = next;
2090 	 if (first) first = FALSE;
2091   }
2092   return NULL;
2093 }
2094 
set_seqnot_visible(Uint2 eID,Uint4 iID,SelEdStructPtr sesp)2095 NLM_EXTERN void set_seqnot_visible (Uint2 eID, Uint4 iID, SelEdStructPtr sesp)
2096 {
2097   SelEdStructPtr tmp;
2098   for (tmp=sesp;tmp!=NULL; tmp=tmp->next)
2099   {
2100      if (tmp->entityID==eID && tmp->itemID==iID)
2101         tmp->visible = FALSE;
2102   }
2103 }
2104 
set_seqvisible(Uint2 eID,Uint4 iID,SelEdStructPtr sesp)2105 NLM_EXTERN void set_seqvisible (Uint2 eID, Uint4 iID, SelEdStructPtr sesp)
2106 {
2107   SelEdStructPtr tmp;
2108   for (tmp=sesp;tmp!=NULL; tmp=tmp->next)
2109   {
2110      if (tmp->entityID==eID && tmp->itemID==iID)
2111         tmp->visible = TRUE;
2112   }
2113 }
is_seqvisible(Uint2 eID,Uint4 iID,SelEdStructPtr sesp)2114 NLM_EXTERN Boolean is_seqvisible (Uint2 eID, Uint4 iID, SelEdStructPtr sesp)
2115 {
2116   SelEdStructPtr tmp;
2117   for (tmp=sesp;tmp!=NULL; tmp=tmp->next)
2118   {
2119      if (tmp->entityID==eID && tmp->itemID==iID)
2120         return (Boolean)tmp->visible;
2121   }
2122   return FALSE;
2123 }
2124 
2125 /******************************************************************/
setposition_toses(SelEdStructPtr ssp,Int4 from,Int4 to)2126 NLM_EXTERN void setposition_toses (SelEdStructPtr ssp, Int4 from, Int4 to)
2127 {
2128   SeqLocPtr        slp;
2129   SeqIntPtr        sitcaret;
2130   if (ssp == NULL) return;
2131   if (ssp->region == NULL) return;
2132   slp = (SeqLocPtr) ssp->region;
2133   sitcaret = (SeqIntPtr) slp->data.ptrvalue;
2134   sitcaret->from = from;
2135   sitcaret->to = to;
2136 }
2137 
2138 
ss_to_ses(SelStructPtr ssp)2139 NLM_EXTERN SelEdStructPtr ss_to_ses (SelStructPtr ssp)
2140 {
2141   SeqLocPtr      slp, slpses;
2142   SelEdStructPtr sesp;
2143   SeqIdPtr       sip;
2144   BioseqPtr      bsp;
2145 
2146   if (ssp == NULL) return NULL;
2147   sesp = (SelEdStructPtr) MemNew (sizeof (SelEdStruct));
2148   sesp->entityID = ssp->entityID;
2149   sesp->itemID = ssp->itemID;
2150   sesp->itemtype = ssp->itemtype;
2151   if ( ssp->regiontype == OM_REGION_SEQLOC) {
2152          sesp->regiontype = OM_REGION_SEQLOC;
2153          if ( ssp->region == NULL ) {
2154                ErrPostEx(SEV_WARNING, 0, 0, "Fail in SelStructCpy [1]");
2155                return NULL;
2156          }
2157          slp = (SeqLocPtr) ssp->region;
2158          sip = NULL;
2159          bsp = BioseqLockById (SeqLocId (slp));
2160          if (bsp != NULL) {
2161             for (sip=bsp->id; sip!= NULL; sip = sip->next) {
2162                if (sip->choice == SEQID_GI)
2163                   break;
2164             }
2165             BioseqUnlock (bsp);
2166          }
2167          if (sip == NULL) {
2168             sip = SeqLocId (slp);
2169             sip = SeqIdFindBest (sip, 0);
2170          }
2171          slpses = SeqLocIntNew (SeqLocStart(slp), SeqLocStop(slp), SeqLocStrand(slp), sip);
2172          if ( slpses == NULL ) {
2173                ErrPostEx(SEV_WARNING, 0, 0, "Fail in SelStructCpy [2]");
2174                return NULL;
2175          }
2176          sesp->region = (Pointer) slpses;
2177   }
2178   else {
2179          sesp->regiontype = 0;
2180          sesp->region = NULL;
2181   }
2182   sesp->label[0] ='\0';
2183   sesp->data = NULL;
2184   sesp->next = NULL;
2185   sesp->prev = NULL;
2186   return sesp;
2187 }
2188 
ses_to_ss(SelEdStructPtr sesp)2189 NLM_EXTERN SelStructPtr ses_to_ss (SelEdStructPtr sesp)
2190 {
2191   SeqLocPtr    slp, slpses;
2192   SelStructPtr ssp;
2193 
2194   if (sesp == NULL) return NULL;
2195   ssp = (SelStructPtr) MemNew (sizeof (SelStruct));
2196   ssp->entityID = sesp->entityID;
2197   ssp->itemID = sesp->itemID;
2198   ssp->itemtype = sesp->itemtype;
2199   if ( sesp->regiontype == OM_REGION_SEQLOC ) {
2200          ssp->regiontype = OM_REGION_SEQLOC;
2201          if ( sesp->region == NULL ) {
2202                ErrPostEx(SEV_WARNING, 0, 0, "Fail in SelStructCpy [1]");
2203                return NULL;
2204          }
2205          slpses = (SeqLocPtr) sesp->region;
2206          slp = SeqLocIntNew (SeqLocStart(slpses), SeqLocStop(slpses),
2207                               SeqLocStrand(slpses), SeqLocId(slpses));
2208          if ( slp == NULL ) {
2209                ErrPostEx(SEV_WARNING, 0, 0, "Fail in SelStructCpy [2]");
2210                return NULL;
2211          }
2212          ssp->region = (Pointer) slp;
2213   }
2214   else {
2215          ssp->regiontype = 0;
2216          ssp->region = NULL;
2217   }
2218   ssp->next = NULL;
2219   ssp->prev = NULL;
2220   return ssp;
2221 }
2222 
2223 /******************************************************************/
is_samess_ses(SelStructPtr ssp1,SelEdStructPtr ssp2)2224 NLM_EXTERN Boolean is_samess_ses (SelStructPtr ssp1, SelEdStructPtr ssp2)
2225 {
2226   if (ssp1 == NULL || ssp2 == NULL) return FALSE;
2227   if (ssp1->entityID != ssp2->entityID) return FALSE;
2228   if (ssp1->itemID != ssp2->itemID) return FALSE;
2229   if (ssp1->itemtype != ssp2->itemtype) return FALSE;
2230   return TRUE;
2231 }
2232 
2233 /******************************************************************/
is_sameses(SelEdStructPtr ssp1,SelEdStructPtr ssp2)2234 NLM_EXTERN Boolean is_sameses (SelEdStructPtr ssp1, SelEdStructPtr ssp2)
2235 {
2236   if (ssp1 == NULL || ssp2 == NULL) return FALSE;
2237   if (ssp1->entityID != ssp2->entityID) return FALSE;
2238   if (ssp1->itemID != ssp2->itemID) return FALSE;
2239   if (ssp1->itemtype != ssp2->itemtype) return FALSE;
2240   return TRUE;
2241 }
2242 
2243 /*********************************************************
2244 ***
2245 ***  ObjMgr procedures
2246 ***
2247 **********************************************************/
AfterAlsoSelect(void)2248 NLM_EXTERN Boolean AfterAlsoSelect (void)
2249 {
2250   SelStructPtr sspa = NULL,
2251                sspb = NULL;
2252   SeqLocPtr    slpa,
2253                slpb;
2254   SeqIntPtr    sint;
2255   Uint2        eIDa, ita,
2256                eIDb, itb;
2257   Uint4        iIDa, iIDb;
2258   Boolean      check = TRUE,
2259                loopin = TRUE,
2260                changed = FALSE;
2261 
2262   while (check)
2263   {
2264      check = FALSE;
2265      loopin = TRUE;
2266      sspa = ObjMgrGetSelected ();
2267      while (sspa != NULL && loopin)
2268      {
2269         if ( checkssp_for_editor (sspa))
2270         {
2271            for (sspb = sspa->next; sspb != NULL; sspb = sspb->next)
2272            {
2273               eIDa = sspa->entityID;
2274               iIDa = sspa->itemID;
2275               ita = sspa->itemtype;
2276               eIDb = sspb->entityID;
2277               iIDb = sspb->itemID;
2278               itb = sspb->itemtype;
2279               if ( checkssp_for_editor (sspb) && is_sameId (eIDa, iIDa, ita, 255, eIDb, iIDb, itb, 255) )
2280               {
2281                  slpa = (SeqLocPtr)sspa->region;
2282                  slpb = (SeqLocPtr)sspb->region;
2283                  if (SeqLocCompare (slpa, slpb) == SLC_A_IN_B) {
2284                     ObjMgrDeSelect(eIDa, iIDa, ita, sspa->regiontype, slpa);
2285                     check = TRUE;
2286                     loopin = FALSE;
2287                     changed = TRUE;
2288                     break;
2289                  }
2290                  else if (SeqLocCompare (slpa, slpb) == SLC_B_IN_A) {
2291                     ObjMgrDeSelect(eIDb, iIDb, itb, sspb->regiontype, slpb);
2292                     check = TRUE;
2293                     loopin = FALSE;
2294                     changed = TRUE;
2295                     break;
2296                  }
2297                  else if (SeqLocCompare (slpa, slpb) == SLC_A_EQ_B) {
2298                     ObjMgrDeSelect(eIDb, iIDb, itb, sspb->regiontype, slpb);
2299                     check = TRUE;
2300                     loopin = FALSE;
2301                     changed = TRUE;
2302                     break;
2303                  }
2304                  else if (SeqLocCompare (slpa, slpb) == SLC_A_OVERLAP_B) {
2305                     sint = (SeqIntPtr) slpa->data.ptrvalue;
2306                     if (SeqLocStart(slpa) < SeqLocStart(slpb)) {
2307                        sint->to = SeqLocStop(slpb);
2308                     }
2309                     else {
2310                        sint = (SeqIntPtr) slpa->data.ptrvalue;
2311                        sint->from = SeqLocStart(slpb);
2312                     }
2313                     ObjMgrDeSelect(eIDb, iIDb, itb, sspb->regiontype, slpb);
2314                     check = TRUE;
2315                     loopin = FALSE;
2316                     changed = TRUE;
2317                     break;
2318                  }
2319                  else if (SeqLocStop(slpa) == SeqLocStart(slpb)-1) {
2320                     sint = (SeqIntPtr) slpa->data.ptrvalue;
2321                     sint->to = SeqLocStop(slpb);
2322                     ObjMgrDeSelect(eIDb, iIDb, itb, sspb->regiontype, slpb);
2323                     check = TRUE;
2324                     loopin = FALSE;
2325                     changed = TRUE;
2326                     break;
2327                  }
2328                  else if (SeqLocStart(slpa) == SeqLocStop(slpb)+1) {
2329                     sint = (SeqIntPtr) slpa->data.ptrvalue;
2330                     sint->from = SeqLocStart(slpb);
2331                     ObjMgrDeSelect(eIDb, iIDb, itb, sspb->regiontype, slpb);
2332                     check = TRUE;
2333                     loopin = FALSE;
2334                     changed = TRUE;
2335                     break;
2336                  }
2337               }
2338            }
2339         }
2340         if (loopin && sspa != NULL)
2341            sspa = sspa->next;
2342      }
2343   }
2344   return changed;
2345 }
2346 
ObjMgrSelectPrint(void)2347 NLM_EXTERN void ObjMgrSelectPrint (void)
2348 {
2349   SelStructPtr ssp = NULL;
2350   FILE *fout;
2351   Char    strLog[128];
2352 
2353   fout = FileOpen("LogFile", "a");
2354   if (fout==NULL)
2355      return;
2356 
2357   fprintf (fout, "ObjMgrSelectPrint\n");
2358   ssp = ObjMgrGetSelected();
2359   for (; ssp != NULL; ssp = ssp->next)
2360   {
2361     if (ssp->regiontype == OM_REGION_SEQLOC) {
2362      SeqIdWrite(SeqLocId((SeqLocPtr)ssp->region),strLog,PRINTID_FASTA_LONG,120);
2363      if ( ssp->region != NULL ) {
2364         fprintf (fout, "selstruc  %d %d %d  %s %ld %ld \n", (int)ssp->entityID, (int)ssp->itemID, (int)ssp->itemtype, strLog, (long)SeqLocStart((SeqLocPtr)ssp->region), (long)SeqLocStop((SeqLocPtr)ssp->region));
2365      }
2366      else
2367         fprintf (fout, "selstruc %d %d %d region=NULL\n", (int)ssp->entityID, (int)ssp->itemID, (int)ssp->itemtype);
2368     }
2369     else
2370      fprintf (fout, "selstruc %d %d %d regiontype=0\n", (int)ssp->entityID, (int)ssp->itemID, (int)ssp->itemtype);
2371   }
2372   fprintf (fout, "\n");
2373   FileClose(fout);
2374 }
2375 
2376 /******************************************************************/
SelectType(EditAlignDataPtr adp,Uint2 feattype,Int4 slpto)2377 NLM_EXTERN void SelectType (EditAlignDataPtr adp, Uint2 feattype, Int4 slpto)
2378 {
2379   ValNodePtr       vnp;
2380   SeqLocPtr        slp;
2381   SelStructPtr     ssp;
2382   Uint2            ei, it;
2383   Uint4            ii;
2384   AlignNodePtr     anp;
2385   Boolean          first = TRUE;
2386 
2387   if (adp == NULL) return;
2388   if (adp->anp_list == NULL) return;
2389   for (vnp =adp->anp_list; vnp !=NULL; vnp =vnp->next)
2390   {
2391      anp = (AlignNodePtr) vnp->data.ptrvalue;
2392      if (anp != NULL)
2393      {
2394            ei = anp->seq_entityID;
2395            ii = anp->bsp_itemID;
2396            it = feattype;
2397            ssp = is_selectedbyID (ei, ii, it);
2398            if (ssp == NULL)
2399            {
2400               slp = SeqLocIntNew (0, slpto, Seq_strand_plus, anp->sip);
2401               if (first) {
2402                  ObjMgrSelect (ei, ii, it, OM_REGION_SEQLOC, slp);
2403                  first = FALSE;
2404               }
2405               else
2406                  ObjMgrAlsoSelect (ei, ii, it, OM_REGION_SEQLOC, slp);
2407            }
2408      }
2409   }
2410   return;
2411 }
2412 
2413 /******************************************************************/
GetNumberObjMgrSelect(void)2414 NLM_EXTERN Int2 GetNumberObjMgrSelect (void)
2415 {
2416   SelStructPtr ssp = NULL;
2417   Int2         nselect = 0;
2418 
2419   ssp = ObjMgrGetSelected ();
2420   for (; ssp != NULL; ssp = ssp->next)
2421      if (checkssp_for_editor(ssp)) nselect++;
2422   return nselect;
2423 }
2424 
2425 /******************************************************************/
checkOMss_for_itemtype(Uint2 itemtype)2426 NLM_EXTERN Int2 checkOMss_for_itemtype (Uint2 itemtype)
2427 {
2428   SelStructPtr ssp = NULL;
2429   Int2         nselect = 0;
2430 
2431   ssp = ObjMgrGetSelected ();
2432   for (; ssp != NULL; ssp = ssp->next)
2433      if (ssp->itemtype == itemtype && checkssp_for_editor (ssp))
2434         nselect++;
2435   return nselect;
2436 }
2437 
2438 /******************************************************************/
getOMselect_for_itemtype(Uint2 itemtype)2439 NLM_EXTERN SelStructPtr getOMselect_for_itemtype (Uint2 itemtype)
2440 {
2441   SelStructPtr ssp = NULL;
2442 
2443   ssp = ObjMgrGetSelected ();
2444   for (; ssp != NULL; ssp = ssp->next)
2445      if (ssp->itemtype == itemtype && checkssp_for_editor (ssp))
2446         break;
2447   return ssp;
2448 }
2449 
2450 /******************************************************************/
is_selectedbyID(Uint2 entityID,Uint4 itemID,Uint2 itemtype)2451 NLM_EXTERN SelStructPtr is_selectedbyID (Uint2 entityID, Uint4 itemID, Uint2 itemtype)
2452 {
2453   SelStructPtr ssp = NULL;
2454 
2455   ssp = ObjMgrGetSelected();
2456   for (; ssp != NULL; ssp = ssp->next)
2457   {
2458      if ( is_sameId (ssp->entityID, ssp->itemID, ssp->itemtype, 255, entityID, itemID, itemtype, 255) )
2459      {
2460         break;
2461      }
2462   }
2463   return ssp;
2464 }
2465 
getCDSselect(ValNodePtr seqfeathead,ValNodePtr feathead)2466 NLM_EXTERN SelEdStructPtr getCDSselect (ValNodePtr seqfeathead, ValNodePtr feathead)
2467 {
2468   SelStructPtr   ssp = NULL;
2469   SelEdStructPtr feat = NULL;
2470 
2471   ssp = ObjMgrGetSelected ();
2472   for (; ssp != NULL; ssp = ssp->next) if (checkssp_for_editor (ssp)) {
2473      if ( ssp->itemtype == OBJ_SEQFEAT )
2474      {
2475         feat = get_feat_fromid (seqfeathead, FEATDEF_CDS, ssp->entityID, ssp->itemID, -1, NULL);
2476         if (feat != NULL) break;
2477      }
2478      else if ( ssp->itemtype == OBJ_VIRT )
2479      {
2480         feat = get_feat_fromid (feathead, SEQFEAT_CDREGION, ssp->entityID, ssp->itemID, -1, NULL);
2481         if (feat != NULL) break;
2482      }
2483   }
2484   return feat;
2485 }
2486 
checkCDSselect_forprotein(ValNodePtr seqfeathead,ValNodePtr feathead,Boolean with_prot)2487 NLM_EXTERN Int2 checkCDSselect_forprotein (ValNodePtr seqfeathead, ValNodePtr feathead, Boolean with_prot)
2488 {
2489   SelStructPtr   ssp = NULL;
2490   SelEdStructPtr feat = NULL;
2491   Int2           nselect = 0;
2492 
2493   ssp = ObjMgrGetSelected ();
2494   for (; ssp != NULL; ssp = ssp->next) if (checkssp_for_editor (ssp)) {
2495      if ( ssp->itemtype == OBJ_SEQFEAT )
2496      {
2497         feat = get_feat_fromid (seqfeathead, FEATDEF_CDS, ssp->entityID, ssp->itemID, -1, NULL);
2498         if (feat != NULL)
2499         {
2500            if (with_prot && feat->data != NULL) {
2501                nselect++;
2502            } else if (!with_prot && feat->data == NULL) {
2503                nselect++;
2504            }
2505         }
2506      }
2507      else if ( ssp->itemtype == OBJ_VIRT )
2508      {
2509         feat = get_feat_fromid (feathead, SEQFEAT_CDREGION, ssp->entityID, ssp->itemID, -1, NULL);
2510         if (feat != NULL)
2511            if (with_prot && feat->data != NULL)
2512               nselect++;
2513            else if (!with_prot && feat->data == NULL)
2514               nselect++;
2515      }
2516   }
2517   return nselect;
2518 }
2519 
2520 
2521 /******************************************************************
2522 ***   checkssp_for_editor
2523 ******************************************************************/
checkssp_for_editor(SelStructPtr ssp)2524 NLM_EXTERN Boolean checkssp_for_editor (SelStructPtr ssp)
2525 {
2526   if (ssp == NULL) return FALSE;
2527   if (ssp->regiontype == 0) return FALSE;
2528   if (ssp->region == NULL) {
2529          ssp->regiontype = 0;
2530          return FALSE;
2531   }
2532   return TRUE;
2533 }
2534 
2535 /***********************************************************
2536 ***
2537 ***
2538 ************************************************************/
checkseqlocfeature_for_editor(Uint2 entityID,Uint4 itemID,ValNodePtr headfeat)2539 NLM_EXTERN SeqLocPtr checkseqlocfeature_for_editor (Uint2 entityID, Uint4 itemID, ValNodePtr headfeat)
2540 {
2541   SelEdStructPtr feat;
2542   SeqLocPtr      slp = NULL,
2543                  tmp;
2544   SeqIdPtr       sip;
2545   Int4           start, stop;
2546   Uint1          strand;
2547 
2548   if (headfeat == NULL)
2549      return NULL;
2550   feat = get_feat_fromid (headfeat, 255, entityID, itemID, -1, NULL);
2551   if (feat != NULL)
2552   {
2553      tmp = (SeqLocPtr) feat->region;
2554      sip = SeqLocId (tmp);
2555      strand = SeqLocStrand (tmp);
2556      start = SeqLocStart (tmp);
2557      if (feat->next != NULL)
2558         while (feat->next != NULL) feat = feat->next;
2559      stop = SeqLocStop ((SeqLocPtr) feat->region);
2560      slp = SeqLocIntNew (start, stop, strand, sip);
2561   }
2562   return slp;
2563 }
2564 
checkselectsequinfeature_for_editor(ValNodePtr headfeat)2565 NLM_EXTERN void checkselectsequinfeature_for_editor (ValNodePtr headfeat)
2566 {
2567   SelStructPtr   ssp;
2568   SelEdStructPtr feat;
2569   SeqLocPtr      slp, tmp;
2570   SeqIdPtr       sip;
2571   Int4           start, stop;
2572   Int2           k = 0;
2573   Uint1          strand;
2574 
2575   if (headfeat == NULL) return;
2576   for (ssp = ObjMgrGetSelected (); ssp != NULL; ssp = ssp->next)
2577           if (ssp->itemtype == OBJ_SEQFEAT) k++;
2578   if (k > 0) {
2579      ssp = ObjMgrGetSelected ();
2580      for (; ssp != NULL; ssp = ssp->next) {
2581         if (ssp->itemtype == OBJ_SEQFEAT)
2582         {
2583            if (ssp->regiontype != OM_REGION_SEQLOC || ssp->region == NULL)
2584            {
2585               feat = get_feat_fromid (headfeat, 255, ssp->entityID, ssp->itemID, -1, NULL);
2586               if (feat != NULL) {
2587                  tmp = (SeqLocPtr) feat->region;
2588                  sip = SeqLocId (tmp);
2589                  strand = SeqLocStrand (tmp);
2590                  start = SeqLocStart (tmp);
2591                  if (feat->next != NULL)
2592                     while (feat->next != NULL) feat = feat->next;
2593                  stop = SeqLocStop ((SeqLocPtr) feat->region);
2594                  slp = SeqLocIntNew (start, stop, strand, sip);
2595                  ssp->regiontype = OM_REGION_SEQLOC;
2596                  ssp->region = slp;
2597               }
2598            }
2599         }
2600      }
2601   }
2602 }
2603 
getminpos_fromOMselect(Uint2 itemsubtype)2604 NLM_EXTERN Int4 getminpos_fromOMselect (Uint2 itemsubtype)
2605 {
2606   SelStructPtr   ssp;
2607   Int4           minpos = INT4_MAX;
2608   Int4           from;
2609 
2610   ssp = ObjMgrGetSelected ();
2611   for (; ssp != NULL; ssp = ssp->next)
2612   {
2613      if (ssp->itemtype == itemsubtype)
2614      {
2615         if (ssp->regiontype == OM_REGION_SEQLOC && ssp->region != NULL)
2616         {
2617             from = SeqLocStart ((SeqLocPtr)ssp->region);
2618             if (from < minpos) minpos = from;
2619         }
2620      }
2621   }
2622   if ( minpos < INT4_MAX ) return minpos;
2623   return -1;
2624 }
2625 
2626 /***********************************************
2627 ***  locate_in_seqalign
2628 ***    in : pos in Align coordinates
2629 ***    out: seen TRUE if pos in salp
2630 ************************************************/
locate_in_seqalign(Int4 pos,Int2 dim,Int2 dspnumseg,BoolPtr * dspstart,Int4Ptr * dsplens,Int2 * numseg_before,Int2 * subdsplens,Int4 * sumdsplens_before)2631 NLM_EXTERN Boolean locate_in_seqalign (Int4 pos, Int2 dim, Int2 dspnumseg, BoolPtr *dspstart, Int4Ptr *dsplens, Int2 *numseg_before, Int2 *subdsplens, Int4 *sumdsplens_before)
2632 {
2633   BoolPtr     start = *dspstart;
2634   Int4Ptr     lens  = *dsplens;
2635   Int4        sumlens= 0;
2636   Int4        sumlensseq= 0;
2637   Int2        numseg = 0;
2638   Int2        sublens;
2639   Boolean     seen = FALSE;
2640 
2641   if ( dspnumseg == 0 || start == NULL || lens == NULL ) {
2642          ErrPostEx(SEV_WARNING, 0, 0, "fail in locate_in_seqalign [1]\n");
2643          return FALSE;
2644   }
2645   while ( !seen && numseg < dspnumseg ) {
2646          numseg++;
2647          if ( pos  >= sumlens && pos < sumlens + *lens ) {
2648                 sublens = abs (pos - sumlens);
2649                 seen = TRUE;
2650          }
2651          else {
2652                 if ((Boolean)(*start)) sumlensseq += *lens;
2653                 if ( numseg == dspnumseg ) break;
2654                 start += dim;
2655                 sumlens += *lens;
2656                 lens++;
2657          }
2658   }
2659   if ( seen )
2660   {
2661         *dspstart  = start;
2662         *dsplens   = lens;
2663         *numseg_before = numseg;
2664         *subdsplens= sublens;
2665         *sumdsplens_before= sumlensseq;
2666   }
2667   return seen;
2668 }
2669 
2670 /************************************
2671 *** SeqCoordToAlignCoord
2672 **
2673 ************************************/
SeqCoordToAlignCoord(Int4 position,SeqIdPtr sip,SeqAlignPtr salp,Int2 intersalpwidth,Int2 is_end)2674 NLM_EXTERN Int4 SeqCoordToAlignCoord (Int4 position, SeqIdPtr sip, SeqAlignPtr salp, Int2 intersalpwidth, Int2 is_end)
2675 {
2676   CompSegPtr  dsp;
2677   BoolPtr     dspstart;
2678   Int4Ptr     dsplens;
2679   Int4        from;
2680   Int4        sumlens = 0;
2681   Int4        seqlens = 0;
2682   Int4        lensplus;
2683   Int4        start, stop;
2684   Int2        numseg;
2685   Int2        inter_salp = 0;
2686   Int2        index;
2687   Uint1       dspstrand = Seq_strand_unknown;
2688   Boolean     seen = FALSE;
2689 
2690   if (is_end == NO_RESIDUE)
2691      return position;
2692   if (position < 0)
2693      return position;
2694 
2695   dsp = (CompSegPtr) salp->segs;
2696   if (dsp == NULL) {
2697      return GAP_RESIDUE;
2698   }
2699   index = SeqIdOrderInBioseqIdList (sip, dsp->ids);
2700   if (index < 1) {
2701      return GAP_RESIDUE;
2702   }
2703   index -= 1;
2704   from = *(dsp->from + index);
2705   if (is_end == GAP_RESIDUE)
2706      position = from;
2707   dspstart = dsp->starts + index;
2708   dsplens = dsp->lens;
2709   if (dspstart == NULL || dsplens == NULL ) {
2710      return GAP_RESIDUE;
2711   }
2712   if (dsp->strands!=NULL)
2713      dspstrand = *(dsp->strands + index);
2714 
2715   numseg = 1;
2716   while ( !seen && numseg <= dsp->numseg )
2717   {
2718      if (dspstrand ==Seq_strand_minus) {
2719         start= from - seqlens - *dsplens;
2720         stop = from - seqlens;
2721      } else {
2722         start= from + seqlens;
2723         stop = from + seqlens + *dsplens;
2724      }
2725      if (*dspstart && position >= start && position < stop)
2726      {
2727         if (dspstrand ==Seq_strand_minus)
2728            lensplus = abs (from + seqlens - position);
2729         else
2730            lensplus = abs (position - from - seqlens);
2731         seen = TRUE;
2732      }
2733      else if (*dspstart && position <= stop
2734           /***/ && is_end == APPEND_RESIDUE ) /***!!!!!!!!!**********/
2735      {
2736            if (dspstrand ==Seq_strand_minus)
2737               lensplus = abs (from + seqlens - position);
2738            else
2739               lensplus = abs (position - from - seqlens);
2740            seen = TRUE;
2741      }
2742      else if ( numseg == dsp->numseg )
2743      {
2744         if ( salp->next == NULL ) break;
2745         else
2746         {
2747            salp = salp->next;
2748            dsp = (CompSegPtr) salp->segs;
2749            from = *(dsp->from + index);
2750            dspstart = dsp->starts + index;
2751            dsplens = dsp->lens;
2752            inter_salp++;
2753            numseg = 1;
2754         }
2755      }
2756      else if (numseg < dsp->numseg)
2757      {
2758            sumlens += *dsplens;
2759            if (*dspstart)
2760               seqlens += *dsplens;
2761            dspstart += dsp->dim;
2762            dsplens++;
2763      }
2764      numseg++;
2765   }
2766   if ( !seen ) {
2767      if (!(*dspstart))    /***** if after sequence 2 mais seq1 last segment***/
2768          return seqlens;
2769      return GAP_RESIDUE;
2770   }
2771   if (position == APPEND_RESIDUE)
2772      return position;
2773   position = sumlens + lensplus + intersalpwidth*inter_salp;
2774   return position;
2775 }
2776 
2777 /************************************
2778 *** AlignCoordToSeqCoord
2779 ************************************/
AlignCoordToSeqCoord(Int4 position,SeqIdPtr sip,SeqAlignPtr salp,ValNodePtr sqloc_list,Int2 intersalpwidth)2780 NLM_EXTERN Int4 AlignCoordToSeqCoord (Int4 position, SeqIdPtr sip, SeqAlignPtr salp,ValNodePtr sqloc_list, Int2 intersalpwidth)
2781 {
2782   CompSegPtr  dsp;
2783   BoolPtr     dspstart;
2784   Int4Ptr     dsplens;
2785   Int4        from;
2786   Int4        sumlens = 0;
2787   Int4        sumstart = 0;
2788   Int4        seqlens = 0;
2789   Int2        numseg = 0;
2790   Int2        inter_salp = 0;
2791   Int2        index;
2792   Uint1       dspstrand = Seq_strand_unknown;
2793   Boolean     seen = FALSE;
2794 
2795   if (position == APPEND_RESIDUE)
2796      return position;
2797   dsp = (CompSegPtr) salp->segs;
2798   if (dsp == NULL)
2799      return (Int4)GAP_RESIDUE;
2800   index = SeqIdOrderInBioseqIdList (sip, dsp->ids);
2801   if (index < 1)
2802      return (Int4)GAP_RESIDUE;
2803   index -= 1;
2804   from = *(dsp->from + index);
2805 /*
2806   if (position <= from) {
2807      return from;
2808   }
2809 */
2810   dspstart = dsp->starts + index;
2811   dsplens = dsp->lens;
2812   if (dspstart == NULL || dsplens == NULL ) {
2813      return (Int4)GAP_RESIDUE;
2814   }
2815   if (!(*dspstart) && (position < *dsplens)) {
2816      return (Int4)GAP_RESIDUE;
2817   }
2818   if (dsp->strands!=NULL)
2819      dspstrand = *(dsp->strands + index);
2820   numseg = 0;
2821   sumlens = 0;
2822   for (; dsplens != NULL && numseg < dsp->numseg; dsplens++, numseg++)
2823      sumlens += *dsplens;
2824   dsplens = dsp->lens;
2825   if (position >= sumlens) {
2826      sumlens = 0;
2827      numseg = 0;
2828      for (; dsplens != NULL && numseg < dsp->numseg; dsplens++, numseg++) {
2829         if (*dspstart)  sumlens += *dsplens;
2830         dspstart += dsp->dim;
2831      }
2832     seen = TRUE;
2833      if (dspstrand == Seq_strand_minus) {
2834         position = from - sumlens;
2835      } else {
2836         position = from + sumlens;
2837      }
2838   }
2839   else {
2840         sumlens = 0;
2841         numseg = 0;
2842         while ( !seen && numseg < dsp->numseg ) {
2843             numseg++;
2844             if (position >=sumlens && position <sumlens +*dsplens ) {
2845                if (*dspstart)
2846                   seqlens += abs (position - sumlens);
2847                seen = TRUE;
2848             }
2849             else if ( numseg == dsp->numseg )
2850             {
2851                if ( salp->next == NULL ) break;
2852                else
2853                {
2854                   sumstart += sumlens + *dsplens;
2855                   salp = salp->next;
2856                   dsp = (CompSegPtr) salp->segs;
2857                   from = *(dsp->from + index);
2858                   dspstart = dsp->starts + index;
2859                   dsplens = dsp->lens;
2860                   inter_salp++;
2861                   numseg = 0;
2862                }
2863             }
2864             else
2865             {
2866                if ( *dspstart )
2867                   seqlens += *dsplens;
2868                sumlens += *dsplens;
2869                dspstart += dsp->dim;
2870                dsplens++;
2871             }
2872         }
2873         if (seen) {
2874            if (dspstrand == Seq_strand_minus) {
2875               position = from - seqlens - sumstart;
2876            } else {
2877               position = from + seqlens - sumstart;
2878            }
2879      }
2880   }
2881   if ( !seen ) {
2882      return (Int4)GAP_RESIDUE;
2883   }
2884   index =chkloc (sip, position, sqloc_list, &from);
2885   if (index == GAP_RESIDUE || index == APPEND_RESIDUE) {
2886      return (Int4)index;
2887   }
2888   return position;
2889 }
2890 
AlignCoordToSeqCoord2(Int4 position,SeqIdPtr sip,SeqAlignPtr salp,ValNodePtr sqloc_list,Int2 intersalpwidth)2891 NLM_EXTERN Int4 AlignCoordToSeqCoord2 (Int4 position, SeqIdPtr sip, SeqAlignPtr salp,ValNodePtr sqloc_list, Int2 intersalpwidth)
2892 {
2893   CompSegPtr  dsp;
2894   BoolPtr     dspstart;
2895   Int4Ptr     dsplens;
2896   Int4        from;
2897   Int4        sumlens = 0;
2898   Int4        sumstart = 0;
2899   Int4        seqlens = 0;
2900   Int2        numseg = 0;
2901   Int2        inter_salp = 0;
2902   Int2        index;
2903   Uint1       dspstrand = Seq_strand_unknown;
2904   Boolean     seen = FALSE;
2905 
2906   if (position == APPEND_RESIDUE)
2907      return position;
2908   dsp = (CompSegPtr) salp->segs;
2909   if (dsp == NULL)
2910      return (Int4)GAP_RESIDUE;
2911   index = SeqIdOrderInBioseqIdList (sip, dsp->ids);
2912   if (index < 1)
2913      return (Int4)GAP_RESIDUE;
2914   index -= 1;
2915   from = *(dsp->from + index);
2916 /*
2917   if (position <= from) {
2918      return from;
2919   }
2920 */
2921   dspstart = dsp->starts + index;
2922   dsplens = dsp->lens;
2923   if (dspstart == NULL || dsplens == NULL ) {
2924      return (Int4)GAP_RESIDUE;
2925   }
2926   if (!(*dspstart) && (position < *dsplens)) {
2927      return (Int4)GAP_RESIDUE;
2928   }
2929   if (dsp->strands!=NULL)
2930      dspstrand = *(dsp->strands + index);
2931   numseg = 0;
2932   sumlens = 0;
2933   for (; dsplens != NULL && numseg < dsp->numseg; dsplens++, numseg++)
2934      sumlens += *dsplens;
2935   dsplens = dsp->lens;
2936   if (position >= sumlens) {
2937      position = (Int4)APPEND_RESIDUE;
2938 /*****
2939      sumlens = 0;
2940      numseg = 0;
2941      for (; dsplens != NULL && numseg < dsp->numseg; dsplens++, numseg++) {
2942         if (*dspstart)  sumlens += *dsplens;
2943         dspstart += dsp->dim;
2944      }
2945     seen = TRUE;
2946      if (dspstrand == Seq_strand_minus) {
2947         position = from - sumlens;
2948      } else {
2949         position = from + sumlens;
2950      }
2951 ****/
2952   }
2953   else {
2954         sumlens = 0;
2955         numseg = 0;
2956         while ( !seen && numseg < dsp->numseg ) {
2957             numseg++;
2958             if (position >=sumlens && position <sumlens +*dsplens ) {
2959                if (*dspstart)
2960                   seqlens += abs (position - sumlens);
2961                seen = TRUE;
2962             }
2963             else if ( numseg == dsp->numseg )
2964             {
2965                if ( salp->next == NULL ) break;
2966                else
2967                {
2968                   sumstart += sumlens + *dsplens;
2969                   salp = salp->next;
2970                   dsp = (CompSegPtr) salp->segs;
2971                   from = *(dsp->from + index);
2972                   dspstart = dsp->starts + index;
2973                   dsplens = dsp->lens;
2974                   inter_salp++;
2975                   numseg = 0;
2976                }
2977             }
2978             else
2979             {
2980                if ( *dspstart )
2981                   seqlens += *dsplens;
2982                sumlens += *dsplens;
2983                dspstart += dsp->dim;
2984                dsplens++;
2985             }
2986         }
2987         if (seen) {
2988            if (dspstrand == Seq_strand_minus) {
2989               position = from - seqlens - sumstart;
2990            } else {
2991               if (!(*dspstart)) {
2992                  position =chkloc (sip, position, sqloc_list, &from);
2993                  if (position==0)
2994                     position = (Int4)GAP_RESIDUE;
2995               }
2996               else
2997                  position = from + seqlens - sumstart;
2998            }
2999      }
3000   }
3001   if ( !seen ) {
3002      position =chkloc (sip, position, sqloc_list, &from);
3003   }
3004   return position;
3005 }
3006 
3007 
GetAlignCoordFromSeqLoc(SeqLocPtr slp,SeqAlignPtr salp,Int4 * start,Int4 * stop)3008 NLM_EXTERN Boolean GetAlignCoordFromSeqLoc (SeqLocPtr slp, SeqAlignPtr salp, Int4 *start, Int4 *stop)
3009 {
3010   SeqIdPtr         sip;
3011   Int4             from, to;
3012 
3013   sip = SeqLocId (slp);
3014   from = SeqLocStart (slp);
3015   from = SeqCoordToAlignCoord (from, sip, salp, 0, 0);
3016   to = SeqLocStop (slp);
3017   to = SeqCoordToAlignCoord (to, sip, salp, 0, 0);
3018   if (from < 0 || to < 0)
3019      return FALSE;
3020   *start = from;
3021   *stop = to;
3022   return TRUE;
3023 }
3024 
3025 /************************************************************************
3026 ***
3027 ***   SeqPosToLineColumn
3028 ***
3029 ************************************************************************/
SeqPosToLineColumn(Uint4 itemID,Uint2 entityID,Uint2 itemtype,Int4 pos,Int4 * line,Int4 * column,Int4 hoffset,EditAlignDataPtr adp)3030 NLM_EXTERN Boolean  SeqPosToLineColumn (Uint4 itemID, Uint2 entityID, Uint2 itemtype,
3031            Int4 pos, Int4 *line, Int4 *column, Int4 hoffset, EditAlignDataPtr adp)
3032 {
3033   Boolean seen = FALSE;
3034   Int4    from, to;
3035   Int2    col = 0;
3036   Int4    length;
3037   Int2    j;
3038 
3039   *line = -1;
3040   *column = -1;
3041   if ( pos < 0 )
3042          return FALSE;
3043   hoffset -= adp->bufferstart;
3044   length = adp->length - adp->bufferstart;
3045   for ( j=0; !seen && j < MAXLineWindow; j++ )
3046   {
3047          if (itemID == adp->item_id[j] && entityID == adp->seqEntity_id[j] && itemtype == adp->itemtype[j] )
3048          {
3049                 from = hoffset + adp->alignline [j] * adp->visibleWidth;
3050                 if (from > length) break;
3051                 to = hoffset + (adp->alignline [j]+1) * adp->visibleWidth -1;
3052                 if (to > length) to = length;
3053 /**
3054                 WriteLog ("SeqPosToLineColumn %d %ld %ld %ld %d %d %d %d\n", j,
3055                        pos, from, to, adp->item_id[j], adp->seqEntity_id[j],
3056                        adp->colonne[from], adp->colonne[to]);
3057 **/
3058                 if( pos >= adp->colonne[from]  && pos <= adp->colonne[to])
3059                 {
3060                        col = (Int2)(pos - adp->colonne[from]);
3061                        if (adp->columnpcell > 0)
3062                           col += (Int2)(col) / (Int2)(adp->columnpcell);
3063                        seen = TRUE;
3064                        break;
3065                 }
3066          }
3067   }
3068   if ( !seen || j == MAXLineWindow )
3069          return FALSE;
3070   *line = j;
3071   *column = col;
3072   return TRUE;
3073 }
3074 
3075 /************************************************************************
3076 ***
3077 ***   SeqPosInLineColumn
3078 ***
3079 ************************************************************************/
SeqPosInLineColumn(Int4 pos,Int4 alignline,Int4 * column,Int4 hoffset,EditAlignDataPtr adp)3080 NLM_EXTERN Boolean  SeqPosInLineColumn (Int4 pos, Int4 alignline, Int4 *column,
3081            Int4 hoffset, EditAlignDataPtr adp)
3082 {
3083   Int4    from, to;
3084   Int4    length;
3085   Int4    col = 0;
3086 
3087   if ( pos < 0 )  return FALSE;
3088   hoffset -= adp->bufferstart;
3089   length = adp->length - adp->bufferstart;
3090   from = hoffset + alignline * adp->visibleWidth;
3091   to = hoffset + (alignline +1) * adp->visibleWidth - 1;
3092   if (from > length) return FALSE;
3093   if (to > length) to = length;
3094 
3095   if ( pos < adp->colonne[from] )
3096   {
3097          *column = -1;
3098          return FALSE;
3099   }
3100   if ( pos > adp->colonne[to] )
3101   {
3102          *column = -2;
3103          return FALSE;
3104   }
3105   col = pos - adp->colonne[from];
3106   if (adp->columnpcell > 0)
3107          col += (Int4) col / (Int4) adp->columnpcell;
3108   *column = (Int2) col;
3109   return TRUE;
3110 }
3111 
3112 /***********************************************
3113 ***  LocateInSeqAlign
3114 ***    in : pos in Align coordinates
3115 ***    out: seen TRUE if pos in salp
3116 ************************************************/
LocateInSeqAlign(Int4 pos,Int2 dim,Int2 dspnumseg,BoolPtr * dspstart,Int4Ptr * dsplens,Int2 * numseg_before,Int4 * subdsplens,Int4 * sumdsplens_before)3117 NLM_EXTERN Boolean LocateInSeqAlign (Int4 pos, Int2 dim, Int2 dspnumseg, BoolPtr *dspstart, Int4Ptr *dsplens, Int2 *numseg_before, Int4 *subdsplens, Int4 *sumdsplens_before)
3118 {
3119   BoolPtr     start = *dspstart;
3120   Int4Ptr     lens  = *dsplens;
3121   Int4        sumlens= 0;
3122   Int4        sumlensseq= 0;
3123   Int2        numseg = 0;
3124   Int4        sublens;
3125   Boolean     seen = FALSE;
3126 
3127   if ( dspnumseg == 0 || start == NULL || lens == NULL ) {
3128          ErrPostEx(SEV_WARNING, 0, 0, "fail in LocateInSeqAlign [1]\n");
3129          return FALSE;
3130   }
3131   while ( !seen && numseg < dspnumseg ) {
3132          numseg++;
3133          if ( pos  >= sumlens && pos < sumlens + *lens ) {
3134                 sublens = abs (pos - sumlens);
3135                 seen = TRUE;
3136          }
3137          else {
3138                 if ((Boolean)(*start)) sumlensseq += *lens;
3139                 if ( numseg == dspnumseg ) break;
3140                 start += dim;
3141                 sumlens += *lens;
3142                 lens++;
3143          }
3144   }
3145   if ( seen )
3146   {
3147         *dspstart  = start;
3148         *dsplens   = lens;
3149         *numseg_before = numseg;
3150         *subdsplens= sublens;
3151         *sumdsplens_before= sumlensseq;
3152   }
3153   return seen;
3154 }
3155 
3156 
3157 /***********************************************
3158 ***  LocateInSeqAlign
3159 ***    in : pos in Align coordinates
3160 ***    out: seen TRUE if pos in salp
3161 ************************************************/
LocateInSeqAlignDenSeg(Int4 pos,Int2 dim,Int2 dspnumseg,Int4Ptr * dspstart,Int4Ptr * dsplens,Int2 * numseg_before,Int4 * subdsplens)3162 NLM_EXTERN Boolean LocateInSeqAlignDenSeg (Int4 pos, Int2 dim, Int2 dspnumseg, Int4Ptr *dspstart, Int4Ptr *dsplens, Int2 *numseg_before, Int4 *subdsplens)
3163 {
3164   Int4Ptr     start = *dspstart;
3165   Int4Ptr     lens  = *dsplens;
3166   Int4        sumlens= 0;
3167   Int2        numseg = 0;
3168   Int4        sublens= 0;
3169   Boolean     seen = FALSE;
3170 
3171   if ( dspnumseg == 0 || start == NULL || lens == NULL ) {
3172          ErrPostEx(SEV_WARNING, 0, 0, "fail in LocateInSeqAlignDenSeg [1]");
3173          return FALSE;
3174   }
3175   if (pos == -1 || *start == -1)
3176   {
3177      numseg = 1;      /*!!!!!!!!!!!!!!!!!!!!!*/
3178      seen = TRUE;
3179   }
3180   else {
3181      sumlens = *start;
3182      while ( !seen && numseg < dspnumseg ) {
3183          numseg++;
3184          if ( pos  >= sumlens && pos < sumlens + *lens ) {
3185                 sublens = abs (pos - sumlens);
3186                 seen = TRUE;
3187          }
3188          else {
3189                 if ( numseg == dspnumseg ) break;
3190                 start += dim;
3191                 sumlens += *lens;
3192                 lens++;
3193          }
3194      }
3195   }
3196   if ( seen )
3197   {
3198         *dspstart  = start;
3199         *dsplens   = lens;
3200         *numseg_before = numseg;
3201         *subdsplens= sublens;
3202   }
3203   return seen;
3204 }
3205 
3206 /*******************************************************
3207 ***
3208 ***   FIND functions
3209 ***
3210 *******************************************************/
SetupMatchPatList(ValNodePtr match_pat,ValNodePtr anp_list)3211 NLM_EXTERN SelStructPtr SetupMatchPatList (ValNodePtr match_pat, ValNodePtr anp_list)
3212 {
3213   SelStructPtr    head = NULL;
3214   SelStructPtr    ssp,
3215                   tmp;
3216   SeqLocPtr       slp;
3217   AlignNodePtr    anp;
3218   ValNodePtr      vnp, vnp2;
3219 
3220   if (match_pat == NULL || anp_list == NULL)
3221      return NULL;
3222   for (vnp2 = match_pat; vnp2!=NULL; vnp2=vnp2->next)
3223   {
3224      slp = (SeqLocPtr) vnp2->data.ptrvalue;
3225      if (slp != NULL) {
3226         for (vnp = anp_list; vnp != NULL; vnp = vnp->next) {
3227            anp = (AlignNodePtr) vnp->data.ptrvalue;
3228            if (SeqIdForSameBioseq(anp->sip, SeqLocId(slp)))
3229               break;
3230         }
3231         if (vnp != NULL)
3232         {
3233            ssp = SelStructNew (anp->seq_entityID, anp->bsp_itemID, OBJ_BIOSEQ, SeqLocStart(slp), SeqLocStop(slp), SeqLocId(slp), Seq_strand_plus, FALSE);
3234            if (head == NULL)
3235               head = ssp;
3236            else {
3237               for (tmp=head; tmp->next!=NULL; tmp=tmp->next)
3238                  continue;
3239               tmp->next = ssp;
3240               ssp->prev = tmp;
3241            }
3242         }
3243      }
3244   }
3245   ValNodeFreeType (&match_pat, TypeSeqLoc);
3246   return head;
3247 }
3248 
ShowNextPattern(SelStructPtr match_pat,SelStructPtr cur_pat,Int4 start)3249 NLM_EXTERN SelStructPtr ShowNextPattern (SelStructPtr match_pat, SelStructPtr cur_pat, Int4 start)
3250 {
3251   SeqLocPtr     slp;
3252   SelStructPtr  cur;
3253   Int4          from;
3254 
3255   if (match_pat == NULL)
3256      return NULL;
3257   if (cur_pat == NULL) {
3258      cur_pat = match_pat;
3259   }
3260   else if (cur_pat->next == NULL) {
3261      if (cur_pat == match_pat)
3262         Beep();
3263      else cur_pat = match_pat;
3264   }
3265   else {
3266      cur_pat = cur_pat->next;
3267   }
3268   if (start > 0 && cur_pat != NULL) {
3269      from = SeqLocStart((SeqLocPtr)cur_pat->region);
3270      if (from < start) {
3271         for (cur = cur_pat; cur != NULL; cur = cur->next) {
3272            from = SeqLocStart((SeqLocPtr)cur->region);
3273            if (from > start)
3274               break;
3275         }
3276         if (cur != NULL)
3277            cur_pat = cur;
3278      }
3279   }
3280   if (cur_pat != NULL) {
3281      if (cur_pat->regiontype == OM_REGION_SEQLOC && cur_pat->region != NULL) {
3282         slp = (SeqLocPtr)AsnIoMemCopy (cur_pat->region, (AsnReadFunc) SeqLocAsnRead, (AsnWriteFunc) SeqLocAsnWrite);
3283         ObjMgrSelect(cur_pat->entityID, cur_pat->itemID, OBJ_BIOSEQ, OM_REGION_SEQLOC, (Pointer)slp);
3284      }
3285   }
3286   return cur_pat;
3287 }
3288 
ShowPrecPattern(SelStructPtr match_pat,SelStructPtr cur_pat,Int4 start)3289 NLM_EXTERN SelStructPtr ShowPrecPattern (SelStructPtr match_pat, SelStructPtr cur_pat, Int4 start)
3290 {
3291   SelStructPtr  tmp;
3292   SeqLocPtr     slp;
3293 
3294   if (match_pat == NULL)
3295      return NULL;
3296   if (cur_pat == NULL) {
3297      cur_pat = match_pat;
3298   }
3299   else if (cur_pat->prev == NULL) {
3300      for (tmp=match_pat; tmp->next!=NULL; tmp=tmp->next)
3301         continue;
3302      cur_pat = tmp;
3303      if (cur_pat == match_pat)
3304         Beep();
3305   }
3306   else {
3307      cur_pat = cur_pat->prev;
3308   }
3309   if (cur_pat != NULL) {
3310      if (cur_pat->regiontype == OM_REGION_SEQLOC && cur_pat->region != NULL) {
3311         slp = (SeqLocPtr)AsnIoMemCopy (cur_pat->region, (AsnReadFunc) SeqLocAsnRead, (AsnWriteFunc) SeqLocAsnWrite);
3312         ObjMgrSelect(cur_pat->entityID, cur_pat->itemID, OBJ_BIOSEQ, OM_REGION_SEQLOC, (Pointer)slp);
3313      }
3314   }
3315   return cur_pat;
3316 }
3317 
3318 /**************************************
3319 ***
3320 ***  EDITOR Procedure
3321 ***
3322 ***************************************/
3323 static Char insertstr[] = "Insertboard";
3324 
insertchar(CharPtr str,Int4 pos,SeqIdPtr target,Uint1 mol_type,Boolean spliteditmode)3325 NLM_EXTERN Boolean insertchar (CharPtr str, Int4 pos, SeqIdPtr target, Uint1 mol_type, Boolean spliteditmode)
3326 {
3327   SeqEntryPtr      sep;
3328   Int4             strlens;
3329   SeqId            insertboard;
3330   ObjectId         oip;
3331   Boolean          ok = FALSE;
3332 
3333   if ( str == NULL )
3334      return FALSE;
3335   strlens = StringLen (str);
3336   if (strlens < 1)
3337      return FALSE;
3338   if (pos < -2 || target == NULL)
3339      return FALSE;
3340   insertboard.choice = SEQID_LOCAL;
3341   oip.str = insertstr;
3342   oip.id = 0;
3343   insertboard.data.ptrvalue = (ObjectIdPtr) &oip;
3344   insertboard.next = NULL;
3345   sep = StringToSeqEntry (str, &insertboard, strlens, mol_type);
3346   if (sep != NULL) {
3347      ok = BioseqInsert(&insertboard, FIRST_RESIDUE, LAST_RESIDUE, Seq_strand_plus, target, pos, TRUE, TRUE, spliteditmode);
3348      SeqEntryFree (sep);
3349   }
3350   else
3351      ErrPostEx(SEV_WARNING, 0, 0,  "fail in insertchar [2]");
3352   return ok;
3353 }
3354 
insertchar_atcaret(CharPtr str,EditAlignDataPtr adp)3355 NLM_EXTERN Boolean insertchar_atcaret (CharPtr str, EditAlignDataPtr adp)
3356 {
3357   SeqLocPtr        slpcaret;
3358   Int4             pos;
3359   Int4             strlens;
3360 
3361   if ( str == NULL )
3362      return FALSE;
3363   strlens = StringLen (str);
3364   if (strlens < 1)
3365      return FALSE;
3366   if ( adp->caret.regiontype == 0 )
3367      return FALSE;
3368   slpcaret = (SeqLocPtr) adp->caret.region;
3369   pos = SeqLocStart (slpcaret);
3370   if ( pos == adp->length )
3371      pos = -2;
3372   return (Boolean) insertchar (str, pos, SeqLocId (slpcaret), adp->mol_type, adp->spliteditmode);
3373 }
3374 
char_to_insert(Char * ch,Int4 lens,Uint1 mol_type)3375 NLM_EXTERN CharPtr char_to_insert (Char *ch, Int4 lens, Uint1 mol_type)
3376 {
3377   CharPtr      str, strp;
3378   Int4         j;
3379 
3380   str = (CharPtr)MemNew ((size_t)((lens+2)*sizeof(Char)));
3381   for (j=0, strp = ch; j<lens && *strp!='\0'; j++, strp++)
3382      str[j] = *strp;
3383   str[j] = '\0';
3384   lens = StringLen (str);
3385 
3386   if ( ISA_na (mol_type))
3387   {
3388      for (j=0, strp = str; j<lens; j++, strp++) {
3389         *strp = TO_LOWER(*strp);
3390         if ( StringChr ("abcdghkmnrstuvwy", *strp) == NULL ) {
3391            MemFree (str);
3392            return NULL;
3393         }
3394      }
3395   }
3396   else if ( ISA_aa (mol_type) )
3397   {
3398      for (j=0, strp = str; j<lens; j++, strp++) {
3399         *strp = TO_UPPER(*strp);
3400         if ( StringChr ("ABCDEFGHIKLMNPQRSTUVWXYZ-*", *strp) == NULL ) {
3401            MemFree (str);
3402            return NULL;
3403         }
3404      }
3405   }
3406   else {
3407      for (j=0, strp = str; j<lens; j++, strp++) {
3408         *strp = TO_UPPER(*strp);
3409      }
3410   }
3411   return str;
3412 }
3413 
3414 
getfirst_sep(SeqEntryPtr sep,Uint1 bsp_mol)3415 NLM_EXTERN SeqEntryPtr getfirst_sep(SeqEntryPtr sep, Uint1 bsp_mol)
3416 {
3417   SeqEntryPtr  septmp = NULL;
3418   SeqEntryPtr  sep2 = NULL;
3419   BioseqSetPtr bssp;
3420   BioseqPtr    bsp;
3421 
3422   if (sep == NULL) return NULL;
3423   if (IS_Bioseq_set(sep)) {
3424          bssp = (BioseqSetPtr)sep->data.ptrvalue;
3425          if (bssp == NULL)
3426                 return NULL;
3427          septmp = (SeqEntryPtr) bssp->seq_set;
3428          if (septmp == NULL)
3429                 return NULL;
3430   }
3431   else if (IS_Bioseq(sep))
3432      septmp = sep;
3433 
3434   while ( septmp != NULL ) {
3435      if  (IS_Bioseq_set(septmp)) {
3436         bssp = (BioseqSetPtr) septmp->data.ptrvalue;
3437         for (sep2 = bssp->seq_set; sep2 != NULL; sep2 = sep2->next) {
3438            if (IS_Bioseq(sep2)) {
3439               bsp = (BioseqPtr) sep2->data.ptrvalue;
3440               if (bsp!=NULL && bsp->id!=NULL) {
3441                  if (ISA_na(bsp->mol) == ISA_na(bsp_mol)) {
3442                     return sep2;
3443                  }
3444               }
3445            }
3446         }
3447      }
3448      else if (IS_Bioseq(septmp)) {
3449         bsp = (BioseqPtr) septmp->data.ptrvalue;
3450         if (bsp!=NULL && bsp->id!=NULL)
3451         {
3452            if (ISA_na(bsp->mol) == ISA_na(bsp_mol)) {
3453               return septmp;
3454            }
3455         }
3456      }
3457      septmp = septmp->next;
3458   }
3459   return NULL;
3460 }
3461 
3462 
3463 /*
3464 static SeqAnnotPtr SeqAnnotForHistSeqAlign (SeqAlignPtr salp)
3465 {
3466   SeqAnnotPtr      sap = NULL;
3467   ObjectIdPtr      oip;
3468   UserFieldPtr     ufp;
3469   UserObjectPtr    uop;
3470 
3471   sap = SeqAnnotForSeqAlign (salp);
3472   if (sap != NULL && sap->type == 2)
3473   {
3474           oip = ObjectIdNew ();
3475           oip->str = StringSave ("Hist Seqalign");
3476           uop = UserObjectNew ();
3477           uop->type = oip;
3478 
3479           oip = ObjectIdNew();
3480           oip->str = StringSave ("Hist Seqalign");
3481           ufp = UserFieldNew ();
3482           ufp->choice = 4;
3483           ufp->data.boolvalue = TRUE;
3484           ufp->label = oip;
3485 
3486           uop->data = ufp;
3487           ValNodeAddPointer (&(sap->desc), Annot_descr_user, (Pointer) uop);
3488   }
3489   return sap;
3490 }
3491 
3492 static void AttachSeqAligntoBioseq (Uint2 entityID, SeqAlignPtr salp)
3493 {
3494   SeqEntryPtr      sep = NULL;
3495   SeqAnnotPtr      sap = NULL, tmp;
3496   BioseqSetPtr     bssp= NULL;
3497   BioseqPtr        bsp = NULL;
3498 
3499   sep = GetTopSeqEntryForEntityID (entityID);
3500   if (sep!=NULL) {
3501      if (IS_Bioseq(sep))
3502         bsp = (BioseqPtr) sep->data.ptrvalue;
3503      else if (IS_Bioseq_set(sep)) {
3504         bssp = (BioseqSetPtr) sep->data.ptrvalue;
3505         while (bssp!=NULL && bssp->_class == 7) {
3506            sep = bssp->seq_set;
3507            bssp = NULL;
3508            if (IS_Bioseq(sep))  {
3509               bsp = (BioseqPtr) sep->data.ptrvalue;
3510               break;
3511            }
3512            else if (IS_Bioseq_set(sep))
3513               bssp = (BioseqSetPtr) sep->data.ptrvalue;
3514         }
3515      }
3516      if (bssp!=NULL) {
3517         if (bssp->annot==NULL) {
3518            sap = SeqAnnotForHistSeqAlign (salp);
3519            bssp->annot = sap;
3520         } else {
3521            for (tmp=bssp->annot;tmp->next!= NULL; tmp=tmp->next)
3522               continue;
3523            sap = SeqAnnotForHistSeqAlign (salp);
3524            tmp->next = sap;
3525         }
3526      }
3527      else if (bsp != NULL) {
3528         if (bsp->annot==NULL) {
3529            sap = SeqAnnotForHistSeqAlign (salp);
3530            bsp->annot = sap;
3531         } else {
3532            for (tmp=bsp->annot;tmp->next!= NULL; tmp=tmp->next)
3533               continue;
3534            sap = SeqAnnotForHistSeqAlign (salp);
3535            tmp->next = sap;
3536         }
3537      }
3538   }
3539 }
3540 */
3541 /*
3542 static Uint2 GetEntityIDForBioseqSet (void)
3543 {
3544   SeqEntryPtr  sep;
3545   ValNodePtr   vnp;
3546   AlignNodePtr anp;
3547   Uint2        eID = 0;
3548 
3549   sep=GetBestTopParentForItemID (master->entityID, master->itemID, OBJ_BIOSEQ);
3550   sep = GetTopSeqEntryForEntityID (master->entityID);
3551   if (sep == NULL) return 0;
3552   if (IS_Bioseq_set(sep)) {
3553      eID = SeqMgrGetEntityIDForSeqEntry(sep);
3554   }
3555   else {
3556      for (vnp=anp_list; vnp!=NULL; vnp=vnp->next) {
3557         anp = (AlignNodePtr) vnp->data.ptrvalue;
3558         sep = GetBestTopParentForItemID (anp->seq_entityID, anp->bsp_itemID, OBJ_BIOSEQ);
3559         sep = GetTopSeqEntryForEntityID (anp->seq_entityID);
3560         if (sep!=NULL)
3561            if (IS_Bioseq_set(sep)) {
3562               eID = SeqMgrGetEntityIDForSeqEntry(sep);
3563               break;
3564            }
3565      }
3566   }
3567   return eID;
3568 }
3569 */
3570 
3571 
CleanUpSegGap(SeqAlignPtr sap)3572 NLM_EXTERN Boolean CleanUpSegGap (SeqAlignPtr sap)
3573 {
3574    DenseSegPtr  dsp;
3575    DenseSegPtr  dsp_new;
3576    Boolean      found, any_found = FALSE;
3577    Int4         i;
3578    Int4         j;
3579    Int4         k;
3580    Int4         numgap;
3581 
3582    if (sap == NULL || sap->segtype != SAS_DENSEG)
3583       return FALSE;
3584    dsp = (DenseSegPtr)(sap->segs);
3585    numgap = 0;
3586    for (i=0; i<dsp->numseg; i++)
3587    {
3588       found = FALSE;
3589       for (j=0; !found && j<dsp->dim; j++)
3590       {
3591          if (dsp->starts[i*dsp->dim+j] != -1)
3592             found = TRUE;
3593       }
3594       if (!found)
3595          numgap++;
3596    }
3597    if (numgap == 0)
3598       return FALSE;
3599    dsp_new = DenseSegNew();
3600    dsp_new->dim = dsp->dim;
3601    dsp_new->ids = dsp->ids;
3602    dsp->ids = NULL;
3603    dsp_new->numseg = dsp->numseg - numgap;
3604    dsp_new->starts = (Int4Ptr)MemNew(dsp_new->numseg*dsp_new->dim*sizeof(Int4));
3605    dsp_new->strands = (Uint1Ptr)MemNew(dsp_new->numseg*dsp_new->dim*sizeof(Uint1));
3606    dsp_new->lens = (Int4Ptr)MemNew(dsp_new->numseg*sizeof(Int4));
3607    k = 0;
3608    for (i=0; i<dsp->numseg; i++)
3609    {
3610       found = FALSE;
3611       for (j=0; !found && j<dsp->dim; j++)
3612       {
3613          if (dsp->starts[i*dsp->dim+j] != -1)
3614             found = TRUE;
3615       }
3616       if (found)
3617       {
3618          for (j=0; j<dsp_new->dim; j++)
3619          {
3620             dsp_new->starts[k*dsp_new->dim+j] = dsp->starts[i*dsp->dim+j];
3621             dsp_new->strands[k*dsp_new->dim+j] = dsp->strands[i*dsp->dim+j];
3622          }
3623          dsp_new->lens[k] = dsp->lens[i];
3624          k++;
3625          any_found = TRUE;
3626       }
3627    }
3628    DenseSegFree(dsp);
3629    sap->segs = (Pointer)(dsp_new);
3630    return any_found;
3631 }
3632 
3633 
NoMoreSegGapForOneAlignment(SeqAlignPtr sap,Pointer userdata)3634 static void NoMoreSegGapForOneAlignment (SeqAlignPtr sap, Pointer userdata)
3635 {
3636   SeqAlignPtr       salp;
3637   Int4Ptr           p_i;
3638 
3639   salp = sap;
3640   while (salp != NULL)
3641   {
3642     if (salp->saip != NULL)
3643     {
3644        SeqAlignIndexFree(salp->saip);
3645        salp->saip = NULL;
3646     } else {
3647        AlnMgr2IndexSingleChildSeqAlign(salp); /* make sure it's dense-seg */
3648        SeqAlignIndexFree(salp->saip);
3649        salp->saip = NULL;
3650     }
3651     if (CleanUpSegGap(salp)) {
3652       if ((p_i = (Int4Ptr) userdata) != NULL) {
3653         (*p_i)++;
3654       }
3655     }
3656     AlnMgr2IndexSingleChildSeqAlign(salp);
3657     salp = salp->next;
3658   }
3659 }
3660 
3661 
RemoveSegGapsInSeqEntry(SeqEntryPtr sep)3662 NLM_EXTERN Int4 RemoveSegGapsInSeqEntry (SeqEntryPtr sep)
3663 {
3664   Int4 num_affected = 0;
3665 
3666   VisitAlignmentsInSep (sep, &num_affected, NoMoreSegGapForOneAlignment);
3667 
3668   return num_affected;
3669 }
3670