/
tables.py
1960 lines (1683 loc) · 80.3 KB
/
tables.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Tree sequence IO via the tables API.
"""
from __future__ import division
from __future__ import print_function
import base64
import collections
import datetime
import warnings
import numpy as np
from six.moves import copyreg
import _tskit
# This circular import is ugly but it seems hard to avoid it since table collection
# and tree sequence depend on each other. Unless they're in the same module they
# need to import each other. In Py3 at least we can import the modules but we
# can't do this in Py3.
import tskit
IndividualTableRow = collections.namedtuple(
"IndividualTableRow",
["flags", "location", "metadata"])
NodeTableRow = collections.namedtuple(
"NodeTableRow",
["flags", "time", "population", "individual", "metadata"])
EdgeTableRow = collections.namedtuple(
"EdgeTableRow",
["left", "right", "parent", "child"])
MigrationTableRow = collections.namedtuple(
"MigrationTableRow",
["left", "right", "node", "source", "dest", "time"])
SiteTableRow = collections.namedtuple(
"SiteTableRow",
["position", "ancestral_state", "metadata"])
MutationTableRow = collections.namedtuple(
"MutationTableRow",
["site", "node", "derived_state", "parent", "metadata"])
PopulationTableRow = collections.namedtuple(
"PopulationTableRow",
["metadata"])
ProvenanceTableRow = collections.namedtuple(
"ProvenanceTableRow",
["timestamp", "record"])
# TODO We could abstract quite a lot more functionality up into this baseclass
# if each class kept a list of its columns. Then it would be pretty simple to
# define generic implementation of copy, etc.
class BaseTable(object):
"""
Superclass of high-level tables. Not intended for direct instantiation.
"""
def __init__(self, ll_table, row_class):
self.ll_table = ll_table
self.row_class = row_class
@property
def num_rows(self):
return self.ll_table.num_rows
@property
def max_rows(self):
return self.ll_table.max_rows
@property
def max_rows_increment(self):
return self.ll_table.max_rows_increment
def __eq__(self, other):
ret = False
if type(other) is type(self):
ret = bool(self.ll_table.equals(other.ll_table))
return ret
def __ne__(self, other):
return not self.__eq__(other)
def __len__(self):
return self.num_rows
def __getitem__(self, index):
if index < 0:
index += len(self)
return self.row_class(*self.ll_table.get_row(index))
def clear(self):
"""
Deletes all rows in this table.
"""
self.ll_table.clear()
def reset(self):
# Deprecated alias for clear
self.clear()
def truncate(self, num_rows):
"""
Truncates this table so that the only the first ``num_rows`` are retained.
:param int num_rows: The number of rows to retain in this table.
"""
return self.ll_table.truncate(num_rows)
# Unpickle support
def __setstate__(self, state):
self.set_columns(**state)
class IndividualTable(BaseTable):
"""
A table defining the individuals in a tree sequence. Note that although
each Individual has associated nodes, reference to these is not stored in
the individual table, but rather reference to the individual is stored for
each node in the :class:`NodeTable`. This is similar to the way in which
the relationship between sites and mutations is modelled.
:warning: The numpy arrays returned by table attribute accesses are **copies**
of the underlying data. In particular, this means that you cannot edit
the values in the columns by updating the attribute arrays.
**NOTE:** this behaviour may change in future.
:ivar flags: The array of flags values.
:vartype flags: numpy.ndarray, dtype=np.uint32
:ivar location: The flattened array of floating point location values. See
:ref:`sec_encoding_ragged_columns` for more details.
:vartype location: numpy.ndarray, dtype=np.float64
:ivar location_offset: The array of offsets into the location column. See
:ref:`sec_encoding_ragged_columns` for more details.
:vartype location_offset: numpy.ndarray, dtype=np.uint32
:ivar metadata: The flattened array of binary metadata values. See
:ref:`sec_tables_api_binary_columns` for more details.
:vartype metadata: numpy.ndarray, dtype=np.int8
:ivar metadata_offset: The array of offsets into the metadata column. See
:ref:`sec_tables_api_binary_columns` for more details.
:vartype metadata_offset: numpy.ndarray, dtype=np.uint32
"""
def __init__(self, max_rows_increment=0, ll_table=None):
if ll_table is None:
ll_table = _tskit.IndividualTable(max_rows_increment=max_rows_increment)
super(IndividualTable, self).__init__(ll_table, IndividualTableRow)
@property
def flags(self):
return self.ll_table.flags
@property
def location(self):
return self.ll_table.location
@property
def location_offset(self):
return self.ll_table.location_offset
@property
def metadata(self):
return self.ll_table.metadata
@property
def metadata_offset(self):
return self.ll_table.metadata_offset
def __str__(self):
flags = self.flags
location = self.location
location_offset = self.location_offset
metadata = unpack_bytes(self.metadata, self.metadata_offset)
ret = "id\tflags\tlocation\tmetadata\n"
for j in range(self.num_rows):
md = base64.b64encode(metadata[j]).decode('utf8')
location_str = ",".join(map(
str, location[location_offset[j]: location_offset[j + 1]]))
ret += "{}\t{}\t{}\t{}\n".format(j, flags[j], location_str, md)
return ret[:-1]
def copy(self):
"""
Returns a deep copy of this table.
"""
copy = IndividualTable()
copy.set_columns(
flags=self.flags,
location=self.location, location_offset=self.location_offset,
metadata=self.metadata, metadata_offset=self.metadata_offset)
return copy
def add_row(self, flags=0, location=None, metadata=None):
"""
Adds a new row to this :class:`IndividualTable` and returns the ID of the
corresponding individual.
:param int flags: The bitwise flags for the new node.
:param array-like location: A list of numeric values or one-dimensional numpy
array describing the location of this individual. If not specified
or None, a zero-dimensional location is stored.
:param bytes metadata: The binary-encoded metadata for the new node. If not
specified or None, a zero-length byte string is stored.
:return: The ID of the newly added node.
:rtype: int
"""
return self.ll_table.add_row(flags=flags, location=location, metadata=metadata)
def set_columns(
self, flags, location=None, location_offset=None,
metadata=None, metadata_offset=None):
"""
Sets the values for each column in this :class:`.IndividualTable` using the
values in the specified arrays. Overwrites any data currently stored in
the table.
The ``flags`` array is mandatory and defines the number of individuals
the table will contain.
The ``location`` and ``location_offset`` parameters must be supplied
together, and meet the requirements for :ref:`sec_encoding_ragged_columns`.
The ``metadata`` and ``metadata_offset`` parameters must be supplied
together, and meet the requirements for :ref:`sec_encoding_ragged_columns`.
See :ref:`sec_tables_api_binary_columns` for more information.
:param flags: The bitwise flags for each individual. Required.
:type flags: numpy.ndarray, dtype=np.uint32
:param location: The flattened location array. Must be specified along
with ``location_offset``. If not specified or None, an empty location
value is stored for each individual.
:type location: numpy.ndarray, dtype=np.float64
:param location_offset: The offsets into the ``location`` array.
:type location_offset: numpy.ndarray, dtype=np.uint32.
:param metadata: The flattened metadata array. Must be specified along
with ``metadata_offset``. If not specified or None, an empty metadata
value is stored for each individual.
:type metadata: numpy.ndarray, dtype=np.int8
:param metadata_offset: The offsets into the ``metadata`` array.
:type metadata_offset: numpy.ndarray, dtype=np.uint32.
"""
self.ll_table.set_columns(
flags, location=location, location_offset=location_offset,
metadata=metadata, metadata_offset=metadata_offset)
def append_columns(
self, flags, location=None, location_offset=None, metadata=None,
metadata_offset=None):
"""
Appends the specified arrays to the end of the columns in this
:class:`IndividualTable`. This allows many new rows to be added at once.
The ``flags`` array is mandatory and defines the number of
extra individuals to add to the table.
The ``location`` and ``location_offset`` parameters must be supplied
together, and meet the requirements for :ref:`sec_encoding_ragged_columns`.
The ``metadata`` and ``metadata_offset`` parameters must be supplied
together, and meet the requirements for :ref:`sec_encoding_ragged_columns`.
See :ref:`sec_tables_api_binary_columns` for more information.
:param flags: The bitwise flags for each individual. Required.
:type flags: numpy.ndarray, dtype=np.uint32
:param location: The flattened location array. Must be specified along
with ``location_offset``. If not specified or None, an empty location
value is stored for each individual.
:type location: numpy.ndarray, dtype=np.float64
:param location_offset: The offsets into the ``location`` array.
:type location_offset: numpy.ndarray, dtype=np.uint32.
:param metadata: The flattened metadata array. Must be specified along
with ``metadata_offset``. If not specified or None, an empty metadata
value is stored for each individual.
:type metadata: numpy.ndarray, dtype=np.int8
:param metadata_offset: The offsets into the ``metadata`` array.
:type metadata_offset: numpy.ndarray, dtype=np.uint32.
"""
self.ll_table.append_columns(
flags, location=location, location_offset=location_offset,
metadata=metadata, metadata_offset=metadata_offset)
# Pickle support. See copyreg registration for this function below.
def _pickle_individual_table(table):
state = {
"flags": table.flags,
"location": table.location,
"location_offset": table.location_offset,
"metadata": table.metadata,
"metadata_offset": table.metadata_offset,
}
return IndividualTable, tuple(), state
class NodeTable(BaseTable):
"""
A table defining the nodes in a tree sequence. See the
:ref:`definitions <sec_node_table_definition>` for details on the columns
in this table and the
:ref:`tree sequence requirements <sec_valid_tree_sequence_requirements>` section
for the properties needed for a node table to be a part of a valid tree sequence.
:warning: The numpy arrays returned by table attribute accesses are **copies**
of the underlying data. In particular, this means that you cannot edit
the values in the columns by updating the attribute arrays.
**NOTE:** this behaviour may change in future.
:ivar time: The array of time values.
:vartype time: numpy.ndarray, dtype=np.float64
:ivar flags: The array of flags values.
:vartype flags: numpy.ndarray, dtype=np.uint32
:ivar population: The array of population IDs.
:vartype population: numpy.ndarray, dtype=np.int32
:ivar individual: The array of individual IDs that each node belongs to.
:vartype individual: numpy.ndarray, dtype=np.int32
:ivar metadata: The flattened array of binary metadata values. See
:ref:`sec_tables_api_binary_columns` for more details.
:vartype metadata: numpy.ndarray, dtype=np.int8
:ivar metadata_offset: The array of offsets into the metadata column. See
:ref:`sec_tables_api_binary_columns` for more details.
:vartype metadata_offset: numpy.ndarray, dtype=np.uint32
"""
def __init__(self, max_rows_increment=0, ll_table=None):
if ll_table is None:
ll_table = _tskit.NodeTable(max_rows_increment=max_rows_increment)
super(NodeTable, self).__init__(ll_table, NodeTableRow)
@property
def time(self):
return self.ll_table.time
@property
def flags(self):
return self.ll_table.flags
@property
def population(self):
return self.ll_table.population
@property
def individual(self):
return self.ll_table.individual
# EXPERIMENTAL interface for setting a single column. This is done
# quite a bit in tests. Not part of the public API as yet, but we
# probably will want to allow something like this in general.
@individual.setter
def individual(self, individual):
self.set_columns(
flags=self.flags, time=self.time, population=self.population,
metadata=self.metadata, metadata_offset=self.metadata_offset,
individual=individual)
@property
def metadata(self):
return self.ll_table.metadata
@property
def metadata_offset(self):
return self.ll_table.metadata_offset
def __str__(self):
time = self.time
flags = self.flags
population = self.population
individual = self.individual
metadata = unpack_bytes(self.metadata, self.metadata_offset)
ret = "id\tflags\tpopulation\tindividual\ttime\tmetadata\n"
for j in range(self.num_rows):
md = base64.b64encode(metadata[j]).decode('utf8')
ret += "{}\t{}\t{}\t{}\t{:.14f}\t{}\n".format(
j, flags[j], population[j], individual[j], time[j], md)
return ret[:-1]
def copy(self):
"""
Returns a deep copy of this table.
"""
copy = NodeTable()
copy.set_columns(
flags=self.flags, time=self.time, population=self.population,
individual=self.individual, metadata=self.metadata,
metadata_offset=self.metadata_offset)
return copy
def add_row(self, flags=0, time=0, population=-1, individual=-1, metadata=None):
"""
Adds a new row to this :class:`NodeTable` and returns the ID of the
corresponding node.
:param int flags: The bitwise flags for the new node.
:param float time: The birth time for the new node.
:param int population: The ID of the population in which the new node was born.
Defaults to the :const:`.NULL_POPULATION`.
:param int individual: The ID of the individual in which the new node was born.
Defaults to the :const:`.NULL_INDIVIDUAL`.
:param bytes metadata: The binary-encoded metadata for the new node. If not
specified or None, a zero-length byte string is stored.
:return: The ID of the newly added node.
:rtype: int
"""
return self.ll_table.add_row(flags, time, population, individual, metadata)
def set_columns(
self, flags, time, population=None, individual=None, metadata=None,
metadata_offset=None):
"""
Sets the values for each column in this :class:`.NodeTable` using the values in
the specified arrays. Overwrites any data currently stored in the table.
The ``flags``, ``time`` and ``population`` arrays must all be of the same length,
which is equal to the number of nodes the table will contain. The
``metadata`` and ``metadata_offset`` parameters must be supplied together, and
meet the requirements for :ref:`sec_encoding_ragged_columns`.
See :ref:`sec_tables_api_binary_columns` for more information.
:param flags: The bitwise flags for each node. Required.
:type flags: numpy.ndarray, dtype=np.uint32
:param time: The time values for each node. Required.
:type time: numpy.ndarray, dtype=np.float64
:param population: The population values for each node. If not specified
or None, the :const:`.NULL_POPULATION` value is stored for each node.
:type population: numpy.ndarray, dtype=np.int32
:param individual: The individual values for each node. If not specified
or None, the :const:`.NULL_INDIVIDUAL` value is stored for each node.
:type individual: numpy.ndarray, dtype=np.int32
:param metadata: The flattened metadata array. Must be specified along
with ``metadata_offset``. If not specified or None, an empty metadata
value is stored for each node.
:type metadata: numpy.ndarray, dtype=np.int8
:param metadata_offset: The offsets into the ``metadata`` array.
:type metadata_offset: numpy.ndarray, dtype=np.uint32.
"""
self.ll_table.set_columns(
flags, time, population=population, individual=individual, metadata=metadata,
metadata_offset=metadata_offset)
def append_columns(
self, flags, time, population=None, individual=None, metadata=None,
metadata_offset=None):
"""
Appends the specified arrays to the end of the columns in this
:class:`NodeTable`. This allows many new rows to be added at once.
The ``flags``, ``time`` and ``population`` arrays must all be of the same length,
which is equal to the number of nodes that will be added to the table. The
``metadata`` and ``metadata_offset`` parameters must be supplied together, and
meet the requirements for :ref:`sec_encoding_ragged_columns`.
See :ref:`sec_tables_api_binary_columns` for more information.
:param flags: The bitwise flags for each node. Required.
:type flags: numpy.ndarray, dtype=np.uint32
:param time: The time values for each node. Required.
:type time: numpy.ndarray, dtype=np.float64
:param population: The population values for each node. If not specified
or None, the :const:`.NULL_POPULATION` value is stored for each node.
:type population: numpy.ndarray, dtype=np.int32
:param individual: The individual values for each node. If not specified
or None, the :const:`.NULL_INDIVIDUAL` value is stored for each node.
:type individual: numpy.ndarray, dtype=np.int32
:param metadata: The flattened metadata array. Must be specified along
with ``metadata_offset``. If not specified or None, an empty metadata
value is stored for each node.
:type metadata: numpy.ndarray, dtype=np.int8
:param metadata_offset: The offsets into the ``metadata`` array.
:type metadata_offset: numpy.ndarray, dtype=np.uint32.
"""
self.ll_table.append_columns(
flags, time, population=population, individual=individual,
metadata=metadata, metadata_offset=metadata_offset)
# Pickle support. See copyreg registration for this function below.
def _pickle_node_table(table):
state = {
"time": table.time,
"flags": table.flags,
"population": table.population,
"individual": table.individual,
"metadata": table.metadata,
"metadata_offset": table.metadata_offset,
}
return NodeTable, tuple(), state
class EdgeTable(BaseTable):
"""
A table defining the edges in a tree sequence. See the
:ref:`definitions <sec_edge_table_definition>` for details on the columns
in this table and the
:ref:`tree sequence requirements <sec_valid_tree_sequence_requirements>` section
for the properties needed for an edge table to be a part of a valid tree sequence.
:warning: The numpy arrays returned by table attribute accesses are **copies**
of the underlying data. In particular, this means that you cannot edit
the values in the columns by updating the attribute arrays.
**NOTE:** this behaviour may change in future.
:ivar left: The array of left coordinates.
:vartype left: numpy.ndarray, dtype=np.float64
:ivar right: The array of right coordinates.
:vartype right: numpy.ndarray, dtype=np.float64
:ivar parent: The array of parent node IDs.
:vartype parent: numpy.ndarray, dtype=np.int32
:ivar child: The array of child node IDs.
:vartype child: numpy.ndarray, dtype=np.int32
"""
def __init__(self, max_rows_increment=0, ll_table=None):
if ll_table is None:
ll_table = _tskit.EdgeTable(max_rows_increment=max_rows_increment)
super(EdgeTable, self).__init__(ll_table, EdgeTableRow)
@property
def left(self):
return self.ll_table.left
@property
def right(self):
return self.ll_table.right
@property
def parent(self):
return self.ll_table.parent
@property
def child(self):
return self.ll_table.child
def __str__(self):
left = self.left
right = self.right
parent = self.parent
child = self.child
ret = "id\tleft\t\tright\t\tparent\tchild\n"
for j in range(self.num_rows):
ret += "{}\t{:.8f}\t{:.8f}\t{}\t{}\n".format(
j, left[j], right[j], parent[j], child[j])
return ret[:-1]
def copy(self):
"""
Returns a deep copy of this table.
"""
copy = EdgeTable()
copy.set_columns(
left=self.left, right=self.right, parent=self.parent, child=self.child)
return copy
def add_row(self, left, right, parent, child):
"""
Adds a new row to this :class:`EdgeTable` and returns the ID of the
corresponding edge.
:param float left: The left coordinate (inclusive).
:param float right: The right coordinate (exclusive).
:param int parent: The ID of parent node.
:param int child: The ID of child node.
:return: The ID of the newly added edge.
:rtype: int
"""
return self.ll_table.add_row(left, right, parent, child)
def set_columns(self, left, right, parent, child):
"""
Sets the values for each column in this :class:`.EdgeTable` using the values
in the specified arrays. Overwrites any data currently stored in the table.
All four parameters are mandatory, and must be numpy arrays of the
same length (which is equal to the number of edges the table will contain).
:param left: The left coordinates (inclusive).
:type left: numpy.ndarray, dtype=np.float64
:param right: The right coordinates (exclusive).
:type right: numpy.ndarray, dtype=np.float64
:param parent: The parent node IDs.
:type parent: numpy.ndarray, dtype=np.int32
:param child: The child node IDs.
:type child: numpy.ndarray, dtype=np.int32
"""
self.ll_table.set_columns(left, right, parent, child)
def append_columns(self, left, right, parent, child):
"""
Appends the specified arrays to the end of the columns of this
:class:`EdgeTable`. This allows many new rows to be added at once.
All four parameters are mandatory, and must be numpy arrays of the
same length (which is equal to the number of additional edges to
add to the table).
:param left: The left coordinates (inclusive).
:type left: numpy.ndarray, dtype=np.float64
:param right: The right coordinates (exclusive).
:type right: numpy.ndarray, dtype=np.float64
:param parent: The parent node IDs.
:type parent: numpy.ndarray, dtype=np.int32
:param child: The child node IDs.
:type child: numpy.ndarray, dtype=np.int32
"""
self.ll_table.append_columns(left, right, parent, child)
# Pickle support. See copyreg registration for this function below.
def _edge_table_pickle(table):
state = {
"left": table.left,
"right": table.right,
"parent": table.parent,
"child": table.child,
}
return EdgeTable, tuple(), state
class MigrationTable(BaseTable):
"""
A table defining the migrations in a tree sequence. See the
:ref:`definitions <sec_migration_table_definition>` for details on the columns
in this table and the
:ref:`tree sequence requirements <sec_valid_tree_sequence_requirements>` section
for the properties needed for a migration table to be a part of a valid tree
sequence.
:warning: The numpy arrays returned by table attribute accesses are **copies**
of the underlying data. In particular, this means that you cannot edit
the values in the columns by updating the attribute arrays.
**NOTE:** this behaviour may change in future.
:ivar left: The array of left coordinates.
:vartype left: numpy.ndarray, dtype=np.float64
:ivar right: The array of right coordinates.
:vartype right: numpy.ndarray, dtype=np.float64
:ivar node: The array of node IDs.
:vartype node: numpy.ndarray, dtype=np.int32
:ivar source: The array of source population IDs.
:vartype source: numpy.ndarray, dtype=np.int32
:ivar dest: The array of destination population IDs.
:vartype dest: numpy.ndarray, dtype=np.int32
:ivar time: The array of time values.
:vartype time: numpy.ndarray, dtype=np.float64
"""
def __init__(self, max_rows_increment=0, ll_table=None):
if ll_table is None:
ll_table = _tskit.MigrationTable(max_rows_increment=max_rows_increment)
super(MigrationTable, self).__init__(ll_table, MigrationTableRow)
@property
def left(self):
return self.ll_table.left
@property
def right(self):
return self.ll_table.right
@property
def node(self):
return self.ll_table.node
@property
def source(self):
return self.ll_table.source
@property
def dest(self):
return self.ll_table.dest
@property
def time(self):
return self.ll_table.time
def __str__(self):
left = self.left
right = self.right
node = self.node
source = self.source
dest = self.dest
time = self.time
ret = "id\tleft\tright\tnode\tsource\tdest\ttime\n"
for j in range(self.num_rows):
ret += "{}\t{:.8f}\t{:.8f}\t{}\t{}\t{}\t{:.8f}\n".format(
j, left[j], right[j], node[j], source[j], dest[j], time[j])
return ret[:-1]
def copy(self):
"""
Returns a deep copy of this table.
"""
copy = MigrationTable()
copy.set_columns(
left=self.left, right=self.right, node=self.node, source=self.source,
dest=self.dest, time=self.time)
return copy
def add_row(self, left, right, node, source, dest, time):
"""
Adds a new row to this :class:`MigrationTable` and returns the ID of the
corresponding migration.
:param float left: The left coordinate (inclusive).
:param float right: The right coordinate (exclusive).
:param int node: The node ID.
:param int source: The ID of the source population.
:param int dest: The ID of the destination population.
:param float time: The time of the migration event.
:return: The ID of the newly added migration.
:rtype: int
"""
return self.ll_table.add_row(left, right, node, source, dest, time)
def set_columns(self, left, right, node, source, dest, time):
"""
Sets the values for each column in this :class:`.MigrationTable` using the values
in the specified arrays. Overwrites any data currently stored in the table.
All six parameters are mandatory, and must be numpy arrays of the
same length (which is equal to the number of migrations the table will contain).
:param left: The left coordinates (inclusive).
:type left: numpy.ndarray, dtype=np.float64
:param right: The right coordinates (exclusive).
:type right: numpy.ndarray, dtype=np.float64
:param node: The node IDs.
:type node: numpy.ndarray, dtype=np.int32
:param source: The source population IDs.
:type source: numpy.ndarray, dtype=np.int32
:param dest: The destination population IDs.
:type dest: numpy.ndarray, dtype=np.int32
:param time: The time of each migration.
:type time: numpy.ndarray, dtype=np.int64
"""
self.ll_table.set_columns(left, right, node, source, dest, time)
def append_columns(self, left, right, node, source, dest, time):
"""
Appends the specified arrays to the end of the columns of this
:class:`MigrationTable`. This allows many new rows to be added at once.
All six parameters are mandatory, and must be numpy arrays of the
same length (which is equal to the number of additional migrations
to add to the table).
:param left: The left coordinates (inclusive).
:type left: numpy.ndarray, dtype=np.float64
:param right: The right coordinates (exclusive).
:type right: numpy.ndarray, dtype=np.float64
:param node: The node IDs.
:type node: numpy.ndarray, dtype=np.int32
:param source: The source population IDs.
:type source: numpy.ndarray, dtype=np.int32
:param dest: The destination population IDs.
:type dest: numpy.ndarray, dtype=np.int32
:param time: The time of each migration.
:type time: numpy.ndarray, dtype=np.int64
"""
self.ll_table.append_columns(left, right, node, source, dest, time)
# Pickle support. See copyreg registration for this function below.
def _migration_table_pickle(table):
state = {
"left": table.left,
"right": table.right,
"node": table.node,
"source": table.source,
"dest": table.dest,
"time": table.time,
}
return MigrationTable, tuple(), state
class SiteTable(BaseTable):
"""
A table defining the sites in a tree sequence. See the
:ref:`definitions <sec_site_table_definition>` for details on the columns
in this table and the
:ref:`tree sequence requirements <sec_valid_tree_sequence_requirements>` section
for the properties needed for a site table to be a part of a valid tree
sequence.
:warning: The numpy arrays returned by table attribute accesses are **copies**
of the underlying data. In particular, this means that you cannot edit
the values in the columns by updating the attribute arrays.
**NOTE:** this behaviour may change in future.
:ivar position: The array of site position coordinates.
:vartype position: numpy.ndarray, dtype=np.float64
:ivar ancestral_state: The flattened array of ancestral state strings.
See :ref:`sec_tables_api_text_columns` for more details.
:vartype ancestral_state: numpy.ndarray, dtype=np.int8
:ivar ancestral_state_offset: The offsets of rows in the ancestral_state
array. See :ref:`sec_tables_api_text_columns` for more details.
:vartype ancestral_state_offset: numpy.ndarray, dtype=np.uint32
:ivar metadata: The flattened array of binary metadata values. See
:ref:`sec_tables_api_binary_columns` for more details.
:vartype metadata: numpy.ndarray, dtype=np.int8
:ivar metadata_offset: The array of offsets into the metadata column. See
:ref:`sec_tables_api_binary_columns` for more details.
:vartype metadata_offset: numpy.ndarray, dtype=np.uint32
"""
def __init__(self, max_rows_increment=0, ll_table=None):
if ll_table is None:
ll_table = _tskit.SiteTable(max_rows_increment=max_rows_increment)
super(SiteTable, self).__init__(ll_table, SiteTableRow)
@property
def position(self):
return self.ll_table.position
@property
def ancestral_state(self):
return self.ll_table.ancestral_state
@property
def ancestral_state_offset(self):
return self.ll_table.ancestral_state_offset
@property
def metadata(self):
return self.ll_table.metadata
@property
def metadata_offset(self):
return self.ll_table.metadata_offset
def __str__(self):
position = self.position
ancestral_state = unpack_strings(
self.ancestral_state, self.ancestral_state_offset)
metadata = unpack_bytes(self.metadata, self.metadata_offset)
ret = "id\tposition\tancestral_state\tmetadata\n"
for j in range(self.num_rows):
md = base64.b64encode(metadata[j]).decode('utf8')
ret += "{}\t{:.8f}\t{}\t{}\n".format(
j, position[j], ancestral_state[j], md)
return ret[:-1]
def copy(self):
"""
Returns a deep copy of this table.
"""
copy = SiteTable()
copy.set_columns(
position=self.position,
ancestral_state=self.ancestral_state,
ancestral_state_offset=self.ancestral_state_offset,
metadata=self.metadata,
metadata_offset=self.metadata_offset)
return copy
def add_row(self, position, ancestral_state, metadata=None):
"""
Adds a new row to this :class:`SiteTable` and returns the ID of the
corresponding site.
:param float position: The position of this site in genome coordinates.
:param str ancestral_state: The state of this site at the root of the tree.
:param bytes metadata: The binary-encoded metadata for the new node. If not
specified or None, a zero-length byte string is stored.
:return: The ID of the newly added site.
:rtype: int
"""
return self.ll_table.add_row(position, ancestral_state, metadata)
def set_columns(
self, position, ancestral_state, ancestral_state_offset,
metadata=None, metadata_offset=None):
"""
Sets the values for each column in this :class:`.SiteTable` using the values
in the specified arrays. Overwrites any data currently stored in the table.
The ``position``, ``ancestral_state`` and ``ancestral_state_offset``
parameters are mandatory, and must be 1D numpy arrays. The length
of the ``position`` array determines the number of rows in table.
The ``ancestral_state`` and ``ancestral_state_offset`` parameters must
be supplied together, and meet the requirements for
:ref:`sec_encoding_ragged_columns` (see
:ref:`sec_tables_api_text_columns` for more information). The
``metadata`` and ``metadata_offset`` parameters must be supplied
together, and meet the requirements for
:ref:`sec_encoding_ragged_columns` (see
:ref:`sec_tables_api_binary_columns` for more information).
:param position: The position of each site in genome coordinates.
:type position: numpy.ndarray, dtype=np.float64
:param ancestral_state: The flattened ancestral_state array. Required.
:type ancestral_state: numpy.ndarray, dtype=np.int8
:param ancestral_state_offset: The offsets into the ``ancestral_state`` array.
:type ancestral_state_offset: numpy.ndarray, dtype=np.uint32.
:param metadata: The flattened metadata array. Must be specified along
with ``metadata_offset``. If not specified or None, an empty metadata
value is stored for each node.
:type metadata: numpy.ndarray, dtype=np.int8
:param metadata_offset: The offsets into the ``metadata`` array.
:type metadata_offset: numpy.ndarray, dtype=np.uint32.
"""
self.ll_table.set_columns(
position, ancestral_state=ancestral_state,
ancestral_state_offset=ancestral_state_offset,
metadata=metadata, metadata_offset=metadata_offset)
def append_columns(
self, position, ancestral_state, ancestral_state_offset,
metadata=None, metadata_offset=None):
"""
Appends the specified arrays to the end of the columns of this
:class:`SiteTable`. This allows many new rows to be added at once.
The ``position``, ``ancestral_state`` and ``ancestral_state_offset``
parameters are mandatory, and must be 1D numpy arrays. The length
of the ``position`` array determines the number of additional rows
to add the table.
The ``ancestral_state`` and ``ancestral_state_offset`` parameters must
be supplied together, and meet the requirements for
:ref:`sec_encoding_ragged_columns` (see
:ref:`sec_tables_api_text_columns` for more information). The
``metadata`` and ``metadata_offset`` parameters must be supplied
together, and meet the requirements for
:ref:`sec_encoding_ragged_columns` (see
:ref:`sec_tables_api_binary_columns` for more information).
:param position: The position of each site in genome coordinates.
:type position: numpy.ndarray, dtype=np.float64
:param ancestral_state: The flattened ancestral_state array. Required.
:type ancestral_state: numpy.ndarray, dtype=np.int8
:param ancestral_state_offset: The offsets into the ``ancestral_state`` array.
:type ancestral_state_offset: numpy.ndarray, dtype=np.uint32.
:param metadata: The flattened metadata array. Must be specified along
with ``metadata_offset``. If not specified or None, an empty metadata
value is stored for each node.
:type metadata: numpy.ndarray, dtype=np.int8
:param metadata_offset: The offsets into the ``metadata`` array.
:type metadata_offset: numpy.ndarray, dtype=np.uint32.
"""
self.ll_table.append_columns(
position, ancestral_state=ancestral_state,
ancestral_state_offset=ancestral_state_offset,
metadata=metadata, metadata_offset=metadata_offset)
# Pickle support. See copyreg registration for this function below.
def _site_table_pickle(table):
state = {
"position": table.position,
"ancestral_state": table.ancestral_state,
"ancestral_state_offset": table.ancestral_state_offset,
"metadata": table.metadata,
"metadata_offset": table.metadata_offset,
}
return SiteTable, tuple(), state
class MutationTable(BaseTable):
"""
A table defining the mutations in a tree sequence. See the
:ref:`definitions <sec_mutation_table_definition>` for details on the columns
in this table and the
:ref:`tree sequence requirements <sec_valid_tree_sequence_requirements>` section
for the properties needed for a mutation table to be a part of a valid tree
sequence.
:warning: The numpy arrays returned by table attribute accesses are **copies**
of the underlying data. In particular, this means that you cannot edit
the values in the columns by updating the attribute arrays.
**NOTE:** this behaviour may change in future.
:ivar site: The array of site IDs.
:vartype site: numpy.ndarray, dtype=np.int32
:ivar node: The array of node IDs.
:vartype node: numpy.ndarray, dtype=np.int32
:ivar derived_state: The flattened array of derived state strings.
See :ref:`sec_tables_api_text_columns` for more details.
:vartype derived_state: numpy.ndarray, dtype=np.int8
:ivar derived_state_offset: The offsets of rows in the derived_state
array. See :ref:`sec_tables_api_text_columns` for more details.
:vartype derived_state_offset: numpy.ndarray, dtype=np.uint32
:ivar parent: The array of parent mutation IDs.
:vartype parent: numpy.ndarray, dtype=np.int32
:ivar metadata: The flattened array of binary metadata values. See
:ref:`sec_tables_api_binary_columns` for more details.
:vartype metadata: numpy.ndarray, dtype=np.int8
:ivar metadata_offset: The array of offsets into the metadata column. See
:ref:`sec_tables_api_binary_columns` for more details.
:vartype metadata_offset: numpy.ndarray, dtype=np.uint32
"""
def __init__(self, max_rows_increment=0, ll_table=None):
if ll_table is None:
ll_table = _tskit.MutationTable(max_rows_increment=max_rows_increment)