def _create_outcomes(self): """ Create outcome and required entities """ # Create study study = Study(external_id='phs001') # Create two outcomes oc = ['Deceased', 'Alive'] o1 = Outcome(vital_status=oc[0]) o2 = Outcome(vital_status=oc[1]) p = Participant(external_id='p1', is_proband=True, study=study) # Add to participant and save p.outcomes.extend([o1, o2]) db.session.add(p) db.session.commit() return [o1, o2], p
def test_create(self): """ Test create outcome """ # Create study study = Study(external_id='phs001') # Create and save participant participant_id = 'Test subject 0' p = Participant(external_id=participant_id, is_proband=True, study=study) db.session.add(p) db.session.commit() # Create outcomes data = { 'external_id': 'test_0', 'vital_status': 'Alive', 'disease_related': False, 'age_at_event_days': 120, 'participant_id': p.kf_id } dt = datetime.now() o1 = Outcome(**data) db.session.add(o1) data['vital_status'] = 'Deceased' data['disease_related'] = 'True' o2 = Outcome(**data) db.session.add(o2) db.session.commit() self.assertEqual(Outcome.query.count(), 2) new_outcome = Outcome.query.all()[1] self.assertGreater(new_outcome.created_at, dt) self.assertGreater(new_outcome.modified_at, dt) self.assertIs(type(uuid.UUID(new_outcome.uuid)), uuid.UUID) self.assertEqual(new_outcome.vital_status, data['vital_status']) self.assertEqual(new_outcome.disease_related, data['disease_related'])
def test_foreign_key_constraint(self): """ Test that a outcome cannot be created without an existing reference Participant. This checks foreign key constraint """ # Create outcome data = { 'vital_status': 'Alive', 'participant_id': '' # empty blank foreign key } o = Outcome(**data) # Add to db self.assertRaises(IntegrityError, db.session.add(o))
def test_not_null_constraint(self): """ Test that a outcome cannot be created without required parameters such as participant_id """ # Create outcome data = { 'vital_status': 'Alive', # non-existent required param: participant_id } o = Outcome(**data) # Add to db self.assertRaises(IntegrityError, db.session.add(o))
def _create_outcomes(self, total): """ creates outcomes """ outcomes_list = [] for i in range(total): # Fill disaese_related only if person is dead vs = random.choice(self.vital_status_list) if vs == 'Dead': dr = random.choice(self.disease_related_list) data = { 'vital_status': vs, 'disease_related': dr, 'age_at_event_days': random.randint(0, 32872) } outcomes_list.append(Outcome(**data)) break else: data = { 'vital_status': vs, 'age_at_event_days': random.randint(0, 32872) } outcomes_list.append(Outcome(**data)) return outcomes_list
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()