示例#1
0
    def get(self, filter_params, after, limit):
        """
        Get all family_relationships
        ---
        description: Get all family_relationships
        template:
          path:
            get_list.yml
          properties:
            resource:
              FamilyRelationship
        """
        # Get and remove special filter parameters - those which are not
        # part of model properties
        # Study id
        study_id = filter_params.pop('study_id', None)
        # Participant id
        participant_id = filter_params.pop('participant_id', None)

        # Get family relationships joined w participants
        q = FamilyRelationship.query_all_relationships(
            participant_kf_id=participant_id,
            model_filter_params=filter_params)

        # Filter by study
        if study_id:
            from dataservice.api.participant.models import Participant
            q = (q.filter(Participant.study_id == study_id))

        return (FamilyRelationshipSchema(many=True)
                .jsonify(Pagination(q, after, limit)))
示例#2
0
    def _create_save_to_db(self):
        """
        Create and save family_relationship

        Requires creating a participant
        Create a family_relationship and add it to participant as kwarg
        Save participant
        """
        # Create study
        study = Study(external_id='phs001')

        # Create participants
        p1 = Participant(external_id='Fred', is_proband=False)
        p2 = Participant(external_id='Wilma', is_proband=False)
        p3 = Participant(external_id='Pebbles', is_proband=True)
        p4 = Participant(external_id='Dino', is_proband=True)

        study.participants.extend([p1, p2, p3, p4])
        db.session.add(study)
        db.session.commit()

        # Create family_relationship
        kwargs = {
            'participant1_id': p1.kf_id,
            'participant2_id': p3.kf_id,
            'participant1_to_participant2_relation': 'father',
            'source_text_notes': 'Notes 1'
        }
        fr = FamilyRelationship(**kwargs)

        db.session.add(fr)
        db.session.commit()
        kwargs['kf_id'] = fr.kf_id
        kwargs['participant2_to_participant1_relation'] = \
            fr.participant2_to_participant1_relation

        fr.external_id = str(fr)
        db.session.commit()

        return p1, p2, p3, p4, study, kwargs
示例#3
0
 def _create_family_relationships(self):
     """
     Create family relationships between pairs of participants
     """
     participants = Participant.query.all()
     for participant, relative in self._pairwise(participants):
         gender = participant.gender
         rel = 'mother'
         if gender == 'male':
             rel = 'father'
         r = FamilyRelationship(participant=participant, relative=relative,
                                participant_to_relative_relation=rel)
         db.session.add(r)
     db.session.commit()
    def participants(client):

        # Add a bunch of studies for pagination
        for i in range(101):
            s = Study(external_id='Study_{}'.format(i))
            db.session.add(s)

        for i in range(101):
            ca = CavaticaApp(name='app', revision=0)
            db.session.add(ca)

        # Add a bunch of study files
        s0 = Study.query.filter_by(external_id='Study_0').one()
        s1 = Study.query.filter_by(external_id='Study_1').one()
        for i in range(101):
            sf = StudyFile(file_name='blah', study_id=s0.kf_id)
            db.session.add(sf)

        # Add a bunch of investigators
        for _ in range(102):
            inv = Investigator(name='test')
            inv.studies.extend([s0, s1])
            db.session.add(inv)

        # Add a bunch of families
        families = []
        for i in range(101):
            families.append(Family(external_id='Family_{}'.format(i)))
        db.session.add_all(families)
        db.session.flush()

        participants = []
        f0 = Family.query.filter_by(external_id='Family_0').one()
        f1 = Family.query.filter_by(external_id='Family_1').one()
        seq_cen = None
        for i in range(102):
            f = f0 if i < 50 else f1
            s = s0 if i < 50 else s1
            data = {
                'external_id': "test",
                'is_proband': True,
                'race': 'Asian',
                'ethnicity': 'Hispanic or Latino',
                'diagnosis_category': 'Cancer',
                'gender': 'Male'
            }
            p = Participant(**data, study_id=s.kf_id, family_id=f.kf_id)
            diag = Diagnosis()
            p.diagnoses = [diag]
            outcome = Outcome()
            p.outcomes = [outcome]
            phen = Phenotype()
            p.phenotypes = [phen]
            participants.append(p)
            db.session.add(p)
            db.session.flush()

            seq_data = {
                'external_id': 'Seq_0',
                'experiment_strategy': 'WXS',
                'library_name': 'Test_library_name_1',
                'library_strand': 'Unstranded',
                'is_paired_end': False,
                'platform': 'Test_platform_name_1'
            }
            gf_kwargs = {
                'external_id': 'gf_0',
                'file_name': 'hg38.fq',
                'data_type': 'Aligned Reads',
                'file_format': 'fastq',
                'size': 1000,
                'urls': ['s3://bucket/key'],
                'hashes': {
                    'md5': str(uuid.uuid4())
                },
                'controlled_access': False
            }
            seq_cen = SequencingCenter.query.filter_by(name="Baylor")\
                .one_or_none()
            if seq_cen is None:
                seq_cen = SequencingCenter(external_id='SC_0', name="Baylor")
                db.session.add(seq_cen)
                db.session.flush()
            seq_exp = SequencingExperiment(**seq_data,
                                           sequencing_center_id=seq_cen.kf_id)
            db.session.add(seq_exp)
            samp = Biospecimen(analyte_type='an analyte',
                               sequencing_center_id=seq_cen.kf_id,
                               participant=p)
            db.session.add(samp)
            p.biospecimens = [samp]

            gf = GenomicFile(**gf_kwargs,
                             sequencing_experiment_id=seq_exp.kf_id)
            db.session.add(gf)
            samp.genomic_files.append(gf)
            samp.diagnoses.append(diag)

            db.session.flush()

            rg = ReadGroup(lane_number=4, flow_cell='FL0123')
            rg.genomic_files.append(gf)

            ct = CavaticaTask(name='task_{}'.format(i))
            ct.genomic_files.append(gf)
            ca.cavatica_tasks.append(ct)

        # Family relationships
        for participant1, participant2 in iterate_pairwise(participants):
            gender = participant1.gender
            rel = 'mother'
            if gender == 'male':
                rel = 'father'
            r = FamilyRelationship(participant1=participant1,
                                   participant2=participant2,
                                   participant1_to_participant2_relation=rel)
            db.session.add(r)

        db.session.commit()
示例#5
0
def make_entities(client):
    # Create initial entities
    with db.session.no_autoflush:
        _entities = defaultdict(list)
        for model, endpoint in ENTITY_ENDPOINT_MAP.items():
            if model in {
                    FamilyRelationship, TaskGenomicFile,
                    BiospecimenGenomicFile, BiospecimenDiagnosis,
                    ReadGroupGenomicFile, SequencingExperimentGenomicFile
            }:
                continue
            for i in range(ENTITY_TOTAL):
                data = ENTITY_PARAMS['fields'][endpoint].copy()
                if i % 2 != 0:
                    if endpoint in ENTITY_PARAMS['filter_params']:
                        data.update(
                            ENTITY_PARAMS.get('filter_params')[endpoint]
                            ['valid'])
                if model == Participant:
                    data['external_id'] = 'Participant_{}'.format(i)
                if model == SequencingCenter:
                    _name = 'SequencingCenter_{}'.format(i)
                    data['name'] = _name
                    ENTITY_PARAMS['fields']['/sequencing-centers'].update(
                        {'name': _name})
                m = model(**data)
                _entities[model].append(m)

                db.session.add(m)

        # Family relationships
        for participant, participant2 in iterate_pairwise(
                _entities[Participant]):
            gender = participant.gender
            rel = 'mother'
            if gender == 'male':
                rel = 'father'
            r = FamilyRelationship(participant1=participant,
                                   participant2=participant2,
                                   participant1_to_participant2_relation=rel)
            if model not in _entities:
                _entities[FamilyRelationship] = []
            _entities[FamilyRelationship].append(r)

            ENTITY_PARAMS['fields']['/family-relationships'].update(
                {'participant1_to_participant2_relation': rel})

            db.session.add(r)

        # Task genomic files
        for i, (ct,
                gf) in enumerate(zip(_entities[Task], _entities[GenomicFile])):
            is_input = True
            if i % 2 == 0:
                is_input = False
            ctgf = TaskGenomicFile(task=ct, genomic_file=gf, is_input=is_input)
            _entities[TaskGenomicFile].append(ctgf)

            ENTITY_PARAMS['fields']['/task-genomic-files'].update(
                {'is_input': is_input})

            db.session.add(ctgf)

        # Biospecimen genomic files
        for i, (bs, gf) in enumerate(
                zip(_entities[Biospecimen], _entities[GenomicFile])):
            bsgf = BiospecimenGenomicFile(biospecimen=bs, genomic_file=gf)
            _entities[BiospecimenGenomicFile].append(bsgf)
            db.session.add(bsgf)

        # Read Group Genomic Files
        for i, (rg, gf) in enumerate(
                zip(_entities[ReadGroup], _entities[GenomicFile])):
            rggf = ReadGroupGenomicFile(read_group=rg, genomic_file=gf)
            _entities[ReadGroupGenomicFile].append(rggf)
            db.session.add(rggf)

        # Sequencing Experiment Genomic Files
        for i, (se, gf) in enumerate(
                zip(_entities[SequencingExperiment], _entities[GenomicFile])):
            segf = SequencingExperimentGenomicFile(sequencing_experiment=se,
                                                   genomic_file=gf)
            _entities[SequencingExperimentGenomicFile].append(segf)
            db.session.add(segf)

        # Biospecimen genomic files
        for i, (b, d) in enumerate(
                zip(_entities[Biospecimen], _entities[Diagnosis])):
            bd = BiospecimenDiagnosis(biospecimen=b, diagnosis=d)
            _entities[BiospecimenDiagnosis].append(bd)
            db.session.add(bd)

        # Add relations
        s0 = _entities[Study][0]
        f0 = _entities[Family][0]
        p0 = _entities[Participant][0]
        sc0 = _entities[SequencingCenter][0]
        ca0 = _entities[CavaticaApp][0]

        # Investigator
        for inv in _entities[Investigator]:
            inv.studies.append(s0)
        # Study file
        for sf in _entities[StudyFile]:
            sf.study = s0
        # Participant
        for ent in _entities[Participant]:
            ent.study = s0
            ent.family = f0

        # Biospecimen, Diagnosis, Phenotype, Outcome
        participant_ents = [Biospecimen, Diagnosis, Phenotype, Outcome]
        for participant_ent in participant_ents:
            for ent in _entities[participant_ent]:
                ent.participant = p0
                if Biospecimen == participant_ent:
                    ent.sequencing_center = sc0
        # SequencingExperiment
        for ent in _entities[SequencingExperiment]:
            ent.sequencing_center = sc0

        # Task
        for ent in _entities[CavaticaApp]:
            ent.cavatica_app = ca0

        db.session.commit()

    return _entities
示例#6
0
    def test_special_filter_param(self):
        """
        Test special filter param participant_id

        /family-relationships?participant_id
        """
        # Add some family relationships
        p1, p2, p3, p4, s1, kwargs = self._create_save_to_db()
        r2 = FamilyRelationship(participant1=p1,
                                participant2=p4,
                                participant1_to_participant2_relation='father')
        r3 = FamilyRelationship(participant1=p2,
                                participant2=p3,
                                participant1_to_participant2_relation='mother')
        r4 = FamilyRelationship(participant1=p2,
                                participant2=p4,
                                participant1_to_participant2_relation='mother')
        db.session.add_all([r2, r3, r4])
        db.session.commit()

        # Case 1 - Participant with no family defined
        url = (url_for(FAMILY_RELATIONSHIPS_LIST_URL) +
               '?participant_id={}'.format(p3.kf_id))
        response = self.client.get(url, headers=self._api_headers())
        self.assertEqual(response.status_code, 200)
        response = json.loads(response.data.decode("utf-8"))
        content = response.get('results')
        # Only immediate family relationships returned
        self.assertEqual(len(content), 2)

        # Test with additional filter parameters
        url = (url_for(FAMILY_RELATIONSHIPS_LIST_URL) + '?participant_id={}'
               '&study_id={}&participant1_to_participant2_relation={}'.format(
                   p3.kf_id, s1.kf_id, 'father'))
        response = self.client.get(url, headers=self._api_headers())
        self.assertEqual(response.status_code, 200)
        response = json.loads(response.data.decode("utf-8"))
        content = response.get('results')
        self.assertEqual(len(content), 1)

        # Case 2 - Participant with a family defined
        f0 = Family(external_id='phs001-family')
        f0.participants.extend([p1, p2, p3, p4])
        db.session.add(f0)
        db.session.commit()

        url = (url_for(FAMILY_RELATIONSHIPS_LIST_URL) +
               '?participant_id={}'.format(p3.kf_id))
        response = self.client.get(url, headers=self._api_headers())
        self.assertEqual(response.status_code, 200)
        response = json.loads(response.data.decode("utf-8"))
        content = response.get('results')
        # All family relationships returned
        self.assertEqual(len(content), 4)

        # Add another study with a family and relationships
        s2 = Study(external_id='phs002')
        f2 = Family(external_id='phs002-family')
        p_1 = Participant(external_id='Fred_1', is_proband=False)
        p_2 = Participant(external_id='Wilma_1', is_proband=False)
        p_3 = Participant(external_id='Pebbles_1', is_proband=True)

        r_1 = FamilyRelationship(
            participant1=p_1,
            participant2=p_3,
            participant1_to_participant2_relation='father')
        r_2 = FamilyRelationship(
            participant1=p_2,
            participant2=p_3,
            participant1_to_participant2_relation='mother')

        s2.participants.extend([p_1, p_2, p_3])
        f2.participants.extend([p_1, p_2, p_3])
        db.session.add(s2)
        db.session.add(f2)
        db.session.add_all([r_1, r_2])
        db.session.commit()

        # Should see same results for p3
        url = (url_for(FAMILY_RELATIONSHIPS_LIST_URL) +
               '?participant_id={}'.format(p3.kf_id))
        response = self.client.get(url, headers=self._api_headers())
        self.assertEqual(response.status_code, 200)
        response = json.loads(response.data.decode("utf-8"))
        content = response.get('results')
        # All family relationships returned
        self.assertEqual(len(content), 4)