Esempio n. 1
0
    def test_process_pedline_relationship(self):
        """Test a pedline with father or mother ids"""

        father, mother, child = self.get_relationships()

        # process data and insert records
        for i, item in enumerate([father, mother, child]):
            test = self.plinkio._process_pedline(item, self.dataset, 'top')

            # define smarter_id
            smarter_id = f"ITOA-TEX-00000000{i+1}"

            # test ped line items
            self.assertEqual(test[0], "TEX")
            self.assertEqual(test[1], smarter_id)
            self.assertEqual(test[4], item[4])

            # assert database objects
            sample = SampleSheep.objects(smarter_id=smarter_id).get()

            # special child case
            if item == child:
                sample_father = SampleSheep.objects(
                    original_id=item[2], dataset=self.dataset).get()
                sample_mother = SampleSheep.objects(
                    original_id=item[3], dataset=self.dataset).get()

                self.assertIsNone(sample.sex)
                self.assertEqual(sample.father_id, sample_father)
                self.assertEqual(sample.mother_id, sample_mother)
                self.assertEqual(test[2], sample_father.smarter_id)
                self.assertEqual(test[3], sample_mother.smarter_id)

            else:
                self.assertEqual(sample.sex, SEX(int(item[4])))
Esempio n. 2
0
    def setUp(self):
        self.runner = CliRunner()

        # add a sample in database
        self.sample = SampleSheep(
            original_id="test-1",
            country="Italy",
            species="Sheep",
            breed="Texel",
            breed_code="TEX",
            dataset=self.dst_dataset,
            chip_name=self.chip_name,
        )
        self.sample.save()
    def setUp(self):
        self.runner = CliRunner()

        # need also a sample
        self.sample = SampleSheep(
            original_id="test-1",
            smarter_id="ITOA-TEX-000000001",
            country="Italy",
            species="Sheep",
            breed="Texel",
            breed_code="TEX",
            dataset=self.dst_dataset,
            chip_name=self.chip_name,
        )
        self.sample.save()
Esempio n. 4
0
    def test_update_relationship(self):
        """Test the possibility to update a sample relationship"""

        # ped lines could be in the wrong order. In such way, a child sample
        # could be written before its parents, and so ped lines can't be
        # written correctly

        # define fake relationships
        father, mother, child = self.get_relationships()

        # process data and insert records
        for i, item in enumerate([child, father, mother]):
            self.plinkio._process_pedline(item, self.dataset, 'top')

            # special child case
            if item == child:
                # define smarter_id
                smarter_id = f"ITOA-TEX-00000000{i+1}"

                # assert database objects
                sample_child = SampleSheep.objects(smarter_id=smarter_id).get()

        # assert child has no relationship
        self.assertIsNone(sample_child.father_id)
        self.assertIsNone(sample_child.mother_id)
        self.assertIsNone(sample_child.sex)

        # ok now try to process child again
        test = self.plinkio._process_pedline(child, self.dataset, 'top')

        # refresh database object
        sample_child.reload()
        sample_father = sample_child.father_id.fetch()
        sample_mother = sample_child.mother_id.fetch()

        self.assertEqual(sample_child.smarter_id, test[1])
        self.assertEqual(sample_father.smarter_id, test[2])
        self.assertEqual(sample_mother.smarter_id, test[3])
Esempio n. 5
0
class TestImportSamples(
        SmarterIDMixin, IlluminaChipMixin, MongoMockMixin, unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        # getting destination dataset
        cls.dst_dataset = Dataset.objects.get(file="test.zip")

        # create a src dataset
        cls.src_dataset = Dataset(
            file="test2.zip",
            country="Italy",
            species="Sheep",
            contents=[
                "metadata.xlsx"
            ]
        )
        cls.src_dataset.save()

        # create a workbook
        cls.workbook = Workbook()
        cls.sheet = cls.workbook.active

        # adding header
        cls.sheet.cell(row=1, column=1, value="Code")
        cls.sheet.cell(row=1, column=2, value="Id")
        cls.sheet.cell(row=1, column=3, value="Country")
        cls.sheet.cell(row=1, column=4, value="Sex")

        # adding values
        cls.sheet.cell(row=2, column=1, value="TEX_IT")
        cls.sheet.cell(row=2, column=2, value="test-1")
        cls.sheet.cell(row=2, column=3, value="Italy")
        cls.sheet.cell(row=2, column=4, value="BHO")

        # adding values
        cls.sheet.cell(row=3, column=1, value="TEX_IT")
        cls.sheet.cell(row=3, column=2, value="test-2")
        cls.sheet.cell(row=3, column=3, value="SPAIN")
        cls.sheet.cell(row=3, column=4, value="F")

    def setUp(self):
        self.runner = CliRunner()

        # add a sample in database
        self.sample = SampleSheep(
            original_id="test-1",
            country="Italy",
            species="Sheep",
            breed="Texel",
            breed_code="TEX",
            dataset=self.dst_dataset,
            chip_name=self.chip_name,
        )
        self.sample.save()

    def tearDown(self):
        SampleSheep.objects.delete()

        super().tearDown()

    def test_help(self):
        result = self.runner.invoke(import_samples, ["--help"])
        self.assertEqual(0, result.exit_code)
        self.assertIn('Usage: main', result.output)

    @patch('src.features.smarterdb.Dataset.working_dir',
           new_callable=PropertyMock)
    def test_import_mandatory(self, my_working_dir):
        # create a temporary directory using the context manager
        with tempfile.TemporaryDirectory() as tmpdirname:
            working_dir = pathlib.Path(tmpdirname)
            my_working_dir.return_value = working_dir

            # save worksheet in temporary folder
            self.workbook.save(f"{working_dir}/metadata.xlsx")

            # got first sample from database
            self.assertEqual(SampleSheep.objects.count(), 1)

            result = self.runner.invoke(
                import_samples,
                [
                    "--src_dataset",
                    "test2.zip",
                    "--dst_dataset",
                    "test.zip",
                    "--datafile",
                    "metadata.xlsx",
                    "--code_column",
                    "Code",
                    "--country_column",
                    "Country",
                    "--id_column",
                    "Id",
                    "--chip_name",
                    self.chip_name
                ]
            )

            self.assertEqual(0, result.exit_code, msg=result.exception)

            # I should have two record for samples, One already present one new
            self.assertEqual(SampleSheep.objects.count(), 2)

            # get the new sample
            sample = SampleSheep.objects.get(original_id="test-2")
            self.assertEqual(sample.smarter_id, "ESOA-TEX-000000002")

    @patch('src.features.smarterdb.Dataset.working_dir',
           new_callable=PropertyMock)
    def test_import_with_sex(self, my_working_dir):
        # create a temporary directory using the context manager
        with tempfile.TemporaryDirectory() as tmpdirname:
            working_dir = pathlib.Path(tmpdirname)
            my_working_dir.return_value = working_dir

            # save worksheet in temporary folder
            self.workbook.save(f"{working_dir}/metadata.xlsx")

            # got first sample from database
            self.assertEqual(SampleSheep.objects.count(), 1)

            result = self.runner.invoke(
                import_samples,
                [
                    "--src_dataset",
                    "test2.zip",
                    "--dst_dataset",
                    "test.zip",
                    "--datafile",
                    "metadata.xlsx",
                    "--code_column",
                    "Code",
                    "--country_column",
                    "Country",
                    "--id_column",
                    "Id",
                    "--sex_column",
                    "Sex",
                    "--chip_name",
                    self.chip_name
                ]
            )

            self.assertEqual(0, result.exit_code, msg=result.exception)

            # I should have two record for samples, One already present one new
            self.assertEqual(SampleSheep.objects.count(), 2)

            # first sampla wasn't been updated. Unknown sex
            self.sample.reload()
            self.assertIsNone(self.sample.sex)

            # get the new sample
            sample = SampleSheep.objects.get(original_id="test-2")
            self.assertEqual(sample.smarter_id, "ESOA-TEX-000000002")
            self.assertEqual(sample.sex, SEX.FEMALE)
class PhenotypeMixin(SmarterIDMixin, IlluminaChipMixin, MongoMockMixin):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        # getting destination dataset
        cls.dst_dataset = Dataset.objects.get(file="test.zip")

        # create a src dataset
        cls.src_dataset = Dataset(file="test2.zip",
                                  country="Italy",
                                  species="Sheep",
                                  contents=["phenotypes.xlsx"])
        cls.src_dataset.save()

        # create a workbook
        cls.workbook = Workbook()
        cls.sheet = cls.workbook.active

        # adding header
        cls.sheet.cell(row=1, column=1, value="Code")
        cls.sheet.cell(row=1, column=2, value="Name")
        cls.sheet.cell(row=1, column=3, value="Country")
        cls.sheet.cell(row=1, column=4, value="Id")
        cls.sheet.cell(row=1, column=5, value="ChestGirth")
        cls.sheet.cell(row=1, column=6, value="Height")
        cls.sheet.cell(row=1, column=7, value="Length")
        cls.sheet.cell(row=1, column=8, value="WidthOfPinBones")
        cls.sheet.cell(row=1, column=9, value="FAMACHA")
        cls.sheet.cell(row=1, column=10, value="Purpose")

        # adding values
        cls.sheet.cell(row=2, column=1, value="TEX")
        cls.sheet.cell(row=2, column=2, value="Texel")
        cls.sheet.cell(row=2, column=3, value="Italy")
        cls.sheet.cell(row=2, column=4, value="test-1")
        cls.sheet.cell(row=2, column=5, value=77.5)
        cls.sheet.cell(row=2, column=6, value=60.5)
        cls.sheet.cell(row=2, column=7, value=69.5)
        cls.sheet.cell(row=2, column=8, value=16)
        cls.sheet.cell(row=2, column=9, value="D")
        cls.sheet.cell(row=2, column=10, value="Milk")

    @classmethod
    def tearDownClass(cls):
        Dataset.objects.delete()
        SampleSheep.objects.delete()

        super().tearDownClass()

    def setUp(self):
        self.runner = CliRunner()

        # need also a sample
        self.sample = SampleSheep(
            original_id="test-1",
            smarter_id="ITOA-TEX-000000001",
            country="Italy",
            species="Sheep",
            breed="Texel",
            breed_code="TEX",
            dataset=self.dst_dataset,
            chip_name=self.chip_name,
        )
        self.sample.save()

    def tearDown(self):
        SampleSheep.objects.delete()

        super().tearDown()
Esempio n. 7
0
class MetaDataMixin(SmarterIDMixin, IlluminaChipMixin, MongoMockMixin):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()

        # getting destination dataset
        cls.dst_dataset = Dataset.objects.get(file="test.zip")

        # create a src dataset
        cls.src_dataset = Dataset(file="test2.zip",
                                  country="Italy",
                                  species="Sheep",
                                  contents=["metadata.xlsx"])
        cls.src_dataset.save()

        # create a workbook
        cls.workbook = Workbook()
        cls.sheet = cls.workbook.active

        # adding header
        cls.sheet.cell(row=1, column=1, value="Code")
        cls.sheet.cell(row=1, column=2, value="Name")
        cls.sheet.cell(row=1, column=3, value="Country")
        cls.sheet.cell(row=1, column=4, value="Lat")
        cls.sheet.cell(row=1, column=5, value="Lon")
        cls.sheet.cell(row=1, column=6, value="Col1")
        cls.sheet.cell(row=1, column=7, value="Col 2")
        cls.sheet.cell(row=1, column=8, value="Id")

        # adding values
        cls.sheet.cell(row=2, column=1, value="TEX")
        cls.sheet.cell(row=2, column=2, value="Texel")
        cls.sheet.cell(row=2, column=3, value="Italy")
        cls.sheet.cell(row=2, column=4, value=45.46427)
        cls.sheet.cell(row=2, column=5, value=9.18951)
        cls.sheet.cell(row=2, column=6, value="Val1")
        cls.sheet.cell(row=2, column=7, value="Val2")
        cls.sheet.cell(row=2, column=8, value="test-1")

    @classmethod
    def tearDownClass(cls):
        Dataset.objects.delete()
        SampleSheep.objects.delete()

        super().tearDownClass()

    def setUp(self):
        self.runner = CliRunner()

        # need also a sample
        self.sample = SampleSheep(
            original_id="test-1",
            smarter_id="ITOA-TEX-000000001",
            country="Italy",
            species="Sheep",
            breed="Texel",
            breed_code="TEX",
            dataset=self.dst_dataset,
            chip_name=self.chip_name,
        )
        self.sample.save()

    def tearDown(self):
        SampleSheep.objects.delete()

        super().tearDown()