Skip to content

Tables

synapseclient.table

Tables

Synapse Tables enable storage of tabular data in Synapse in a form that can be queried using a SQL-like query language.

A table has a Schema and holds a set of rows conforming to that schema.

A Schema defines a series of Column of the following types:

  • STRING
  • DOUBLE
  • INTEGER
  • BOOLEAN
  • DATE
  • ENTITYID
  • FILEHANDLEID
  • LINK
  • LARGETEXT
  • USERID

Read more information about using Table in synapse in the tutorials section.

Classes

SchemaBase

Bases: Entity

This is the an Abstract Class for EntityViewSchema and Schema containing the common methods for both. You can not create an object of this type.

Source code in synapseclient/table.py
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
class SchemaBase(Entity, metaclass=abc.ABCMeta):
    """
    This is the an Abstract Class for EntityViewSchema and Schema containing the common methods for both.
    You can not create an object of this type.
    """

    _property_keys = Entity._property_keys + ["columnIds"]
    _local_keys = Entity._local_keys + ["columns_to_store"]

    @property
    @abc.abstractmethod  # forces subclasses to define _synapse_entity_type
    def _synapse_entity_type(self):
        pass

    @abc.abstractmethod
    def __init__(
        self, name, columns, properties, annotations, local_state, parent, **kwargs
    ):
        self.properties.setdefault("columnIds", [])
        self.__dict__.setdefault("columns_to_store", [])

        if name:
            kwargs["name"] = name
        super(SchemaBase, self).__init__(
            properties=properties,
            annotations=annotations,
            local_state=local_state,
            parent=parent,
            **kwargs,
        )
        if columns:
            self.addColumns(columns)

    def addColumn(self, column) -> None:
        """
        Store the column

        Arguments:
            column: A column object or its ID

        Raises:
            ValueError: If the given column is not a string, integer or [Column][synapseclient.table.Column] object
        """
        if isinstance(column, str) or isinstance(column, int) or hasattr(column, "id"):
            self.properties.columnIds.append(id_of(column))
        elif isinstance(column, Column):
            if not self.__dict__.get("columns_to_store", None):
                self.__dict__["columns_to_store"] = []
            self.__dict__["columns_to_store"].append(column)
        else:
            raise ValueError("Not a column? %s" % str(column))

    def addColumns(self, columns: list) -> None:
        """
        Add columns

        Arguments:
            columns: A list of column objects or their ID
        """
        for column in columns:
            self.addColumn(column)

    def removeColumn(self, column) -> None:
        """
        Remove column

        Arguments:
            column: A column object or its ID

        Raises:
            ValueError: If the given column is not a string, integer or [Column][synapseclient.table.Column] object
        """
        if isinstance(column, str) or isinstance(column, int) or hasattr(column, "id"):
            self.properties.columnIds.remove(id_of(column))
        elif isinstance(column, Column) and self.columns_to_store:
            self.columns_to_store.remove(column)
        else:
            ValueError("Can't remove column %s" + str(column))

    def has_columns(self):
        """Does this schema have columns specified?"""
        return bool(
            self.properties.get("columnIds", None)
            or self.__dict__.get("columns_to_store", None)
        )

    def _before_synapse_store(self, syn):
        if len(self.columns_to_store) + len(self.columnIds) > MAX_NUM_TABLE_COLUMNS:
            raise ValueError(
                "Too many columns. The limit is %s columns per table"
                % MAX_NUM_TABLE_COLUMNS
            )

        # store any columns before storing table
        if self.columns_to_store:
            self.properties.columnIds.extend(
                column.id for column in syn.createColumns(self.columns_to_store)
            )
            self.columns_to_store = []
Functions
addColumn
addColumn(column) -> None

Store the column

PARAMETER DESCRIPTION
column

A column object or its ID

RAISES DESCRIPTION
ValueError

If the given column is not a string, integer or Column object

Source code in synapseclient/table.py
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
def addColumn(self, column) -> None:
    """
    Store the column

    Arguments:
        column: A column object or its ID

    Raises:
        ValueError: If the given column is not a string, integer or [Column][synapseclient.table.Column] object
    """
    if isinstance(column, str) or isinstance(column, int) or hasattr(column, "id"):
        self.properties.columnIds.append(id_of(column))
    elif isinstance(column, Column):
        if not self.__dict__.get("columns_to_store", None):
            self.__dict__["columns_to_store"] = []
        self.__dict__["columns_to_store"].append(column)
    else:
        raise ValueError("Not a column? %s" % str(column))
addColumns
addColumns(columns: list) -> None

Add columns

PARAMETER DESCRIPTION
columns

A list of column objects or their ID

TYPE: list

Source code in synapseclient/table.py
608
609
610
611
612
613
614
615
616
def addColumns(self, columns: list) -> None:
    """
    Add columns

    Arguments:
        columns: A list of column objects or their ID
    """
    for column in columns:
        self.addColumn(column)
removeColumn
removeColumn(column) -> None

Remove column

PARAMETER DESCRIPTION
column

A column object or its ID

RAISES DESCRIPTION
ValueError

If the given column is not a string, integer or Column object

Source code in synapseclient/table.py
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
def removeColumn(self, column) -> None:
    """
    Remove column

    Arguments:
        column: A column object or its ID

    Raises:
        ValueError: If the given column is not a string, integer or [Column][synapseclient.table.Column] object
    """
    if isinstance(column, str) or isinstance(column, int) or hasattr(column, "id"):
        self.properties.columnIds.remove(id_of(column))
    elif isinstance(column, Column) and self.columns_to_store:
        self.columns_to_store.remove(column)
    else:
        ValueError("Can't remove column %s" + str(column))
has_columns
has_columns()

Does this schema have columns specified?

Source code in synapseclient/table.py
635
636
637
638
639
640
def has_columns(self):
    """Does this schema have columns specified?"""
    return bool(
        self.properties.get("columnIds", None)
        or self.__dict__.get("columns_to_store", None)
    )

Schema

Bases: SchemaBase

A Schema is an Entity that defines a set of columns in a table.

ATTRIBUTE DESCRIPTION
name

The name for the Table Schema object

description

User readable description of the schema

columns

A list of Column objects or their IDs

parent

The project in Synapse to which this table belongs

properties

A map of Synapse properties

annotations

A map of user defined annotations

local_state

Internal use only

Example:

cols = [Column(name='Isotope', columnType='STRING'),
        Column(name='Atomic Mass', columnType='INTEGER'),
        Column(name='Halflife', columnType='DOUBLE'),
        Column(name='Discovered', columnType='DATE')]

schema = syn.store(Schema(name='MyTable', columns=cols, parent=project))
Source code in synapseclient/table.py
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
class Schema(SchemaBase):
    """
    A Schema is an [Entity][synapseclient.entity.Entity] that defines a set of columns in a table.

    Attributes:
        name:        The name for the Table Schema object
        description: User readable description of the schema
        columns:     A list of [Column][synapseclient.table.Column] objects or their IDs
        parent:      The project in Synapse to which this table belongs
        properties:  A map of Synapse properties
        annotations: A map of user defined annotations
        local_state: Internal use only

    Example:

        cols = [Column(name='Isotope', columnType='STRING'),
                Column(name='Atomic Mass', columnType='INTEGER'),
                Column(name='Halflife', columnType='DOUBLE'),
                Column(name='Discovered', columnType='DATE')]

        schema = syn.store(Schema(name='MyTable', columns=cols, parent=project))
    """

    _synapse_entity_type = "org.sagebionetworks.repo.model.table.TableEntity"

    def __init__(
        self,
        name=None,
        columns=None,
        parent=None,
        properties=None,
        annotations=None,
        local_state=None,
        **kwargs,
    ):
        super(Schema, self).__init__(
            name=name,
            columns=columns,
            properties=properties,
            annotations=annotations,
            local_state=local_state,
            parent=parent,
            **kwargs,
        )

MaterializedViewSchema

Bases: SchemaBase

A MaterializedViewSchema is an Entity that defines a set of columns in a materialized view along with the SQL statement.

ATTRIBUTE DESCRIPTION
name

The name for the Materialized View Schema object

description

User readable description of the schema

definingSQL

The synapse SQL statement that defines the data in the materialized view. The SQL contain JOIN clauses on multiple tables.

columns

A list of Column objects or their IDs

parent

The project in Synapse to which this Materialized View belongs

properties

A map of Synapse properties

annotations

A map of user defined annotations

local_state

Internal use only

Example:

defining_sql = "SELECT * FROM syn111 F JOIN syn2222 P on (F.patient_id = P.patient_id)"

schema = syn.store(MaterializedViewSchema(name='MyTable', parent=project, definingSQL=defining_sql))
Source code in synapseclient/table.py
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
class MaterializedViewSchema(SchemaBase):
    """
    A MaterializedViewSchema is an [Entity][synapseclient.entity.Entity] that defines a set of columns in a
    materialized view along with the SQL statement.

    Attributes:
        name:        The name for the Materialized View Schema object
        description: User readable description of the schema
        definingSQL: The synapse SQL statement that defines the data in the materialized view. The SQL                   contain JOIN clauses on multiple tables.
        columns:     A list of [Column][synapseclient.table.Column] objects or their IDs
        parent:      The project in Synapse to which this Materialized View belongs
        properties:  A map of Synapse properties
        annotations: A map of user defined annotations
        local_state: Internal use only

    Example:

        defining_sql = "SELECT * FROM syn111 F JOIN syn2222 P on (F.patient_id = P.patient_id)"

        schema = syn.store(MaterializedViewSchema(name='MyTable', parent=project, definingSQL=defining_sql))
    """

    _synapse_entity_type = "org.sagebionetworks.repo.model.table.MaterializedView"
    _property_keys = SchemaBase._property_keys + ["definingSQL"]

    def __init__(
        self,
        name=None,
        columns=None,
        parent=None,
        definingSQL=None,
        properties=None,
        annotations=None,
        local_state=None,
        **kwargs,
    ):
        if definingSQL is not None:
            kwargs["definingSQL"] = definingSQL
        super(MaterializedViewSchema, self).__init__(
            name=name,
            columns=columns,
            properties=properties,
            annotations=annotations,
            local_state=local_state,
            parent=parent,
            **kwargs,
        )

ViewBase

Bases: SchemaBase

This is a helper class for EntityViewSchema and SubmissionViewSchema containing the common methods for both.

Source code in synapseclient/table.py
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
class ViewBase(SchemaBase):
    """
    This is a helper class for EntityViewSchema and SubmissionViewSchema
    containing the common methods for both.
    """

    _synapse_entity_type = ""
    _property_keys = SchemaBase._property_keys + ["viewTypeMask", "scopeIds"]
    _local_keys = SchemaBase._local_keys + [
        "addDefaultViewColumns",
        "addAnnotationColumns",
        "ignoredAnnotationColumnNames",
    ]

    def add_scope(self, entities: Union[Project, Folder, Evaluation, list, str]):
        """
        Add scope

        Arguments:
            entities: A [Project][synapseclient.entity.Project], [Folder][synapseclient.entity.Folder],
                      [Evaluation][synapseclient.evaluation.Evaluation] object or its ID, can also be a list of them
        """
        if isinstance(entities, list):
            # add ids to a temp list so that we don't partially modify scopeIds on an exception in id_of()
            temp_list = [id_of(entity) for entity in entities]
            self.scopeIds.extend(temp_list)
        else:
            self.scopeIds.append(id_of(entities))

    def _filter_duplicate_columns(self, syn, columns_to_add):
        """
        If a column to be added has the same name and same type as an existing column, it will be considered a duplicate
         and not added.

        Arguments:
            syn:             A [Synapse][synapseclient.client.Synapse] object that is logged in
            columns_to_add:  A iterable collection of type [Column][synapseclient.table.Column] objects

        Returns:
            A filtered list of columns to add
        """

        # no point in making HTTP calls to retrieve existing Columns if we not adding any new columns
        if not columns_to_add:
            return columns_to_add

        # set up Column name/type tracking
        # map of str -> set(str), where str is the column type as a string and set is a set of column name strings
        column_type_to_annotation_names = {}

        # add to existing columns the columns that user has added but not yet created in synapse
        column_generator = (
            itertools.chain(syn.getColumns(self.columnIds), self.columns_to_store)
            if self.columns_to_store
            else syn.getColumns(self.columnIds)
        )

        for column in column_generator:
            column_name = column["name"]
            column_type = column["columnType"]

            column_type_to_annotation_names.setdefault(column_type, set()).add(
                column_name
            )

        valid_columns = []
        for column in columns_to_add:
            new_col_name = column["name"]
            new_col_type = column["columnType"]

            typed_col_name_set = column_type_to_annotation_names.setdefault(
                new_col_type, set()
            )
            if new_col_name not in typed_col_name_set:
                typed_col_name_set.add(new_col_name)
                valid_columns.append(column)
        return valid_columns

    def _before_synapse_store(self, syn):
        # get the default EntityView columns from Synapse and add them to the columns list
        additional_columns = []
        view_type = self._synapse_entity_type.split(".")[-1].lower()
        mask = self.get("viewTypeMask")

        if self.addDefaultViewColumns:
            additional_columns.extend(
                syn._get_default_view_columns(view_type, view_type_mask=mask)
            )

        # get default annotations
        if self.addAnnotationColumns:
            anno_columns = [
                x
                for x in syn._get_annotation_view_columns(
                    self.scopeIds, view_type, view_type_mask=mask
                )
                if x["name"] not in self.ignoredAnnotationColumnNames
            ]
            additional_columns.extend(anno_columns)

        self.addColumns(self._filter_duplicate_columns(syn, additional_columns))

        # set these boolean flags to false so they are not repeated.
        self.addDefaultViewColumns = False
        self.addAnnotationColumns = False

        super(ViewBase, self)._before_synapse_store(syn)
Functions
add_scope
add_scope(entities: Union[Project, Folder, Evaluation, list, str])

Add scope

PARAMETER DESCRIPTION
entities

A Project, Folder, Evaluation object or its ID, can also be a list of them

TYPE: Union[Project, Folder, Evaluation, list, str]

Source code in synapseclient/table.py
766
767
768
769
770
771
772
773
774
775
776
777
778
779
def add_scope(self, entities: Union[Project, Folder, Evaluation, list, str]):
    """
    Add scope

    Arguments:
        entities: A [Project][synapseclient.entity.Project], [Folder][synapseclient.entity.Folder],
                  [Evaluation][synapseclient.evaluation.Evaluation] object or its ID, can also be a list of them
    """
    if isinstance(entities, list):
        # add ids to a temp list so that we don't partially modify scopeIds on an exception in id_of()
        temp_list = [id_of(entity) for entity in entities]
        self.scopeIds.extend(temp_list)
    else:
        self.scopeIds.append(id_of(entities))

Dataset

Bases: ViewBase

A Dataset is an Entity that defines a flat list of entities as a tableview (a.k.a. a "dataset").

ATTRIBUTE DESCRIPTION
name

The name for the Dataset object

description

User readable description of the schema

columns

A list of Column objects or their IDs

parent

The Synapse Project to which this Dataset belongs

properties

A map of Synapse properties

annotations

A map of user defined annotations

dataset_items

A list of items characterized by entityId and versionNumber

folder

A list of Folder IDs

local_state

Internal use only

Using Dataset

Load Dataset

from synapseclient import Dataset

Create a Dataset with pre-defined DatasetItems. Default Dataset columns are used if no schema is provided.

dataset_items = [
    {'entityId': "syn000", 'versionNumber': 1},
    {...},
]

dataset = syn.store(Dataset(
    name="My Dataset",
    parent=project,
    dataset_items=dataset_items))

Add/remove specific Synapse IDs to/from the Dataset

dataset.add_item({'entityId': "syn111", 'versionNumber': 1})
dataset.remove_item("syn000")
dataset = syn.store(dataset)

Add a list of Synapse IDs to the Dataset

new_items = [
    {'entityId': "syn222", 'versionNumber': 2},
    {'entityId': "syn333", 'versionNumber': 1}
]
dataset.add_items(new_items)
dataset = syn.store(dataset)

Folders can easily be added recursively to a dataset, that is, all files within the folder (including sub-folders) will be added. Note that using the following methods will add files with the latest version number ONLY. If another version number is desired, use add_item or add_items.

Add folder to Dataset

Add a single Folder to the Dataset.

dataset.add_folder("syn123")

Add a list of Folders, overwriting any existing files in the dataset.

dataset.add_folders(["syn456", "syn789"], force=True)
dataset = syn.store(dataset)
Truncate a Dataset

empty() can be used to truncate a dataset, that is, remove all current items from the set.

dataset.empty()
dataset = syn.store(dataset)
Check items in a Dataset

To get the number of entities in the dataset, use len().

print(f"{dataset.name} has {len(dataset)} items.")
Create a snapshot of the Dataset

To create a snapshot version of the Dataset, use create_snapshot_version.

syn = synapseclient.login()
syn.create_snapshot_version(
    dataset.id,
    label="v1.0",
    comment="This is version 1")
Source code in synapseclient/table.py
 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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
class Dataset(ViewBase):
    """
    A Dataset is an [Entity][synapseclient.entity.Entity] that defines a
    flat list of entities as a tableview (a.k.a. a "dataset").

    Attributes:
        name:          The name for the Dataset object
        description:   User readable description of the schema
        columns:       A list of [Column][synapseclient.table.Column] objects or their IDs
        parent:        The Synapse Project to which this Dataset belongs
        properties:    A map of Synapse properties
        annotations:   A map of user defined annotations
        dataset_items: A list of items characterized by entityId and versionNumber
        folder:        A list of Folder IDs
        local_state:   Internal use only

    Example: Using Dataset
        Load Dataset

            from synapseclient import Dataset

        Create a Dataset with pre-defined DatasetItems. Default Dataset columns
        are used if no schema is provided.

            dataset_items = [
                {'entityId': "syn000", 'versionNumber': 1},
                {...},
            ]

            dataset = syn.store(Dataset(
                name="My Dataset",
                parent=project,
                dataset_items=dataset_items))

        Add/remove specific Synapse IDs to/from the Dataset

            dataset.add_item({'entityId': "syn111", 'versionNumber': 1})
            dataset.remove_item("syn000")
            dataset = syn.store(dataset)

        Add a list of Synapse IDs to the Dataset

            new_items = [
                {'entityId': "syn222", 'versionNumber': 2},
                {'entityId': "syn333", 'versionNumber': 1}
            ]
            dataset.add_items(new_items)
            dataset = syn.store(dataset)

    Folders can easily be added recursively to a dataset, that is, all files
    within the folder (including sub-folders) will be added.  Note that using
    the following methods will add files with the latest version number ONLY.
    If another version number is desired, use [add_item][synapseclient.table.Dataset.add_item]
    or [add_items][synapseclient.table.Dataset.add_items].

    Example: Add folder to Dataset
        Add a single Folder to the Dataset.

            dataset.add_folder("syn123")

        Add a list of Folders, overwriting any existing files in the dataset.

            dataset.add_folders(["syn456", "syn789"], force=True)
            dataset = syn.store(dataset)

    Example: Truncate a Dataset
        empty() can be used to truncate a dataset, that is, remove all current items from the set.

            dataset.empty()
            dataset = syn.store(dataset)



    Example: Check items in a Dataset
        To get the number of entities in the dataset, use len().

            print(f"{dataset.name} has {len(dataset)} items.")

    Example: Create a snapshot of the Dataset
        To create a snapshot version of the Dataset, use
        [create_snapshot_version][synapseclient.Synapse.create_snapshot_version].

            syn = synapseclient.login()
            syn.create_snapshot_version(
                dataset.id,
                label="v1.0",
                comment="This is version 1")
    """

    _synapse_entity_type: str = "org.sagebionetworks.repo.model.table.Dataset"
    _property_keys: List[str] = ViewBase._property_keys + ["datasetItems"]
    _local_keys: List[str] = ViewBase._local_keys + ["folders_to_add", "force"]

    def __init__(
        self,
        name=None,
        columns=None,
        parent=None,
        properties=None,
        addDefaultViewColumns=True,
        addAnnotationColumns=True,
        ignoredAnnotationColumnNames=[],
        annotations=None,
        local_state=None,
        dataset_items=None,
        folders=None,
        force=False,
        **kwargs,
    ):
        self.properties.setdefault("datasetItems", [])
        self.__dict__.setdefault("folders_to_add", set())
        self.ignoredAnnotationColumnNames = set(ignoredAnnotationColumnNames)
        self.viewTypeMask = EntityViewType.DATASET.value
        super(Dataset, self).__init__(
            name=name,
            columns=columns,
            properties=properties,
            annotations=annotations,
            local_state=local_state,
            parent=parent,
            **kwargs,
        )

        self.force = force
        if dataset_items:
            self.add_items(dataset_items, force)
        if folders:
            self.add_folders(folders, force)

        # HACK: make sure we don't try to add columns to schemas that we retrieve from synapse
        is_from_normal_constructor = not (properties or local_state)
        # allowing annotations because user might want to update annotations all at once
        self.addDefaultViewColumns = (
            addDefaultViewColumns and is_from_normal_constructor
        )
        self.addAnnotationColumns = addAnnotationColumns and is_from_normal_constructor

    def __len__(self):
        return len(self.properties.datasetItems)

    @staticmethod
    def _check_needed_keys(keys: List[str]):
        required_keys = {"entityId", "versionNumber"}
        if required_keys - keys:
            raise LookupError(
                "DatasetItem missing a required property: %s"
                % str(required_keys - keys)
            )
        return True

    def add_item(self, dataset_item: Dict[str, str], force: bool = True):
        """
        Add a dataset item

        Arguments:
            dataset_item: A single dataset item
            force:        Force add item

        Raises:
            ValueError: If duplicate item is found
            ValueError: The item is not a DatasetItem
        """
        if isinstance(dataset_item, dict) and self._check_needed_keys(
            dataset_item.keys()
        ):
            if not self.has_item(dataset_item.get("entityId")):
                self.properties.datasetItems.append(dataset_item)
            else:
                if force:
                    self.remove_item(dataset_item.get("entityId"))
                    self.properties.datasetItems.append(dataset_item)
                else:
                    raise ValueError(
                        f"Duplicate item found: {dataset_item.get('entityId')}. "
                        "Set force=True to overwrite the existing item."
                    )
        else:
            raise ValueError("Not a DatasetItem? %s" % str(dataset_item))

    def add_items(self, dataset_items: List[Dict[str, str]], force: bool = True):
        """
        Add items

        Arguments:
            dataset_items: A list of dataset items
            force:         Force add items
        """
        for dataset_item in dataset_items:
            self.add_item(dataset_item, force)

    def remove_item(self, item_id: str):
        """
        Remove item

        Arguments:
            item_id: A single dataset item Synapse ID
        """
        item_id = id_of(item_id)
        if item_id.startswith("syn"):
            for i, curr_item in enumerate(self.properties.datasetItems):
                if curr_item.get("entityId") == item_id:
                    del self.properties.datasetItems[i]
                    break
        else:
            raise ValueError("Not a Synapse ID: %s" % str(item_id))

    def empty(self):
        self.properties.datasetItems = []

    def has_item(self, item_id: str) -> bool:
        """
        Check if has dataset item

        Arguments:
            item_id: A single dataset item Synapse ID
        """
        return any(item["entityId"] == item_id for item in self.properties.datasetItems)

    def add_folder(self, folder: str, force: bool = True):
        """
        Add a folder

        Arguments:
            folder: A single Synapse Folder ID
            force:  Force add items from folder
        """
        if not self.__dict__.get("folders_to_add", None):
            self.__dict__["folders_to_add"] = set()
        self.__dict__["folders_to_add"].add(folder)
        # if self.force != force:
        self.force = force

    def add_folders(self, folders: List[str], force: bool = True):
        """
        Add folders

        Arguments:
            folders: A list of Synapse Folder IDs
            force:   Force add items from folders
        """
        if (
            isinstance(folders, list)
            or isinstance(folders, set)
            or isinstance(folders, tuple)
        ):
            self.force = force
            for folder in folders:
                self.add_folder(folder, force)
        else:
            raise ValueError(f"Not a list of Folder IDs: {folders}")

    def _add_folder_files(self, syn, folder):
        files = []
        children = syn.getChildren(folder)
        for child in children:
            if child.get("type") == "org.sagebionetworks.repo.model.Folder":
                files.extend(self._add_folder_files(syn, child.get("id")))
            elif child.get("type") == "org.sagebionetworks.repo.model.FileEntity":
                files.append(
                    {
                        "entityId": child.get("id"),
                        "versionNumber": child.get("versionNumber"),
                    }
                )
            else:
                raise ValueError(f"Not a Folder?: {folder}")
        return files

    def _before_synapse_store(self, syn):
        # Add files from folders (if any) before storing dataset.
        if self.folders_to_add:
            for folder in self.folders_to_add:
                items_to_add = self._add_folder_files(syn, folder)
                self.add_items(items_to_add, self.force)
            self.folders_to_add = set()
        # Must set this scopeIds is used to get all annotations from the
        # entities
        self.scopeIds = [item["entityId"] for item in self.properties.datasetItems]
        super()._before_synapse_store(syn)
        # Reset attribute to force-add items from folders.
        self.force = True
        # Remap `datasetItems` back to `items` before storing (since `items`
        # is the accepted field name in the API, not `datasetItems`).
        self.properties.items = self.properties.datasetItems
Functions
add_item
add_item(dataset_item: Dict[str, str], force: bool = True)

Add a dataset item

PARAMETER DESCRIPTION
dataset_item

A single dataset item

TYPE: Dict[str, str]

force

Force add item

TYPE: bool DEFAULT: True

RAISES DESCRIPTION
ValueError

If duplicate item is found

ValueError

The item is not a DatasetItem

Source code in synapseclient/table.py
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
def add_item(self, dataset_item: Dict[str, str], force: bool = True):
    """
    Add a dataset item

    Arguments:
        dataset_item: A single dataset item
        force:        Force add item

    Raises:
        ValueError: If duplicate item is found
        ValueError: The item is not a DatasetItem
    """
    if isinstance(dataset_item, dict) and self._check_needed_keys(
        dataset_item.keys()
    ):
        if not self.has_item(dataset_item.get("entityId")):
            self.properties.datasetItems.append(dataset_item)
        else:
            if force:
                self.remove_item(dataset_item.get("entityId"))
                self.properties.datasetItems.append(dataset_item)
            else:
                raise ValueError(
                    f"Duplicate item found: {dataset_item.get('entityId')}. "
                    "Set force=True to overwrite the existing item."
                )
    else:
        raise ValueError("Not a DatasetItem? %s" % str(dataset_item))
add_items
add_items(dataset_items: List[Dict[str, str]], force: bool = True)

Add items

PARAMETER DESCRIPTION
dataset_items

A list of dataset items

TYPE: List[Dict[str, str]]

force

Force add items

TYPE: bool DEFAULT: True

Source code in synapseclient/table.py
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
def add_items(self, dataset_items: List[Dict[str, str]], force: bool = True):
    """
    Add items

    Arguments:
        dataset_items: A list of dataset items
        force:         Force add items
    """
    for dataset_item in dataset_items:
        self.add_item(dataset_item, force)
remove_item
remove_item(item_id: str)

Remove item

PARAMETER DESCRIPTION
item_id

A single dataset item Synapse ID

TYPE: str

Source code in synapseclient/table.py
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
def remove_item(self, item_id: str):
    """
    Remove item

    Arguments:
        item_id: A single dataset item Synapse ID
    """
    item_id = id_of(item_id)
    if item_id.startswith("syn"):
        for i, curr_item in enumerate(self.properties.datasetItems):
            if curr_item.get("entityId") == item_id:
                del self.properties.datasetItems[i]
                break
    else:
        raise ValueError("Not a Synapse ID: %s" % str(item_id))
has_item
has_item(item_id: str) -> bool

Check if has dataset item

PARAMETER DESCRIPTION
item_id

A single dataset item Synapse ID

TYPE: str

Source code in synapseclient/table.py
1070
1071
1072
1073
1074
1075
1076
1077
def has_item(self, item_id: str) -> bool:
    """
    Check if has dataset item

    Arguments:
        item_id: A single dataset item Synapse ID
    """
    return any(item["entityId"] == item_id for item in self.properties.datasetItems)
add_folder
add_folder(folder: str, force: bool = True)

Add a folder

PARAMETER DESCRIPTION
folder

A single Synapse Folder ID

TYPE: str

force

Force add items from folder

TYPE: bool DEFAULT: True

Source code in synapseclient/table.py
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
def add_folder(self, folder: str, force: bool = True):
    """
    Add a folder

    Arguments:
        folder: A single Synapse Folder ID
        force:  Force add items from folder
    """
    if not self.__dict__.get("folders_to_add", None):
        self.__dict__["folders_to_add"] = set()
    self.__dict__["folders_to_add"].add(folder)
    # if self.force != force:
    self.force = force
add_folders
add_folders(folders: List[str], force: bool = True)

Add folders

PARAMETER DESCRIPTION
folders

A list of Synapse Folder IDs

TYPE: List[str]

force

Force add items from folders

TYPE: bool DEFAULT: True

Source code in synapseclient/table.py
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
def add_folders(self, folders: List[str], force: bool = True):
    """
    Add folders

    Arguments:
        folders: A list of Synapse Folder IDs
        force:   Force add items from folders
    """
    if (
        isinstance(folders, list)
        or isinstance(folders, set)
        or isinstance(folders, tuple)
    ):
        self.force = force
        for folder in folders:
            self.add_folder(folder, force)
    else:
        raise ValueError(f"Not a list of Folder IDs: {folders}")

EntityViewSchema

Bases: ViewBase

A EntityViewSchema is a Entity that displays all files/projects (depending on user choice) within a given set of scopes.

ATTRIBUTE DESCRIPTION
name

The name of the Entity View Table object

columns

(Optional) A list of Column objects or their IDs.

parent

The project in Synapse to which this table belongs

scopes

A list of Projects/Folders or their ids

type

This field is deprecated. Please use includeEntityTypes

includeEntityTypes

A list of entity types to include in the view. Supported entity types are:

  • EntityViewType.FILE
  • EntityViewType.PROJECT
  • EntityViewType.TABLE
  • EntityViewType.FOLDER
  • EntityViewType.VIEW
  • EntityViewType.DOCKER

If none is provided, the view will default to include EntityViewType.FILE.

addDefaultViewColumns

If true, adds all default columns (e.g. name, createdOn, modifiedBy etc.) Defaults to True. The default columns will be added after a call to store.

addAnnotationColumns

If true, adds columns for all annotation keys defined across all Entities in the EntityViewSchema's scope. Defaults to True. The annotation columns will be added after a call to store.

ignoredAnnotationColumnNames

A list of strings representing annotation names. When addAnnotationColumns is True, the names in this list will not be automatically added as columns to the EntityViewSchema if they exist in any of the defined scopes.

properties

A map of Synapse properties

annotations

A map of user defined annotations

local_state

Internal use only

Example:

from synapseclient import EntityViewType

project_or_folder = syn.get("syn123")
schema = syn.store(EntityViewSchema(name='MyTable', parent=project, scopes=[project_or_folder_id, 'syn123'],
 includeEntityTypes=[EntityViewType.FILE]))
Source code in synapseclient/table.py
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
class EntityViewSchema(ViewBase):
    """
    A EntityViewSchema is a [Entity][synapseclient.entity.Entity] that displays all files/projects
    (depending on user choice) within a given set of scopes.

    Attributes:
        name:                         The name of the Entity View Table object
        columns:                      (Optional) A list of [Column][synapseclient.table.Column] objects or their IDs.
        parent:                       The project in Synapse to which this table belongs
        scopes:                       A list of Projects/Folders or their ids
        type:                         This field is deprecated. Please use `includeEntityTypes`
        includeEntityTypes:           A list of entity types to include in the view. Supported entity types are:

            - `EntityViewType.FILE`
            - `EntityViewType.PROJECT`
            - `EntityViewType.TABLE`
            - `EntityViewType.FOLDER`
            - `EntityViewType.VIEW`
            - `EntityViewType.DOCKER`

            If none is provided, the view will default to include `EntityViewType.FILE`.
        addDefaultViewColumns:        If true, adds all default columns (e.g. name, createdOn, modifiedBy etc.)
                                      Defaults to True.
                                      The default columns will be added after a call to
                                      [store][synapseclient.Synapse.store].
        addAnnotationColumns:         If true, adds columns for all annotation keys defined across all Entities in
                                      the EntityViewSchema's scope. Defaults to True.
                                      The annotation columns will be added after a call to
                                      [store][synapseclient.Synapse.store].
        ignoredAnnotationColumnNames: A list of strings representing annotation names.
                                      When addAnnotationColumns is True, the names in this list will not be
                                      automatically added as columns to the EntityViewSchema if they exist in any
                                      of the defined scopes.
        properties:                   A map of Synapse properties
        annotations:                  A map of user defined annotations
        local_state:                  Internal use only

    Example:

        from synapseclient import EntityViewType

        project_or_folder = syn.get("syn123")
        schema = syn.store(EntityViewSchema(name='MyTable', parent=project, scopes=[project_or_folder_id, 'syn123'],
         includeEntityTypes=[EntityViewType.FILE]))
    """

    _synapse_entity_type = "org.sagebionetworks.repo.model.table.EntityView"

    def __init__(
        self,
        name=None,
        columns=None,
        parent=None,
        scopes=None,
        type=None,
        includeEntityTypes=None,
        addDefaultViewColumns=True,
        addAnnotationColumns=True,
        ignoredAnnotationColumnNames=[],
        properties=None,
        annotations=None,
        local_state=None,
        **kwargs,
    ):
        if includeEntityTypes:
            kwargs["viewTypeMask"] = _get_view_type_mask(includeEntityTypes)
        elif type:
            kwargs["viewTypeMask"] = _get_view_type_mask_for_deprecated_type(type)
        elif properties and "type" in properties:
            kwargs["viewTypeMask"] = _get_view_type_mask_for_deprecated_type(
                properties["type"]
            )
            properties["type"] = None

        self.ignoredAnnotationColumnNames = set(ignoredAnnotationColumnNames)
        super(EntityViewSchema, self).__init__(
            name=name,
            columns=columns,
            properties=properties,
            annotations=annotations,
            local_state=local_state,
            parent=parent,
            **kwargs,
        )

        # This is a hacky solution to make sure we don't try to add columns to schemas that we retrieve from synapse
        is_from_normal_constructor = not (properties or local_state)
        # allowing annotations because user might want to update annotations all at once
        self.addDefaultViewColumns = (
            addDefaultViewColumns and is_from_normal_constructor
        )
        self.addAnnotationColumns = addAnnotationColumns and is_from_normal_constructor

        # set default values after constructor so we don't overwrite the values defined in properties using .get()
        # because properties, unlike local_state, do not have nonexistent keys assigned with a value of None
        if self.get("viewTypeMask") is None:
            self.viewTypeMask = EntityViewType.FILE.value
        if self.get("scopeIds") is None:
            self.scopeIds = []

        # add the scopes last so that we can append the passed in scopes to those defined in properties
        if scopes is not None:
            self.add_scope(scopes)

    def set_entity_types(self, includeEntityTypes):
        """
        Set entity types

        Arguments:
            includeEntityTypes: A list of entity types to include in the view. This list will replace the previous
                                settings. Supported entity types are:

                - `EntityViewType.FILE`
                - `EntityViewType.PROJECT`
                - `EntityViewType.TABLE`
                - `EntityViewType.FOLDER`
                - `EntityViewType.VIEW`
                - `EntityViewType.DOCKER`
        """
        self.viewTypeMask = _get_view_type_mask(includeEntityTypes)
Functions
set_entity_types
set_entity_types(includeEntityTypes)

Set entity types

PARAMETER DESCRIPTION
includeEntityTypes

A list of entity types to include in the view. This list will replace the previous settings. Supported entity types are:

  • EntityViewType.FILE
  • EntityViewType.PROJECT
  • EntityViewType.TABLE
  • EntityViewType.FOLDER
  • EntityViewType.VIEW
  • EntityViewType.DOCKER

Source code in synapseclient/table.py
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
def set_entity_types(self, includeEntityTypes):
    """
    Set entity types

    Arguments:
        includeEntityTypes: A list of entity types to include in the view. This list will replace the previous
                            settings. Supported entity types are:

            - `EntityViewType.FILE`
            - `EntityViewType.PROJECT`
            - `EntityViewType.TABLE`
            - `EntityViewType.FOLDER`
            - `EntityViewType.VIEW`
            - `EntityViewType.DOCKER`
    """
    self.viewTypeMask = _get_view_type_mask(includeEntityTypes)

SubmissionViewSchema

Bases: ViewBase

A SubmissionViewSchema is a Entity that displays all files/projects (depending on user choice) within a given set of scopes.

ATTRIBUTE DESCRIPTION
name

The name of the Entity View Table object

columns

A list of Column objects or their IDs. These are optional.

parent

The project in Synapse to which this table belongs

scopes

A list of Evaluation Queues or their ids

addDefaultViewColumns

If true, adds all default columns (e.g. name, createdOn, modifiedBy etc.) Defaults to True. The default columns will be added after a call to store.

addAnnotationColumns

If true, adds columns for all annotation keys defined across all Entities in the SubmissionViewSchema's scope. Defaults to True. The annotation columns will be added after a call to store.

ignoredAnnotationColumnNames

A list of strings representing annotation names. When addAnnotationColumns is True, the names in this list will not be automatically added as columns to the SubmissionViewSchema if they exist in any of the defined scopes.

properties

A map of Synapse properties

annotations

A map of user defined annotations

local_state

Internal use only

Example

from synapseclient import SubmissionViewSchema

project = syn.get("syn123") schema = syn.store(SubmissionViewSchema(name='My Submission View', parent=project, scopes=['9614543']))

Source code in synapseclient/table.py
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
class SubmissionViewSchema(ViewBase):
    """
    A SubmissionViewSchema is a [Entity][synapseclient.entity.Entity] that displays all files/projects
    (depending on user choice) within a given set of scopes.

    Attributes:
        name:                         The name of the Entity View Table object
        columns:                      A list of [Column][synapseclient.table.Column] objects or their IDs. These are optional.
        parent:                       The project in Synapse to which this table belongs
        scopes:                       A list of Evaluation Queues or their ids
        addDefaultViewColumns:        If true, adds all default columns (e.g. name, createdOn, modifiedBy etc.)
                                      Defaults to True.
                                      The default columns will be added after a call to
                                      [store][synapseclient.Synapse.store].
        addAnnotationColumns:         If true, adds columns for all annotation keys defined across all Entities in
                                      the SubmissionViewSchema's scope. Defaults to True.
                                      The annotation columns will be added after a call to
                                      [store][synapseclient.Synapse.store].
        ignoredAnnotationColumnNames: A list of strings representing annotation names.
                                      When addAnnotationColumns is True, the names in this list will not be
                                      automatically added as columns to the SubmissionViewSchema if they exist in
                                      any of the defined scopes.
        properties:                   A map of Synapse properties
        annotations:                  A map of user defined annotations
        local_state:                  Internal use only

    Example:
        from synapseclient import SubmissionViewSchema

        project = syn.get("syn123")
        schema = syn.store(SubmissionViewSchema(name='My Submission View', parent=project, scopes=['9614543']))
    """

    _synapse_entity_type = "org.sagebionetworks.repo.model.table.SubmissionView"

    def __init__(
        self,
        name=None,
        columns=None,
        parent=None,
        scopes=None,
        addDefaultViewColumns=True,
        addAnnotationColumns=True,
        ignoredAnnotationColumnNames=[],
        properties=None,
        annotations=None,
        local_state=None,
        **kwargs,
    ):
        self.ignoredAnnotationColumnNames = set(ignoredAnnotationColumnNames)
        super(SubmissionViewSchema, self).__init__(
            name=name,
            columns=columns,
            properties=properties,
            annotations=annotations,
            local_state=local_state,
            parent=parent,
            **kwargs,
        )
        # This is a hacky solution to make sure we don't try to add columns to schemas that we retrieve from synapse
        is_from_normal_constructor = not (properties or local_state)
        # allowing annotations because user might want to update annotations all at once
        self.addDefaultViewColumns = (
            addDefaultViewColumns and is_from_normal_constructor
        )
        self.addAnnotationColumns = addAnnotationColumns and is_from_normal_constructor

        if self.get("scopeIds") is None:
            self.scopeIds = []

        # add the scopes last so that we can append the passed in scopes to those defined in properties
        if scopes is not None:
            self.add_scope(scopes)

SelectColumn

Bases: DictObject

Defines a column to be used in a table Schema.

ATTRIBUTE DESCRIPTION
id

An immutable ID issued by the platform

columnType

Can be any of:

  • STRING
  • DOUBLE
  • INTEGER
  • BOOLEAN
  • DATE
  • FILEHANDLEID
  • ENTITYID

name

The display name of the column

Source code in synapseclient/table.py
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
class SelectColumn(DictObject):
    """
    Defines a column to be used in a table [Schema][synapseclient.table.Schema].

    Attributes:
        id:         An immutable ID issued by the platform
        columnType: Can be any of:

            - `STRING`
            - `DOUBLE`
            - `INTEGER`
            - `BOOLEAN`
            - `DATE`
            - `FILEHANDLEID`
            - `ENTITYID`

        name:       The display name of the column
    """

    def __init__(self, id=None, columnType=None, name=None, **kwargs):
        super(SelectColumn, self).__init__()
        if id:
            self.id = id

        if name:
            self.name = name

        if columnType:
            self.columnType = columnType

        # Notes that this param is only used to support forward compatibility.
        self.update(kwargs)

    @classmethod
    def from_column(cls, column):
        return cls(
            column.get("id", None),
            column.get("columnType", None),
            column.get("name", None),
        )

Column

Bases: DictObject

Defines a column to be used in a table Schema EntityViewSchema.

ATTRIBUTE DESCRIPTION
id

An immutable ID issued by the platform

columnType

The column type determines the type of data that can be stored in a column. It can be any of:

  • STRING
  • DOUBLE
  • INTEGER
  • BOOLEAN
  • DATE
  • FILEHANDLEID
  • ENTITYID
  • LINK
  • LARGETEXT
  • USERID

For more information, please see: https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/table/ColumnType.html

maximumSize

A parameter for columnTypes with a maximum size. For example, ColumnType.STRINGs have a default maximum size of 50 characters, but can be set to a maximumSize of 1 to 1000 characters.

maximumListLength

Required if using a columnType with a "_LIST" suffix. Describes the maximum number of values that will appear in that list. Value range 1-100 inclusive. Default 100

name

The display name of the column

enumValues

Columns type of STRING can be constrained to an enumeration values set on this list.

defaultValue

The default value for this column. Columns of type FILEHANDLEID and ENTITYID are not allowed to have default values.

Source code in synapseclient/table.py
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
class Column(DictObject):
    """
    Defines a column to be used in a table [Schema][synapseclient.table.Schema]
    [EntityViewSchema][synapseclient.table.EntityViewSchema].

    Attributes:
        id:                An immutable ID issued by the platform
        columnType:        The column type determines the type of data that can be stored in a column. It can be any
                           of:

            - `STRING`
            - `DOUBLE`
            - `INTEGER`
            - `BOOLEAN`
            - `DATE`
            - `FILEHANDLEID`
            - `ENTITYID`
            - `LINK`
            - `LARGETEXT`
            - `USERID`

            For more information, please see:
            <https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/table/ColumnType.html>
        maximumSize:       A parameter for columnTypes with a maximum size. For example, ColumnType.STRINGs have a
                           default maximum size of 50 characters, but can be set to a `maximumSize` of 1 to 1000
                           characters.
        maximumListLength: Required if using a columnType with a "_LIST" suffix. Describes the maximum number of
                           values that will appear in that list. Value range 1-100 inclusive. Default 100
        name:              The display name of the column
        enumValues:        Columns type of STRING can be constrained to an enumeration values set on this list.
        defaultValue:      The default value for this column. Columns of type FILEHANDLEID and ENTITYID are not
                           allowed to have default values.
    """

    @classmethod
    def getURI(cls, id):
        return "/column/%s" % id

    def __init__(self, **kwargs):
        super(Column, self).__init__(kwargs)
        self["concreteType"] = concrete_types.COLUMN_MODEL

    def postURI(self):
        return "/column"

AppendableRowset

Bases: DictObject

Abstract Base Class for RowSet and PartialRowset

Source code in synapseclient/table.py
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
class AppendableRowset(DictObject, metaclass=abc.ABCMeta):
    """
    Abstract Base Class for [RowSet][synapseclient.table.RowSet] and [PartialRowset][synapseclient.table.PartialRowset]
    """

    @abc.abstractmethod
    def __init__(self, schema, **kwargs):
        if ("tableId" not in kwargs) and schema:
            kwargs["tableId"] = id_of(schema)

        if not kwargs.get("tableId", None):
            raise ValueError(
                "Table schema ID must be defined to create a %s" % type(self).__name__
            )
        super(AppendableRowset, self).__init__(kwargs)

    def _synapse_store(self, syn):
        """
        Creates and POSTs an [AppendableRowSetRequest](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/table/AppendableRowSetRequest.html)
        """
        append_rowset_request = {
            "concreteType": concrete_types.APPENDABLE_ROWSET_REQUEST,
            "toAppend": self,
            "entityId": self.tableId,
        }

        response = syn._async_table_update(
            self.tableId, [append_rowset_request], wait=True
        )
        syn._check_table_transaction_response(response)
        return response["results"][0]

PartialRowset

Bases: AppendableRowset

A set of Partial Rows used for updating cells of a table. PartialRowsets allow you to push only the individual cells you wish to change instead of pushing entire rows with many unchanged cells.

ATTRIBUTE DESCRIPTION
schema

The Schema of the table to update or its tableId as a string

rows

A list of PartialRows

Update cells in

The following code will change cells in a hypothetical table, syn123: these same steps will also work for using EntityView tables to change Entity annotations

From:

    +-------------+--------------+
    | Column One  | Column Two   |
    +=============+==============+
    | Data 1      | Data A       |
    +-------------+--------------+
    | Data 2      | Data B       |
    +-------------+--------------+
    | Data 3      | Data C       |
    +-------------+--------------+

To

    +-------------+--------------+
    | Column One  | Column Two   |
    +=============+==============+
    | Data 1  2   | Data A       |
    +-------------+--------------+
    | Data 2      | Data B  D    |
    +-------------+--------------+
    | Data 3      | Data C       |
    +-------------+--------------+

query_results = syn.tableQuery("SELECT * FROM syn123")

The easiest way to know the rowId of the row you wish to change is by converting the table to a pandas DataFrame with rowIdAndVersionInIndex=False

df = query_results.asDataFrame(rowIdAndVersionInIndex=False)

partial_changes = {df['ROW_ID'][0]: {'fooCol': 'foo foo 1'},
                   df['ROW_ID'][1]: {'barCol': 'bar bar 2'}}

You will need to pass in your original query result as an argument so that we can perform column id translation and etag retrieval on your behalf:

partial_rowset = PartialRowset.from_mapping(partial_changes, query_results)
syn.store(partial_rowset)
Source code in synapseclient/table.py
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
class PartialRowset(AppendableRowset):
    """
    A set of Partial Rows used for updating cells of a table.
    PartialRowsets allow you to push only the individual cells you wish to change instead of pushing entire rows with
    many unchanged cells.

    Attributes:
        schema: The [Schema][synapseclient.table.Schema] of the table to update or its tableId as a string
        rows:   A list of PartialRows

    Example: Update cells in
        The following code will change cells in a hypothetical table, syn123:
        these same steps will also work for using EntityView tables to change Entity annotations

        From:

                +-------------+--------------+
                | Column One  | Column Two   |
                +=============+==============+
                | Data 1      | Data A       |
                +-------------+--------------+
                | Data 2      | Data B       |
                +-------------+--------------+
                | Data 3      | Data C       |
                +-------------+--------------+

        To

                +-------------+--------------+
                | Column One  | Column Two   |
                +=============+==============+
                | Data 1  2   | Data A       |
                +-------------+--------------+
                | Data 2      | Data B  D    |
                +-------------+--------------+
                | Data 3      | Data C       |
                +-------------+--------------+

            query_results = syn.tableQuery("SELECT * FROM syn123")

        The easiest way to know the rowId of the row you wish to change
        is by converting the table to a pandas DataFrame with rowIdAndVersionInIndex=False

            df = query_results.asDataFrame(rowIdAndVersionInIndex=False)

            partial_changes = {df['ROW_ID'][0]: {'fooCol': 'foo foo 1'},
                               df['ROW_ID'][1]: {'barCol': 'bar bar 2'}}

        You will need to pass in your original query result as an argument
        so that we can perform column id translation and etag retrieval on your behalf:

            partial_rowset = PartialRowset.from_mapping(partial_changes, query_results)
            syn.store(partial_rowset)
    """

    @classmethod
    def from_mapping(cls, mapping, originalQueryResult):
        """
        Creates a PartialRowset

        Arguments:
            mapping:             A mapping of mappings in the structure: {ROW_ID : {COLUMN_NAME: NEW_COL_VALUE}}
            originalQueryResult: The original query result

        Returns:
            A PartialRowset that can be syn.store()-ed to apply the changes
        """
        if not isinstance(mapping, collections.abc.Mapping):
            raise ValueError("mapping must be a supported Mapping type such as 'dict'")

        try:
            name_to_column_id = {
                col.name: col.id for col in originalQueryResult.headers if "id" in col
            }
        except AttributeError:
            raise ValueError(
                "originalQueryResult must be the result of a syn.tableQuery()"
            )

        row_ids = set(int(id) for id in mapping.keys())

        # row_ids in the originalQueryResult are not guaranteed to be in ascending order
        # iterate over all etags but only map the row_ids used for this partial update to their etags
        row_etags = {
            row_id: etag
            for row_id, row_version, etag in originalQueryResult.iter_row_metadata()
            if row_id in row_ids and etag is not None
        }

        partial_rows = [
            PartialRow(
                row_changes,
                row_id,
                etag=row_etags.get(int(row_id)),
                nameToColumnId=name_to_column_id,
            )
            for row_id, row_changes in mapping.items()
        ]

        return cls(originalQueryResult.tableId, partial_rows)

    def __init__(self, schema, rows):
        super(PartialRowset, self).__init__(schema)
        self.concreteType = concrete_types.PARTIAL_ROW_SET

        if isinstance(rows, PartialRow):
            self.rows = [rows]
        else:
            try:
                if all(isinstance(row, PartialRow) for row in rows):
                    self.rows = list(rows)
                else:
                    raise ValueError("rows must contain only values of type PartialRow")
            except TypeError:
                raise ValueError("rows must be iterable")
Functions
from_mapping classmethod
from_mapping(mapping, originalQueryResult)

Creates a PartialRowset

PARAMETER DESCRIPTION
mapping

A mapping of mappings in the structure: {ROW_ID : {COLUMN_NAME: NEW_COL_VALUE}}

originalQueryResult

The original query result

RETURNS DESCRIPTION

A PartialRowset that can be syn.store()-ed to apply the changes

Source code in synapseclient/table.py
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
@classmethod
def from_mapping(cls, mapping, originalQueryResult):
    """
    Creates a PartialRowset

    Arguments:
        mapping:             A mapping of mappings in the structure: {ROW_ID : {COLUMN_NAME: NEW_COL_VALUE}}
        originalQueryResult: The original query result

    Returns:
        A PartialRowset that can be syn.store()-ed to apply the changes
    """
    if not isinstance(mapping, collections.abc.Mapping):
        raise ValueError("mapping must be a supported Mapping type such as 'dict'")

    try:
        name_to_column_id = {
            col.name: col.id for col in originalQueryResult.headers if "id" in col
        }
    except AttributeError:
        raise ValueError(
            "originalQueryResult must be the result of a syn.tableQuery()"
        )

    row_ids = set(int(id) for id in mapping.keys())

    # row_ids in the originalQueryResult are not guaranteed to be in ascending order
    # iterate over all etags but only map the row_ids used for this partial update to their etags
    row_etags = {
        row_id: etag
        for row_id, row_version, etag in originalQueryResult.iter_row_metadata()
        if row_id in row_ids and etag is not None
    }

    partial_rows = [
        PartialRow(
            row_changes,
            row_id,
            etag=row_etags.get(int(row_id)),
            nameToColumnId=name_to_column_id,
        )
        for row_id, row_changes in mapping.items()
    ]

    return cls(originalQueryResult.tableId, partial_rows)

RowSet

Bases: AppendableRowset

A Synapse object of type org.sagebionetworks.repo.model.table.RowSet.

ATTRIBUTE DESCRIPTION
schema

A Schema object that will be used to set the tableId

headers

The list of SelectColumn objects that describe the fields in each row.

columns

An alternative to 'headers', a list of column objects that describe the fields in each row.

tableId

The ID of the TableEntity that owns these rows

rows

The Row s of this set. The index of each row value aligns with the index of each header.

etag

Any RowSet returned from Synapse will contain the current etag of the change set. To update any rows from a RowSet the etag must be provided with the POST.

Source code in synapseclient/table.py
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
class RowSet(AppendableRowset):
    """
    A Synapse object of type [org.sagebionetworks.repo.model.table.RowSet](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/table/RowSet.html).

    Attributes:
        schema:  A [Schema][synapseclient.table.Schema] object that will be used to set the tableId
        headers: The list of SelectColumn objects that describe the fields in each row.
        columns: An alternative to 'headers', a list of column objects that describe the fields in each row.
        tableId: The ID of the TableEntity that owns these rows
        rows:    The [Row][synapseclient.table.Row] s of this set. The index of each row value aligns with the
                 index of each header.
        etag:    Any RowSet returned from Synapse will contain the current etag of the change set. To update any
                 rows from a RowSet the etag must be provided with the POST.
    """

    @classmethod
    def from_json(cls, json):
        headers = [SelectColumn(**header) for header in json.get("headers", [])]
        rows = [cast_row(Row(**row), headers) for row in json.get("rows", [])]
        return cls(
            headers=headers,
            rows=rows,
            **{key: json[key] for key in json.keys() if key not in ["headers", "rows"]},
        )

    def __init__(self, columns=None, schema=None, **kwargs):
        if "headers" not in kwargs:
            if columns and schema:
                raise ValueError(
                    "Please only user either 'columns' or 'schema' as an argument but not both."
                )
            if columns:
                kwargs.setdefault("headers", []).extend(
                    [SelectColumn.from_column(column) for column in columns]
                )
            elif schema and isinstance(schema, Schema):
                kwargs.setdefault("headers", []).extend(
                    [SelectColumn(id=id) for id in schema["columnIds"]]
                )

        if not kwargs.get("headers", None):
            raise ValueError("Column headers must be defined to create a RowSet")
        kwargs["concreteType"] = "org.sagebionetworks.repo.model.table.RowSet"

        super(RowSet, self).__init__(schema, **kwargs)

    def _synapse_store(self, syn):
        response = super(RowSet, self)._synapse_store(syn)
        return response.get("rowReferenceSet", response)

    def _synapse_delete(self, syn):
        """
        Delete the rows in the RowSet.
        Example:
            syn.delete(syn.tableQuery('select name from %s where no_good = true' % schema1.id))
        """
        row_id_vers_generator = ((row.rowId, row.versionNumber) for row in self.rows)
        _delete_rows(syn, self.tableId, row_id_vers_generator)

Row

Bases: DictObject

A row in a Table.

ATTRIBUTE DESCRIPTION
values

A list of values

rowId

The immutable ID issued to a new row

versionNumber

The version number of this row. Each row version is immutable, so when a row is updated a new version is created.

Source code in synapseclient/table.py
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
class Row(DictObject):
    """
    A [row](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/table/Row.html) in a Table.

    Attributes:
        values:        A list of values
        rowId:         The immutable ID issued to a new row
        versionNumber: The version number of this row. Each row version is immutable, so when a row is updated a
                       new version is created.
    """

    def __init__(self, values, rowId=None, versionNumber=None, etag=None, **kwargs):
        super(Row, self).__init__()
        self.values = values
        if rowId is not None:
            self.rowId = rowId
        if versionNumber is not None:
            self.versionNumber = versionNumber
        if etag is not None:
            self.etag = etag

        # Notes that this param is only used to support forward compatibility.
        self.update(kwargs)

PartialRow

Bases: DictObject

This is a lower-level class for use in PartialRowset to update individual cells within a table.

ATTRIBUTE DESCRIPTION
values

A Mapping where:

  • The key is name of the column (or its columnId) to change in the desired row
  • The value is the new desired value for that column

rowId

The id of the row to be updated

etag

Used for updating File/Project Views(EntityViewSchema). Not necessary for a Schema Table

nameToColumnId

Optional map column names to column Ids. If this is provided, the keys of your values Mapping will be replaced with the column ids in the nameToColumnId dict. Include this as an argument when you are providing the column names instead of columnIds as the keys to the values Mapping.

Using PartialRow

It is recommended you use from_mapping to construct partial change sets to a table.

If you want to do the tedious parts yourself:

To change cells in the "foo"(colId:1234) and "bar"(colId:456) columns of a row with rowId = 5 Pass in with columnIds as key:

PartialRow({123: 'fooVal', 456:'barVal'}, rowId)

Pass in with a nameToColumnId argument. You can either manually define:

nameToColumnId = {'foo':123, 'bar':456}

OR if you have the result of a tableQuery() you can generate nameToColumnId using:

query_result = syn.tableQuery("SELECT * FROM syn123")
nameToColumnId = {col.name:col.id for col in query_result.headers}

PartialRow({'foo': 'fooVal', 'bar':'barVal'}, rowId, nameToColumnId=nameToColumnId)
Source code in synapseclient/table.py
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
class PartialRow(DictObject):
    """
    This is a lower-level class for use in [PartialRowset][synapseclient.table.PartialRowset] to update individual
    cells within a table.

    Attributes:
        values:         A Mapping where:

            - The key is name of the column (or its columnId) to change in the desired row
            - The value is the new desired value for that column

        rowId:          The id of the row to be updated
        etag:           Used for updating File/Project Views([EntityViewSchema][synapseclient.table.EntityViewSchema]).
                        Not necessary for a [Schema][synapseclient.table.Schema] Table
        nameToColumnId: Optional map column names to column Ids. If this is provided, the keys of your `values`
                        Mapping will be replaced with the column ids in the `nameToColumnId` dict. Include this
                        as an argument when you are providing the column names instead of columnIds as the keys
                        to the `values` Mapping.

    Example: Using PartialRow
        It is recommended you use [from_mapping][synapseclient.table.PartialRowset.from_mapping]
        to construct partial change sets to a table.

        If you want to do the tedious parts yourself:

        To change cells in the "foo"(colId:1234) and "bar"(colId:456) columns of a row with `rowId = 5`
        Pass in with `columnIds` as key:

            PartialRow({123: 'fooVal', 456:'barVal'}, rowId)

        Pass in with a `nameToColumnId` argument. You can either manually define:

            nameToColumnId = {'foo':123, 'bar':456}

        OR if you have the result of a `tableQuery()` you can generate `nameToColumnId` using:

            query_result = syn.tableQuery("SELECT * FROM syn123")
            nameToColumnId = {col.name:col.id for col in query_result.headers}

            PartialRow({'foo': 'fooVal', 'bar':'barVal'}, rowId, nameToColumnId=nameToColumnId)
    """

    def __init__(self, values, rowId, etag=None, nameToColumnId=None):
        super(PartialRow, self).__init__()
        if not isinstance(values, collections.abc.Mapping):
            raise ValueError("values must be a Mapping")

        rowId = int(rowId)

        self.values = [
            {
                "key": nameToColumnId[x_key] if nameToColumnId is not None else x_key,
                "value": x_value,
            }
            for x_key, x_value in values.items()
        ]
        self.rowId = rowId
        if etag is not None:
            self.etag = etag

TableAbstractBaseClass

Bases: Iterable, Sized

Abstract base class for Tables based on different data containers.

Source code in synapseclient/table.py
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
class TableAbstractBaseClass(collections.abc.Iterable, collections.abc.Sized):
    """
    Abstract base class for Tables based on different data containers.
    """

    RowMetadataTuple = collections.namedtuple(
        "RowMetadataTuple", ["row_id", "row_version", "row_etag"]
    )

    def __init__(self, schema, headers=None, etag=None):
        if isinstance(schema, Schema):
            self.schema = schema
            self.tableId = schema.id if schema and "id" in schema else None
            self.headers = (
                headers if headers else [SelectColumn(id=id) for id in schema.columnIds]
            )
            self.etag = etag
        elif isinstance(schema, str):
            self.schema = None
            self.tableId = schema
            self.headers = headers
            self.etag = etag
        else:
            ValueError("Must provide a schema or a synapse ID of a Table Entity")

    def asDataFrame(self):
        raise NotImplementedError()

    def asRowSet(self):
        return RowSet(
            headers=self.headers,
            tableId=self.tableId,
            etag=self.etag,
            rows=[row if isinstance(row, Row) else Row(row) for row in self],
        )

    def _synapse_store(self, syn):
        raise NotImplementedError()

    def _synapse_delete(self, syn):
        """
        Delete the rows that result from a table query.

        Example:
            syn.delete(syn.tableQuery('select name from %s where no_good = true' % schema1.id))
        """
        row_id_vers_generator = (
            (metadata.row_id, metadata.row_version)
            for metadata in self.iter_row_metadata()
        )
        _delete_rows(syn, self.tableId, row_id_vers_generator)

    @abc.abstractmethod
    def iter_row_metadata(self):
        """
        Iterates the table results to get row_id and row_etag. If an etag does not exist for a row, it will
        generated as (row_id, None)

        Returns:
            A generator that gives [collections.namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple) with format (row_id, row_etag)
        """
        pass
Functions
iter_row_metadata abstractmethod
iter_row_metadata()

Iterates the table results to get row_id and row_etag. If an etag does not exist for a row, it will generated as (row_id, None)

RETURNS DESCRIPTION

A generator that gives collections.namedtuple with format (row_id, row_etag)

Source code in synapseclient/table.py
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
@abc.abstractmethod
def iter_row_metadata(self):
    """
    Iterates the table results to get row_id and row_etag. If an etag does not exist for a row, it will
    generated as (row_id, None)

    Returns:
        A generator that gives [collections.namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple) with format (row_id, row_etag)
    """
    pass

RowSetTable

Bases: TableAbstractBaseClass

A Table object that wraps a RowSet.

Source code in synapseclient/table.py
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
class RowSetTable(TableAbstractBaseClass):
    """
    A Table object that wraps a RowSet.
    """

    def __init__(self, schema, rowset):
        super(RowSetTable, self).__init__(schema, etag=rowset.get("etag", None))
        self.rowset = rowset

    def _synapse_store(self, syn):
        row_reference_set = syn.store(self.rowset)
        return RowSetTable(self.schema, row_reference_set)

    def asDataFrame(self):
        test_import_pandas()
        import pandas as pd

        if any([row["rowId"] for row in self.rowset["rows"]]):
            rownames = row_labels_from_rows(self.rowset["rows"])
        else:
            rownames = None

        series = collections.OrderedDict()
        for i, header in enumerate(self.rowset["headers"]):
            series[header.name] = pd.Series(
                name=header.name,
                data=[row["values"][i] for row in self.rowset["rows"]],
                index=rownames,
            )

        return pd.DataFrame(data=series, index=rownames)

    def asRowSet(self):
        return self.rowset

    def __iter__(self):
        def iterate_rows(rows, headers):
            for row in rows:
                yield cast_values(row, headers)

        return iterate_rows(self.rowset["rows"], self.rowset["headers"])

    def __len__(self):
        return len(self.rowset["rows"])

    def iter_row_metadata(self):
        raise NotImplementedError("iter_metadata is not supported for RowSetTable")

TableQueryResult

Bases: TableAbstractBaseClass

An object to wrap rows returned as a result of a table query. The TableQueryResult object can be used to iterate over results of a query.

Example:

results = syn.tableQuery("select * from syn1234")
for row in results:
    print(row)
Source code in synapseclient/table.py
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
class TableQueryResult(TableAbstractBaseClass):
    """
    An object to wrap rows returned as a result of a table query.
    The TableQueryResult object can be used to iterate over results of a query.

    Example:

        results = syn.tableQuery("select * from syn1234")
        for row in results:
            print(row)
    """

    def __init__(self, synapse, query, limit=None, offset=None, isConsistent=True):
        self.syn = synapse

        self.query = query
        self.limit = limit
        self.offset = offset
        self.isConsistent = isConsistent

        result = self.syn._queryTable(
            query=query, limit=limit, offset=offset, isConsistent=isConsistent
        )

        self.rowset = RowSet.from_json(result["queryResult"]["queryResults"])

        self.columnModels = [Column(**col) for col in result.get("columnModels", [])]
        self.nextPageToken = result["queryResult"].get("nextPageToken", None)
        self.count = result.get("queryCount", None)
        self.maxRowsPerPage = result.get("maxRowsPerPage", None)
        self.i = -1

        super(TableQueryResult, self).__init__(
            schema=self.rowset.get("tableId", None),
            headers=self.rowset.headers,
            etag=self.rowset.get("etag", None),
        )

    def _synapse_store(self, syn):
        raise SynapseError(
            "A TableQueryResult is a read only object and can't be stored in Synapse. Convert to a"
            " DataFrame or RowSet instead."
        )

    def asDataFrame(self, rowIdAndVersionInIndex=True):
        """
        Convert query result to a Pandas DataFrame.

        Arguments:
            rowIdAndVersionInIndex: Make the dataframe index consist of the row_id and row_version (and row_etag
                                    if it exists)
        """
        test_import_pandas()
        import pandas as pd

        # To turn a TableQueryResult into a data frame, we add a page of rows
        # at a time on the untested theory that it's more efficient than
        # adding a single row at a time to the data frame.

        def construct_rownames(rowset, offset=0):
            try:
                return (
                    row_labels_from_rows(rowset["rows"])
                    if rowIdAndVersionInIndex
                    else None
                )
            except KeyError:
                # if we don't have row id and version, just number the rows
                # python3 cast range to list for safety
                return list(range(offset, offset + len(rowset["rows"])))

        # first page of rows
        offset = 0
        rownames = construct_rownames(self.rowset, offset)
        offset += len(self.rowset["rows"])
        series = collections.OrderedDict()

        if not rowIdAndVersionInIndex:
            # Since we use an OrderedDict this must happen before we construct the other columns
            # add row id, verison, and etag as rows
            append_etag = False  # only useful when (not rowIdAndVersionInIndex), hooray for lazy variables!
            series["ROW_ID"] = pd.Series(
                name="ROW_ID", data=[row["rowId"] for row in self.rowset["rows"]]
            )
            series["ROW_VERSION"] = pd.Series(
                name="ROW_VERSION",
                data=[row["versionNumber"] for row in self.rowset["rows"]],
            )

            row_etag = [row.get("etag") for row in self.rowset["rows"]]
            if any(row_etag):
                append_etag = True
                series["ROW_ETAG"] = pd.Series(name="ROW_ETAG", data=row_etag)

        for i, header in enumerate(self.rowset["headers"]):
            column_name = header.name
            series[column_name] = pd.Series(
                name=column_name,
                data=[row["values"][i] for row in self.rowset["rows"]],
                index=rownames,
            )

        # subsequent pages of rows
        while self.nextPageToken:
            result = self.syn._queryTableNext(self.nextPageToken, self.tableId)
            self.rowset = RowSet.from_json(result["queryResults"])
            self.nextPageToken = result.get("nextPageToken", None)
            self.i = 0

            rownames = construct_rownames(self.rowset, offset)
            offset += len(self.rowset["rows"])

            if not rowIdAndVersionInIndex:
                # TODO: Look into why this isn't being assigned
                series["ROW_ID"].append(
                    pd.Series(
                        name="ROW_ID", data=[row["id"] for row in self.rowset["rows"]]
                    )
                )
                series["ROW_VERSION"].append(
                    pd.Series(
                        name="ROW_VERSION",
                        data=[row["version"] for row in self.rowset["rows"]],
                    )
                )
                if append_etag:
                    series["ROW_ETAG"] = pd.Series(
                        name="ROW_ETAG",
                        data=[row.get("etag") for row in self.rowset["rows"]],
                    )

            for i, header in enumerate(self.rowset["headers"]):
                column_name = header.name
                series[column_name] = pd.concat(
                    [
                        series[column_name],
                        pd.Series(
                            name=column_name,
                            data=[row["values"][i] for row in self.rowset["rows"]],
                            index=rownames,
                        ),
                    ],
                    # can't verify integrity when indices are just numbers instead of 'rowid_rowversion'
                    verify_integrity=rowIdAndVersionInIndex,
                )

        return pd.DataFrame(data=series)

    def asRowSet(self):
        # Note that as of stack 60, an empty query will omit the headers field
        # see PLFM-3014
        return RowSet(
            headers=self.headers,
            tableId=self.tableId,
            etag=self.etag,
            rows=[row for row in self],
        )

    def __iter__(self):
        return self

    def next(self):
        """
        Python 2 iterator
        """
        self.i += 1
        if self.i >= len(self.rowset["rows"]):
            if self.nextPageToken:
                result = self.syn._queryTableNext(self.nextPageToken, self.tableId)
                self.rowset = RowSet.from_json(result["queryResults"])
                self.nextPageToken = result.get("nextPageToken", None)
                self.i = 0
            else:
                raise StopIteration()
        return self.rowset["rows"][self.i]

    def __next__(self):
        """
        Python 3 iterator
        """
        return self.next()

    def __len__(self):
        return len(self.rowset["rows"])

    def iter_row_metadata(self):
        """
        Iterates the table results to get row_id and row_etag. If an etag does not exist for a row, it will
        generated as (row_id, row_version,None)

        Returns:
            A generator that gives [collections.namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple)
            with format (row_id, row_version, row_etag)
        """
        for row in self:
            yield type(self).RowMetadataTuple(
                int(row["rowId"]), int(row["versionNumber"]), row.get("etag")
            )
Functions
asDataFrame
asDataFrame(rowIdAndVersionInIndex=True)

Convert query result to a Pandas DataFrame.

PARAMETER DESCRIPTION
rowIdAndVersionInIndex

Make the dataframe index consist of the row_id and row_version (and row_etag if it exists)

DEFAULT: True

Source code in synapseclient/table.py
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
def asDataFrame(self, rowIdAndVersionInIndex=True):
    """
    Convert query result to a Pandas DataFrame.

    Arguments:
        rowIdAndVersionInIndex: Make the dataframe index consist of the row_id and row_version (and row_etag
                                if it exists)
    """
    test_import_pandas()
    import pandas as pd

    # To turn a TableQueryResult into a data frame, we add a page of rows
    # at a time on the untested theory that it's more efficient than
    # adding a single row at a time to the data frame.

    def construct_rownames(rowset, offset=0):
        try:
            return (
                row_labels_from_rows(rowset["rows"])
                if rowIdAndVersionInIndex
                else None
            )
        except KeyError:
            # if we don't have row id and version, just number the rows
            # python3 cast range to list for safety
            return list(range(offset, offset + len(rowset["rows"])))

    # first page of rows
    offset = 0
    rownames = construct_rownames(self.rowset, offset)
    offset += len(self.rowset["rows"])
    series = collections.OrderedDict()

    if not rowIdAndVersionInIndex:
        # Since we use an OrderedDict this must happen before we construct the other columns
        # add row id, verison, and etag as rows
        append_etag = False  # only useful when (not rowIdAndVersionInIndex), hooray for lazy variables!
        series["ROW_ID"] = pd.Series(
            name="ROW_ID", data=[row["rowId"] for row in self.rowset["rows"]]
        )
        series["ROW_VERSION"] = pd.Series(
            name="ROW_VERSION",
            data=[row["versionNumber"] for row in self.rowset["rows"]],
        )

        row_etag = [row.get("etag") for row in self.rowset["rows"]]
        if any(row_etag):
            append_etag = True
            series["ROW_ETAG"] = pd.Series(name="ROW_ETAG", data=row_etag)

    for i, header in enumerate(self.rowset["headers"]):
        column_name = header.name
        series[column_name] = pd.Series(
            name=column_name,
            data=[row["values"][i] for row in self.rowset["rows"]],
            index=rownames,
        )

    # subsequent pages of rows
    while self.nextPageToken:
        result = self.syn._queryTableNext(self.nextPageToken, self.tableId)
        self.rowset = RowSet.from_json(result["queryResults"])
        self.nextPageToken = result.get("nextPageToken", None)
        self.i = 0

        rownames = construct_rownames(self.rowset, offset)
        offset += len(self.rowset["rows"])

        if not rowIdAndVersionInIndex:
            # TODO: Look into why this isn't being assigned
            series["ROW_ID"].append(
                pd.Series(
                    name="ROW_ID", data=[row["id"] for row in self.rowset["rows"]]
                )
            )
            series["ROW_VERSION"].append(
                pd.Series(
                    name="ROW_VERSION",
                    data=[row["version"] for row in self.rowset["rows"]],
                )
            )
            if append_etag:
                series["ROW_ETAG"] = pd.Series(
                    name="ROW_ETAG",
                    data=[row.get("etag") for row in self.rowset["rows"]],
                )

        for i, header in enumerate(self.rowset["headers"]):
            column_name = header.name
            series[column_name] = pd.concat(
                [
                    series[column_name],
                    pd.Series(
                        name=column_name,
                        data=[row["values"][i] for row in self.rowset["rows"]],
                        index=rownames,
                    ),
                ],
                # can't verify integrity when indices are just numbers instead of 'rowid_rowversion'
                verify_integrity=rowIdAndVersionInIndex,
            )

    return pd.DataFrame(data=series)
next
next()

Python 2 iterator

Source code in synapseclient/table.py
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
def next(self):
    """
    Python 2 iterator
    """
    self.i += 1
    if self.i >= len(self.rowset["rows"]):
        if self.nextPageToken:
            result = self.syn._queryTableNext(self.nextPageToken, self.tableId)
            self.rowset = RowSet.from_json(result["queryResults"])
            self.nextPageToken = result.get("nextPageToken", None)
            self.i = 0
        else:
            raise StopIteration()
    return self.rowset["rows"][self.i]
iter_row_metadata
iter_row_metadata()

Iterates the table results to get row_id and row_etag. If an etag does not exist for a row, it will generated as (row_id, row_version,None)

RETURNS DESCRIPTION

A generator that gives collections.namedtuple

with format (row_id, row_version, row_etag)

Source code in synapseclient/table.py
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
def iter_row_metadata(self):
    """
    Iterates the table results to get row_id and row_etag. If an etag does not exist for a row, it will
    generated as (row_id, row_version,None)

    Returns:
        A generator that gives [collections.namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple)
        with format (row_id, row_version, row_etag)
    """
    for row in self:
        yield type(self).RowMetadataTuple(
            int(row["rowId"]), int(row["versionNumber"]), row.get("etag")
        )

CsvFileTable

Bases: TableAbstractBaseClass

An object to wrap a CSV file that may be stored into a Synapse table or returned as a result of a table query.

Source code in synapseclient/table.py
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
class CsvFileTable(TableAbstractBaseClass):
    """
    An object to wrap a CSV file that may be stored into a Synapse table or
    returned as a result of a table query.
    """

    @classmethod
    def from_table_query(
        cls,
        synapse,
        query,
        quoteCharacter='"',
        escapeCharacter="\\",
        lineEnd=str(os.linesep),
        separator=",",
        header=True,
        includeRowIdAndRowVersion=True,
        downloadLocation=None,
    ):
        """
        Create a Table object wrapping a CSV file resulting from querying a Synapse table.
        Mostly for internal use.
        """

        download_from_table_result, path = synapse._queryTableCsv(
            query=query,
            quoteCharacter=quoteCharacter,
            escapeCharacter=escapeCharacter,
            lineEnd=lineEnd,
            separator=separator,
            header=header,
            includeRowIdAndRowVersion=includeRowIdAndRowVersion,
            downloadLocation=downloadLocation,
        )

        # A dirty hack to find out if we got back row ID and Version
        # in particular, we don't get these back from aggregate queries
        with io.open(path, "r", encoding="utf-8") as f:
            reader = csv.reader(
                f,
                delimiter=separator,
                escapechar=escapeCharacter,
                lineterminator=lineEnd,
                quotechar=quoteCharacter,
            )
            first_line = next(reader)
        if len(download_from_table_result["headers"]) + 2 == len(first_line):
            includeRowIdAndRowVersion = True
        else:
            includeRowIdAndRowVersion = False

        self = cls(
            filepath=path,
            schema=download_from_table_result.get("tableId", None),
            etag=download_from_table_result.get("etag", None),
            quoteCharacter=quoteCharacter,
            escapeCharacter=escapeCharacter,
            lineEnd=lineEnd,
            separator=separator,
            header=header,
            includeRowIdAndRowVersion=includeRowIdAndRowVersion,
            headers=[
                SelectColumn(**header)
                for header in download_from_table_result["headers"]
            ],
        )

        return self

    @classmethod
    def from_data_frame(
        cls,
        schema,
        df,
        filepath=None,
        etag=None,
        quoteCharacter='"',
        escapeCharacter="\\",
        lineEnd=str(os.linesep),
        separator=",",
        header=True,
        includeRowIdAndRowVersion=None,
        headers=None,
        **kwargs,
    ):
        # infer columns from data frame if not specified
        if not headers:
            cols = as_table_columns(df)
            headers = [SelectColumn.from_column(col) for col in cols]

        # if the schema has no columns, use the inferred columns
        if isinstance(schema, Schema) and not schema.has_columns():
            schema.addColumns(cols)

        # convert row names in the format [row_id]_[version] or [row_id]_[version]_[etag] back to columns
        # etag is essentially a UUID
        etag_pattern = r"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}"
        row_id_version_pattern = re.compile(r"(\d+)_(\d+)(_(" + etag_pattern + r"))?")

        row_id = []
        row_version = []
        row_etag = []
        for row_name in df.index.values:
            m = row_id_version_pattern.match(str(row_name))
            row_id.append(m.group(1) if m else None)
            row_version.append(m.group(2) if m else None)
            row_etag.append(m.group(4) if m else None)

        # include row ID and version, if we're asked to OR if it's encoded in row names
        if includeRowIdAndRowVersion or (
            includeRowIdAndRowVersion is None and any(row_id)
        ):
            df2 = df.copy()

            cls._insert_dataframe_column_if_not_exist(df2, 0, "ROW_ID", row_id)
            cls._insert_dataframe_column_if_not_exist(
                df2, 1, "ROW_VERSION", row_version
            )
            if any(row_etag):
                cls._insert_dataframe_column_if_not_exist(df2, 2, "ROW_ETAG", row_etag)

            df = df2
            includeRowIdAndRowVersion = True

        f = None
        try:
            if not filepath:
                temp_dir = tempfile.mkdtemp()
                filepath = os.path.join(temp_dir, "table.csv")

            f = io.open(filepath, mode="w", encoding="utf-8", newline="")

            test_import_pandas()
            import pandas as pd

            if isinstance(schema, Schema):
                for col in schema.columns_to_store:
                    if col["columnType"] == "DATE":

                        def _trailing_date_time_millisecond(t):
                            if isinstance(t, str):
                                return t[:-3]

                        df[col.name] = pd.to_datetime(
                            df[col.name], errors="coerce"
                        ).dt.strftime("%s%f")
                        df[col.name] = df[col.name].apply(
                            lambda x: _trailing_date_time_millisecond(x)
                        )

            df.to_csv(
                f,
                index=False,
                sep=separator,
                header=header,
                quotechar=quoteCharacter,
                escapechar=escapeCharacter,
                lineterminator=lineEnd,
                na_rep=kwargs.get("na_rep", ""),
                float_format="%.12g",
            )
            # NOTE: reason for flat_format='%.12g':
            # pandas automatically converts int columns into float64 columns when some cells in the column have no
            # value. If we write the whole number back as a decimal (e.g. '3.0'), Synapse complains that we are writing
            # a float into a INTEGER(synapse table type) column. Using the 'g' will strip off '.0' from whole number
            # values. pandas by default (with no float_format parameter) seems to keep 12 values after decimal, so we
            # use '%.12g'.c
            # see SYNPY-267.
        finally:
            if f:
                f.close()

        return cls(
            schema=schema,
            filepath=filepath,
            etag=etag,
            quoteCharacter=quoteCharacter,
            escapeCharacter=escapeCharacter,
            lineEnd=lineEnd,
            separator=separator,
            header=header,
            includeRowIdAndRowVersion=includeRowIdAndRowVersion,
            headers=headers,
        )

    @staticmethod
    def _insert_dataframe_column_if_not_exist(
        dataframe, insert_index, col_name, insert_column_data
    ):
        # if the column already exists verify the column data is same as what we parsed
        if col_name in dataframe.columns:
            if dataframe[col_name].tolist() != insert_column_data:
                raise SynapseError(
                    (
                        "A column named '{0}' already exists and does not match the '{0}' values present in"
                        " the DataFrame's row names. Please refain from using or modifying '{0}' as a"
                        " column for your data because it is necessary for version tracking in Synapse's"
                        " tables"
                    ).format(col_name)
                )
        else:
            dataframe.insert(insert_index, col_name, insert_column_data)

    @classmethod
    def from_list_of_rows(
        cls,
        schema,
        values,
        filepath=None,
        etag=None,
        quoteCharacter='"',
        escapeCharacter="\\",
        lineEnd=str(os.linesep),
        separator=",",
        linesToSkip=0,
        includeRowIdAndRowVersion=None,
        headers=None,
    ):
        # create CSV file
        f = None
        try:
            if not filepath:
                temp_dir = tempfile.mkdtemp()
                filepath = os.path.join(temp_dir, "table.csv")

            f = io.open(filepath, "w", encoding="utf-8", newline="")

            writer = csv.writer(
                f,
                quoting=csv.QUOTE_NONNUMERIC,
                delimiter=separator,
                escapechar=escapeCharacter,
                lineterminator=lineEnd,
                quotechar=quoteCharacter,
                skipinitialspace=linesToSkip,
            )

            # if we haven't explicitly set columns, try to grab them from
            # the schema object
            if (
                not headers
                and "columns_to_store" in schema
                and schema.columns_to_store is not None
            ):
                headers = [
                    SelectColumn.from_column(col) for col in schema.columns_to_store
                ]

            # write headers?
            if headers:
                writer.writerow([header.name for header in headers])
                header = True
            else:
                header = False

            # write row data
            for row in values:
                writer.writerow(row)

        finally:
            if f:
                f.close()

        return cls(
            schema=schema,
            filepath=filepath,
            etag=etag,
            quoteCharacter=quoteCharacter,
            escapeCharacter=escapeCharacter,
            lineEnd=lineEnd,
            separator=separator,
            header=header,
            headers=headers,
            includeRowIdAndRowVersion=includeRowIdAndRowVersion,
        )

    def __init__(
        self,
        schema,
        filepath,
        etag=None,
        quoteCharacter=DEFAULT_QUOTE_CHARACTER,
        escapeCharacter=DEFAULT_ESCAPSE_CHAR,
        lineEnd=str(os.linesep),
        separator=DEFAULT_SEPARATOR,
        header=True,
        linesToSkip=0,
        includeRowIdAndRowVersion=None,
        headers=None,
    ):
        """Initialize a CsvFileTable object.

        Note: Some arguments provided to this constructor are passed to pandas.read_csv()
        including `quoteCharacter`, `escapeCharacter`, `lineEnd`, and `separator`.
        These arguments can be overwritten by passing `pandas.read_csv` kwargs to `asDataFrame`.
        """
        self.filepath = filepath

        self.includeRowIdAndRowVersion = includeRowIdAndRowVersion

        # CsvTableDescriptor fields
        self.linesToSkip = linesToSkip
        self.quoteCharacter = quoteCharacter
        self.escapeCharacter = escapeCharacter
        self.lineEnd = lineEnd
        self.separator = separator
        self.header = header

        super(CsvFileTable, self).__init__(schema, headers=headers, etag=etag)

        self.setColumnHeaders(headers)

    def _synapse_store(self, syn):
        copied_self = copy.copy(self)
        return copied_self._update_self(syn)

    def _update_self(self, syn):
        if isinstance(self.schema, Schema) and self.schema.get("id", None) is None:
            # store schema
            self.schema = syn.store(self.schema)
            self.tableId = self.schema.id

        result = syn._uploadCsv(
            self.filepath,
            self.schema if self.schema else self.tableId,
            updateEtag=self.etag,
            quoteCharacter=self.quoteCharacter,
            escapeCharacter=self.escapeCharacter,
            lineEnd=self.lineEnd,
            separator=self.separator,
            header=self.header,
            linesToSkip=self.linesToSkip,
        )

        upload_to_table_result = result["results"][0]

        assert upload_to_table_result["concreteType"] in (
            "org.sagebionetworks.repo.model.table.EntityUpdateResults",
            "org.sagebionetworks.repo.model.table.UploadToTableResult",
        ), "Not an UploadToTableResult or EntityUpdateResults."
        if "etag" in upload_to_table_result:
            self.etag = upload_to_table_result["etag"]
        return self

    def asDataFrame(
        self,
        rowIdAndVersionInIndex: bool = True,
        convert_to_datetime: bool = False,
        **kwargs,
    ):
        """Convert query result to a Pandas DataFrame.

        Note: Class attributes  `quoteCharacter`, `escapeCharacter`, `lineEnd`, and `separator`
        are used as `quotechar`, `escapechar`, `lineterminator`, and `sep` in `pandas.read_csv`
        within this method. If you want to override these values, you can do so by passing the
        appropriate keyword arguments to this method.

        Arguments:
            rowIdAndVersionInIndex: Make the dataframe index consist of the
                                    row_id and row_version (and row_etag if it exists)
            convert_to_datetime:    If set to True, will convert all Synapse DATE columns from UNIX timestamp
                                    integers into UTC datetime objects
            kwargs:                 Additional keyword arguments to pass to
                                    pandas.read_csv via _csv_to_pandas_df. See
                                    https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
                                    for complete list of supported arguments.

        Returns:
            A Pandas dataframe with results
        """
        test_import_pandas()
        import pandas as pd

        try:
            # Handle bug in pandas 0.19 requiring quotechar to be str not unicode or newstr
            quoteChar = self.quoteCharacter

            # determine which columns are DATE columns so we can convert milisecond timestamps into datetime objects
            date_columns = []
            list_columns = []
            dtype = {}

            if self.headers is not None:
                for select_column in self.headers:
                    if select_column.columnType == "STRING":
                        # we want to identify string columns so that pandas doesn't try to
                        # automatically parse strings in a string column to other data types
                        dtype[select_column.name] = str
                    elif select_column.columnType in LIST_COLUMN_TYPES:
                        list_columns.append(select_column.name)
                    elif select_column.columnType == "DATE" and convert_to_datetime:
                        date_columns.append(select_column.name)

            return _csv_to_pandas_df(
                self.filepath,
                separator=self.separator,
                quote_char=quoteChar,
                escape_char=self.escapeCharacter,
                contain_headers=self.header,
                lines_to_skip=self.linesToSkip,
                date_columns=date_columns,
                list_columns=list_columns,
                rowIdAndVersionInIndex=rowIdAndVersionInIndex,
                dtype=dtype,
                **kwargs,
            )

        except pd.errors.ParserError:
            return pd.DataFrame()

    def asRowSet(self):
        # Extract row id and version, if present in rows
        row_id_col = None
        row_ver_col = None
        for i, header in enumerate(self.headers):
            if header.name == "ROW_ID":
                row_id_col = i
            elif header.name == "ROW_VERSION":
                row_ver_col = i

        def to_row_object(row, row_id_col=None, row_ver_col=None):
            if isinstance(row, Row):
                return row
            rowId = row[row_id_col] if row_id_col is not None else None
            versionNumber = row[row_ver_col] if row_ver_col is not None else None
            values = [
                elem for i, elem in enumerate(row) if i not in [row_id_col, row_ver_col]
            ]
            return Row(values, rowId=rowId, versionNumber=versionNumber)

        return RowSet(
            headers=[
                elem
                for i, elem in enumerate(self.headers)
                if i not in [row_id_col, row_ver_col]
            ],
            tableId=self.tableId,
            etag=self.etag,
            rows=[to_row_object(row, row_id_col, row_ver_col) for row in self],
        )

    def setColumnHeaders(self, headers):
        """
        Set the list of [SelectColumn][synapseclient.table.SelectColumn] objects that will be used to convert fields to the
        appropriate data types.

        Column headers are automatically set when querying.
        """
        if self.includeRowIdAndRowVersion:
            names = [header.name for header in headers]
            if "ROW_ID" not in names and "ROW_VERSION" not in names:
                headers = [
                    SelectColumn(name="ROW_ID", columnType="STRING"),
                    SelectColumn(name="ROW_VERSION", columnType="STRING"),
                ] + headers
        self.headers = headers

    def __iter__(self):
        def iterate_rows(filepath, headers):
            if not self.header or not self.headers:
                raise ValueError("Iteration not supported for table without headers.")

            header_name = {header.name for header in headers}
            row_metadata_headers = {"ROW_ID", "ROW_VERSION", "ROW_ETAG"}
            num_row_metadata_in_headers = len(header_name & row_metadata_headers)
            with io.open(filepath, encoding="utf-8", newline=self.lineEnd) as f:
                reader = csv.reader(
                    f,
                    delimiter=self.separator,
                    escapechar=self.escapeCharacter,
                    lineterminator=self.lineEnd,
                    quotechar=self.quoteCharacter,
                )
                csv_header = set(next(reader))
                # the number of row metadata differences between the csv headers and self.headers
                num_metadata_cols_diff = (
                    len(csv_header & row_metadata_headers) - num_row_metadata_in_headers
                )
                # we only process 2 cases:
                # 1. matching row metadata
                # 2. if metadata does not match, self.headers must not contains row metadata
                if num_metadata_cols_diff == 0 or num_row_metadata_in_headers == 0:
                    for row in reader:
                        yield cast_values(row[num_metadata_cols_diff:], headers)
                else:
                    raise ValueError(
                        "There is mismatching row metadata in the csv file and in headers."
                    )

        return iterate_rows(self.filepath, self.headers)

    def __len__(self):
        with io.open(self.filepath, encoding="utf-8", newline=self.lineEnd) as f:
            if self.header:  # ignore the header line
                f.readline()

            return sum(1 for line in f)

    def iter_row_metadata(self):
        """
        Iterates the table results to get row_id and row_etag. If an etag does not exist for a row,
        it will generated as (row_id, None)

        Returns:
            A generator that gives [collections.namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple) with format (row_id, row_etag)
        """
        with io.open(self.filepath, encoding="utf-8", newline=self.lineEnd) as f:
            reader = csv.reader(
                f,
                delimiter=self.separator,
                escapechar=self.escapeCharacter,
                lineterminator=self.lineEnd,
                quotechar=self.quoteCharacter,
            )
            header = next(reader)

            # The ROW_... headers are always in a predefined order
            row_id_index = header.index("ROW_ID")
            row_version_index = header.index("ROW_VERSION")
            try:
                row_etag_index = header.index("ROW_ETAG")
            except ValueError:
                row_etag_index = None

            for row in reader:
                yield type(self).RowMetadataTuple(
                    int(row[row_id_index]),
                    int(row[row_version_index]),
                    row[row_etag_index] if (row_etag_index is not None) else None,
                )
Functions
from_table_query classmethod
from_table_query(synapse, query, quoteCharacter='"', escapeCharacter='\\', lineEnd=str(linesep), separator=',', header=True, includeRowIdAndRowVersion=True, downloadLocation=None)

Create a Table object wrapping a CSV file resulting from querying a Synapse table. Mostly for internal use.

Source code in synapseclient/table.py
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
@classmethod
def from_table_query(
    cls,
    synapse,
    query,
    quoteCharacter='"',
    escapeCharacter="\\",
    lineEnd=str(os.linesep),
    separator=",",
    header=True,
    includeRowIdAndRowVersion=True,
    downloadLocation=None,
):
    """
    Create a Table object wrapping a CSV file resulting from querying a Synapse table.
    Mostly for internal use.
    """

    download_from_table_result, path = synapse._queryTableCsv(
        query=query,
        quoteCharacter=quoteCharacter,
        escapeCharacter=escapeCharacter,
        lineEnd=lineEnd,
        separator=separator,
        header=header,
        includeRowIdAndRowVersion=includeRowIdAndRowVersion,
        downloadLocation=downloadLocation,
    )

    # A dirty hack to find out if we got back row ID and Version
    # in particular, we don't get these back from aggregate queries
    with io.open(path, "r", encoding="utf-8") as f:
        reader = csv.reader(
            f,
            delimiter=separator,
            escapechar=escapeCharacter,
            lineterminator=lineEnd,
            quotechar=quoteCharacter,
        )
        first_line = next(reader)
    if len(download_from_table_result["headers"]) + 2 == len(first_line):
        includeRowIdAndRowVersion = True
    else:
        includeRowIdAndRowVersion = False

    self = cls(
        filepath=path,
        schema=download_from_table_result.get("tableId", None),
        etag=download_from_table_result.get("etag", None),
        quoteCharacter=quoteCharacter,
        escapeCharacter=escapeCharacter,
        lineEnd=lineEnd,
        separator=separator,
        header=header,
        includeRowIdAndRowVersion=includeRowIdAndRowVersion,
        headers=[
            SelectColumn(**header)
            for header in download_from_table_result["headers"]
        ],
    )

    return self
asDataFrame
asDataFrame(rowIdAndVersionInIndex: bool = True, convert_to_datetime: bool = False, **kwargs)

Convert query result to a Pandas DataFrame.

Note: Class attributes quoteCharacter, escapeCharacter, lineEnd, and separator are used as quotechar, escapechar, lineterminator, and sep in pandas.read_csv within this method. If you want to override these values, you can do so by passing the appropriate keyword arguments to this method.

PARAMETER DESCRIPTION
rowIdAndVersionInIndex

Make the dataframe index consist of the row_id and row_version (and row_etag if it exists)

TYPE: bool DEFAULT: True

convert_to_datetime

If set to True, will convert all Synapse DATE columns from UNIX timestamp integers into UTC datetime objects

TYPE: bool DEFAULT: False

kwargs

Additional keyword arguments to pass to pandas.read_csv via _csv_to_pandas_df. See https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html for complete list of supported arguments.

DEFAULT: {}

RETURNS DESCRIPTION

A Pandas dataframe with results

Source code in synapseclient/table.py
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
def asDataFrame(
    self,
    rowIdAndVersionInIndex: bool = True,
    convert_to_datetime: bool = False,
    **kwargs,
):
    """Convert query result to a Pandas DataFrame.

    Note: Class attributes  `quoteCharacter`, `escapeCharacter`, `lineEnd`, and `separator`
    are used as `quotechar`, `escapechar`, `lineterminator`, and `sep` in `pandas.read_csv`
    within this method. If you want to override these values, you can do so by passing the
    appropriate keyword arguments to this method.

    Arguments:
        rowIdAndVersionInIndex: Make the dataframe index consist of the
                                row_id and row_version (and row_etag if it exists)
        convert_to_datetime:    If set to True, will convert all Synapse DATE columns from UNIX timestamp
                                integers into UTC datetime objects
        kwargs:                 Additional keyword arguments to pass to
                                pandas.read_csv via _csv_to_pandas_df. See
                                https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
                                for complete list of supported arguments.

    Returns:
        A Pandas dataframe with results
    """
    test_import_pandas()
    import pandas as pd

    try:
        # Handle bug in pandas 0.19 requiring quotechar to be str not unicode or newstr
        quoteChar = self.quoteCharacter

        # determine which columns are DATE columns so we can convert milisecond timestamps into datetime objects
        date_columns = []
        list_columns = []
        dtype = {}

        if self.headers is not None:
            for select_column in self.headers:
                if select_column.columnType == "STRING":
                    # we want to identify string columns so that pandas doesn't try to
                    # automatically parse strings in a string column to other data types
                    dtype[select_column.name] = str
                elif select_column.columnType in LIST_COLUMN_TYPES:
                    list_columns.append(select_column.name)
                elif select_column.columnType == "DATE" and convert_to_datetime:
                    date_columns.append(select_column.name)

        return _csv_to_pandas_df(
            self.filepath,
            separator=self.separator,
            quote_char=quoteChar,
            escape_char=self.escapeCharacter,
            contain_headers=self.header,
            lines_to_skip=self.linesToSkip,
            date_columns=date_columns,
            list_columns=list_columns,
            rowIdAndVersionInIndex=rowIdAndVersionInIndex,
            dtype=dtype,
            **kwargs,
        )

    except pd.errors.ParserError:
        return pd.DataFrame()
setColumnHeaders
setColumnHeaders(headers)

Set the list of SelectColumn objects that will be used to convert fields to the appropriate data types.

Column headers are automatically set when querying.

Source code in synapseclient/table.py
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
def setColumnHeaders(self, headers):
    """
    Set the list of [SelectColumn][synapseclient.table.SelectColumn] objects that will be used to convert fields to the
    appropriate data types.

    Column headers are automatically set when querying.
    """
    if self.includeRowIdAndRowVersion:
        names = [header.name for header in headers]
        if "ROW_ID" not in names and "ROW_VERSION" not in names:
            headers = [
                SelectColumn(name="ROW_ID", columnType="STRING"),
                SelectColumn(name="ROW_VERSION", columnType="STRING"),
            ] + headers
    self.headers = headers
iter_row_metadata
iter_row_metadata()

Iterates the table results to get row_id and row_etag. If an etag does not exist for a row, it will generated as (row_id, None)

RETURNS DESCRIPTION

A generator that gives collections.namedtuple with format (row_id, row_etag)

Source code in synapseclient/table.py
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
def iter_row_metadata(self):
    """
    Iterates the table results to get row_id and row_etag. If an etag does not exist for a row,
    it will generated as (row_id, None)

    Returns:
        A generator that gives [collections.namedtuple](https://docs.python.org/3/library/collections.html#collections.namedtuple) with format (row_id, row_etag)
    """
    with io.open(self.filepath, encoding="utf-8", newline=self.lineEnd) as f:
        reader = csv.reader(
            f,
            delimiter=self.separator,
            escapechar=self.escapeCharacter,
            lineterminator=self.lineEnd,
            quotechar=self.quoteCharacter,
        )
        header = next(reader)

        # The ROW_... headers are always in a predefined order
        row_id_index = header.index("ROW_ID")
        row_version_index = header.index("ROW_VERSION")
        try:
            row_etag_index = header.index("ROW_ETAG")
        except ValueError:
            row_etag_index = None

        for row in reader:
            yield type(self).RowMetadataTuple(
                int(row[row_id_index]),
                int(row[row_version_index]),
                row[row_etag_index] if (row_etag_index is not None) else None,
            )

Functions

as_table_columns

as_table_columns(values: Union[str, DataFrameType])

Return a list of Synapse table Column objects that correspond to the columns in the given values.

PARAMETER DESCRIPTION
values

An object that holds the content of the tables.

  • A string holding the path to a CSV file, a filehandle, or StringIO containing valid csv content
  • A Pandas DataFrame

TYPE: Union[str, DataFrameType]

RETURNS DESCRIPTION

A list of Synapse table Column objects

Example:

import pandas as pd

df = pd.DataFrame(dict(a=[1, 2, 3], b=["c", "d", "e"]))
cols = as_table_columns(df)
Source code in synapseclient/table.py
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
def as_table_columns(values: Union[str, DataFrameType]):
    """
    Return a list of Synapse table [Column][synapseclient.table.Column] objects
    that correspond to the columns in the given values.

    Arguments:
        values: An object that holds the content of the tables.

            - A string holding the path to a CSV file, a filehandle, or StringIO containing valid csv content
            - A [Pandas DataFrame](http://pandas.pydata.org/pandas-docs/stable/api.html#dataframe)

    Returns:
        A list of Synapse table [Column][synapseclient.table.Column] objects

    Example:

        import pandas as pd

        df = pd.DataFrame(dict(a=[1, 2, 3], b=["c", "d", "e"]))
        cols = as_table_columns(df)
    """
    test_import_pandas()
    import pandas as pd
    from pandas.api.types import infer_dtype

    df = None

    # pandas DataFrame
    if isinstance(values, pd.DataFrame):
        df = values
    # filename of a csv file
    # in Python 3, we can check that the values is instanceof io.IOBase
    # for now, check if values has attr `read`
    elif isinstance(values, str) or hasattr(values, "read"):
        df = _csv_to_pandas_df(values)

    if df is None:
        raise ValueError("Values of type %s is not yet supported." % type(values))

    cols = list()
    for col in df:
        inferred_type = infer_dtype(df[col], skipna=True)
        columnType = PANDAS_TABLE_TYPE.get(inferred_type, "STRING")
        if columnType == "STRING":
            maxStrLen = df[col].str.len().max()
            if maxStrLen > 1000:
                cols.append(Column(name=col, columnType="LARGETEXT", defaultValue=""))
            else:
                size = int(
                    round(min(1000, max(30, maxStrLen * 1.5)))
                )  # Determine the length of the longest string
                cols.append(
                    Column(
                        name=col,
                        columnType=columnType,
                        maximumSize=size,
                        defaultValue="",
                    )
                )
        else:
            cols.append(Column(name=col, columnType=columnType))
    return cols

df2Table

df2Table(df, syn, tableName, parentProject)

Creates a new table from data in pandas data frame. parameters: df, tableName, parentProject

Source code in synapseclient/table.py
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
def df2Table(df, syn, tableName, parentProject):
    """Creates a new table from data in pandas data frame.
    parameters: df, tableName, parentProject
    """

    # Create columns:
    cols = as_table_columns(df)
    cols = [syn.store(col) for col in cols]

    # Create Table Schema
    schema1 = Schema(name=tableName, columns=cols, parent=parentProject)
    schema1 = syn.store(schema1)

    # Add data to Table
    for i in range(0, df.shape[0] / 1200 + 1):
        start = i * 1200
        end = min((i + 1) * 1200, df.shape[0])
        rowset1 = RowSet(
            columns=cols,
            schema=schema1,
            rows=[Row(list(df.ix[j, :])) for j in range(start, end)],
        )
        syn.store(rowset1)

    return schema1

to_boolean

to_boolean(value)

Convert a string to boolean, case insensitively, where true values are: true, t, and 1 and false values are: false, f, 0. Raise a ValueError for all other values.

Source code in synapseclient/table.py
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
def to_boolean(value):
    """
    Convert a string to boolean, case insensitively,
    where true values are: true, t, and 1 and false values are: false, f, 0.
    Raise a ValueError for all other values.
    """
    if isinstance(value, bool):
        return value

    if isinstance(value, str):
        lower_value = value.lower()
        if lower_value in ["true", "t", "1"]:
            return True
        if lower_value in ["false", "f", "0"]:
            return False

    raise ValueError("Can't convert %s to boolean." % value)

cast_values

cast_values(values, headers)

Convert a row of table query results from strings to the correct column type.

See: https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/table/ColumnType.html

Source code in synapseclient/table.py
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
def cast_values(values, headers):
    """
    Convert a row of table query results from strings to the correct column type.

    See: <https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/table/ColumnType.html>
    """
    if len(values) != len(headers):
        raise ValueError(
            "The number of columns in the csv file does not match the given headers. %d fields, %d headers"
            % (len(values), len(headers))
        )

    result = []
    for header, field in zip(headers, values):
        columnType = header.get("columnType", "STRING")

        # convert field to column type
        if field is None or field == "":
            result.append(None)
        elif columnType in {
            "STRING",
            "ENTITYID",
            "FILEHANDLEID",
            "LARGETEXT",
            "USERID",
            "LINK",
        }:
            result.append(field)
        elif columnType == "DOUBLE":
            result.append(float(field))
        elif columnType == "INTEGER":
            result.append(int(field))
        elif columnType == "BOOLEAN":
            result.append(to_boolean(field))
        elif columnType == "DATE":
            result.append(from_unix_epoch_time(field))
        elif columnType in {
            "STRING_LIST",
            "INTEGER_LIST",
            "BOOLEAN_LIST",
            "ENTITYID_LIST",
            "USERID_LIST",
        }:
            result.append(json.loads(field))
        elif columnType == "DATE_LIST":
            result.append(json.loads(field, parse_int=from_unix_epoch_time))
        else:
            # default to string for unknown column type
            result.append(field)

    return result

escape_column_name

escape_column_name(column: Union[str, Mapping]) -> str

Escape the name of the given column for use in a Synapse table query statement

PARAMETER DESCRIPTION
column

a string or column dictionary object with a 'name' key

TYPE: Union[str, Mapping]

RETURNS DESCRIPTION
str

Escaped column name

Source code in synapseclient/table.py
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
def escape_column_name(column: Union[str, collections.abc.Mapping]) -> str:
    """
    Escape the name of the given column for use in a Synapse table query statement

    Arguments:
        column: a string or column dictionary object with a 'name' key

    Returns:
        Escaped column name
    """
    col_name = (
        column["name"] if isinstance(column, collections.abc.Mapping) else str(column)
    )
    escaped_name = col_name.replace('"', '""')
    return f'"{escaped_name}"'

join_column_names

join_column_names(columns: Union[List, Dict[str, str]])

Join the names of the given columns into a comma delimited list suitable for use in a Synapse table query

PARAMETER DESCRIPTION
columns

A sequence of column string names or dictionary objets with column 'name' keys

TYPE: Union[List, Dict[str, str]]

Source code in synapseclient/table.py
356
357
358
359
360
361
362
363
def join_column_names(columns: Union[List, Dict[str, str]]):
    """
    Join the names of the given columns into a comma delimited list suitable for use in a Synapse table query

    Arguments:
        columns: A sequence of column string names or dictionary objets with column 'name' keys
    """
    return ",".join(escape_column_name(c) for c in columns)

delete_rows

delete_rows(syn, table_id: str, row_id_vers_list: List[Tuple[int, int]])

Deletes rows from a synapse table

PARAMETER DESCRIPTION
syn

An instance of Synapse

table_id

The ID of the table to delete rows from

TYPE: str

row_id_vers_list

An iterable containing tuples with format: (row_id, row_version)

TYPE: List[Tuple[int, int]]

Source code in synapseclient/table.py
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
def delete_rows(
    syn,
    table_id: str,
    row_id_vers_list: List[Tuple[int, int]],
):
    """
    Deletes rows from a synapse table

    Arguments:
        syn:              An instance of [Synapse][synapseclient.client.Synapse]
        table_id:         The ID of the table to delete rows from
        row_id_vers_list: An iterable containing tuples with format: (row_id, row_version)
    """
    delete_row_csv_filepath = _create_row_delete_csv(
        row_id_vers_iterable=row_id_vers_list
    )
    try:
        syn._uploadCsv(filepath=delete_row_csv_filepath, schema=table_id)
    finally:
        os.remove(delete_row_csv_filepath)

build_table

build_table(name, parent, values)

Build a Table object

PARAMETER DESCRIPTION
name

The name for the Table Schema object

parent

The project in Synapse to which this table belongs

values

An object that holds the content of the tables

RETURNS DESCRIPTION

A Table object suitable for storing

Example:

path = "/path/to/file.csv"
table = build_table("simple_table", "syn123", path)
table = syn.store(table)

import pandas as pd

df = pd.DataFrame(dict(a=[1, 2, 3], b=["c", "d", "e"]))
table = build_table("simple_table", "syn123", df)
table = syn.store(table)
Source code in synapseclient/table.py
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
def build_table(name, parent, values):
    """
    Build a Table object

    Arguments:
        name:    The name for the Table Schema object
        parent:  The project in Synapse to which this table belongs
        values:  An object that holds the content of the tables

            - A string holding the path to a CSV file
            - A [Pandas DataFrame](http://pandas.pydata.org/pandas-docs/stable/api.html#dataframe)

    Returns:
        A Table object suitable for storing

    Example:

        path = "/path/to/file.csv"
        table = build_table("simple_table", "syn123", path)
        table = syn.store(table)

        import pandas as pd

        df = pd.DataFrame(dict(a=[1, 2, 3], b=["c", "d", "e"]))
        table = build_table("simple_table", "syn123", df)
        table = syn.store(table)
    """
    test_import_pandas()
    import pandas as pd

    if not isinstance(values, pd.DataFrame) and not isinstance(values, str):
        raise ValueError("Values of type %s is not yet supported." % type(values))
    cols = as_table_columns(values)
    schema = Schema(name=name, columns=cols, parent=parent)
    headers = [SelectColumn.from_column(col) for col in cols]
    return Table(schema, values, headers=headers)

Table

Table(schema, values, **kwargs)

Combine a table schema and a set of values into some type of Table object depending on what type of values are given.

PARAMETER DESCRIPTION
schema

A table Schema object or Synapse Id of Table.

values

An object that holds the content of the tables

RETURNS DESCRIPTION

A Table object suitable for storing

Usually, the immediate next step after creating a Table object is to store it:

table = syn.store(Table(schema, values))

End users should not need to know the details of these Table subclasses:

Source code in synapseclient/table.py
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
def Table(schema, values, **kwargs):
    """
    Combine a table schema and a set of values into some type of Table object
    depending on what type of values are given.

    Arguments:
        schema: A table [Schema][synapseclient.table.Schema] object or Synapse Id of Table.
        values: An object that holds the content of the tables

            - A [RowSet][synapseclient.table.RowSet]
            - A list of lists (or tuples) where each element is a row
            - A string holding the path to a CSV file
            - A [Pandas DataFrame](http://pandas.pydata.org/pandas-docs/stable/api.html#dataframe)
            - A dict which will be wrapped by a [Pandas DataFrame](http://pandas.pydata.org/pandas-docs/stable/api.html#dataframe)

    Returns:
        A Table object suitable for storing

    Usually, the immediate next step after creating a Table object is to store it:

        table = syn.store(Table(schema, values))

    End users should not need to know the details of these Table subclasses:

    - [TableAbstractBaseClass][synapseclient.table.TableAbstractBaseClass]
    - [RowSetTable][synapseclient.table.RowSetTable]
    - [TableQueryResult][synapseclient.table.TableQueryResult]
    - [CsvFileTable][synapseclient.table.CsvFileTable]
    """

    try:
        import pandas as pd

        pandas_available = True
    except:  # noqa
        pandas_available = False

    # a RowSet
    if isinstance(values, RowSet):
        return RowSetTable(schema, values, **kwargs)

    # a list of rows
    elif isinstance(values, (list, tuple)):
        return CsvFileTable.from_list_of_rows(schema, values, **kwargs)

    # filename of a csv file
    elif isinstance(values, str):
        return CsvFileTable(schema, filepath=values, **kwargs)

    # pandas DataFrame
    elif pandas_available and isinstance(values, pd.DataFrame):
        return CsvFileTable.from_data_frame(schema, values, **kwargs)

    # dict
    elif pandas_available and isinstance(values, dict):
        return CsvFileTable.from_data_frame(schema, pd.DataFrame(values), **kwargs)

    else:
        raise ValueError(
            "Don't know how to make tables from values of type %s." % type(values)
        )