예제 #1
0
    def test_to_pandas_data_frame_to_biom_table(self):
        filepath = self.get_data_path('feature-table_v100.biom')
        transformer1 = self.get_transformer(BIOMV100Format, pd.DataFrame)
        input = BIOMV100Format(filepath, mode='r')
        df = transformer1(input)

        transformer2 = self.get_transformer(pd.DataFrame, biom.Table)
        obs = transformer2(df)
        self.assertIsInstance(obs, biom.Table)
예제 #2
0
def cluster_closed_reference(sequences: QIIME1DemuxFormat,
                             reference_database: NinjaOpsDBDirFmt) \
                                     -> (BIOMV100Format, QIIME1DemuxFormat):
    # Input paths supplied to ninja.py.
    sequences_fp = str(sequences)
    reference_database_dir = os.path.join(str(reference_database), 'db')

    # Output directory to store ninja.py results.
    output_dirfmt = model.DirectoryFormat()
    output_dir = str(output_dirfmt)

    cmd = [
        'ninja.py', '--input', sequences_fp, '--database',
        reference_database_dir, '--output', output_dir, '--full_output'
    ]
    run_command(cmd)

    biom_fp = os.path.join(output_dir, 'ninja_otutable.biom')
    output_biom_fmt = BIOMV100Format(biom_fp, mode='r')
    # Keep a reference to the DirectoryFormat this BIOM file resides in so that
    # the directory isn't deleted when `output_dirfmt` goes out of scope upon
    # function exit. The directory will be cleaned up appropriately when
    # `output_biom_fmt` is cleaned up and avoids copying the BIOM file.
    output_biom_fmt.__dirfmt = output_dirfmt

    # Get the set of IDs that failed to hit the reference database.
    failed_ids = set()
    failed_ids_fp = os.path.join(output_dir, 'ninja_fail.txt')
    with open(failed_ids_fp, 'r') as fh:
        for line in fh:
            id = line.rstrip('\n')
            failed_ids.add(id)

    # Filter the input sequences to only those that failed to hit the reference
    # database.
    output_failures_fmt = QIIME1DemuxFormat()
    with output_failures_fmt.open() as fh:
        for seq in skbio.io.read(sequences_fp, format='fasta'):
            id = seq.metadata['id']
            if id in failed_ids:
                # Turning off roundtripping options to speed up writing. We can
                # safely turn these options off because we know the sequence
                # IDs are rountrip-safe since we're reading them from a FASTA
                # file.
                #
                # http://scikit-bio.org/docs/latest/generated/
                #     skbio.io.format.fasta.html#writer-specific-parameters
                seq.write(fh,
                          id_whitespace_replacement=None,
                          description_newline_replacement=None)

    return output_biom_fmt, output_failures_fmt
예제 #3
0
    def test_biomv100_format_validate_negative(self):
        filepath = self.get_data_path('feature-table_v210.biom')
        format = BIOMV100Format(filepath, mode='r')

        with self.assertRaisesRegex(ValidationError, 'BIOMV100Format'):
            format.validate()
예제 #4
0
    def test_biomv100_format_validate_positive(self):
        filepath = self.get_data_path('feature-table_v100.biom')
        format = BIOMV100Format(filepath, mode='r')

        format.validate()