def test_update(self):
        """
        Test update investigator
        """
        # Create studies and investigator
        studies, investigator, kwargs = self.create_investigator()

        # Add new study to investigator
        s_new = Study(external_id='phs002', investigator_id=investigator.kf_id)
        db.session.add(s_new)
        db.session.commit()

        # Check database
        self.assertEqual(
            3, len(Investigator.query.get(investigator.kf_id).studies))
        self.assertIn(s_new,
                      Investigator.query.get(investigator.kf_id).studies)

        # Add study to new investigator
        i = Investigator(name='Alma')
        i.studies = [s_new]
        db.session.commit()

        # Check database
        self.assertEqual(
            i.studies[0].kf_id,
            Study.query.filter_by(external_id='phs002').one().kf_id)
        self.assertNotIn(i, Investigator.query.get(investigator.kf_id).studies)
    def create_investigator(self):
        # Create investigator
        investigator = Investigator(name='Adam',
                                    institution='CHOP',
                                    external_id='Invest_1')

        # Create studies
        studies = []
        for i in range(2):
            kwargs = {
                'attribution': ('https://dbgap.ncbi.nlm.nih.gov/'
                                'aa/wga.cgi?view_pdf&stacc=phs000178.v9.p8'),
                'external_id':
                'phs00{}'.format(i),
                'name':
                'study_{}'.format(i),
                'version':
                'v1'
            }
            studies.append(Study(**kwargs))

        # Add study to investigator
        investigator.studies.extend(studies)
        db.session.add(investigator)
        db.session.commit()

        return studies, investigator, kwargs
 def test_foreign_key_constraint(self):
     """
     Test that a investigator can be created without existing
     reference Study. This checks foreign key constraint
     """
     # Create investigator
     i = Investigator(name='Adam', institution='CHOP')
     db.session.add(i)
     db.session.commit()
     # Check for database
     self.assertEqual(1, Investigator.query.count())
示例#4
0
    def _create_studies_investigators(self, total=None):
        """
        Create studies and investigators
        investigator.csv contains list of investigator name, instistuion name
        and studies
        """
        pref_file = open('dataservice/util/data_gen/investigator.csv', 'r')
        reader = csv.reader(pref_file)
        investigator_chosen_list = []
        for row in reader:
            investigator_chosen_list.append(row)

        invest_study = random.choice(investigator_chosen_list)
        # Create Investigators
        min_investigators = 1
        max_investigators = len(invest_study)
        if not total:
            total = random.randint(min_investigators, max_investigators)
        investigators = []
        for i in range(total):
            kwargs = {'name': invest_study[0], 'institution': invest_study[1]}
            inv = Investigator(**kwargs)
            investigators.append(inv)
            db.session.add(inv)
        db.session.commit()
        # Create Studies
        studies = []
        for i in range(total):
            kwargs = {
                'attribution': ('https://dbgap.ncbi.nlm.nih.gov/'
                                'aa/wga.cgi?view_pdf&stacc=phs000178.v9.p8'),
                'external_id':
                'phs00{}'.format(i),
                'name':
                invest_study[2],
                'version':
                'v1',
                'investigator_id':
                investigators[i].kf_id
            }
            study_files = self._create_study_files(random.randint(0, total))
            s = Study(**kwargs, study_files=study_files)
            studies.append(s)
            db.session.add(s)
        db.session.commit()

        return studies
示例#5
0
    def _make_study(self, external_id='TEST-0001', include_nullables=True):
        """
        Convenience method to create a study with a given source name
        """
        inv = Investigator(name='donald duck')
        db.session.add(inv)
        db.session.flush()

        body = {
            'external_id': external_id,
            'version': '1.0',
            'release_status': 'Pending'
        }
        if include_nullables:
            body.update({'investigator_id': inv.kf_id})

        response = self.client.post(url_for(STUDY_LIST_URL),
                                    headers=self._api_headers(),
                                    data=json.dumps(body))
        return response
    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()