class BiospecimenDiagnosis(db.Model, Base):
    """
    Represents association table between biospecimen table and
    diagnosis table. Contains all biospecimen, diagnosis combiniations.
    :param kf_id: Unique id given by the Kid's First DCC
    :param created_at: Time of object creation
    :param modified_at: Last time of object modification
    """
    __tablename__ = 'biospecimen_diagnosis'
    __prefix__ = 'BD'
    __table_args__ = (db.UniqueConstraint('diagnosis_id', 'biospecimen_id'), )
    diagnosis_id = db.Column(KfId(),
                             db.ForeignKey('diagnosis.kf_id'),
                             nullable=False)

    biospecimen_id = db.Column(KfId(),
                               db.ForeignKey('biospecimen.kf_id'),
                               nullable=False)
    external_id = db.Column(db.Text(), doc='external id used by contributor')

    biospecimen = db.relationship(Biospecimen,
                                  backref=db.backref(
                                      'biospecimen_diagnoses',
                                      cascade='all, delete-orphan'))

    diagnosis = db.relationship(Diagnosis,
                                backref=db.backref(
                                    'biospecimen_diagnoses',
                                    cascade='all, delete-orphan'))
Example #2
0
class ReadGroupGenomicFile(db.Model, Base):
    """
    Represents association table between read_group table and
    genomic_file table. Contains all read_group, genomic_file combiniations.
    :param kf_id: Unique id given by the Kid's First DCC
    :param created_at: Time of object creation
    :param modified_at: Last time of object modification
    """
    __tablename__ = 'read_group_genomic_file'
    __prefix__ = 'RF'
    __table_args__ = (db.UniqueConstraint(
        'read_group_id',
        'genomic_file_id',
    ), )
    read_group_id = db.Column(KfId(),
                              db.ForeignKey('read_group.kf_id'),
                              nullable=False)

    genomic_file_id = db.Column(KfId(),
                                db.ForeignKey('genomic_file.kf_id'),
                                nullable=False)
    external_id = db.Column(db.Text(), doc='external id used by contributor')
Example #3
0
class TaskGenomicFile(db.Model, Base):
    """
    Represents association table between task table and
    genomic_file table. Contains all task, genomic_file combiniations.

    :param kf_id: Unique id given by the Kid's First DCC
    :param created_at: Time of object creation
    :param modified_at: Last time of object modification
    :param is_input: Denotes whether the genomic file was an input to the
        executed task. True = Input, False = Output
    """

    __tablename__ = 'task_genomic_file'
    __prefix__ = 'TG'
    __table_args__ = (db.UniqueConstraint('genomic_file_id', 'task_id',
                                          'is_input'), )
    genomic_file_id = db.Column(KfId(),
                                db.ForeignKey('genomic_file.kf_id'),
                                nullable=False)

    task_id = db.Column(KfId(), db.ForeignKey('task.kf_id'), nullable=False)
    is_input = db.Column(db.Boolean(), nullable=False, default=True)
Example #4
0
class FamilyRelationship(db.Model, Base):
    """
    Represents a relationship between two family members.

    The relationship table represents a directed graph. One or more
    relationships may exist between any two participants.
    (P1 -> P2 is different than P2 -> P1)

    :param kf_id: Primary key given by the Kid's First DCC
    :param created_at: Time of object creation
    :param modified_at: Last time of object modification
    :param external_id: Name given to family_relationship by contributor
    :param participant1_id: Kids first id of the first Participant in the
    relationship
    :param participant2_id: Kids first id of the second Participant
    in the relationship
    :param relationship_type: Text describing the nature of the
    relationship (i.e. father, mother, sister, brother)
    :param _rel_name: an autogenerated parameter used to ensure that the
    relationships are not duplicated and the graph is undirected
    :param source_text_notes: Text notes from source describing the
    relationship
    """
    __tablename__ = 'family_relationship'
    __prefix__ = 'FR'
    __table_args__ = (db.UniqueConstraint(
        'participant1_id', 'participant2_id',
        'participant1_to_participant2_relation',
        'participant2_to_participant1_relation'), )
    external_id = db.Column(db.Text(), doc='external id used by contributor')
    participant1_id = db.Column(
        KfId(),
        db.ForeignKey('participant.kf_id'),
        nullable=False,
        doc='kf_id of one participant in the relationship')

    participant2_id = db.Column(
        KfId(),
        db.ForeignKey('participant.kf_id'),
        nullable=False,
        doc='kf_id of the other participant in the relationship')

    participant1_to_participant2_relation = db.Column(db.Text(),
                                                      nullable=False)

    participant2_to_participant1_relation = db.Column(db.Text())
    source_text_notes = db.Column(db.Text(),
                                  doc='Text notes from source describing '
                                  'the relationship')

    participant1 = db.relationship(
        Participant,
        primaryjoin=participant1_id == Participant.kf_id,
        backref=db.backref('outgoing_family_relationships',
                           cascade='all, delete-orphan'))

    participant2 = db.relationship(
        Participant,
        primaryjoin=participant2_id == Participant.kf_id,
        backref=db.backref('incoming_family_relationships',
                           cascade='all, delete-orphan'))

    @classmethod
    def query_all_relationships(cls,
                                participant_kf_id=None,
                                model_filter_params=None):
        """
        Find all family relationships for a participant

        :param participant_kf_id: Kids First ID of the participant
        :param model_filter_params: Filter parameters to the query

        Given a participant's kf_id, return all of the biological
        family relationships of the participant and the relationships
        of the participant's family members.

        If the participant does not have a family defined, then return
        all of the immediate/direct family relationships of the participant.
        """
        # Apply model property filter params
        if model_filter_params is None:
            model_filter_params = {}
        q = FamilyRelationship.query.filter_by(**model_filter_params)

        # Get family relationships and join with participants
        q = q.join(
            Participant,
            or_(FamilyRelationship.participant1,
                FamilyRelationship.participant2))

        # Do this bc query.get() errors out if passed None
        if participant_kf_id:
            pt = Participant.query.get(participant_kf_id)
            family_id = pt.family_id if pt else None

            # Use family to get all family relationships in participants family
            if family_id:
                q = q.filter(Participant.family_id == family_id)

            # No family provided, use just family relationships
            # to get only immediate family relationships for participant
            else:
                q = q.filter(
                    or_(
                        FamilyRelationship.participant1_id ==
                        participant_kf_id, FamilyRelationship.participant2_id
                        == participant_kf_id))

        # Don't want duplicates - return unique family relationships
        q = q.group_by(FamilyRelationship.kf_id)

        return q

    def __repr__(self):
        return '<{} is {} of {}>'.format(
            self.participant1.kf_id,
            self.participant1_to_participant2_relation,
            self.participant2.kf_id)