Beispiel #1
0
def _template_generator(study, full_access):
    """Generates tuples of prep template information

    Parameters
    ----------
    study : Study
        The study to get all the prep templates
    full_access : boolean
        A boolean that indicates if the user has full access to the study

    Returns
    -------
    Generator of tuples of (int, str, PrepTemplate, (str, str, str))
        Each tuple contains the prep template id, the prep template data_type
        the PrepTemplate object and a tuple with 3 strings for the style of
        the prep template status icons
    """

    for pt_id in sorted(study.prep_templates()):
        pt = PrepTemplate(pt_id)
        if full_access or pt.status == 'public':
            yield (pt.id, pt.data_type(), pt, STATUS_STYLER[pt.status])
Beispiel #2
0
class PreprocessedDataTests(TestCase):
    """Tests the PreprocessedData class"""
    def setUp(self):
        self.prep_template = PrepTemplate(1)
        self.study = Study(1)
        self.params_table = "preprocessed_sequence_illumina_params"
        self.params_id = 1
        fd, self.fna_fp = mkstemp(suffix='_seqs.fna')
        close(fd)
        fd, self.qual_fp = mkstemp(suffix='_seqs.qual')
        close(fd)
        self.filepaths = [(self.fna_fp, 4), (self.qual_fp, 5)]
        _, self.db_test_ppd_dir = get_mountpoint(
            'preprocessed_data')[0]
        self.ebi_submission_accession = "EBI123456-A"
        self.ebi_study_accession = "EBI123456-B"

        with open(self.fna_fp, "w") as f:
            f.write("\n")
        with open(self.qual_fp, "w") as f:
            f.write("\n")
        self._clean_up_files = []

    def tearDown(self):
        for f in self._clean_up_files:
            remove(f)

    def test_create(self):
        """Correctly creates the preprocessed data object"""
        # Check that the returned object has the correct id
        new_id = get_count('qiita.preprocessed_data') + 1
        obs = PreprocessedData.create(
            self.study, self.params_table,
            self.params_id, self.filepaths, prep_template=self.prep_template)
        self.assertEqual(obs.id, new_id)

        # Check that all the information is initialized correctly
        self.assertEqual(obs.processed_data, [])
        self.assertEqual(obs.prep_template, self.prep_template.id)
        self.assertEqual(obs.study, self.study.id)
        self.assertEqual(obs.data_type(), self.prep_template.data_type())
        self.assertEqual(obs.data_type(ret_id=True),
                         self.prep_template.data_type(ret_id=True))
        self.assertEqual(obs.submitted_to_vamps_status(), "not submitted")
        self.assertEqual(obs.processing_status, "not_processed")
        self.assertEqual(obs.status, "sandbox")
        self.assertEqual(obs.preprocessing_info,
                         (self.params_table, self.params_id))

    def test_create_data_type_only(self):
        # Check that the returned object has the correct id
        new_id = get_count('qiita.preprocessed_data') + 1
        obs = PreprocessedData.create(self.study, self.params_table,
                                      self.params_id, self.filepaths,
                                      data_type="18S")
        self.assertEqual(obs.id, new_id)

        # Check that all the information is initialized correctly
        self.assertEqual(obs.processed_data, [])
        self.assertEqual(obs.prep_template, [])
        self.assertEqual(obs.study, self.study.id)
        self.assertEqual(obs.data_type(), "18S")
        self.assertEqual(obs.data_type(ret_id=True),
                         convert_to_id("18S", "data_type"))
        self.assertEqual(obs.submitted_to_vamps_status(), "not submitted")
        self.assertEqual(obs.processing_status, "not_processed")
        self.assertEqual(obs.status, "sandbox")
        self.assertEqual(obs.preprocessing_info,
                         (self.params_table, self.params_id))

    def test_delete_basic(self):
        """Correctly deletes a preprocessed data"""
        # testing regular delete
        ppd = PreprocessedData.create(
            self.study, self.params_table,
            self.params_id, self.filepaths, prep_template=self.prep_template)
        PreprocessedData.delete(ppd.id)

        # testing that the deleted preprocessed data can't be instantiated
        with self.assertRaises(QiitaDBUnknownIDError):
            PreprocessedData(ppd.id)
        # and for completeness testing that it raises an error if ID
        # doesn't exist
        with self.assertRaises(QiitaDBUnknownIDError):
            PreprocessedData.delete(ppd.id)

        # testing that we can not remove cause the preprocessed data != sandbox
        with self.assertRaises(QiitaDBStatusError):
            PreprocessedData.delete(1)

    def test_delete_advanced(self):
        # testing that we can not remove cause preprocessed data has been
        # submitted to EBI or VAMPS
        ppd = PreprocessedData.create(
            self.study, self.params_table,
            self.params_id, self.filepaths, prep_template=self.prep_template)

        # fails due to VAMPS submission
        ppd.update_vamps_status('success')
        with self.assertRaises(QiitaDBStatusError):
            PreprocessedData.delete(ppd.id)
        ppd.update_vamps_status('failed')

        ppd = PreprocessedData(1)
        with self.assertRaises(QiitaDBStatusError):
            PreprocessedData.delete(ppd.id)

    def test_create_error_dynamic_table(self):
        """Raises an error if the preprocessed_params_table does not exist"""
        with self.assertRaises(IncompetentQiitaDeveloperError):
            PreprocessedData.create(self.study, "foo", self.params_id,
                                    self.filepaths, data_type="18S")
        with self.assertRaises(IncompetentQiitaDeveloperError):
            PreprocessedData.create(self.study, "preprocessed_foo",
                                    self.params_id, self.filepaths,
                                    data_type="18S")
        with self.assertRaises(IncompetentQiitaDeveloperError):
            PreprocessedData.create(self.study, "foo_params", self.params_id,
                                    self.filepaths, data_type="18S")
        with self.assertRaises(IncompetentQiitaDeveloperError):
            PreprocessedData.create(self.study, "preprocessed_foo_params",
                                    self.params_id, self.filepaths,
                                    data_type="18S")

    def test_create_error_data_type(self):
        with self.assertRaises(QiitaDBLookupError):
            PreprocessedData.create(self.study,
                                    "preprocessed_sequence_illumina_params",
                                    self.params_id, self.filepaths,
                                    data_type="Metabolomics")
        with self.assertRaises(IncompetentQiitaDeveloperError):
            PreprocessedData.create(self.study,
                                    "preprocessed_sequence_illumina_params",
                                    self.params_id, self.filepaths,
                                    data_type="Metabolomics",
                                    prep_template=self.prep_template)

    def test_get_filepaths(self):
        """Correctly returns the filepaths to the preprocessed files"""
        ppd = PreprocessedData(1)
        obs = ppd.get_filepaths()
        exp = [(3, join(self.db_test_ppd_dir, '1_seqs.fna'),
                "preprocessed_fasta"),
               (4, join(self.db_test_ppd_dir, '1_seqs.qual'),
                "preprocessed_fastq"),
               (5, join(self.db_test_ppd_dir, '1_seqs.demux'),
                "preprocessed_demux")]
        self.assertItemsEqual(obs, exp)

    def test_processed_data(self):
        """Correctly returns the processed data id"""
        ppd = PreprocessedData(1)
        self.assertEqual(ppd.processed_data, [1])

    def test_prep_template(self):
        """Correctly returns the prep template"""
        ppd = PreprocessedData(1)
        self.assertEqual(ppd.prep_template, 1)

    def test_study(self):
        """Correctly returns the study"""
        ppd = PreprocessedData(1)
        self.assertEqual(ppd.study, 1)

    def test_data_type(self):
        """Correctly returns the data_type of preprocessed_data"""
        pd = ProcessedData(1)
        self.assertEqual(pd.data_type(), "18S")

    def test_data_type_id(self):
        """Correctly returns the data_type of preprocessed_data"""
        pd = ProcessedData(1)
        self.assertEqual(pd.data_type(ret_id=True), 2)

    def test_link_filepaths_status(self):
        ppd = PreprocessedData(1)
        self.assertEqual(ppd.link_filepaths_status, 'idle')

    def test_link_filepaths_status_setter(self):
        ppd = PreprocessedData(1)
        self.assertEqual(ppd.link_filepaths_status, 'idle')
        ppd._set_link_filepaths_status('linking')
        self.assertEqual(ppd.link_filepaths_status, 'linking')
        ppd._set_link_filepaths_status('unlinking')
        self.assertEqual(ppd.link_filepaths_status, 'unlinking')
        ppd._set_link_filepaths_status('failed: error')
        self.assertEqual(ppd.link_filepaths_status, 'failed: error')

    def test_link_filepaths_status_setter_error(self):
        ppd = PreprocessedData(1)
        with self.assertRaises(ValueError):
            ppd._set_link_filepaths_status('not a valid status')

    def test_vamps_status(self):
        ppd = PreprocessedData(1)

        # verifying current value
        self.assertEqual(ppd.submitted_to_vamps_status(), 'not submitted')

        # changing value and then verifying new value
        ppd.update_vamps_status('failed')
        self.assertEqual(ppd.submitted_to_vamps_status(), 'failed')

        # checking failure
        with self.assertRaises(ValueError):
            ppd.update_vamps_status('not a valid status')

    def test_processing_status(self):
        """processing_status works correctly"""
        # Processed case
        ppd = PreprocessedData(1)
        self.assertEqual(ppd.processing_status, 'not_processed')

        # not processed case
        ppd = PreprocessedData.create(self.study, self.params_table,
                                      self.params_id, self.filepaths,
                                      data_type="18S")
        self.assertEqual(ppd.processing_status, 'not_processed')

    def test_processing_status_setter(self):
        """Able to update the processing status"""
        ppd = PreprocessedData.create(self.study, self.params_table,
                                      self.params_id, self.filepaths,
                                      data_type="18S")
        self.assertEqual(ppd.processing_status, 'not_processed')
        ppd.processing_status = 'processing'
        self.assertEqual(ppd.processing_status, 'processing')
        ppd.processing_status = 'processed'
        self.assertEqual(ppd.processing_status, 'processed')
        state = 'failed: some error message'
        ppd.processing_status = state
        self.assertEqual(ppd.processing_status, state)

    def test_processing_status_setter_valueerror(self):
        """Raises an error if the processing status is not recognized"""
        ppd = PreprocessedData.create(self.study, self.params_table,
                                      self.params_id, self.filepaths,
                                      data_type="18S")
        with self.assertRaises(ValueError):
            ppd.processing_status = 'not a valid state'

    def test_exists(self):
        self.assertTrue(PreprocessedData.exists(1))
        self.assertFalse(PreprocessedData.exists(1000))

    def test_status(self):
        ppd = PreprocessedData(1)
        self.assertEqual(ppd.status, 'private')

        # Since the status is inferred from the processed data, change the
        # status of the processed data so we can check how it changes in the
        # preprocessed data
        pd = ProcessedData(1)
        pd.status = 'public'
        self.assertEqual(ppd.status, 'public')

        # Check that new preprocessed data has sandbox as status since no
        # processed data exists for them
        ppd = PreprocessedData.create(self.study, self.params_table,
                                      self.params_id, self.filepaths,
                                      data_type="16S")
        self.assertEqual(ppd.status, 'sandbox')

    def test_is_submitted_to_ebi(self):
        self.assertTrue(PreprocessedData(1).is_submitted_to_ebi)
        self.assertFalse(PreprocessedData(2).is_submitted_to_ebi)

    def test_ebi_run_accessions(self):
        exp = ['ERR0000001', 'ERR0000002', 'ERR0000003', 'ERR0000004',
               'ERR0000005', 'ERR0000006', 'ERR0000007', 'ERR0000008',
               'ERR0000009', 'ERR0000010', 'ERR0000011', 'ERR0000012',
               'ERR0000013', 'ERR0000014', 'ERR0000015', 'ERR0000016',
               'ERR0000017', 'ERR0000018', 'ERR0000019', 'ERR0000020',
               'ERR0000021', 'ERR0000022', 'ERR0000023', 'ERR0000024',
               'ERR0000025', 'ERR0000026', 'ERR0000027']
        self.assertEqual(PreprocessedData(1).ebi_run_accessions, exp,)
        self.assertEqual(PreprocessedData(2).ebi_run_accessions, [])

    def test_ebi_run_accessions_setter(self):
        new_vals = {
            '1.SKB1.640202': 'ERR1000001',
            '1.SKB2.640194': 'ERR1000002',
            '1.SKB3.640195': 'ERR1000003',
            '1.SKB4.640189': 'ERR1000004',
            '1.SKB5.640181': 'ERR1000005',
            '1.SKB6.640176': 'ERR1000006',
            '1.SKB7.640196': 'ERR1000007',
            '1.SKB8.640193': 'ERR1000008',
            '1.SKB9.640200': 'ERR1000009',
            '1.SKD1.640179': 'ERR1000010',
            '1.SKD2.640178': 'ERR1000011',
            '1.SKD3.640198': 'ERR1000012',
            '1.SKD4.640185': 'ERR1000013',
            '1.SKD5.640186': 'ERR1000014',
            '1.SKD6.640190': 'ERR1000015',
            '1.SKD7.640191': 'ERR1000016',
            '1.SKD8.640184': 'ERR1000017',
            '1.SKD9.640182': 'ERR1000018',
            '1.SKM1.640183': 'ERR1000019',
            '1.SKM2.640199': 'ERR1000020',
            '1.SKM3.640197': 'ERR1000021',
            '1.SKM4.640180': 'ERR1000022',
            '1.SKM5.640177': 'ERR1000023',
            '1.SKM6.640187': 'ERR1000024',
            '1.SKM7.640188': 'ERR1000025',
            '1.SKM8.640201': 'ERR1000026',
            '1.SKM9.640192': 'ERR1000027'}
        with self.assertRaises(QiitaDBError):
            PreprocessedData(1).ebi_run_accessions = new_vals

        ppd = PreprocessedData(2)
        self.assertEqual(ppd.ebi_run_accessions, [])
        ppd.ebi_run_accessions = new_vals
        self.assertEqual(ppd.ebi_run_accessions, sorted(new_vals.values()))