Example #1
0
 def test_deprecated_io(self):
     fh = StringIO()
     npt.assert_warns(UserWarning, self.dm_3x3.to_file, fh)
     fh.seek(0)
     deserialized = npt.assert_warns(UserWarning,
                                     DissimilarityMatrix.from_file, fh)
     self.assertEqual(deserialized, self.dm_3x3)
     self.assertTrue(type(deserialized) == DissimilarityMatrix)
 def test_deprecated_io(self):
     fh = StringIO()
     npt.assert_warns(UserWarning, self.ordination_results.to_file, fh)
     fh.seek(0)
     deserialized = npt.assert_warns(UserWarning,
                                     OrdinationResults.from_file, fh)
     assert_ordination_results_equal(deserialized, self.ordination_results)
     self.assertTrue(type(deserialized) == OrdinationResults)
Example #3
0
 def test_io(self):
     # Very basic check that read/write public API is present and appears to
     # be functioning. Roundtrip from memory -> disk -> memory and ensure
     # results match.
     fh = StringIO()
     self.dm_3x3.write(fh)
     fh.seek(0)
     deserialized = DissimilarityMatrix.read(fh)
     self.assertEqual(deserialized, self.dm_3x3)
     self.assertTrue(type(deserialized) == DissimilarityMatrix)
 def test_io(self):
     # Very basic check that read/write public API is present and appears to
     # be functioning. Roundtrip from memory -> disk -> memory and ensure
     # results match.
     fh = StringIO()
     self.ordination_results.write(fh)
     fh.seek(0)
     deserialized = OrdinationResults.read(fh)
     assert_ordination_results_equal(deserialized, self.ordination_results)
     self.assertTrue(type(deserialized) == OrdinationResults)
    def test_write(self):
        for fp, obj in zip(self.valid_fps, self.ordination_results_objs):
            fh = StringIO()
            _ordination_results_to_ordres(obj, fh)
            obs = fh.getvalue()
            fh.close()

            with open(fp, 'U') as fh:
                exp = fh.read()

            npt.assert_equal(obs, exp)
Example #6
0
    def test_make_study_from_cmd(self):
        fh = StringIO(self.config1)
        load_study_from_cmd('*****@*****.**', 'newstudy', fh)
        sql = ("select study_id from qiita.study where email = %s and "
               "study_title = %s")
        study_id = self.conn_handler.execute_fetchone(sql, ('*****@*****.**',
                                                            'newstudy'))
        self.assertTrue(study_id is not None)

        fh2 = StringIO(self.config2)
        with self.assertRaises(configparser.NoOptionError):
            load_study_from_cmd('*****@*****.**', 'newstudy2', fh2)
    def test_round_trip_read_write(self):
        """Test reading, writing, and reading again works as expected."""
        for dm_f in self.dm_fs:
            # Read.
            dm1 = DissimilarityMatrix.from_file(dm_f)

            # Write.
            out_f = StringIO()
            dm1.to_file(out_f)
            out_f.seek(0)

            # Read.
            dm2 = DissimilarityMatrix.from_file(out_f)
            self.assertEqual(dm1, dm2)
Example #8
0
    def test_round_trip_read_write(self):
        """Test reading, writing, and reading again works as expected."""
        for dm_f in self.dm_fs:
            # Read.
            dm1 = DissimilarityMatrix.from_file(dm_f)

            # Write.
            out_f = StringIO()
            dm1.to_file(out_f)
            out_f.seek(0)

            # Read.
            dm2 = DissimilarityMatrix.from_file(out_f)
            self.assertEqual(dm1, dm2)
Example #9
0
 def setUp(self):
     self.dm = DistanceMatrix(
         [[0.0, 1.0, 2.0], [1.0, 0.0, 3.0], [2.0, 3.0, 0.0]],
         ['a', 'b', 'c'])
     self.grouping = [1, 2, 1]
     # Ordering of IDs shouldn't matter, nor should extra IDs.
     self.df = pd.read_csv(
         StringIO('ID,Group\nb,Group1\na,Group2\nc,Group1\nd,Group3'),
         index_col=0)
     self.df_missing_id = pd.read_csv(
         StringIO('ID,Group\nb,Group1\nc,Group1'), index_col=0)
     self.categorical_stats = CategoricalStats(self.dm, self.grouping)
     self.categorical_stats_from_df = CategoricalStats(self.dm,
                                                       self.df,
                                                       column='Group')
    def test_roundtrip_read_write(self):
        for fp in self.valid_fps:
            # Read.
            obj1 = _ordres_to_ordination_results(fp)

            # Write.
            fh = StringIO()
            _ordination_results_to_ordres(obj1, fh)
            fh.seek(0)

            # Read.
            obj2 = _ordres_to_ordination_results(fh)
            fh.close()

            assert_ordination_results_equal(obj1, obj2)
 def test_to_file(self):
     """Should serialize a DissimilarityMatrix to file."""
     for dm_f_line, dm in zip(self.dm_f_lines, self.dms):
         for file_type in ('file like', 'file name'):
             if file_type == 'file like':
                 obs_f = StringIO()
                 dm.to_file(obs_f)
                 obs = obs_f.getvalue()
                 obs_f.close()
             elif file_type == 'file name':
                 with tempfile.NamedTemporaryFile('r+') as temp_file:
                     dm.to_file(temp_file.name)
                     temp_file.flush()
                     temp_file.seek(0)
                     obs = temp_file.read()
             self.assertEqual(obs, dm_f_line)
Example #12
0
 def test_to_file(self):
     """Should serialize a DissimilarityMatrix to file."""
     for dm_f_line, dm in zip(self.dm_f_lines, self.dms):
         for file_type in ('file like', 'file name'):
             if file_type == 'file like':
                 obs_f = StringIO()
                 dm.to_file(obs_f)
                 obs = obs_f.getvalue()
                 obs_f.close()
             elif file_type == 'file name':
                 with tempfile.NamedTemporaryFile('r+') as temp_file:
                     dm.to_file(temp_file.name)
                     temp_file.flush()
                     temp_file.seek(0)
                     obs = temp_file.read()
             self.assertEqual(obs, dm_f_line)
Example #13
0
    def setUp(self):
        # Distance matrices with and without ties in the ranks, with 2 groups
        # of equal size.
        dm_ids = ['s1', 's2', 's3', 's4']
        grouping_equal = ['Control', 'Control', 'Fast', 'Fast']
        df = pd.read_csv(StringIO(
            'ID,Group\ns2,Control\ns3,Fast\ns4,Fast\ns5,Control\n'
            's1,Control'),
                         index_col=0)

        self.dm_ties = DistanceMatrix(
            [[0, 1, 1, 4], [1, 0, 3, 2], [1, 3, 0, 3], [4, 2, 3, 0]], dm_ids)

        self.dm_no_ties = DistanceMatrix(
            [[0, 1, 5, 4], [1, 0, 3, 2], [5, 3, 0, 3], [4, 2, 3, 0]], dm_ids)

        # Test with 3 groups of unequal size.
        grouping_unequal = [
            'Control', 'Treatment1', 'Treatment2', 'Treatment1', 'Control',
            'Control'
        ]

        self.dm_unequal = DistanceMatrix(
            [[0.0, 1.0, 0.1, 0.5678, 1.0, 1.0],
             [1.0, 0.0, 0.002, 0.42, 0.998, 0.0],
             [0.1, 0.002, 0.0, 1.0, 0.123, 1.0],
             [0.5678, 0.42, 1.0, 0.0, 0.123, 0.43],
             [1.0, 0.998, 0.123, 0.123, 0.0, 0.5],
             [1.0, 0.0, 1.0, 0.43, 0.5, 0.0]],
            ['s1', 's2', 's3', 's4', 's5', 's6'])

        self.permanova_ties = PERMANOVA(self.dm_ties, grouping_equal)
        self.permanova_no_ties = PERMANOVA(self.dm_no_ties, grouping_equal)
        self.permanova_ties_df = PERMANOVA(self.dm_ties, df, column='Group')
        self.permanova_unequal = PERMANOVA(self.dm_unequal, grouping_unequal)
Example #14
0
    def test_to_file(self):
        for scores, test_path in zip(self.scores, self.test_paths):
            for file_type in ('file like', 'file name'):
                if file_type == 'file like':
                    obs_f = StringIO()
                    scores.to_file(obs_f)
                    obs = obs_f.getvalue()
                    obs_f.close()
                elif file_type == 'file name':
                    with tempfile.NamedTemporaryFile('r+') as temp_file:
                        scores.to_file(temp_file.name)
                        temp_file.flush()
                        temp_file.seek(0)
                        obs = temp_file.read()

                with open(get_data_path(test_path), 'U') as f:
                    exp = f.read()

                yield npt.assert_equal, obs, exp
Example #15
0
    def test_to_file(self):
        for scores, test_path in zip(self.scores, self.test_paths):
            for file_type in ('file like', 'file name'):
                if file_type == 'file like':
                    obs_f = StringIO()
                    scores.to_file(obs_f)
                    obs = obs_f.getvalue()
                    obs_f.close()
                elif file_type == 'file name':
                    with tempfile.NamedTemporaryFile('r+') as temp_file:
                        scores.to_file(temp_file.name)
                        temp_file.flush()
                        temp_file.seek(0)
                        obs = temp_file.read()

                with open(get_data_path(test_path), 'U') as f:
                    exp = f.read()

                yield npt.assert_equal, obs, exp
Example #16
0
    def test_roundtrip_read_write(self):
        for reader_fn, writer_fn, fhs in ((_dm_to_dissimilarity_matrix,
                                           _dissimilarity_matrix_to_dm,
                                           self.dissim_fhs),
                                          (_dm_to_distance_matrix,
                                           _distance_matrix_to_dm,
                                           self.dist_fhs)):
            for fh in fhs:
                # Read.
                dm1 = reader_fn(fh)

                # Write.
                out_fh = StringIO()
                writer_fn(dm1, out_fh)
                out_fh.seek(0)

                # Read.
                dm2 = reader_fn(out_fh)
                out_fh.close()

                self.assertEqual(dm1, dm2)
Example #17
0
    def summary(self, delimiter='\t'):
        """Return a formatted summary of results as a string.

        The string is formatted as delimited text.

        Parameters
        ----------
        delimiter : str, optional
            String to delimit fields by in formatted output. Default is tab
            (TSV).

        Returns
        -------
        str
            Delimited-text summary of results.

        """
        summary = StringIO()
        csv_writer = csv.writer(summary, delimiter=delimiter,
                                lineterminator='\n')
        csv_writer.writerow(self._format_header())
        csv_writer.writerow(self._format_data())
        return summary.getvalue()
Example #18
0
    def summary(self, delimiter='\t'):
        """Return a formatted summary of results as a string.

        The string is formatted as delimited text.

        Parameters
        ----------
        delimiter : str, optional
            String to delimit fields by in formatted output. Default is tab
            (TSV).

        Returns
        -------
        str
            Delimited-text summary of results.

        """
        summary = StringIO()
        csv_writer = csv.writer(summary, delimiter=delimiter,
                                lineterminator='\n')
        csv_writer.writerow(self._format_header())
        csv_writer.writerow(self._format_data())
        return summary.getvalue()
Example #19
0
    def test_write(self):
        for fn, objs, strs in ((_dissimilarity_matrix_to_dm, self.dissim_objs,
                                self.dissim_strs),
                               (_distance_matrix_to_dm, self.dist_objs,
                                self.dist_strs)):
            for obj, str_ in zip(objs, strs):
                fh = StringIO()
                fn(obj, fh)
                obs = fh.getvalue()
                fh.close()
                self.assertEqual(obs, str_)

        # Test writing CSV (TSV is written above).
        for fn, cls in ((_dissimilarity_matrix_to_dm, DissimilarityMatrix),
                        (_distance_matrix_to_dm, DistanceMatrix)):
            obj = cls(self.dm_3x3_data, ['a', 'b', 'c'])
            fh = StringIO()
            fn(obj, fh, delimiter=',')
            obs = fh.getvalue()
            fh.close()
            self.assertEqual(obs, DM_3x3_CSV)
Example #20
0
 def setUp(self):
     self.qseqs = [StringIO(qseq1), StringIO(qseq2)]
Example #21
0
    def setUp(self):
        self.fastas = [StringIO(fasta1), StringIO(fasta2), StringIO(fasta3)]
        self.quals = [StringIO(qual1), StringIO(qual2), StringIO(qual3)]

        self.bad_qual_val = [StringIO(qual1), StringIO(qual_bad_val),
                             StringIO(qual3)]
        self.bad_qual_id = [StringIO(qual1), StringIO(qual_bad_id),
                            StringIO(qual3)]
Example #22
0
 def test_from_file(self):
     """Parse a tree from a file"""
     t_io = StringIO("((a,b)c,(d,e)f)g;")
     t = TreeNode.from_file(t_io)
     self.assertEqual(list('abcdefg'), [n.name for n in t.postorder()])
Example #23
0
 def setUp(self):
     self.fastqs = [StringIO(fastq1), StringIO(fastq2)]
Example #24
0
    def setUp(self):
        self.bad_dm_fp = get_data_path('bad_dm.txt')
        self.dm_2x2_asym_fp = get_data_path('dm_2x2_asym.txt')
        self.dm_3x3_fp = get_data_path('dm_3x3.txt')

        fd = open(self.bad_dm_fp, 'U')
        self.bad_dm_f2_lines = ''.join(fd.readlines())
        fd.close()
        fd = open(self.dm_2x2_asym_fp, 'U')
        self.dm_2x2_asym_lines = ''.join(fd.readlines())
        fd.close()
        fd = open(self.dm_3x3_fp, 'U')
        self.dm_3x3_lines = ''.join(fd.readlines())
        fd.close()

        self.dm_1x1_data = [[0.0]]
        self.dm_1x1_f = StringIO(DM_1x1_F)

        self.dm_2x2_data = [[0.0, 0.123], [0.123, 0.0]]
        self.dm_2x2_f = StringIO(DM_2x2_F)

        self.dm_2x2_asym_data = [[0.0, 1.0], [-2.0, 0.0]]
        self.dm_2x2_asym_f = StringIO(self.dm_2x2_asym_lines)

        self.dm_3x3_data = [[0.0, 0.01, 4.2], [0.01, 0.0, 12.0],
                            [4.2, 12.0, 0.0]]
        self.dm_3x3_f = StringIO(self.dm_3x3_lines)
        self.dm_3x3_whitespace_f = StringIO('\n'.join(DM_3x3_WHITESPACE_F))

        self.bad_dm_f1 = StringIO(BAD_DM_F1)
        self.bad_dm_f2 = StringIO(self.bad_dm_f2_lines)
        self.bad_dm_f3 = StringIO(BAD_DM_F3)
        self.bad_dm_f4 = StringIO(BAD_DM_F4)
        self.bad_dm_f5 = StringIO(BAD_DM_F5)
        self.bad_dm_f6 = StringIO(BAD_DM_F6)
Example #25
0
 def test_is_string_or_bytes(self):
     self.assertTrue(_is_string_or_bytes('foo'))
     self.assertTrue(_is_string_or_bytes(u'foo'))
     self.assertTrue(_is_string_or_bytes(b'foo'))
     self.assertFalse(_is_string_or_bytes(StringIO('bar')))
     self.assertFalse(_is_string_or_bytes([1]))
Example #26
0
 def test_load_prep_template_from_cmd(self):
     """Correctly adds a sample template to the DB"""
     fh = StringIO(self.pt_contents)
     st = load_prep_template_from_cmd(fh, self.raw_data.id)
     self.assertEqual(st.id, self.raw_data.id)
Example #27
0
 def test_StringIO(self):
     """StringIO (useful e.g. for testing) slips through."""
     f = StringIO("File contents")
     with open_file(f) as fh:
         self.assertTrue(fh is f)