1 /* @source enscoordsystem *****************************************************
2 **
3 ** Ensembl Coordinate System functions
4 **
5 ** @author Copyright (C) 1999 Ensembl Developers
6 ** @author Copyright (C) 2006 Michael K. Schuster
7 ** @version $Revision: 1.52 $
8 ** @modified 2009 by Alan Bleasby for incorporation into EMBOSS core
9 ** @modified $Date: 2013/02/17 13:02:40 $ by $Author: mks $
10 ** @@
11 **
12 ** This library is free software; you can redistribute it and/or
13 ** modify it under the terms of the GNU Lesser General Public
14 ** License as published by the Free Software Foundation; either
15 ** version 2.1 of the License, or (at your option) any later version.
16 **
17 ** This library is distributed in the hope that it will be useful,
18 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 ** Lesser General Public License for more details.
21 **
22 ** You should have received a copy of the GNU Lesser General Public
23 ** License along with this library; if not, write to the Free Software
24 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
25 ** MA 02110-1301, USA.
26 **
27 ******************************************************************************/
28
29 /* ========================================================================= */
30 /* ============================= include files ============================= */
31 /* ========================================================================= */
32
33 #include "enscoordsystem.h"
34 #include "ensmetainformation.h"
35 #include "enstable.h"
36
37
38
39
40 /* ========================================================================= */
41 /* =============================== constants =============================== */
42 /* ========================================================================= */
43
44
45
46
47 /* ========================================================================= */
48 /* =========================== global variables ============================ */
49 /* ========================================================================= */
50
51
52
53
54 /* ========================================================================= */
55 /* ============================= private data ============================== */
56 /* ========================================================================= */
57
58
59
60
61 /* ========================================================================= */
62 /* =========================== private constants =========================== */
63 /* ========================================================================= */
64
65
66
67
68 /* ========================================================================= */
69 /* =========================== private variables =========================== */
70 /* ========================================================================= */
71
72
73
74
75 /* ========================================================================= */
76 /* =========================== private functions =========================== */
77 /* ========================================================================= */
78
79 static int listCoordsystemCompareIdentifierAscending(
80 const void *item1,
81 const void *item2);
82
83 static int listCoordsystemCompareIdentifierDescending(
84 const void *item1,
85 const void *item2);
86
87 static int listCoordsystemCompareRankAscending(
88 const void *item1,
89 const void *item2);
90
91 static int listCoordsystemCompareRankDescending(
92 const void *item1,
93 const void *item2);
94
95 static AjBool coordsystemadaptorFetchAllbyStatement(
96 EnsPCoordsystemadaptor csa,
97 const AjPStr statement,
98 AjPList css);
99
100 static AjBool coordsystemadaptorCacheInit(EnsPCoordsystemadaptor csa);
101
102 static AjBool coordsystemadaptorMappingpathInit(EnsPCoordsystemadaptor csa);
103
104 static AjBool coordsystemadaptorSeqregionMapInit(EnsPCoordsystemadaptor csa);
105
106 static void coordsystemadaptorMappingpathValdel(void **Pvalue);
107
108 static void coordsystemadaptorFetchAll(const void *key,
109 void **Pvalue,
110 void *cl);
111
112
113
114
115 /* ========================================================================= */
116 /* ======================= All functions by section ======================== */
117 /* ========================================================================= */
118
119
120
121
122 /* @filesection enscoordsystem ************************************************
123 **
124 ** @nam1rule ens Function belongs to the Ensembl library.
125 **
126 ******************************************************************************/
127
128
129
130
131 /* @datasection [EnsPCoordsystem] Ensembl Coordinate System *******************
132 **
133 ** @nam2rule Coordsystem Functions for manipulating
134 ** Ensembl Coordinate System objects
135 **
136 ** @cc Bio::EnsEMBL::CoordSystem
137 ** @cc CVS Revision: 1.14
138 ** @cc CVS Tag: branch-ensembl-68
139 **
140 ******************************************************************************/
141
142
143
144
145 /* @section constructors ******************************************************
146 **
147 ** All constructors return a new Ensembl Coordinate System by pointer.
148 ** It is the responsibility of the user to first destroy any previous
149 ** Coordinate System. The target pointer does not need to be initialised to
150 ** NULL, but it is good programming practice to do so anyway.
151 **
152 ** @fdata [EnsPCoordsystem]
153 **
154 ** @nam3rule New Constructor
155 ** @nam4rule Cpy Constructor with existing object
156 ** @nam4rule Ini Constructor with initial values
157 ** @nam4rule Ref Constructor by incrementing the use counter
158 **
159 ** @argrule Cpy cs [const EnsPCoordsystem] Ensembl Coordinate System
160 ** @argrule Ini csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
161 ** @argrule Ini identifier [ajuint] SQL database-internal identifier
162 ** @argrule Ini name [AjPStr] Name
163 ** @argrule Ini version [AjPStr] Version
164 ** @argrule Ini rank [ajuint] Rank
165 ** @argrule Ini dflt [AjBool] Default attribute
166 ** @argrule Ini toplevel [AjBool] Top-level attrbute
167 ** @argrule Ini seqlevel [AjBool] Sequence-level attribute
168 ** @argrule Ref cs [EnsPCoordsystem] Ensembl Coordinate System
169 **
170 ** @valrule * [EnsPCoordsystem] Ensembl Coordinate System or NULL
171 **
172 ** @fcategory new
173 ******************************************************************************/
174
175
176
177
178 /* @func ensCoordsystemNewCpy *************************************************
179 **
180 ** Object-based constructor function, which returns an independent object.
181 **
182 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
183 **
184 ** @return [EnsPCoordsystem] Ensembl Coordinate System or NULL
185 **
186 ** @release 6.4.0
187 ** @@
188 ******************************************************************************/
189
ensCoordsystemNewCpy(const EnsPCoordsystem cs)190 EnsPCoordsystem ensCoordsystemNewCpy(const EnsPCoordsystem cs)
191 {
192 EnsPCoordsystem pthis = NULL;
193
194 if (!cs)
195 return NULL;
196
197 AJNEW0(pthis);
198
199 pthis->Use = 1U;
200
201 pthis->Identifier = cs->Identifier;
202
203 pthis->Adaptor = cs->Adaptor;
204
205 if (cs->Name)
206 pthis->Name = ajStrNewRef(cs->Name);
207
208 /*
209 ** Although Coordinate System versions are optional, the AJAX String
210 ** should always be defined, since Ensembl Slice names are depending
211 ** on it.
212 */
213
214 if (cs->Version)
215 pthis->Version = ajStrNewRef(cs->Version);
216 else
217 pthis->Version = ajStrNew();
218
219 pthis->Default = cs->Default;
220
221 pthis->SequenceLevel = cs->SequenceLevel;
222
223 pthis->Toplevel = cs->Toplevel;
224
225 pthis->Rank = cs->Rank;
226
227 return pthis;
228 }
229
230
231
232
233 /* @func ensCoordsystemNewIni *************************************************
234 **
235 ** Constructor for an Ensembl Coordinate System with initial values.
236 **
237 ** @cc Bio::EnsEMBL::Storable::new
238 ** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
239 ** @param [r] identifier [ajuint] SQL database-internal identifier
240 ** @cc Bio::EnsEMBL::CoordSystem::new
241 ** @param [u] name [AjPStr] Name
242 ** @param [uN] version [AjPStr] Version
243 ** @param [r] rank [ajuint] Rank
244 ** @param [r] dflt [AjBool] Default attribute
245 ** @param [r] toplevel [AjBool] Top-level attrbute
246 ** @param [r] seqlevel [AjBool] Sequence-level attribute
247 **
248 ** @return [EnsPCoordsystem] Ensembl Coordinate System or NULL
249 **
250 ** @release 6.4.0
251 ** @@
252 ** Many Ensembl Coordinate System objects do not have a concept of a version
253 ** for the entire Ensembl Coordinate System, although they may have a
254 ** per-sequence version.
255 ** The 'chromosome' Ensembl Coordinate System usually has a version - i.e. the
256 ** assembly build version - but the clonal Ensembl Coordinate System does not,
257 ** despite having individual sequence versions. In the case where an
258 ** Ensembl Coordinate System does not have a version an empty string is
259 ** used instead.
260 **
261 ** The highest-level Ensembl Coordinate System (e.g. chromosome) should have
262 ** rank 1, the second-highest level Ensembl Coordinate System (e.g. clone)
263 ** should have rank 2 and so on.
264 **
265 ** Top-level should only be set for creating an artificial
266 ** top-level Ensembl Coordinate System of name 'toplevel'.
267 ******************************************************************************/
268
ensCoordsystemNewIni(EnsPCoordsystemadaptor csa,ajuint identifier,AjPStr name,AjPStr version,ajuint rank,AjBool dflt,AjBool toplevel,AjBool seqlevel)269 EnsPCoordsystem ensCoordsystemNewIni(
270 EnsPCoordsystemadaptor csa,
271 ajuint identifier,
272 AjPStr name,
273 AjPStr version,
274 ajuint rank,
275 AjBool dflt,
276 AjBool toplevel,
277 AjBool seqlevel)
278 {
279 EnsPCoordsystem cs = NULL;
280
281 if (ajDebugTest("ensCoordsystemNewIni"))
282 ajDebug("ensCoordsystemNewIni\n"
283 " csa %p\n"
284 " identifier %u\n"
285 " name '%S'\n"
286 " version '%S'\n"
287 " rank %u\n"
288 " dflt '%B'\n"
289 " toplevel '%B'\n"
290 " seqlevel '%B'\n",
291 csa,
292 identifier,
293 name,
294 version,
295 rank,
296 dflt,
297 toplevel,
298 seqlevel);
299
300 if (toplevel)
301 {
302 if (name && ajStrGetLen(name))
303 {
304 if (!ajStrMatchCaseC(name, "toplevel"))
305 {
306 ajWarn("ensCoordsystemNewIni name parameter must be 'toplevel' "
307 "if the top-level parameter is set.\n");
308
309 return NULL;
310 }
311 }
312
313 if (rank > 0)
314 {
315 ajWarn("ensCoordsystemNewIni rank parameter must be 0 "
316 "if the top-level parameter is set.\n");
317
318 return NULL;
319 }
320
321 if (seqlevel == ajTrue)
322 {
323 ajWarn("ensCoordsystemNewIni sequence-level parameter must not "
324 "be set if the top-level parameter is set.\n");
325
326 return NULL;
327 }
328
329 if (dflt == ajTrue)
330 {
331 ajWarn("ensCoordsystemNewIni default parameter must not be set "
332 "if the top-level parameter is set.\n");
333
334 return NULL;
335 }
336 }
337 else
338 {
339 if (name && ajStrGetLen(name))
340 {
341 if (ajStrMatchCaseC(name, "toplevel"))
342 {
343 ajWarn("ensCoordsystemNewIni name parameter cannot be "
344 "'toplevel' for non-top-level "
345 "Ensembl Coordinate System objects.\n");
346
347 return NULL;
348 }
349 }
350 else
351 {
352 ajWarn("ensCoordsystemNewIni name parameter must be provided for "
353 "non-top-level Ensembl Coordinate System objects.\n");
354
355 return NULL;
356 }
357
358 if (rank == 0)
359 {
360 ajWarn("ensCoordsystemNewIni rank parameter must be non-zero "
361 "for non-top-level Ensembl Coordinate System objects.\n");
362
363 return NULL;
364 }
365 }
366
367 AJNEW0(cs);
368
369 cs->Use = 1U;
370
371 cs->Identifier = identifier;
372
373 cs->Adaptor = csa;
374
375 if (toplevel)
376 cs->Name = ajStrNewC("toplevel");
377 else if (name)
378 cs->Name = ajStrNewRef(name);
379
380 /*
381 ** Although Coordinate System versions are optional, the AJAX String
382 ** should always be defined, since Ensembl Slice names are depending
383 ** on it.
384 */
385
386 if (version)
387 cs->Version = ajStrNewRef(version);
388 else
389 cs->Version = ajStrNew();
390
391 cs->Rank = rank;
392
393 cs->SequenceLevel = seqlevel;
394
395 cs->Toplevel = toplevel;
396
397 cs->Default = dflt;
398
399 return cs;
400 }
401
402
403
404
405 /* @func ensCoordsystemNewRef *************************************************
406 **
407 ** Ensembl Object referencing function, which returns a pointer to the
408 ** Ensembl Object passed in and increases its reference count.
409 **
410 ** @param [u] cs [EnsPCoordsystem] Ensembl Coordinate System
411 **
412 ** @return [EnsPCoordsystem] Ensembl Coordinate System or NULL
413 **
414 ** @release 6.2.0
415 ** @@
416 ******************************************************************************/
417
ensCoordsystemNewRef(EnsPCoordsystem cs)418 EnsPCoordsystem ensCoordsystemNewRef(EnsPCoordsystem cs)
419 {
420 if (ajDebugTest("ensCoordsystemNewRef"))
421 {
422 ajDebug("ensCoordsystemNewRef\n"
423 " cs %p\n",
424 cs);
425
426 ensCoordsystemTrace(cs, 1);
427 }
428
429 if (!cs)
430 return NULL;
431
432 cs->Use++;
433
434 return cs;
435 }
436
437
438
439
440 /* @section destructors *******************************************************
441 **
442 ** Destruction destroys all internal data structures and frees the memory
443 ** allocated for an Ensembl Coordinate System object.
444 **
445 ** @fdata [EnsPCoordsystem]
446 **
447 ** @nam3rule Del Destroy (free) an Ensembl Coordinate System
448 **
449 ** @argrule * Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
450 **
451 ** @valrule * [void]
452 **
453 ** @fcategory delete
454 ******************************************************************************/
455
456
457
458
459 /* @func ensCoordsystemDel ****************************************************
460 **
461 ** Default destructor for an Ensembl Coordinate System.
462 **
463 ** @param [d] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
464 **
465 ** @return [void]
466 **
467 ** @release 6.2.0
468 ** @@
469 ******************************************************************************/
470
ensCoordsystemDel(EnsPCoordsystem * Pcs)471 void ensCoordsystemDel(EnsPCoordsystem *Pcs)
472 {
473 EnsPCoordsystem pthis = NULL;
474
475 if (!Pcs)
476 return;
477
478 #if defined(AJ_DEBUG) && AJ_DEBUG >= 1
479 if (ajDebugTest("ensCoordsystemDel"))
480 {
481 ajDebug("ensCoordsystemDel\n"
482 " *Pcs %p\n",
483 *Pcs);
484
485 ensCoordsystemTrace(*Pcs, 1);
486 }
487 #endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 1 */
488
489 if (!(pthis = *Pcs) || --pthis->Use)
490 {
491 *Pcs = NULL;
492
493 return;
494 }
495
496 ajStrDel(&pthis->Name);
497 ajStrDel(&pthis->Version);
498
499 ajMemFree((void **) Pcs);
500
501 return;
502 }
503
504
505
506
507 /* @section member retrieval **************************************************
508 **
509 ** Functions for returning members of an Ensembl Coordinate System object.
510 **
511 ** @fdata [EnsPCoordsystem]
512 **
513 ** @nam3rule Get Return Coordinate System attribute(s)
514 ** @nam4rule Adaptor Return the Ensembl Coordinate System Adaptor
515 ** @nam4rule Default Return the default attribute
516 ** @nam4rule Identifier Return the SQL database-internal identifier
517 ** @nam4rule Name Return the name
518 ** @nam4rule Rank Return the rank
519 ** @nam4rule Seqlevel Return the sequence-level attribute
520 ** @nam4rule Toplevel Return the top-level attribute
521 ** @nam4rule Version Return the version
522 **
523 ** @argrule * cs [const EnsPCoordsystem] Coordinate System
524 **
525 ** @valrule Adaptor [EnsPCoordsystemadaptor]
526 ** Ensembl Coordinate System Adaptor or NULL
527 ** @valrule Default [AjBool] Default attribute or ajFalse
528 ** @valrule Identifier [ajuint] SQL database-internal identifier or 0U
529 ** @valrule Name [const AjPStr] Name or NULL
530 ** @valrule Rank [ajuint] Rank or 0U
531 ** @valrule Seqlevel [AjBool] Sequence-level attribute or ajFalse
532 ** @valrule Toplevel [AjBool] Top-level attribute or ajFalse
533 ** @valrule Version [const AjPStr] Version or NULL
534 **
535 ** @fcategory use
536 ******************************************************************************/
537
538
539
540
541 /* @func ensCoordsystemGetAdaptor *********************************************
542 **
543 ** Get the Ensembl Coordinate System Adaptor member of an
544 ** Ensembl Coordinate System.
545 **
546 ** @cc Bio::EnsEMBL::Storable::adaptor
547 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
548 **
549 ** @return [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor or NULL
550 **
551 ** @release 6.2.0
552 ** @@
553 ******************************************************************************/
554
ensCoordsystemGetAdaptor(const EnsPCoordsystem cs)555 EnsPCoordsystemadaptor ensCoordsystemGetAdaptor(const EnsPCoordsystem cs)
556 {
557 return (cs) ? cs->Adaptor : NULL;
558 }
559
560
561
562
563 /* @func ensCoordsystemGetDefault *********************************************
564 **
565 ** Get the default member of an Ensembl Coordinate System.
566 **
567 ** @cc Bio::EnsEMBL::CoordSystem::is_default
568 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
569 **
570 ** @return [AjBool] ajTrue if the Ensembl Coordinate System version defines the
571 ** default of all Ensembl Coordinate System objects with the
572 ** same name.
573 **
574 ** @release 6.2.0
575 ** @@
576 ******************************************************************************/
577
ensCoordsystemGetDefault(const EnsPCoordsystem cs)578 AjBool ensCoordsystemGetDefault(const EnsPCoordsystem cs)
579 {
580 return (cs) ? cs->Default : ajFalse;
581 }
582
583
584
585
586 /* @func ensCoordsystemGetIdentifier ******************************************
587 **
588 ** Get the SQL database-internal identifier member of an
589 ** Ensembl Coordinate System.
590 **
591 ** @cc Bio::EnsEMBL::Storable::dbID
592 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
593 **
594 ** @return [ajuint] SQL database-internal identifier or 0U
595 **
596 ** @release 6.2.0
597 ** @@
598 ******************************************************************************/
599
ensCoordsystemGetIdentifier(const EnsPCoordsystem cs)600 ajuint ensCoordsystemGetIdentifier(const EnsPCoordsystem cs)
601 {
602 return (cs) ? cs->Identifier : 0U;
603 }
604
605
606
607
608 /* @func ensCoordsystemGetName ************************************************
609 **
610 ** Get the name member of an Ensembl Coordinate System.
611 **
612 ** @cc Bio::EnsEMBL::CoordSystem::name
613 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
614 **
615 ** @return [const AjPStr] Name or NULL
616 **
617 ** @release 6.2.0
618 ** @@
619 ******************************************************************************/
620
ensCoordsystemGetName(const EnsPCoordsystem cs)621 const AjPStr ensCoordsystemGetName(const EnsPCoordsystem cs)
622 {
623 return (cs) ? cs->Name : NULL;
624 }
625
626
627
628
629 /* @func ensCoordsystemGetRank ************************************************
630 **
631 ** Get the rank member of an Ensembl Coordinate System.
632 **
633 ** @cc Bio::EnsEMBL::CoordSystem::rank
634 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
635 **
636 ** @return [ajuint] Rank or 0U
637 **
638 ** @release 6.2.0
639 ** @@
640 ******************************************************************************/
641
ensCoordsystemGetRank(const EnsPCoordsystem cs)642 ajuint ensCoordsystemGetRank(const EnsPCoordsystem cs)
643 {
644 return (cs) ? cs->Rank : 0U;
645 }
646
647
648
649
650 /* @func ensCoordsystemGetSeqlevel ********************************************
651 **
652 ** Get the sequence-level member of an Ensembl Coordinate System.
653 **
654 ** @cc Bio::EnsEMBL::CoordSystem::is_sequence_level
655 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
656 **
657 ** @return [AjBool] ajTrue if the Coordinate System defines the sequence-level
658 ** or ajFalse
659 **
660 ** @release 6.4.0
661 ** @@
662 ******************************************************************************/
663
ensCoordsystemGetSeqlevel(const EnsPCoordsystem cs)664 AjBool ensCoordsystemGetSeqlevel(const EnsPCoordsystem cs)
665 {
666 return (cs) ? cs->SequenceLevel : ajFalse;
667 }
668
669
670
671
672 /* @func ensCoordsystemGetToplevel ********************************************
673 **
674 ** Get the top-level member of an Ensembl Coordinate System.
675 **
676 ** @cc Bio::EnsEMBL::CoordSystem::is_top_level
677 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
678 **
679 ** @return [AjBool] ajTrue if the coordinate system defines the top-level
680 ** or ajFalse
681 **
682 ** @release 6.4.0
683 ** @@
684 ******************************************************************************/
685
ensCoordsystemGetToplevel(const EnsPCoordsystem cs)686 AjBool ensCoordsystemGetToplevel(const EnsPCoordsystem cs)
687 {
688 return (cs) ? cs->Toplevel : ajFalse;
689 }
690
691
692
693
694 /* @func ensCoordsystemGetVersion *********************************************
695 **
696 ** Get the version member of an Ensembl Coordinate System.
697 **
698 ** @cc Bio::EnsEMBL::CoordSystem::version
699 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
700 **
701 ** @return [const AjPStr] Version or NULL
702 **
703 ** @release 6.2.0
704 ** @@
705 ******************************************************************************/
706
ensCoordsystemGetVersion(const EnsPCoordsystem cs)707 const AjPStr ensCoordsystemGetVersion(const EnsPCoordsystem cs)
708 {
709 return (cs) ? cs->Version : NULL;
710 }
711
712
713
714
715 /* @section member assignment *************************************************
716 **
717 ** Functions for assigning members of an Ensembl Coordinate System object.
718 **
719 ** @fdata [EnsPCoordsystem]
720 **
721 ** @nam3rule Set Set one member of a Coordinate System
722 ** @nam4rule Adaptor Set the Ensembl Coordinate System Adaptor
723 ** @nam4rule Identifier Set the SQL database-internal identifier
724 **
725 ** @argrule * cs [EnsPCoordsystem] Ensembl Coordinate System object
726 ** @argrule Adaptor csa [EnsPCoordsystemadaptor]
727 ** Ensembl Coordinate System Adaptor
728 ** @argrule Identifier identifier [ajuint] SQL database-internal identifier
729 **
730 ** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
731 **
732 ** @fcategory modify
733 ******************************************************************************/
734
735
736
737
738 /* @func ensCoordsystemSetAdaptor *********************************************
739 **
740 ** Set the Object Adaptor member of an Ensembl Coordinate System.
741 **
742 ** @cc Bio::EnsEMBL::Storable::adaptor
743 ** @param [u] cs [EnsPCoordsystem] Ensembl Coordinate System
744 ** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
745 **
746 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
747 **
748 ** @release 6.2.0
749 ** @@
750 ******************************************************************************/
751
ensCoordsystemSetAdaptor(EnsPCoordsystem cs,EnsPCoordsystemadaptor csa)752 AjBool ensCoordsystemSetAdaptor(EnsPCoordsystem cs,
753 EnsPCoordsystemadaptor csa)
754 {
755 if (!cs)
756 return ajFalse;
757
758 cs->Adaptor = csa;
759
760 return ajTrue;
761 }
762
763
764
765
766 /* @func ensCoordsystemSetIdentifier ******************************************
767 **
768 ** Set the SQL database-internal identifier member of an
769 ** Ensembl Coordinate System.
770 **
771 ** @cc Bio::EnsEMBL::Storable::dbID
772 ** @param [u] cs [EnsPCoordsystem] Ensembl Coordinate System
773 ** @param [r] identifier [ajuint] SQL database-internal identifier
774 **
775 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
776 **
777 ** @release 6.2.0
778 ** @@
779 ******************************************************************************/
780
ensCoordsystemSetIdentifier(EnsPCoordsystem cs,ajuint identifier)781 AjBool ensCoordsystemSetIdentifier(EnsPCoordsystem cs, ajuint identifier)
782 {
783 if (!cs)
784 return ajFalse;
785
786 cs->Identifier = identifier;
787
788 return ajTrue;
789 }
790
791
792
793
794 /* @section debugging *********************************************************
795 **
796 ** Functions for reporting of an Ensembl Coordinate System object.
797 **
798 ** @fdata [EnsPCoordsystem]
799 **
800 ** @nam3rule Trace Report Ensembl Coordinate System members to debug file
801 **
802 ** @argrule Trace cs [const EnsPCoordsystem] Ensembl Coordinate System
803 ** @argrule Trace level [ajuint] Indentation level
804 **
805 ** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
806 **
807 ** @fcategory misc
808 ******************************************************************************/
809
810
811
812
813 /* @func ensCoordsystemTrace **************************************************
814 **
815 ** Trace an Ensembl Coordinate System.
816 **
817 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
818 ** @param [r] level [ajuint] Indentation level
819 **
820 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
821 **
822 ** @release 6.2.0
823 ** @@
824 ******************************************************************************/
825
ensCoordsystemTrace(const EnsPCoordsystem cs,ajuint level)826 AjBool ensCoordsystemTrace(const EnsPCoordsystem cs, ajuint level)
827 {
828 AjPStr indent = NULL;
829
830 if (!cs)
831 return ajFalse;
832
833 indent = ajStrNew();
834
835 ajStrAppendCountK(&indent, ' ', level * 2);
836
837 ajDebug("%SensCoordsystemTrace %p\n"
838 "%S Use %u\n"
839 "%S Identifier %u\n"
840 "%S Adaptor %p\n"
841 "%S Name '%S'\n"
842 "%S Version '%S'\n"
843 "%S SequenceLevel '%B'\n"
844 "%S Toplevel '%B'\n"
845 "%S Default '%B'\n"
846 "%S Rank %u\n",
847 indent, cs,
848 indent, cs->Use,
849 indent, cs->Identifier,
850 indent, cs->Adaptor,
851 indent, cs->Name,
852 indent, cs->Version,
853 indent, cs->SequenceLevel,
854 indent, cs->Toplevel,
855 indent, cs->Default,
856 indent, cs->Rank);
857
858 ajStrDel(&indent);
859
860 return ajTrue;
861 }
862
863
864
865
866 /* @section calculate *********************************************************
867 **
868 ** Functions for calculating information from an
869 ** Ensembl Coordinate System object.
870 **
871 ** @fdata [EnsPCoordsystem]
872 **
873 ** @nam3rule Calculate Calculate Ensembl Coordinate System information
874 ** @nam4rule Memsize Calculate the memory size in bytes
875 **
876 ** @argrule * cs [const EnsPCoordsystem] Ensembl Coordinate System
877 **
878 ** @valrule Memsize [size_t] Memory size in bytes or 0
879 **
880 ** @fcategory misc
881 ******************************************************************************/
882
883
884
885
886 /* @func ensCoordsystemCalculateMemsize ***************************************
887 **
888 ** Get the memory size in bytes of an Ensembl Coordinate System.
889 **
890 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
891 **
892 ** @return [size_t] Memory size in bytes or 0
893 **
894 ** @release 6.4.0
895 ** @@
896 ******************************************************************************/
897
ensCoordsystemCalculateMemsize(const EnsPCoordsystem cs)898 size_t ensCoordsystemCalculateMemsize(const EnsPCoordsystem cs)
899 {
900 size_t size = 0;
901
902 if (!cs)
903 return 0;
904
905 size += sizeof (EnsOCoordsystem);
906
907 if (cs->Name)
908 {
909 size += sizeof (AjOStr);
910
911 size += ajStrGetRes(cs->Name);
912 }
913
914 if (cs->Version)
915 {
916 size += sizeof (AjOStr);
917
918 size += ajStrGetRes(cs->Version);
919 }
920
921 return size;
922 }
923
924
925
926
927 /* @section comparison ********************************************************
928 **
929 ** Functions for comparing Ensembl Coordinate System objects.
930 **
931 ** @fdata [EnsPCoordsystem]
932 **
933 ** @nam3rule Match Compare two Ensembl Coordinate System objects
934 **
935 ** @argrule * cs1 [const EnsPCoordsystem] Ensembl Coordinate System
936 ** @argrule * cs2 [const EnsPCoordsystem] Ensembl Coordinate System
937 **
938 ** @valrule * [AjBool] True on success
939 **
940 ** @fcategory use
941 ******************************************************************************/
942
943
944
945
946 /* @func ensCoordsystemMatch **************************************************
947 **
948 ** Test for matching two Ensembl Coordinate System objects.
949 **
950 ** @cc Bio::EnsEMBL::CoordSystem::equals
951 ** @param [r] cs1 [const EnsPCoordsystem] First Ensembl Coordinate System
952 ** @param [r] cs2 [const EnsPCoordsystem] Second Ensembl Coordinate System
953 **
954 ** @return [AjBool] ajTrue if the Ensembl Coordinate System objects are equal
955 **
956 ** @release 6.2.0
957 ** @@
958 ** The comparison is based on an initial pointer equality test and if that
959 ** fails, a case-insensitive string comparison of the name and version members
960 ** is performed.
961 ******************************************************************************/
962
ensCoordsystemMatch(const EnsPCoordsystem cs1,const EnsPCoordsystem cs2)963 AjBool ensCoordsystemMatch(const EnsPCoordsystem cs1,
964 const EnsPCoordsystem cs2)
965 {
966 if (ajDebugTest("ensCoordsystemMatch"))
967 {
968 ajDebug("ensCoordsystemMatch\n"
969 " cs1 %p\n"
970 " cs2 %p\n",
971 cs1,
972 cs2);
973
974 ensCoordsystemTrace(cs1, 1);
975 ensCoordsystemTrace(cs2, 1);
976 }
977
978 if (!cs1)
979 return ajFalse;
980
981 if (!cs2)
982 return ajFalse;
983
984 if (cs1 == cs2)
985 return ajTrue;
986
987 if (cs1->Identifier != cs2->Identifier)
988 return ajFalse;
989
990 if (!ajStrMatchCaseS(cs1->Name, cs2->Name))
991 return ajFalse;
992
993 if (!ajStrMatchCaseS(cs1->Version, cs2->Version))
994 return ajFalse;
995
996 if (cs1->Default != cs2->Default)
997 return ajFalse;
998
999 if (cs1->SequenceLevel != cs2->SequenceLevel)
1000 return ajFalse;
1001
1002 if (cs1->Toplevel != cs2->Toplevel)
1003 return ajFalse;
1004
1005 if (cs1->Rank != cs2->Rank)
1006 return ajFalse;
1007
1008 return ajTrue;
1009 }
1010
1011
1012
1013
1014 /* @section convenience functions *********************************************
1015 **
1016 ** Ensembl Coordinate System convenience functions
1017 **
1018 ** @fdata [EnsPCoordsystem]
1019 **
1020 ** @nam3rule Get Get member(s) of associated objects
1021 ** @nam4rule Species Get the Ensembl Database Adaptor species
1022 **
1023 ** @argrule * cs [const EnsPCoordsystem] Ensembl Coordinate System
1024 **
1025 ** @valrule Species [AjPStr] Ensembl Database Adaptor species or NULL
1026 **
1027 ** @fcategory use
1028 ******************************************************************************/
1029
1030
1031
1032
1033 /* @func ensCoordsystemGetSpecies *********************************************
1034 **
1035 ** Get the species member of the Ensembl Database Adaptor the
1036 ** Ensembl Coordinate System Adaptor of an Ensembl Coordinate System is
1037 ** based on.
1038 **
1039 ** @cc Bio::EnsEMBL::CoordSystem::species
1040 ** @param [r] cs [const EnsPCoordsystem] Ensembl Coordinate System
1041 **
1042 ** @return [AjPStr] Ensembl Database Adaptor species or NULL
1043 **
1044 ** @release 6.2.0
1045 ** @@
1046 ******************************************************************************/
1047
ensCoordsystemGetSpecies(const EnsPCoordsystem cs)1048 AjPStr ensCoordsystemGetSpecies(const EnsPCoordsystem cs)
1049 {
1050 return ensDatabaseadaptorGetSpecies(
1051 ensCoordsystemadaptorGetDatabaseadaptor(
1052 ensCoordsystemGetAdaptor(cs)));
1053 }
1054
1055
1056
1057
1058 /* @datasection [AjPList] AJAX List *******************************************
1059 **
1060 ** @nam2rule List Functions for manipulating AJAX List objects
1061 **
1062 ******************************************************************************/
1063
1064
1065
1066
1067 /* @funcstatic listCoordsystemCompareIdentifierAscending **********************
1068 **
1069 ** AJAX List of Ensembl Coordinate System objects comparison function to
1070 ** sort by Ensembl Coordinate System identifier in ascending order.
1071 **
1072 ** @param [r] item1 [const void*] Ensembl Coordinate System address 1
1073 ** @param [r] item2 [const void*] Ensembl Coordinate System address 2
1074 ** @see ajListSort
1075 **
1076 ** @return [int] The comparison function returns an integer less than,
1077 ** equal to, or greater than zero if the first argument is
1078 ** considered to be respectively less than, equal to, or
1079 ** greater than the second.
1080 **
1081 ** @release 6.4.0
1082 ** @@
1083 ******************************************************************************/
1084
listCoordsystemCompareIdentifierAscending(const void * item1,const void * item2)1085 static int listCoordsystemCompareIdentifierAscending(
1086 const void *item1,
1087 const void *item2)
1088 {
1089 EnsPCoordsystem cs1 = *(EnsOCoordsystem *const *) item1;
1090 EnsPCoordsystem cs2 = *(EnsOCoordsystem *const *) item2;
1091
1092 #if defined(AJ_DEBUG) && AJ_DEBUG >= 2
1093 if (ajDebugTest("listCoordsystemCompareIdentifierAscending"))
1094 ajDebug("listCoordsystemCompareIdentifierAscending\n"
1095 " cs1 %p\n"
1096 " cs2 %p\n",
1097 cs1,
1098 cs2);
1099 #endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 2 */
1100
1101 /* Sort empty values towards the end of the AJAX List. */
1102
1103 if (cs1 && (!cs2))
1104 return -1;
1105
1106 if ((!cs1) && (!cs2))
1107 return 0;
1108
1109 if ((!cs1) && cs2)
1110 return +1;
1111
1112 if (cs1->Identifier < cs2->Identifier)
1113 return -1;
1114
1115 if (cs1->Identifier > cs2->Identifier)
1116 return +1;
1117
1118 return 0;
1119 }
1120
1121
1122
1123
1124 /* @funcstatic listCoordsystemCompareIdentifierDescending *********************
1125 **
1126 ** AJAX List of Ensembl Coordinate System objects comparison function to
1127 ** sort by Ensembl Coordinate System identifier in descending order.
1128 **
1129 ** @param [r] item1 [const void*] Ensembl Coordinate System address 1
1130 ** @param [r] item2 [const void*] Ensembl Coordinate System address 2
1131 ** @see ajListSort
1132 **
1133 ** @return [int] The comparison function returns an integer less than,
1134 ** equal to, or greater than zero if the first argument is
1135 ** considered to be respectively less than, equal to, or
1136 ** greater than the second.
1137 **
1138 ** @release 6.4.0
1139 ** @@
1140 ******************************************************************************/
1141
listCoordsystemCompareIdentifierDescending(const void * item1,const void * item2)1142 static int listCoordsystemCompareIdentifierDescending(
1143 const void *item1,
1144 const void *item2)
1145 {
1146 EnsPCoordsystem cs1 = *(EnsOCoordsystem *const *) item1;
1147 EnsPCoordsystem cs2 = *(EnsOCoordsystem *const *) item2;
1148
1149 #if defined(AJ_DEBUG) && AJ_DEBUG >= 2
1150 if (ajDebugTest("listCoordsystemCompareIdentifierDescending"))
1151 ajDebug("listCoordsystemCompareIdentifierDescending\n"
1152 " cs1 %p\n"
1153 " cs2 %p\n",
1154 cs1,
1155 cs2);
1156 #endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 2 */
1157
1158 /* Sort empty values towards the end of the AJAX List. */
1159
1160 if (cs1 && (!cs2))
1161 return -1;
1162
1163 if ((!cs1) && (!cs2))
1164 return 0;
1165
1166 if ((!cs1) && cs2)
1167 return +1;
1168
1169 if (cs1->Identifier > cs2->Identifier)
1170 return -1;
1171
1172 if (cs1->Identifier < cs2->Identifier)
1173 return +1;
1174
1175 return 0;
1176 }
1177
1178
1179
1180
1181 /* @funcstatic listCoordsystemCompareRankAscending ****************************
1182 **
1183 ** AJAX List of Ensembl Coordinate System objects comparison function to
1184 ** sort by Ensembl Coordinate System rank in ascending order.
1185 **
1186 ** @param [r] item1 [const void*] Ensembl Coordinate System address 1
1187 ** @param [r] item2 [const void*] Ensembl Coordinate System address 2
1188 ** @see ajListSort
1189 **
1190 ** @return [int] The comparison function returns an integer less than,
1191 ** equal to, or greater than zero if the first argument is
1192 ** considered to be respectively less than, equal to, or
1193 ** greater than the second.
1194 **
1195 ** @release 6.4.0
1196 ** @@
1197 ******************************************************************************/
1198
listCoordsystemCompareRankAscending(const void * item1,const void * item2)1199 static int listCoordsystemCompareRankAscending(
1200 const void *item1,
1201 const void *item2)
1202 {
1203 EnsPCoordsystem cs1 = *(EnsOCoordsystem *const *) item1;
1204 EnsPCoordsystem cs2 = *(EnsOCoordsystem *const *) item2;
1205
1206 #if defined(AJ_DEBUG) && AJ_DEBUG >= 2
1207 if (ajDebugTest("listCoordsystemCompareRankAscending"))
1208 ajDebug("listCoordsystemCompareRankAscending\n"
1209 " cs1 %p\n"
1210 " cs2 %p\n",
1211 cs1,
1212 cs2);
1213 #endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 2 */
1214
1215 /* Sort empty values towards the end of the AJAX List. */
1216
1217 if (cs1 && (!cs2))
1218 return -1;
1219
1220 if ((!cs1) && (!cs2))
1221 return 0;
1222
1223 if ((!cs1) && cs2)
1224 return +1;
1225
1226 if (cs1->Rank < cs2->Rank)
1227 return -1;
1228
1229 if (cs1->Rank > cs2->Rank)
1230 return +1;
1231
1232 return 0;
1233 }
1234
1235
1236
1237
1238 /* @funcstatic listCoordsystemCompareRankDescending ***************************
1239 **
1240 ** AJAX List of Ensembl Coordinate System objects comparison function to
1241 ** sort by Ensembl Coordinate System rank in descending order.
1242 **
1243 ** @param [r] item1 [const void*] Ensembl Coordinate System address 1
1244 ** @param [r] item2 [const void*] Ensembl Coordinate System address 2
1245 ** @see ajListSort
1246 **
1247 ** @return [int] The comparison function returns an integer less than,
1248 ** equal to, or greater than zero if the first argument is
1249 ** considered to be respectively less than, equal to, or
1250 ** greater than the second.
1251 **
1252 ** @release 6.4.0
1253 ** @@
1254 ******************************************************************************/
1255
listCoordsystemCompareRankDescending(const void * item1,const void * item2)1256 static int listCoordsystemCompareRankDescending(
1257 const void *item1,
1258 const void *item2)
1259 {
1260 EnsPCoordsystem cs1 = *(EnsOCoordsystem *const *) item1;
1261 EnsPCoordsystem cs2 = *(EnsOCoordsystem *const *) item2;
1262
1263 #if defined(AJ_DEBUG) && AJ_DEBUG >= 2
1264 if (ajDebugTest("listCoordsystemCompareRankDescending"))
1265 ajDebug("listCoordsystemCompareRankDescending\n"
1266 " cs1 %p\n"
1267 " cs2 %p\n",
1268 cs1,
1269 cs2);
1270 #endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 2 */
1271
1272 /* Sort empty values towards the end of the AJAX List. */
1273
1274 if (cs1 && (!cs2))
1275 return -1;
1276
1277 if ((!cs1) && (!cs2))
1278 return 0;
1279
1280 if ((!cs1) && cs2)
1281 return +1;
1282
1283 if (cs1->Rank > cs2->Rank)
1284 return -1;
1285
1286 if (cs1->Rank < cs2->Rank)
1287 return +1;
1288
1289 return 0;
1290 }
1291
1292
1293
1294
1295 /* @section list **************************************************************
1296 **
1297 ** Functions for manipulating AJAX List objects.
1298 **
1299 ** @fdata [AjPList]
1300 **
1301 ** @nam3rule Coordsystem Functions for manipulating AJAX List objects of
1302 ** Ensembl Coordinate System objects
1303 ** @nam4rule Sort Sort functions
1304 ** @nam5rule Identifier Sort by Ensembl Coordinate System identifier member
1305 ** @nam5rule Name Sort by Ensembl Coordinate System name member
1306 ** @nam5rule Rank Sort by Ensembl Coordinate System rank member
1307 ** @nam6rule Ascending Sort in ascending order
1308 ** @nam6rule Descending Sort in descending order
1309 ** @nam4rule Trace Trace Ensembl Coordinate System objects
1310 **
1311 ** @argrule Sort css [AjPList]
1312 ** AJAX List of Ensembl Coordinate System objects
1313 ** @argrule Trace css [const AjPList]
1314 ** AJAX List of Ensembl Coordinate System objects
1315 ** @argrule Trace level [ajuint] Indentation level
1316 **
1317 ** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
1318 **
1319 ** @fcategory misc
1320 ******************************************************************************/
1321
1322
1323
1324
1325 /* @func ensListCoordsystemSortIdentifierAscending ****************************
1326 **
1327 ** Sort an AJAX List of Ensembl Coordinate System objects by their
1328 ** Ensembl Coordinate System identifier in ascending order.
1329 **
1330 ** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
1331 **
1332 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
1333 **
1334 ** @release 6.4.0
1335 ** @@
1336 ******************************************************************************/
1337
ensListCoordsystemSortIdentifierAscending(AjPList css)1338 AjBool ensListCoordsystemSortIdentifierAscending(AjPList css)
1339 {
1340 if (!css)
1341 return ajFalse;
1342
1343 ajListSort(css, &listCoordsystemCompareIdentifierAscending);
1344
1345 return ajTrue;
1346 }
1347
1348
1349
1350
1351 /* @func ensListCoordsystemSortIdentifierDescending ***************************
1352 **
1353 ** Sort an AJAX List of Ensembl Coordinate System objects by their
1354 ** Ensembl Coordinate System identifier in descending order.
1355 **
1356 ** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
1357 **
1358 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
1359 **
1360 ** @release 6.4.0
1361 ** @@
1362 ******************************************************************************/
1363
ensListCoordsystemSortIdentifierDescending(AjPList css)1364 AjBool ensListCoordsystemSortIdentifierDescending(AjPList css)
1365 {
1366 if (!css)
1367 return ajFalse;
1368
1369 ajListSort(css, &listCoordsystemCompareIdentifierDescending);
1370
1371 return ajTrue;
1372 }
1373
1374
1375
1376
1377 /* @func ensListCoordsystemSortRankAscending **********************************
1378 **
1379 ** Sort an AJAX List of Ensembl Coordinate System objects by their
1380 ** Ensembl Coordinate System rank in ascending order.
1381 **
1382 ** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
1383 **
1384 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
1385 **
1386 ** @release 6.4.0
1387 ** @@
1388 ******************************************************************************/
1389
ensListCoordsystemSortRankAscending(AjPList css)1390 AjBool ensListCoordsystemSortRankAscending(AjPList css)
1391 {
1392 if (!css)
1393 return ajFalse;
1394
1395 ajListSort(css, &listCoordsystemCompareRankAscending);
1396
1397 return ajTrue;
1398 }
1399
1400
1401
1402
1403 /* @func ensListCoordsystemSortRankDescending *********************************
1404 **
1405 ** Sort an AJAX List of Ensembl Coordinate System objects by their
1406 ** Ensembl Coordinate System rank in descending order.
1407 **
1408 ** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
1409 **
1410 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
1411 **
1412 ** @release 6.4.0
1413 ** @@
1414 ******************************************************************************/
1415
ensListCoordsystemSortRankDescending(AjPList css)1416 AjBool ensListCoordsystemSortRankDescending(AjPList css)
1417 {
1418 if (!css)
1419 return ajFalse;
1420
1421 ajListSort(css, &listCoordsystemCompareRankDescending);
1422
1423 return ajTrue;
1424 }
1425
1426
1427
1428
1429 /* @func ensListCoordsystemTrace **********************************************
1430 **
1431 ** Trace an Ensembl Coordinate System mapping path.
1432 **
1433 ** @param [r] css [const AjPList] AJAX List of
1434 ** Ensembl Coordinate System objects
1435 ** @param [r] level [ajuint] Indentation level
1436 **
1437 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
1438 **
1439 ** @release 6.4.0
1440 ** @@
1441 ******************************************************************************/
1442
ensListCoordsystemTrace(const AjPList css,ajuint level)1443 AjBool ensListCoordsystemTrace(const AjPList css, ajuint level)
1444 {
1445 AjIList iter = NULL;
1446
1447 AjPStr indent = NULL;
1448
1449 EnsPCoordsystem cs = NULL;
1450
1451 if (!css)
1452 return ajFalse;
1453
1454 indent = ajStrNew();
1455
1456 ajStrAppendCountK(&indent, ' ', level * 2);
1457
1458 ajDebug("%SensListCoordsystemTrace %p\n"
1459 "%S length %Lu\n",
1460 indent, css,
1461 indent, ajListGetLength(css));
1462
1463 iter = ajListIterNewread(css);
1464
1465 while (!ajListIterDone(iter))
1466 {
1467 cs = (EnsPCoordsystem) ajListIterGet(iter);
1468
1469 if (cs)
1470 ensCoordsystemTrace(cs, level + 1);
1471 else
1472 ajDebug("%S <nul>\n", indent);
1473 }
1474
1475 ajListIterDel(&iter);
1476
1477 ajStrDel(&indent);
1478
1479 return ajTrue;
1480 }
1481
1482
1483
1484
1485 /* @datasection [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor ****
1486 **
1487 ** @nam2rule Coordsystemadaptor Functions for manipulating
1488 ** Ensembl Coordinate System Adaptor objects
1489 **
1490 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor
1491 ** @cc CVS Revision: 1.30
1492 ** @cc CVS Tag: branch-ensembl-68
1493 **
1494 ******************************************************************************/
1495
1496
1497
1498
1499
1500 /* @funcstatic coordsystemadaptorFetchAllbyStatement **************************
1501 **
1502 ** Run a SQL statement against an Ensembl Coordinate System Adaptor and
1503 ** consolidate the results into an AJAX List of Ensembl Coordinate System
1504 ** objects.
1505 **
1506 ** The caller is responsible for deleting the
1507 ** Ensembl Coordinate System objects before deleting the AJAX List.
1508 **
1509 ** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
1510 ** @param [r] statement [const AjPStr] SQL statement
1511 ** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
1512 **
1513 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
1514 **
1515 ** @release 6.4.0
1516 ** @@
1517 ******************************************************************************/
1518
coordsystemadaptorFetchAllbyStatement(EnsPCoordsystemadaptor csa,const AjPStr statement,AjPList css)1519 static AjBool coordsystemadaptorFetchAllbyStatement(
1520 EnsPCoordsystemadaptor csa,
1521 const AjPStr statement,
1522 AjPList css)
1523 {
1524 ajuint identifier = 0U;
1525 ajuint rank = 0U;
1526
1527 AjBool dflt = AJFALSE;
1528 AjBool seqlevel = AJFALSE;
1529 AjBool toplevel = AJFALSE;
1530
1531 AjPSqlstatement sqls = NULL;
1532 AjISqlrow sqli = NULL;
1533 AjPSqlrow sqlr = NULL;
1534
1535 AjPStr name = NULL;
1536 AjPStr version = NULL;
1537 AjPStr attribute = NULL;
1538 AjPStr value = NULL;
1539
1540 AjPStrTok attrtoken = NULL;
1541
1542 EnsPCoordsystem cs = NULL;
1543
1544 EnsPDatabaseadaptor dba = NULL;
1545
1546 if (!csa)
1547 return ajFalse;
1548
1549 if (!statement)
1550 return ajFalse;
1551
1552 if (!css)
1553 return ajFalse;
1554
1555 dba = ensCoordsystemadaptorGetDatabaseadaptor(csa);
1556
1557 sqls = ensDatabaseadaptorSqlstatementNew(dba, statement);
1558
1559 sqli = ajSqlrowiterNew(sqls);
1560
1561 while (!ajSqlrowiterDone(sqli))
1562 {
1563 identifier = 0;
1564 name = ajStrNew();
1565 version = ajStrNew();
1566 rank = 0;
1567 attribute = ajStrNew();
1568
1569 value = ajStrNew();
1570
1571 dflt = ajFalse;
1572 toplevel = ajFalse;
1573 seqlevel = ajFalse;
1574
1575 sqlr = ajSqlrowiterGet(sqli);
1576
1577 ajSqlcolumnToUint(sqlr, &identifier);
1578 ajSqlcolumnToStr(sqlr, &name);
1579 ajSqlcolumnToStr(sqlr, &version);
1580 ajSqlcolumnToUint(sqlr, &rank);
1581 ajSqlcolumnToStr(sqlr, &attribute);
1582
1583 attrtoken = ajStrTokenNewC(attribute, ",");
1584
1585 while (ajStrTokenNextParse(attrtoken, &value))
1586 {
1587 if (ajStrMatchCaseC(value, "default_version"))
1588 dflt = ajTrue;
1589
1590 if (ajStrMatchCaseC(value, "sequence_level"))
1591 seqlevel = ajTrue;
1592 }
1593
1594 ajStrTokenDel(&attrtoken);
1595
1596 cs = ensCoordsystemNewIni(csa,
1597 identifier,
1598 name,
1599 version,
1600 rank,
1601 dflt,
1602 toplevel,
1603 seqlevel);
1604
1605 ajListPushAppend(css, (void *) cs);
1606
1607 ajStrDel(&name);
1608 ajStrDel(&version);
1609 ajStrDel(&attribute);
1610 ajStrDel(&value);
1611 }
1612
1613 ajSqlrowiterDel(&sqli);
1614
1615 ensDatabaseadaptorSqlstatementDel(dba, &sqls);
1616
1617 return ajTrue;
1618 }
1619
1620
1621
1622
1623 /* @funcstatic coordsystemadaptorCacheInit ************************************
1624 **
1625 ** Initialise the internal Coordinate System cache of an
1626 ** Ensembl Coordinate System Adaptor.
1627 **
1628 ** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
1629 **
1630 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
1631 **
1632 ** @release 6.3.0
1633 ** @@
1634 ******************************************************************************/
1635
coordsystemadaptorCacheInit(EnsPCoordsystemadaptor csa)1636 static AjBool coordsystemadaptorCacheInit(EnsPCoordsystemadaptor csa)
1637 {
1638 ajuint *Pidentifier = NULL;
1639 ajuint *Prank = NULL;
1640
1641 AjPList css = NULL;
1642
1643 AjPStr statement = NULL;
1644
1645 AjPTable versions = NULL;
1646
1647 EnsPCoordsystem cs = NULL;
1648 EnsPCoordsystem cstemp = NULL;
1649
1650 if (!csa)
1651 return ajFalse;
1652
1653 if (!csa->CacheByIdentifier)
1654 {
1655 csa->CacheByIdentifier = ajTableuintNew(0U);
1656
1657 ajTableSetDestroyvalue(csa->CacheByIdentifier,
1658 (void (*)(void **)) &ensCoordsystemDel);
1659 }
1660
1661 if (!csa->CacheByName)
1662 {
1663 csa->CacheByName = ajTablestrNew(0U);
1664
1665 ajTableSetDestroyvalue(csa->CacheByName,
1666 (void (*)(void **)) &ajTableDel);
1667 }
1668
1669 if (!csa->CacheByRank)
1670 {
1671 csa->CacheByRank = ajTableuintNew(0U);
1672
1673 ajTableSetDestroyvalue(csa->CacheByRank,
1674 (void (*)(void **)) &ensCoordsystemDel);
1675 }
1676
1677 if (!csa->CacheByDefault)
1678 {
1679 csa->CacheByDefault = ajTableuintNew(0U);
1680
1681 ajTableSetDestroyvalue(csa->CacheByDefault,
1682 (void (*)(void **)) &ensCoordsystemDel);
1683 }
1684
1685 statement = ajFmtStr(
1686 "SELECT "
1687 "coord_system.coord_system_id, "
1688 "coord_system.name, "
1689 "coord_system.version, "
1690 "coord_system.rank, "
1691 "coord_system.attrib "
1692 "FROM "
1693 "coord_system "
1694 "WHERE "
1695 "coord_system.species_id = %u",
1696 ensDatabaseadaptorGetIdentifier(
1697 ensCoordsystemadaptorGetDatabaseadaptor(csa)));
1698
1699 css = ajListNew();
1700
1701 coordsystemadaptorFetchAllbyStatement(csa, statement, css);
1702
1703 while (ajListPop(css, (void **) &cs))
1704 {
1705 /* Sequence-level cache */
1706
1707 if (cs->SequenceLevel)
1708 csa->Seqlevel = ensCoordsystemNewRef(cs);
1709
1710 /* Identifier cache */
1711
1712 AJNEW0(Pidentifier);
1713
1714 *Pidentifier = cs->Identifier;
1715
1716 cstemp = (EnsPCoordsystem)
1717 ajTablePut(csa->CacheByIdentifier,
1718 (void *) Pidentifier,
1719 (void *) ensCoordsystemNewRef(cs));
1720
1721 if (cstemp)
1722 {
1723 ajWarn("coordsystemadaptorCacheInit got more than one "
1724 "Ensembl Coordinate System with (PRIMARY KEY) identifier "
1725 "%u.\n",
1726 cstemp->Identifier);
1727
1728 ensCoordsystemDel(&cstemp);
1729 }
1730
1731 /* Name and Version cache */
1732
1733 /*
1734 ** For each Coordinate System of a particular name one or more
1735 ** versions are supported. Thus, Ensembl Coordinate System objects
1736 ** are cached in two levels of AJAX Table objects.
1737 **
1738 ** First-level (Name) AJAX Table: An AJAX Table storing
1739 ** (Ensembl Coordinate System name) AJAX String objects as key data
1740 ** and second-level AJAX Table objects as value data.
1741 **
1742 ** Second-level (Version) AJAX Table: An AJAX Table storing
1743 ** (Ensembl Coordinate System version) AJAX String objects as key data
1744 ** and Ensembl Coordinate System objects as value data.
1745 */
1746
1747 versions = (AjPTable) ajTableFetchmodS(csa->CacheByName, cs->Name);
1748
1749 if (!versions)
1750 {
1751 /*
1752 ** Create a new AJAX Table of AJAX String (version) key and
1753 ** Ensembl Coordinate System value data.
1754 */
1755
1756 versions = ajTablestrNew(0U);
1757
1758 ajTableSetDestroyvalue(versions,
1759 (void (*)(void **)) &ensCoordsystemDel);
1760
1761 ajTablePut(csa->CacheByName,
1762 (void *) ajStrNewS(cs->Name),
1763 (void *) versions);
1764 }
1765
1766 cstemp = (EnsPCoordsystem)
1767 ajTablePut(versions,
1768 (void *) ajStrNewS(cs->Version),
1769 (void *) ensCoordsystemNewRef(cs));
1770
1771 if (cstemp)
1772 {
1773 ajWarn("coordsystemadaptorCacheInit got more than one "
1774 "Ensembl Coordinate System with (UNIQUE) name '%S' and "
1775 "version '%S' with identifiers %u and %u.\n",
1776 cstemp->Name,
1777 cstemp->Version,
1778 cstemp->Identifier,
1779 cs->Identifier);
1780
1781 ensCoordsystemDel(&cstemp);
1782 }
1783
1784 /* Cache by Rank */
1785
1786 AJNEW0(Prank);
1787
1788 *Prank = cs->Rank;
1789
1790 cstemp = (EnsPCoordsystem)
1791 ajTablePut(csa->CacheByRank,
1792 (void *) Prank,
1793 (void *) ensCoordsystemNewRef(cs));
1794
1795 if (cstemp)
1796 {
1797 ajWarn("coordsystemadaptorCacheInit got more than one "
1798 "Ensembl Coordinate System with (UNIQUE) rank %u and "
1799 "identifiers %u and %u.\n",
1800 cstemp->Rank,
1801 cstemp->Identifier,
1802 cs->Identifier);
1803
1804 ensCoordsystemDel(&cstemp);
1805 }
1806
1807 /* Defaults cache */
1808
1809 /*
1810 ** Ensembl supports one or more default Coordinate System objects.
1811 ** Ensembl Coordinate System objects are stored in an AJAX Table
1812 ** with (Ensembl Coordinate System identifier) AJAX unsigned integer
1813 ** key data and Ensembl Coordinate System value data.
1814 */
1815
1816 if (cs->Default)
1817 {
1818 AJNEW0(Pidentifier);
1819
1820 *Pidentifier = cs->Identifier;
1821
1822 cstemp = (EnsPCoordsystem)
1823 ajTablePut(csa->CacheByDefault,
1824 (void *) Pidentifier,
1825 (void *) ensCoordsystemNewRef(cs));
1826
1827 if (cstemp)
1828 {
1829 ajWarn("coordsystemadaptorCacheInit got more than one "
1830 "Ensembl Coordinate System with (PRIMARY KEY) "
1831 "identifier %u.\n",
1832 cstemp->Identifier);
1833
1834 ensCoordsystemDel(&cstemp);
1835 }
1836 }
1837
1838 /*
1839 ** All caches keep internal references to the
1840 ** Ensembl Coordinate System objects.
1841 */
1842
1843 ensCoordsystemDel(&cs);
1844 }
1845
1846 ajListFree(&css);
1847
1848 ajStrDel(&statement);
1849
1850 return ajTrue;
1851 }
1852
1853
1854
1855
1856 /* @funcstatic coordsystemadaptorMappingpathInit ******************************
1857 **
1858 ** Initialise the internal Coordinate System mapping path cache of an
1859 ** Ensembl Coordinate System Adaptor.
1860 **
1861 ** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
1862 **
1863 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
1864 **
1865 ** @release 6.3.0
1866 ** @@
1867 ******************************************************************************/
1868
coordsystemadaptorMappingpathInit(EnsPCoordsystemadaptor csa)1869 static AjBool coordsystemadaptorMappingpathInit(EnsPCoordsystemadaptor csa)
1870 {
1871 AjBool debug = AJFALSE;
1872
1873 AjIList iter = NULL;
1874 AjPList cskeys = NULL;
1875 AjPList css = NULL;
1876 AjPList mis = NULL;
1877 AjPList mappath = NULL;
1878
1879 AjPStr csname = NULL;
1880 AjPStr csversion = NULL;
1881 AjPStr cskey = NULL;
1882 AjPStr cs1key = NULL;
1883 AjPStr cs2key = NULL;
1884 AjPStr mapkey = NULL;
1885 AjPStr metakey = NULL;
1886 AjPStr metaval = NULL;
1887
1888 AjPStrTok cstoken = NULL;
1889 AjPStrTok pathtoken = NULL;
1890
1891 EnsPCoordsystem cs = NULL;
1892 EnsPCoordsystem cs1 = NULL;
1893 EnsPCoordsystem cs2 = NULL;
1894
1895 EnsPMetainformation mi = NULL;
1896
1897 debug = ajDebugTest("coordsystemadaptorMappingpathInit");
1898
1899 if (debug)
1900 ajDebug("coordsystemadaptorMappingpathInit\n"
1901 " csa %p\n",
1902 csa);
1903
1904 if (!csa)
1905 return ajFalse;
1906
1907 if (!csa->MappingPaths)
1908 {
1909 csa->MappingPaths = ajTablestrNew(0U);
1910
1911 ajTableSetDestroyvalue(
1912 csa->MappingPaths,
1913 (void (*)(void **)) &coordsystemadaptorMappingpathValdel);
1914 }
1915
1916 cskeys = ajListNew();
1917
1918 /* Read 'assembly.mapping' keys from the Ensembl Core 'meta' table. */
1919
1920 metakey = ajStrNewC("assembly.mapping");
1921
1922 mis = ajListNew();
1923
1924 ensMetainformationadaptorFetchAllbyKey(
1925 ensRegistryGetMetainformationadaptor(
1926 ensCoordsystemadaptorGetDatabaseadaptor(csa)),
1927 metakey,
1928 mis);
1929
1930 while (ajListPop(mis, (void **) &mi))
1931 {
1932 metaval = ensMetainformationGetValue(mi);
1933
1934 if (debug)
1935 ajDebug("coordsystemadaptorMappingpathInit processing "
1936 "Ensembl Meta Information value '%S'.\n",
1937 metaval);
1938
1939 /*
1940 ** Split 'assembly.mapping' Ensembl Meta Information values on
1941 ** '#' or '|' characters into Ensembl Coordinate System name:version
1942 ** keys.
1943 */
1944
1945 pathtoken = ajStrTokenNewC(metaval, "#|");
1946
1947 cskey = ajStrNew();
1948
1949 while (ajStrTokenNextParse(pathtoken, &cskey))
1950 ajListPushAppend(cskeys, (void *) ajStrNewS(cskey));
1951
1952 ajStrDel(&cskey);
1953
1954 ajStrTokenDel(&pathtoken);
1955
1956 if (ajListGetLength(cskeys) < 2)
1957 ajWarn("coordsystemadaptorMappingpathInit got the incorrectly "
1958 "formatted 'assembly.mapping' value '%S' from the "
1959 "Ensembl Core 'meta' table.",
1960 metaval);
1961 else
1962 {
1963 /*
1964 ** Split Ensembl Coordinate System keys into names and versions and
1965 ** fetch the corresponding Ensembl Coordinate System objects from
1966 ** the database.
1967 */
1968
1969 css = ajListNew();
1970
1971 iter = ajListIterNew(cskeys);
1972
1973 while (!ajListIterDone(iter))
1974 {
1975 cskey = (AjPStr) ajListIterGet(iter);
1976
1977 cstoken = ajStrTokenNewC(cskey, ":");
1978
1979 csname = ajStrNew();
1980 csversion = ajStrNew();
1981
1982 ajStrTokenNextParse(cstoken, &csname);
1983 ajStrTokenNextParse(cstoken, &csversion);
1984
1985 ensCoordsystemadaptorFetchByName(csa,
1986 csname,
1987 csversion,
1988 &cs);
1989
1990 if (cs)
1991 ajListPushAppend(css, (void *) cs);
1992 else
1993 ajWarn("coordsystemadaptorMappingpathInit could not load "
1994 "an Ensembl Coordinate System for name '%S' and "
1995 "version '%S', as specified in the "
1996 "Ensembl Core 'meta' table by '%S'.",
1997 csname, csversion, metaval);
1998
1999 ajStrDel(&csname);
2000 ajStrDel(&csversion);
2001
2002 ajStrTokenDel(&cstoken);
2003 }
2004
2005 ajListIterDel(&iter);
2006
2007 /*
2008 ** Test the 'assembly.mapping' Meta-Information value for
2009 ** '#' characters. A '#' delimiter indicates a special case,
2010 ** where multiple parts of a 'component' region map to the
2011 ** same part of an 'assembled' region. As this looks like the
2012 ** 'long' mapping, we just make the mapping path a bit longer.
2013 */
2014
2015 if ((ajStrFindC(metaval, "#") >= 0) && (ajListGetLength(css) == 2))
2016 {
2017 /*
2018 ** Insert an empty middle node into the mapping path
2019 ** i.e. the AJAX List of Ensembl Coordinate System objects.
2020 */
2021
2022 iter = ajListIterNew(css);
2023
2024 (void) ajListIterGet(iter);
2025
2026 ajListIterInsert(iter, NULL);
2027
2028 ajListIterDel(&iter);
2029
2030 if (debug)
2031 ajDebug("coordsystemadaptorMappingpathInit "
2032 "elongated mapping path '%S'.\n",
2033 metaval);
2034 }
2035
2036 /*
2037 ** Take the first and last Ensembl Coordinate System objects from
2038 ** the AJAX List and generate name:version
2039 ** Ensembl Coordinate System keys, before
2040 ** a name1:version1|name2:version2 map key.
2041 */
2042
2043 ajListPeekFirst(css, (void **) &cs1);
2044 ajListPeekLast(css, (void **) &cs2);
2045
2046 if (debug)
2047 {
2048 ajDebug("coordsystemadaptorMappingpathInit cs1 %p\n", cs1);
2049
2050 ensCoordsystemTrace(cs1, 1);
2051
2052 ajDebug("coordsystemadaptorMappingpathInit cs2 %p\n", cs2);
2053
2054 ensCoordsystemTrace(cs2, 1);
2055 }
2056
2057 if (cs1 && cs2)
2058 {
2059 cs1key = ajFmtStr("%S:%S", cs1->Name, cs1->Version);
2060 cs2key = ajFmtStr("%S:%S", cs2->Name, cs2->Version);
2061 mapkey = ajFmtStr("%S|%S", cs1key, cs2key);
2062
2063 if (debug)
2064 ajDebug("coordsystemadaptorMappingpathInit mapkey '%S'\n",
2065 mapkey);
2066
2067 /* Does a mapping path already exist? */
2068
2069 mappath = (AjPList) ajTableFetchmodS(csa->MappingPaths,
2070 mapkey);
2071
2072 if (mappath)
2073 {
2074 /* A similar map path exists already. */
2075
2076 ajDebug("coordsystemadaptorMappingpathInit got multiple "
2077 "mapping paths between Ensembl Coordinate System "
2078 "objects '%S' and '%S' and chooses the shorter "
2079 "mapping path arbitrarily.\n",
2080 cs1key, cs2key);
2081
2082 if (ajListGetLength(css) < ajListGetLength(mappath))
2083 {
2084 /*
2085 ** The current map path is shorter than the stored map
2086 ** path. Replace the stored List with the current List
2087 ** and delete the (longer) stored List. The Table key
2088 ** String remains in place.
2089 */
2090
2091 mappath = (AjPList) ajTablePut(
2092 csa->MappingPaths,
2093 (void *) ajStrNewS(mapkey),
2094 (void *) css);
2095
2096 ajDebug("coordsystemadaptorMappingpathInit "
2097 "deleted the longer, stored mapping path!\n");
2098
2099 while (ajListPop(mappath, (void **) &cs))
2100 ensCoordsystemDel(&cs);
2101
2102 ajListFree(&mappath);
2103 }
2104 else
2105 {
2106 /*
2107 ** The current mapping path is longer than the stored
2108 ** mapping path. Delete this (longer) mapping path.
2109 */
2110
2111 ajDebug("coordsystemadaptorMappingpathInit "
2112 "deleted the longer, current mapping path!\n");
2113
2114 while (ajListPop(css, (void **) &cs))
2115 ensCoordsystemDel(&cs);
2116
2117 ajListFree(&css);
2118 }
2119 }
2120 else
2121 {
2122 /* No similar mappath exists so store the new mappath. */
2123
2124 ajTablePut(csa->MappingPaths,
2125 (void *) ajStrNewS(mapkey),
2126 (void *) css);
2127
2128 if (debug)
2129 ajDebug("coordsystemadaptorMappingpathInit "
2130 "added a new mapping path '%S'.\n",
2131 mapkey);
2132 }
2133
2134 ajStrDel(&cs1key);
2135 ajStrDel(&cs2key);
2136 ajStrDel(&mapkey);
2137 }
2138 else
2139 ajWarn("coordsystemadaptorMappingpathInit requires that both, "
2140 "first and last Ensembl Coordinate System objects of a "
2141 "mapping path are defined. "
2142 "See Ensembl Core 'meta.meta_value' '%S'.",
2143 metaval);
2144 }
2145
2146 /*
2147 ** Clear the AJAX List of Coordinate System keys, but keep the
2148 ** AJAX List between Ensembl Meta Information entries.
2149 */
2150
2151 while (ajListPop(cskeys, (void **) &cskey))
2152 ajStrDel(&cskey);
2153
2154 ensMetainformationDel(&mi);
2155 }
2156
2157 ajListFree(&mis);
2158 ajListFree(&cskeys);
2159
2160 ajStrDel(&metakey);
2161
2162 return ajTrue;
2163 }
2164
2165
2166
2167
2168 /* @funcstatic coordsystemadaptorSeqregionMapInit *****************************
2169 **
2170 ** Initialise the Ensembl Coordinate System Adaptor-internal
2171 ** Ensembl Sequence Region mapping cache. This allows mapping of internal to
2172 ** external Sequence Region identifiers and vice versa.
2173 **
2174 ** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
2175 **
2176 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
2177 **
2178 ** @release 6.3.0
2179 ** @@
2180 ******************************************************************************/
2181
coordsystemadaptorSeqregionMapInit(EnsPCoordsystemadaptor csa)2182 static AjBool coordsystemadaptorSeqregionMapInit(EnsPCoordsystemadaptor csa)
2183 {
2184 ajuint internal = 0U;
2185 ajuint external = 0U;
2186
2187 ajuint *Pinternal = NULL;
2188 ajuint *Pexternal = NULL;
2189 ajuint *Pprevious = NULL;
2190
2191 AjBool debug = AJFALSE;
2192
2193 AjPSqlstatement sqls = NULL;
2194 AjISqlrow sqli = NULL;
2195 AjPSqlrow sqlr = NULL;
2196
2197 AjPStr build = NULL;
2198 AjPStr statement = NULL;
2199
2200 EnsPDatabaseadaptor dba = NULL;
2201
2202 debug = ajDebugTest("coordsystemadaptorSeqregionMapInit");
2203
2204 if (debug)
2205 ajDebug("coordsystemadaptorSeqregionMapInit\n"
2206 " csa %p\n",
2207 csa);
2208
2209 if (!csa)
2210 return ajFalse;
2211
2212 if (csa->ExternalToInternal)
2213 return ajFalse;
2214 else
2215 {
2216 csa->ExternalToInternal = ajTableuintNew(0U);
2217
2218 ajTableSetDestroyvalue(csa->ExternalToInternal, &ajMemFree);
2219 }
2220
2221 if (csa->InternalToExternal)
2222 return ajFalse;
2223 else
2224 {
2225 csa->InternalToExternal = ajTableuintNew(0U);
2226
2227 ajTableSetDestroyvalue(csa->InternalToExternal, &ajMemFree);
2228 }
2229
2230 dba = ensCoordsystemadaptorGetDatabaseadaptor(csa);
2231
2232 build = ajStrNew();
2233
2234 ensDatabaseadaptorFetchSchemabuild(dba, &build);
2235
2236 if (debug)
2237 ajDebug("coordsystemadaptorSeqregionMapInit got build '%S'.\n", build);
2238
2239 /* Get the relations for the current database. */
2240
2241 statement = ajFmtStr(
2242 "SELECT "
2243 "seq_region_mapping.internal_seq_region_id, "
2244 "seq_region_mapping.external_seq_region_id "
2245 "FROM "
2246 "mapping_set, "
2247 "seq_region_mapping, "
2248 "seq_region, "
2249 "coord_system "
2250 "WHERE "
2251 "mapping_set.schema_build = '%S' "
2252 "AND "
2253 "mapping_set.mapping_set_id = seq_region_mapping.mapping_set_id "
2254 "AND "
2255 "seq_region_mapping.internal_seq_region_id = seq_region.seq_region_id "
2256 "AND "
2257 "seq_region.coord_system_id = coord_system.coord_system_id "
2258 "AND "
2259 "coord_system.species_id = %u",
2260 build,
2261 ensDatabaseadaptorGetIdentifier(dba));
2262
2263 ajStrDel(&build);
2264
2265 sqls = ensDatabaseadaptorSqlstatementNew(dba, statement);
2266
2267 sqli = ajSqlrowiterNew(sqls);
2268
2269 while (!ajSqlrowiterDone(sqli))
2270 {
2271 internal = 0U;
2272 external = 0U;
2273
2274 sqlr = ajSqlrowiterGet(sqli);
2275
2276 ajSqlcolumnToUint(sqlr, &internal);
2277 ajSqlcolumnToUint(sqlr, &external);
2278
2279 /* Internal to external identifier mapping. */
2280
2281 Pprevious = (ajuint *) ajTableFetchmodV(csa->InternalToExternal,
2282 (const void *) &internal);
2283
2284 if (Pprevious)
2285 ajDebug("coordsystemadaptorSeqregionMapInit got duplicate "
2286 "internal Sequence Region identifier:\n"
2287 "%u -> %u\n"
2288 "%u -> %u\n",
2289 internal, *Pprevious,
2290 internal, external);
2291 else
2292 {
2293 AJNEW0(Pinternal);
2294 AJNEW0(Pexternal);
2295
2296 *Pinternal = internal;
2297 *Pexternal = external;
2298
2299 ajTablePut(csa->InternalToExternal,
2300 (void *) Pinternal,
2301 (void *) Pexternal);
2302 }
2303
2304 /* External to internal identifier mapping. */
2305
2306 Pprevious = (ajuint *) ajTableFetchmodV(csa->ExternalToInternal,
2307 (const void *) &external);
2308
2309 if (Pprevious)
2310 ajDebug("coordsystemadaptorSeqregionMapInit got duplicate "
2311 "external Sequence Region identifier:\n"
2312 "%u -> %u\n"
2313 "%u -> %u\n",
2314 external, *Pprevious,
2315 external, internal);
2316 else
2317 {
2318 AJNEW0(Pinternal);
2319 AJNEW0(Pexternal);
2320
2321 *Pinternal = internal;
2322 *Pexternal = external;
2323
2324 ajTablePut(csa->ExternalToInternal,
2325 (void *) Pexternal,
2326 (void *) Pinternal);
2327 }
2328 }
2329
2330 ajSqlrowiterDel(&sqli);
2331
2332 ensDatabaseadaptorSqlstatementDel(dba, &sqls);
2333
2334 ajStrDel(&statement);
2335
2336 return ajTrue;
2337 }
2338
2339
2340
2341
2342 /* @section constructors ******************************************************
2343 **
2344 ** All constructors return a new Ensembl Coordinate System Adaptor by pointer.
2345 ** It is the responsibility of the user to first destroy any previous
2346 ** Coordinate System Adaptor. The target pointer does not need to be
2347 ** initialised to NULL, but it is good programming practice to do so anyway.
2348 **
2349 ** @fdata [EnsPCoordsystemadaptor]
2350 **
2351 ** @nam3rule New Constructor
2352 **
2353 ** @argrule New dba [EnsPDatabaseadaptor] Ensembl Database Adaptor
2354 **
2355 ** @valrule * [EnsPCoordsystemadaptor]
2356 ** Ensembl Coordinate System Adaptor or NULL
2357 **
2358 ** @fcategory new
2359 ******************************************************************************/
2360
2361
2362
2363
2364 /* @func ensCoordsystemadaptorNew *********************************************
2365 **
2366 ** Default constructor for an Ensembl Coordinate System Adaptor.
2367 **
2368 ** Ensembl Object Adaptors are singleton objects in the sense that a single
2369 ** instance of an Ensembl Object Adaptor connected to a particular database is
2370 ** sufficient to instantiate any number of Ensembl Objects from the database.
2371 ** Each Ensembl Object will have a weak reference to the Object Adaptor that
2372 ** instantiated it. Therefore, Ensembl Object Adaptors should not be
2373 ** instantiated directly, but rather obtained from the Ensembl Registry,
2374 ** which will in turn call this function if neccessary.
2375 **
2376 ** @see ensRegistryGetDatabaseadaptor
2377 ** @see ensRegistryGetCoordsystemadaptor
2378 **
2379 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::new
2380 ** @param [u] dba [EnsPDatabaseadaptor] Ensembl Database Adaptor
2381 **
2382 ** @return [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor or NULL
2383 **
2384 ** @release 6.2.0
2385 ** @@
2386 ******************************************************************************/
2387
ensCoordsystemadaptorNew(EnsPDatabaseadaptor dba)2388 EnsPCoordsystemadaptor ensCoordsystemadaptorNew(
2389 EnsPDatabaseadaptor dba)
2390 {
2391 EnsPCoordsystemadaptor csa = NULL;
2392
2393 if (!dba)
2394 return NULL;
2395
2396 AJNEW0(csa);
2397
2398 csa->Adaptor = dba;
2399
2400 coordsystemadaptorCacheInit(csa);
2401
2402 coordsystemadaptorMappingpathInit(csa);
2403
2404 coordsystemadaptorSeqregionMapInit(csa);
2405
2406 /*
2407 ** Create a Pseudo-Coordinate System 'toplevel' and cache it so that only
2408 ** one of these is created for each database.
2409 */
2410
2411 csa->Toplevel = ensCoordsystemNewIni(
2412 csa,
2413 0,
2414 (AjPStr) NULL,
2415 (AjPStr) NULL,
2416 0,
2417 ajFalse,
2418 ajTrue,
2419 ajFalse);
2420
2421 return csa;
2422 }
2423
2424
2425
2426
2427 /* @funcstatic coordsystemadaptorMappingpathValdel ****************************
2428 **
2429 ** An ajTableSetDestroyvalue "valdel" function to clear AJAX Table value data.
2430 ** This function removes and deletes Ensembl Coordinate System objects
2431 ** from an AJAX List object, before deleting the AJAX List object.
2432 **
2433 ** @param [d] Pvalue [void**] AJAX List address
2434 ** @see ajTableSetDestroyvalue
2435 **
2436 ** @return [void]
2437 **
2438 ** @release 6.3.0
2439 ** @@
2440 ******************************************************************************/
2441
coordsystemadaptorMappingpathValdel(void ** Pvalue)2442 static void coordsystemadaptorMappingpathValdel(void **Pvalue)
2443 {
2444 EnsPCoordsystem cs = NULL;
2445
2446 if (!Pvalue)
2447 return;
2448
2449 if (!*Pvalue)
2450 return;
2451
2452 while (ajListPop(*((AjPList *) Pvalue), (void **) &cs))
2453 ensCoordsystemDel(&cs);
2454
2455 ajListFree((AjPList *) Pvalue);
2456
2457 return;
2458 }
2459
2460
2461
2462
2463 /* @section destructors *******************************************************
2464 **
2465 ** Destruction destroys all internal data structures and frees the memory
2466 ** allocated for an Ensembl Coordinate System Adaptor object.
2467 **
2468 ** @fdata [EnsPCoordsystemadaptor]
2469 **
2470 ** @nam3rule Del Destroy (free) an Ensembl Coordinate System Adaptor
2471 **
2472 ** @argrule * Pcsa [EnsPCoordsystemadaptor*]
2473 ** Ensembl Coordinate System Adaptor address
2474 **
2475 ** @valrule * [void]
2476 **
2477 ** @fcategory delete
2478 ******************************************************************************/
2479
2480
2481
2482
2483 /* @func ensCoordsystemadaptorDel *********************************************
2484 **
2485 ** Default destructor for an Ensembl Coordinate System Adaptor.
2486 **
2487 ** This function also clears the internal Ensembl Coordinate System and
2488 ** mapping path caches.
2489 **
2490 ** Ensembl Object Adaptors are singleton objects that are registered in the
2491 ** Ensembl Registry and weakly referenced by Ensembl Objects that have been
2492 ** instantiated by it. Therefore, Ensembl Object Adaptors should never be
2493 ** destroyed directly. Upon exit, the Ensembl Registry will call this function
2494 ** if required.
2495 **
2496 ** @param [d] Pcsa [EnsPCoordsystemadaptor*]
2497 ** Ensembl Coordinate System Adaptor address
2498 **
2499 ** @return [void]
2500 **
2501 ** @release 6.2.0
2502 ** @@
2503 ******************************************************************************/
2504
ensCoordsystemadaptorDel(EnsPCoordsystemadaptor * Pcsa)2505 void ensCoordsystemadaptorDel(EnsPCoordsystemadaptor *Pcsa)
2506 {
2507 EnsPCoordsystemadaptor pthis = NULL;
2508
2509 if (!Pcsa)
2510 return;
2511
2512 #if defined(AJ_DEBUG) && AJ_DEBUG >= 1
2513 if (ajDebugTest("ensCoordsystemadaptorDel"))
2514 ajDebug("ensCoordsystemadaptorDel\n"
2515 " *Pcsa %p\n",
2516 *Pcsa);
2517 #endif /* defined(AJ_DEBUG) && AJ_DEBUG >= 1 */
2518
2519 if (!(pthis = *Pcsa))
2520 return;
2521
2522 ajTableDel(&pthis->CacheByIdentifier);
2523 ajTableDel(&pthis->CacheByName);
2524 ajTableDel(&pthis->CacheByRank);
2525 ajTableDel(&pthis->CacheByDefault);
2526 ajTableDel(&pthis->MappingPaths);
2527 ajTableDel(&pthis->ExternalToInternal);
2528 ajTableDel(&pthis->InternalToExternal);
2529
2530 ensCoordsystemDel(&pthis->Seqlevel);
2531 ensCoordsystemDel(&pthis->Toplevel);
2532
2533 ajMemFree((void **) Pcsa);
2534
2535 return;
2536 }
2537
2538
2539
2540
2541 /* @section member retrieval **************************************************
2542 **
2543 ** Functions for returning members of an Ensembl Coordinate System Adaptor
2544 ** object.
2545 **
2546 ** @fdata [EnsPCoordsystemadaptor]
2547 **
2548 ** @nam3rule Get Return Coordinate System Adaptor attribute(s)
2549 ** @nam4rule Databaseadaptor Return the Ensembl Database Adaptor
2550 **
2551 ** @argrule * csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
2552 **
2553 ** @valrule Databaseadaptor [EnsPDatabaseadaptor]
2554 ** Ensembl Database Adaptor or NULL
2555 **
2556 ** @fcategory use
2557 ******************************************************************************/
2558
2559
2560
2561
2562 /* @func ensCoordsystemadaptorGetDatabaseadaptor ******************************
2563 **
2564 ** Get the Ensembl Database Adaptor member of an
2565 ** Ensembl Coordinate System Adaptor.
2566 **
2567 ** @param [u] csa [EnsPCoordsystemadaptor] Ensembl Coordinate System Adaptor
2568 **
2569 ** @return [EnsPDatabaseadaptor] Ensembl Database Adaptor or NULL
2570 **
2571 ** @release 6.2.0
2572 ** @@
2573 ******************************************************************************/
2574
ensCoordsystemadaptorGetDatabaseadaptor(EnsPCoordsystemadaptor csa)2575 EnsPDatabaseadaptor ensCoordsystemadaptorGetDatabaseadaptor(
2576 EnsPCoordsystemadaptor csa)
2577 {
2578 return (csa) ? csa->Adaptor : NULL;
2579 }
2580
2581
2582
2583
2584 /* @section object fetching ***************************************************
2585 **
2586 ** Functions for fetching Ensembl Coordinate System objects from an
2587 ** Ensembl SQL database.
2588 **
2589 ** @fdata [EnsPCoordsystemadaptor]
2590 **
2591 ** @nam3rule Fetch Fetch Ensembl Coordinate System object(s)
2592 ** @nam4rule All Fetch all Ensembl Coordinate System objects
2593 ** @nam4rule Allby Fetch all Ensembl Coordinate System objects
2594 ** matching a criterion
2595 ** @nam5rule Name Fetch all by a name
2596 ** @nam4rule By Fetch one Ensembl Coordinate System object
2597 ** matching a criterion
2598 ** @nam5rule Identifier Fetch by an SQL database-internal identifier
2599 ** @nam5rule Name Fetch by a name
2600 ** @nam5rule Rank Fetch by a rank
2601 ** @nam4rule Seqlevel Fetch the sequence-level Ensembl Coordinate System
2602 ** @nam4rule Toplevel Fetch the top-level Ensembl Coordinate System
2603 **
2604 ** @argrule * csa [const EnsPCoordsystemadaptor]
2605 ** Ensembl Coordinate System Adaptor
2606 ** @argrule All css [AjPList] AJAX List of Ensembl Coordinate System objects
2607 ** @argrule AllbyName name [const AjPStr] Name
2608 ** @argrule Allby css [AjPList] AJAX List of Ensembl Coordinate System objects
2609 ** @argrule ByIdentifier identifier [ajuint] SQL database-internal identifier
2610 ** @argrule ByIdentifier Pcs [EnsPCoordsystem*] Ensembl Coordinate System
2611 ** address
2612 ** @argrule ByName name [const AjPStr] Name
2613 ** @argrule ByName version [const AjPStr] Version
2614 ** @argrule ByName Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
2615 ** @argrule ByRank rank [ajuint] Rank
2616 ** @argrule ByRank Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
2617 ** @argrule Seqlevel Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
2618 ** @argrule Toplevel Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
2619 **
2620 ** @valrule * [AjBool] ajTrue upon success, ajFalse otherwise
2621 **
2622 ** @fcategory use
2623 ******************************************************************************/
2624
2625
2626
2627
2628 /* @funcstatic coordsystemadaptorFetchAll *************************************
2629 **
2630 ** An ajTableMap "apply" function to return all Ensembl Coordinate System
2631 ** objects from an Ensembl Coordinate System Adaptor-internal cache.
2632 **
2633 ** The caller is responsible for deleting the Ensembl Coordinate System
2634 ** objects before deleting the AJAX List.
2635 **
2636 ** @param [u] key [const void*] AJAX unsigned integer key data address
2637 ** @param [u] Pvalue [void**] Ensembl Coordinate System value data address
2638 ** @param [u] cl [void*] AJAX List of Ensembl Coordinate System objects,
2639 ** passed in via ajTableMap
2640 ** @see ajTableMap
2641 **
2642 ** @return [void]
2643 **
2644 ** @release 6.3.0
2645 ** @@
2646 ******************************************************************************/
2647
coordsystemadaptorFetchAll(const void * key,void ** Pvalue,void * cl)2648 static void coordsystemadaptorFetchAll(const void *key,
2649 void **Pvalue,
2650 void *cl)
2651 {
2652 if (!key)
2653 return;
2654
2655 if (!Pvalue)
2656 return;
2657
2658 if (!*Pvalue)
2659 return;
2660
2661 if (!cl)
2662 return;
2663
2664 ajListPushAppend((AjPList) cl, (void *)
2665 ensCoordsystemNewRef(*((EnsPCoordsystem *) Pvalue)));
2666
2667 return;
2668 }
2669
2670
2671
2672
2673 /* @func ensCoordsystemadaptorFetchAll ****************************************
2674 **
2675 ** Fetch all Ensembl Coordinate System objects in the order of ascending rank.
2676 **
2677 ** The caller is responsible for deleting the Ensembl Coordinate System
2678 ** objects before deleting the AJAX List.
2679 **
2680 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_all
2681 ** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
2682 ** System Adaptor
2683 ** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
2684 **
2685 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
2686 **
2687 ** @release 6.2.0
2688 ** @@
2689 ******************************************************************************/
2690
ensCoordsystemadaptorFetchAll(const EnsPCoordsystemadaptor csa,AjPList css)2691 AjBool ensCoordsystemadaptorFetchAll(const EnsPCoordsystemadaptor csa,
2692 AjPList css)
2693 {
2694 if (!csa)
2695 return ajFalse;
2696
2697 if (!css)
2698 return ajFalse;
2699
2700 ajTableMap(csa->CacheByIdentifier,
2701 &coordsystemadaptorFetchAll,
2702 (void *) css);
2703
2704 ensListCoordsystemSortRankAscending(css);
2705
2706 return ajTrue;
2707 }
2708
2709
2710
2711
2712 /* @func ensCoordsystemadaptorFetchAllbyName **********************************
2713 **
2714 ** Fetch Ensembl Coordinate System objects of all versions for a name.
2715 **
2716 ** The caller is responsible for deleting the Ensembl Coordinate System
2717 ** objects before deleting the AJAX List.
2718 **
2719 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_all_by_name
2720 ** @param [r] csa [const EnsPCoordsystemadaptor]
2721 ** Ensembl Coordinate System Adaptor
2722 ** @param [r] name [const AjPStr] Name
2723 ** @param [u] css [AjPList] AJAX List of Ensembl Coordinate System objects
2724 **
2725 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
2726 **
2727 ** @release 6.4.0
2728 ** @@
2729 ******************************************************************************/
2730
ensCoordsystemadaptorFetchAllbyName(const EnsPCoordsystemadaptor csa,const AjPStr name,AjPList css)2731 AjBool ensCoordsystemadaptorFetchAllbyName(
2732 const EnsPCoordsystemadaptor csa,
2733 const AjPStr name,
2734 AjPList css)
2735 {
2736 AjPTable versions = NULL;
2737
2738 EnsPCoordsystem cs = NULL;
2739
2740 if (!csa)
2741 return ajFalse;
2742
2743 if (!name)
2744 return ajFalse;
2745
2746 if (!css)
2747 return ajFalse;
2748
2749 if (ajStrMatchCaseC(name, "seqlevel"))
2750 {
2751 ensCoordsystemadaptorFetchSeqlevel(csa, &cs);
2752
2753 ajListPushAppend(css, (void *) cs);
2754
2755 return ajTrue;
2756 }
2757
2758 if (ajStrMatchCaseC(name, "toplevel"))
2759 {
2760 ensCoordsystemadaptorFetchToplevel(csa, &cs);
2761
2762 ajListPushAppend(css, (void *) cs);
2763
2764 return ajTrue;
2765 }
2766
2767 versions = (AjPTable) ajTableFetchmodS(csa->CacheByName, name);
2768
2769 if (versions)
2770 ajTableMap(versions, &coordsystemadaptorFetchAll, (void *) css);
2771
2772 ensListCoordsystemSortRankAscending(css);
2773
2774 return ajTrue;
2775 }
2776
2777
2778
2779
2780 /* @func ensCoordsystemadaptorFetchByIdentifier *******************************
2781 **
2782 ** Fetch an Ensembl Coordinate System by its SQL database-internal identifier.
2783 ** The caller is responsible for deleting the Ensembl Coordinate System.
2784 **
2785 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_by_dbID
2786 ** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
2787 ** System Adaptor
2788 ** @param [r] identifier [ajuint] SQL database-internal identifier
2789 ** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
2790 **
2791 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
2792 **
2793 ** @release 6.2.0
2794 ** @@
2795 ******************************************************************************/
2796
ensCoordsystemadaptorFetchByIdentifier(const EnsPCoordsystemadaptor csa,ajuint identifier,EnsPCoordsystem * Pcs)2797 AjBool ensCoordsystemadaptorFetchByIdentifier(
2798 const EnsPCoordsystemadaptor csa,
2799 ajuint identifier,
2800 EnsPCoordsystem *Pcs)
2801 {
2802 if (!csa)
2803 return ajFalse;
2804
2805 if (!identifier)
2806 return ajFalse;
2807
2808 if (!Pcs)
2809 return ajFalse;
2810
2811 *Pcs = (EnsPCoordsystem) ajTableFetchmodV(csa->CacheByIdentifier,
2812 (const void *) &identifier);
2813
2814 ensCoordsystemNewRef(*Pcs);
2815
2816 return ajTrue;
2817 }
2818
2819
2820
2821
2822 /* @func ensCoordsystemadaptorFetchByName *************************************
2823 **
2824 ** Fetch an Ensembl Coordinate System by name and version.
2825 ** The caller is responsible for deleting the Ensembl Coordinate System.
2826 **
2827 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_by_name
2828 ** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
2829 ** System Adaptor
2830 ** @param [r] name [const AjPStr] Name
2831 ** @param [rN] version [const AjPStr] Version
2832 ** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
2833 **
2834 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
2835 **
2836 ** @release 6.2.0
2837 ** @@
2838 ******************************************************************************/
2839
ensCoordsystemadaptorFetchByName(const EnsPCoordsystemadaptor csa,const AjPStr name,const AjPStr version,EnsPCoordsystem * Pcs)2840 AjBool ensCoordsystemadaptorFetchByName(
2841 const EnsPCoordsystemadaptor csa,
2842 const AjPStr name,
2843 const AjPStr version,
2844 EnsPCoordsystem *Pcs)
2845 {
2846 void **valarray = NULL;
2847
2848 register ajuint i = 0U;
2849
2850 AjPTable versions = NULL;
2851
2852 EnsPCoordsystem cs = NULL;
2853
2854 if (ajDebugTest("ensCoordsystemadaptorFetchByName"))
2855 ajDebug("ensCoordsystemadaptorFetchByName\n"
2856 " csa %p\n"
2857 " name '%S'\n"
2858 " version '%S'\n"
2859 " Pcs %p\n",
2860 csa,
2861 name,
2862 version,
2863 Pcs);
2864
2865 if (!csa)
2866 return ajFalse;
2867
2868 if (!name)
2869 return ajFalse;
2870
2871 if (!Pcs)
2872 return ajFalse;
2873
2874 *Pcs = NULL;
2875
2876 if (ajStrMatchCaseC(name, "seqlevel"))
2877 return ensCoordsystemadaptorFetchSeqlevel(csa, Pcs);
2878
2879 if (ajStrMatchCaseC(name, "toplevel"))
2880 return ensCoordsystemadaptorFetchToplevel(csa, Pcs);
2881
2882 versions = (AjPTable) ajTableFetchmodS(csa->CacheByName, name);
2883
2884 if (versions)
2885 {
2886 if (version && ajStrGetLen(version))
2887 *Pcs = (EnsPCoordsystem) ajTableFetchmodS(versions, version);
2888 else
2889 {
2890 /*
2891 ** If no version has been specified search for the default
2892 ** Ensembl Coordinate System of this name.
2893 */
2894
2895 ajTableToarrayValues(versions, &valarray);
2896
2897 for (i = 0U; valarray[i]; i++)
2898 {
2899 cs = (EnsPCoordsystem) valarray[i];
2900
2901 if (cs->Default)
2902 {
2903 *Pcs = cs;
2904
2905 break;
2906 }
2907 }
2908
2909 AJFREE(valarray);
2910 }
2911
2912 ensCoordsystemNewRef(*Pcs);
2913
2914 return ajTrue;
2915 }
2916
2917 return ajTrue;
2918 }
2919
2920
2921
2922
2923 /* @func ensCoordsystemadaptorFetchByRank *************************************
2924 **
2925 ** Fetch an Ensembl Coordinate System by its rank.
2926 ** The caller is responsible for deleting the Ensembl Coordinate System.
2927 **
2928 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_by_rank
2929 ** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
2930 ** System Adaptor
2931 ** @param [r] rank [ajuint] Rank
2932 ** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
2933 **
2934 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
2935 **
2936 ** @release 6.2.0
2937 ** @@
2938 ******************************************************************************/
2939
ensCoordsystemadaptorFetchByRank(const EnsPCoordsystemadaptor csa,ajuint rank,EnsPCoordsystem * Pcs)2940 AjBool ensCoordsystemadaptorFetchByRank(
2941 const EnsPCoordsystemadaptor csa,
2942 ajuint rank,
2943 EnsPCoordsystem *Pcs)
2944 {
2945 if (!csa)
2946 return ajFalse;
2947
2948 if (!rank)
2949 return ensCoordsystemadaptorFetchToplevel(csa, Pcs);
2950
2951 if (!Pcs)
2952 return ajFalse;
2953
2954 *Pcs = (EnsPCoordsystem) ajTableFetchmodV(csa->CacheByRank,
2955 (const void *) &rank);
2956
2957 ensCoordsystemNewRef(*Pcs);
2958
2959 return ajTrue;
2960 }
2961
2962
2963
2964
2965 /* @func ensCoordsystemadaptorFetchSeqlevel ***********************************
2966 **
2967 ** Fetch the sequence-level Ensembl Coordinate System.
2968 ** The caller is responsible for deleting the Ensembl Coordinate System.
2969 **
2970 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_sequence_level
2971 ** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
2972 ** System Adaptor
2973 ** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
2974 **
2975 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
2976 **
2977 ** @release 6.4.0
2978 ** @@
2979 ******************************************************************************/
2980
ensCoordsystemadaptorFetchSeqlevel(const EnsPCoordsystemadaptor csa,EnsPCoordsystem * Pcs)2981 AjBool ensCoordsystemadaptorFetchSeqlevel(
2982 const EnsPCoordsystemadaptor csa,
2983 EnsPCoordsystem *Pcs)
2984 {
2985 if (!csa)
2986 return ajFalse;
2987
2988 if (!Pcs)
2989 return ajFalse;
2990
2991 *Pcs = ensCoordsystemNewRef(csa->Seqlevel);
2992
2993 return ajTrue;
2994 }
2995
2996
2997
2998
2999 /* @func ensCoordsystemadaptorFetchToplevel ***********************************
3000 **
3001 ** Fetch the top-level Ensembl Coordinate System.
3002 ** The caller is responsible for deleting the Ensembl Coordinate System.
3003 **
3004 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::fetch_top_level
3005 ** @param [r] csa [const EnsPCoordsystemadaptor] Ensembl Coordinate
3006 ** System Adaptor
3007 ** @param [wP] Pcs [EnsPCoordsystem*] Ensembl Coordinate System address
3008 **
3009 ** @return [AjBool] ajTrue upon success, ajFalse otherwise
3010 **
3011 ** @release 6.4.0
3012 ** @@
3013 ******************************************************************************/
3014
ensCoordsystemadaptorFetchToplevel(const EnsPCoordsystemadaptor csa,EnsPCoordsystem * Pcs)3015 AjBool ensCoordsystemadaptorFetchToplevel(
3016 const EnsPCoordsystemadaptor csa,
3017 EnsPCoordsystem *Pcs)
3018 {
3019 if (!csa)
3020 return ajFalse;
3021
3022 if (!Pcs)
3023 return ajFalse;
3024
3025 *Pcs = ensCoordsystemNewRef(csa->Toplevel);
3026
3027 return ajTrue;
3028 }
3029
3030
3031
3032
3033 /* @section member retrieval **************************************************
3034 **
3035 ** Functions for returning members of an Ensembl Coordinate System Adaptor
3036 ** object.
3037 **
3038 ** @fdata [EnsPCoordsystemadaptor]
3039 **
3040 ** @nam3rule Get Return Coordinate System Adaptor attribute(s)
3041 ** @nam4rule Mappingpath Return the Ensembl Coordinate System mapping path
3042 ** @nam4rule Seqregionidentifier Return an Ensembl Sequence Region identifier
3043 ** @nam5rule External Return the external identifier
3044 ** @nam5rule Internal Return the internal identifier
3045 **
3046 ** @argrule * csa [const EnsPCoordsystemadaptor] Coordinate System Adaptor
3047 ** @argrule Mappingpath cs1 [EnsPCoordsystem] First Ensembl Coordinate System
3048 ** @argrule Mappingpath cs2 [EnsPCoordsystem] Second Ensembl Coordinate System
3049 ** @argrule External srid [ajuint] Internal Ensembl Sequence Region identifier
3050 ** @argrule Internal srid [ajuint] External Ensembl Sequence Region identifier
3051 **
3052 ** @valrule Mappingpath [const AjPList] AJAX List of
3053 ** Ensembl Coordinate System objects or NULL
3054 ** @valrule External [ajuint] External Ensembl Sequence Region identifier or 0U
3055 ** @valrule Internal [ajuint] Internal Ensembl Sequence Region identifier or 0U
3056 **
3057 ** @fcategory use
3058 ******************************************************************************/
3059
3060
3061
3062
3063 /* @func ensCoordsystemadaptorGetMappingpath **********************************
3064 **
3065 ** Fetch a mapping path between two Ensembl Coordinate System objects.
3066 **
3067 ** @cc Bio::EnsEMBL::DBSQL::CoordSystemAdaptor::get_mapping_path
3068 ** @param [r] csa [const EnsPCoordsystemadaptor]
3069 ** Ensembl Coordinate System Adaptor
3070 ** @param [u] cs1 [EnsPCoordsystem] First Ensembl Coordinate System
3071 ** @param [u] cs2 [EnsPCoordsystem] Second Ensembl Coordinate System
3072 **
3073 ** @return [const AjPList]
3074 ** AJAX List of Ensembl Coordinate System objects or NULL
3075 **
3076 ** @release 6.4.0
3077 ** @@
3078 ** Given two Ensembl Coordinate System objects this will return a mapping path
3079 ** between them if one has been defined. Allowed mapping paths are explicitly
3080 ** defined in the 'meta' table. The following is an example:
3081 **
3082 ** mysql> select * from meta where meta_key = 'assembly.mapping';
3083 **
3084 ** +---------+------------------+--------------------------------------+
3085 ** | meta_id | meta_key | meta_value |
3086 ** +---------+------------------+--------------------------------------+
3087 ** | 20 | assembly.mapping | chromosome:NCBI34|contig |
3088 ** | 21 | assembly.mapping | clone|contig |
3089 ** | 22 | assembly.mapping | supercontig|contig |
3090 ** | 23 | assembly.mapping | chromosome:NCBI34|contig|clone |
3091 ** | 24 | assembly.mapping | chromosome:NCBI34|contig|supercontig |
3092 ** | 25 | assembly.mapping | supercontig|contig|clone |
3093 ** +---------+------------------+--------------------------------------+
3094 **
3095 ** For a one-step mapping path to be valid there needs to be a relationship
3096 ** between the two Ensembl Coordinate System objects defined in the assembly
3097 ** table. Two step mapping paths work by building on the one-step mapping
3098 ** paths, which are already defined.
3099 **
3100 ** The first Ensembl Coordinate System in a one step mapping path must be the
3101 ** assembled Ensembl Coordinate System and the second must be the component.
3102 **
3103 ** A '#' delimiter indicates a special case where multiple parts of a
3104 ** 'component' region map to the same part of an 'assembled' region.
3105 ** In those cases, the mapping path is elongated by an empty middle node.
3106 ******************************************************************************/
3107
ensCoordsystemadaptorGetMappingpath(const EnsPCoordsystemadaptor csa,EnsPCoordsystem cs1,EnsPCoordsystem cs2)3108 const AjPList ensCoordsystemadaptorGetMappingpath(
3109 const EnsPCoordsystemadaptor csa,
3110 EnsPCoordsystem cs1,
3111 EnsPCoordsystem cs2)
3112 {
3113 void **keyarray = NULL;
3114 void **valarray = NULL;
3115
3116 register ajuint i = 0U;
3117
3118 ajint match = 0;
3119
3120 AjBool debug = AJFALSE;
3121
3122 AjPList mappath = NULL;
3123 AjPList midpath = NULL;
3124 AjPList tmppath = NULL;
3125
3126 AjPStr cs1key = NULL;
3127 AjPStr cs2key = NULL;
3128 AjPStr midkey = NULL;
3129 AjPStr mapkey = NULL;
3130
3131 AjPStr cs1str = NULL;
3132 AjPStr cs2str = NULL;
3133 AjPStr midstr = NULL;
3134
3135 AjPTable midcs1 = NULL;
3136 AjPTable midcs2 = NULL;
3137
3138 EnsPCoordsystem csp0 = NULL;
3139 EnsPCoordsystem csp1 = NULL;
3140 EnsPCoordsystem midcs = NULL;
3141
3142 debug = ajDebugTest("ensCoordsystemadaptorGetMappingpath");
3143
3144 if (debug)
3145 {
3146 ajDebug("ensCoordsystemadaptorGetMappingpath\n"
3147 " csa %p\n"
3148 " cs1 %p\n"
3149 " cs2 %p\n",
3150 csa,
3151 cs1,
3152 cs2);
3153
3154 ensCoordsystemTrace(cs1, 1);
3155 ensCoordsystemTrace(cs2, 1);
3156 }
3157
3158 if (!csa)
3159 return NULL;
3160
3161 if (!cs1)
3162 return NULL;
3163
3164 if (!cs2)
3165 return NULL;
3166
3167 cs1key = ajFmtStr("%S:%S", cs1->Name, cs1->Version);
3168 cs2key = ajFmtStr("%S:%S", cs2->Name, cs2->Version);
3169
3170 /* Lookup path for cs1key|cs2key. */
3171
3172 mapkey = ajFmtStr("%S|%S", cs1key, cs2key);
3173
3174 mappath = (AjPList) ajTableFetchmodV(csa->MappingPaths,
3175 (const void *) mapkey);
3176
3177 ajStrDel(&mapkey);
3178
3179 if (mappath && ajListGetLength(mappath))
3180 {
3181 ajStrDel(&cs1key);
3182 ajStrDel(&cs2key);
3183
3184 return mappath;
3185 }
3186
3187 /* Lookup path for cs2key|cs1key. */
3188
3189 mapkey = ajFmtStr("%S|%S", cs2key, cs1key);
3190
3191 mappath = (AjPList) ajTableFetchmodV(csa->MappingPaths,
3192 (const void *) mapkey);
3193
3194 ajStrDel(&mapkey);
3195
3196 if (mappath && ajListGetLength(mappath))
3197 {
3198 ajStrDel(&cs1key);
3199 ajStrDel(&cs2key);
3200
3201 return mappath;
3202 }
3203
3204 /*
3205 ** Still no success. Search for a chained mapping path involving two
3206 ** coordinate system pairs with a shared middle coordinate system.
3207 */
3208
3209 if (debug)
3210 ajDebug("ensCoordsystemadaptorGetMappingpath got no explicit mapping "
3211 "path between Ensembl Coordinate System objects "
3212 "'%S' and '%S'.\n",
3213 cs1key, cs2key);
3214
3215 /*
3216 ** Initialise temporary AJAX Table objects of AJAX String key and
3217 ** AJAX List of Ensembl Coordinate System objects value data.
3218 */
3219
3220 midcs1 = ajTablestrNew(0U);
3221 midcs2 = ajTablestrNew(0U);
3222
3223 ajTableSetDestroyvalue(
3224 midcs1,
3225 (void (*)(void **)) &coordsystemadaptorMappingpathValdel);
3226 ajTableSetDestroyvalue(
3227 midcs2,
3228 (void (*)(void **)) &coordsystemadaptorMappingpathValdel);
3229
3230 /*
3231 ** Iterate over all mapping paths stored in the
3232 ** Coordinate System Adaptor.
3233 */
3234
3235 ajTableToarrayKeysValues(csa->MappingPaths, &keyarray, &valarray);
3236
3237 for (i = 0U; keyarray[i]; i++)
3238 {
3239 tmppath = (AjPList) valarray[i];
3240
3241 /* Consider only paths of two components. */
3242
3243 if (ajListGetLength(tmppath) != 2)
3244 continue;
3245
3246 ajListPeekNumber(tmppath, 0, (void **) &csp0);
3247 ajListPeekNumber(tmppath, 1, (void **) &csp1);
3248
3249 match = -1;
3250
3251 if (ensCoordsystemMatch(csp0, cs1))
3252 match = 1;
3253
3254 if (ensCoordsystemMatch(csp1, cs1))
3255 match = 0;
3256
3257 if (match >= 0)
3258 {
3259 ajListPeekNumber(tmppath, (ajuint) match, (void **) &midcs);
3260
3261 midkey = ajFmtStr("%S:%S", midcs->Name, midcs->Version);
3262
3263 if (ajTableMatchS(midcs2, midkey))
3264 {
3265 mapkey = ajFmtStr("%S|%S", cs1key, cs2key);
3266
3267 midpath = ajListNew();
3268
3269 ajListPushAppend(midpath,
3270 (void *) ensCoordsystemNewRef(cs1));
3271
3272 ajListPushAppend(midpath,
3273 (void *) ensCoordsystemNewRef(midcs));
3274
3275 ajListPushAppend(midpath,
3276 (void *) ensCoordsystemNewRef(cs2));
3277
3278 ajTablePut(csa->MappingPaths,
3279 (void *) mapkey,
3280 (void *) midpath);
3281
3282 mappath = midpath;
3283
3284 /*
3285 ** Inform the user that an explicit assembly.mapping entry is
3286 ** missing from the Ensembl Core meta table.
3287 */
3288
3289 cs1str = (ajStrGetLen(cs1->Version)) ?
3290 ajFmtStr("%S:%S", cs1->Name, cs1->Version) :
3291 ajStrNewS(cs1->Name);
3292
3293 midstr = (ajStrGetLen(midcs->Version)) ?
3294 ajFmtStr("%S:%S", midcs->Name, midcs->Version) :
3295 ajStrNewS(midcs->Name);
3296
3297 cs2str = (ajStrGetLen(cs2->Version)) ?
3298 ajFmtStr("%S:%S", cs2->Name, cs2->Version) :
3299 ajStrNewS(cs2->Name);
3300
3301 ajWarn("ensCoordsystemadaptorGetMappingpath uses an implicit"
3302 "mapping path between "
3303 "Ensembl Coordinate System objects '%S' and '%S'.\n"
3304 "An explicit 'assembly.mapping' entry should be "
3305 "added to the Ensembl Core 'meta' table.\n"
3306 "Example: '%S|%S|%S'\n",
3307 cs1str, cs2str,
3308 cs1str, midstr, cs2str);
3309
3310 ajStrDel(&cs1str);
3311 ajStrDel(&cs2str);
3312 ajStrDel(&midstr);
3313 ajStrDel(&midkey);
3314
3315 break;
3316 }
3317 else
3318 {
3319 midpath = ajListNew();
3320
3321 ajListPushAppend(midpath,
3322 (void *) ensCoordsystemNewRef(midcs));
3323
3324 ajTablePut(midcs1,
3325 (void *) ajStrNewS(midkey),
3326 (void *) midpath);
3327 }
3328
3329 ajStrDel(&midkey);
3330 }
3331
3332 match = -1;
3333
3334 if (ensCoordsystemMatch(csp0, cs2))
3335 match = 1;
3336
3337 if (ensCoordsystemMatch(csp1, cs2))
3338 match = 0;
3339
3340 if (match >= 0)
3341 {
3342 ajListPeekNumber(tmppath, (ajuint) match, (void **) &midcs);
3343
3344 midkey = ajFmtStr("%S:%S", midcs->Name, midcs->Version);
3345
3346 if (ajTableMatchS(midcs1, midkey))
3347 {
3348 mapkey = ajFmtStr("%S|%S", cs2key, cs1key);
3349
3350 midpath = ajListNew();
3351
3352 ajListPushAppend(midpath,
3353 (void *) ensCoordsystemNewRef(cs2));
3354
3355 ajListPushAppend(midpath,
3356 (void *) ensCoordsystemNewRef(midcs));
3357
3358 ajListPushAppend(midpath,
3359 (void *) ensCoordsystemNewRef(cs1));
3360
3361 ajTablePut(csa->MappingPaths,
3362 (void *) mapkey,
3363 (void *) midpath);
3364
3365 mappath = midpath;
3366
3367 /*
3368 ** Inform the user that an explicit assembly.mapping entry is
3369 ** missing from the Ensembl Core meta table.
3370 */
3371
3372 cs1str = (ajStrGetLen(cs1->Version)) ?
3373 ajFmtStr("%S:%S", cs1->Name, cs1->Version) :
3374 ajStrNewS(cs1->Name);
3375
3376 midstr = (ajStrGetLen(midcs->Version)) ?
3377 ajFmtStr("%S:%S", midcs->Name, midcs->Version) :
3378 ajStrNewS(midcs->Name);
3379
3380 cs2str = (ajStrGetLen(cs2->Version)) ?
3381 ajFmtStr("%S:%S", cs2->Name, cs2->Version) :
3382 ajStrNewS(cs2->Name);
3383
3384 ajWarn("ensCoordsystemadaptorGetMappingpath uses an implicit "
3385 "mapping path between "
3386 "Ensembl Coordinate System objects '%S' and '%S'.\n"
3387 "An explicit 'assembly.mapping' entry should be "
3388 "added to the Ensembl Core 'meta' table.\n"
3389 "Example: '%S|%S|%S'\n",
3390 cs1str, cs2str,
3391 cs1str, midstr, cs2str);
3392
3393 ajStrDel(&cs1str);
3394 ajStrDel(&cs2str);
3395 ajStrDel(&midstr);
3396 ajStrDel(&midkey);
3397
3398 break;
3399 }
3400 else
3401 {
3402 midpath = ajListNew();
3403
3404 ajListPushAppend(midpath,
3405 (void *) ensCoordsystemNewRef(midcs));
3406
3407 ajTablePut(midcs2,
3408 (void *) ajStrNewS(midkey),
3409 (void *) midpath);
3410 }
3411
3412 ajStrDel(&midkey);
3413 }
3414 }
3415
3416 AJFREE(keyarray);
3417 AJFREE(valarray);
3418
3419 ajTableDel(&midcs1);
3420 ajTableDel(&midcs2);
3421
3422 ajStrDel(&cs1key);
3423 ajStrDel(&cs2key);
3424
3425 return mappath;
3426 }
3427
3428
3429
3430
3431 /* @func ensCoordsystemadaptorGetSeqregionidentifierExternal ******************
3432 **
3433 ** Get the external Ensembl Sequence Region identifier for an
3434 ** internal Ensembl Sequence Region identifier.
3435 ** If the external Ensembl Sequence Region is not present in the mapping table,
3436 ** the identifier for the internal one will be returned.
3437 **
3438 ** @param [r] csa [const EnsPCoordsystemadaptor]
3439 ** Ensembl Coordinate System Adaptor
3440 ** @param [r] srid [ajuint] Internal Ensembl Sequence Region identifier
3441 **
3442 ** @return [ajuint] External Ensembl Sequence Region identifier or 0U
3443 **
3444 ** @release 6.4.0
3445 ** @@
3446 ******************************************************************************/
3447
ensCoordsystemadaptorGetSeqregionidentifierExternal(const EnsPCoordsystemadaptor csa,ajuint srid)3448 ajuint ensCoordsystemadaptorGetSeqregionidentifierExternal(
3449 const EnsPCoordsystemadaptor csa,
3450 ajuint srid)
3451 {
3452 ajuint *Pidentifier = NULL;
3453
3454 if (!csa)
3455 return 0U;
3456
3457 if (!srid)
3458 return 0U;
3459
3460 Pidentifier = (ajuint *) ajTableFetchmodV(csa->InternalToExternal,
3461 (const void *) &srid);
3462
3463 return (Pidentifier) ? *Pidentifier : srid;
3464 }
3465
3466
3467
3468
3469 /* @func ensCoordsystemadaptorGetSeqregionidentifierInternal ******************
3470 **
3471 ** Get the internal Ensembl Sequence Region identifier for an
3472 ** external Ensembl Sequence Region identifier.
3473 ** If the internal Ensembl Sequence Region is not present in the mapping table,
3474 ** the identifier for the external one will be returned.
3475 **
3476 ** @param [r] csa [const EnsPCoordsystemadaptor]
3477 ** Ensembl Coordinate System Adaptor
3478 ** @param [r] srid [ajuint] External Ensembl Sequence Region identifier
3479 **
3480 ** @return [ajuint] Internal Ensembl Sequence Region identifier or 0U
3481 **
3482 ** @release 6.4.0
3483 ** @@
3484 ******************************************************************************/
3485
ensCoordsystemadaptorGetSeqregionidentifierInternal(const EnsPCoordsystemadaptor csa,ajuint srid)3486 ajuint ensCoordsystemadaptorGetSeqregionidentifierInternal(
3487 const EnsPCoordsystemadaptor csa,
3488 ajuint srid)
3489 {
3490 ajuint *Pidentifier = NULL;
3491
3492 if (!csa)
3493 return 0U;
3494
3495 if (!srid)
3496 return 0U;
3497
3498 Pidentifier = (ajuint *) ajTableFetchmodV(csa->ExternalToInternal,
3499 (const void *) &srid);
3500
3501 return (Pidentifier) ? *Pidentifier : srid;
3502 }
3503