def filter_samples(demux: _PlotQualView, metadata: Metadata, where: str = None, exclude_ids: bool = False) \ -> CasavaOneEightSingleLanePerSampleDirFmt: results = CasavaOneEightSingleLanePerSampleDirFmt() paired = demux.paired samples = demux.directory_format ids_to_keep = metadata.get_ids(where=where) if not ids_to_keep: raise ValueError('No filtering requested.') manifest = samples.manifest.view(pd.DataFrame) if exclude_ids: ids_to_keep = set(manifest.index) - set(ids_to_keep) try: for id in ids_to_keep: forward = manifest.loc[id].forward duplicate(forward, os.path.join(str(results), os.path.split(forward)[1])) if paired: reverse = manifest.loc[id].reverse duplicate( reverse, os.path.join(str(results), os.path.split(reverse)[1])) except KeyError: raise ValueError(f'{id!r} is not a sample present in the ' 'demultiplexed data.') return results
def test_perm_error_EPERM(self, mocked_link): util.duplicate(self.src, self.dst1) assert mocked_link.called assert os.path.exists(self.dst1) with open(self.dst1) as fh: self.assertEqual(fh.read(), SECRET)
def test_cross_device_perm_error(self, mocked_link, mocked_copyfile): with self.assertRaisesRegex( PermissionError, "insufficient r/w permissions"): util.duplicate(self.src, self.dst1) assert mocked_link.called assert mocked_copyfile.called
def test_perm_error_EPERM(self, mocked_link): util.duplicate(self.src, self.dst1) assert mocked_link.called assert os.path.exists(self.dst1) with open(self.dst1) as fh: self.assertEqual(fh.read(), SECRET)
def test_cross_device_dst_not_exists(self, mocked_link): util.duplicate(self.src, self.dst1) assert mocked_link.called assert os.path.exists(self.dst1) with open(self.dst1) as fh: self.assertEqual(fh.read(), SECRET)
def test_cross_device_perm_error(self, mocked_link, mocked_copyfile): with self.assertRaisesRegex( PermissionError, "insufficient r/w permissions"): util.duplicate(self.src, self.dst1) assert mocked_link.called assert mocked_copyfile.called
def test_cross_device_dst_not_exists(self, mocked_link): util.duplicate(self.src, self.dst1) assert mocked_link.called assert os.path.exists(self.dst1) with open(self.dst1) as fh: self.assertEqual(fh.read(), SECRET)
def setup_database_dir(tmpdir, database, refseqs, reftaxa): BOWTIE_PATH = 'bowtie2' duplicate(str(refseqs), os.path.join(tmpdir, 'refseqs.fna')) reftaxa.to_csv(os.path.join(tmpdir, 'taxa.tsv'), sep='\t') shutil.copytree(str(database), os.path.join(tmpdir, BOWTIE_PATH), copy_function=duplicate) # 'bowtie2': os.path.join(BOWTIE_PATH, database.get_name()) bowtie_filename = glob.glob(str(database) + '/*.rev.1.bt2') if not bowtie_filename: raise Exception("Couldn't find bowtie index files at %s" % (str(database))) bowtie_basename = bowtie_filename[0].split('/')[-1].replace( '.rev.1.bt2', '') params = { 'general': { 'taxonomy': 'taxa.tsv', 'fasta': 'refseqs.fna' }, #'bowtie2': os.path.join(BOWTIE_PATH, 'genomes.small') 'bowtie2': os.path.join(BOWTIE_PATH, bowtie_basename) } with open(os.path.join(tmpdir, 'metadata.yaml'), 'w') as fh: yaml.dump(params, fh, default_flow_style=False)
def setup_database_dir(tmpdir, database, refseqs, reftaxa): BOWTIE_PATH = 'bowtie2' duplicate(str(refseqs), os.path.join(tmpdir, 'refseqs.fna')) reftaxa.to_csv(os.path.join(tmpdir, 'taxa.tsv'), sep='\t') shutil.copytree(str(database), os.path.join(tmpdir, BOWTIE_PATH), copy_function=duplicate) params = { 'general': { 'taxonomy': 'taxa.tsv', 'fasta': 'refseqs.fna' }, 'bowtie2': os.path.join(BOWTIE_PATH, database.get_basename()) } with open(os.path.join(tmpdir, 'metadata.yaml'), 'w') as fh: yaml.dump(params, fh, default_flow_style=False)
def test_cross_device_dst_exists(self, mocked_link): with self.assertRaisesRegex(FileExistsError, self.dst2): util.duplicate(self.src, self.dst2)
def test_cross_device_src_not_exists(self, mocked_link): with self.assertRaisesRegex(FileNotFoundError, self.missing): util.duplicate(self.missing, self.dst1)
def test_perm_error_EACCES(self, mocked_link): with self.assertRaisesRegex( PermissionError, "insufficient r/w permissions"): util.duplicate(self.src, self.dst1) assert mocked_link.called
def test_dst_dir(self): with self.assertRaisesRegex(IsADirectoryError, self.dir): util.duplicate(self.src, self.dir)
def test_perm_error(self, mocked_link): with self.assertRaisesRegex(PermissionError, "find this"): util.duplicate(self.src, self.dst1) assert mocked_link.called
def test_cross_device_src_not_exists(self, mocked_link): with self.assertRaisesRegex(FileNotFoundError, self.missing): util.duplicate(self.missing, self.dst1)
def test_dst_exists(self): with self.assertRaisesRegex(FileExistsError, self.dst2): util.duplicate(self.src, self.dst2)
def test_src_not_exists(self): with self.assertRaisesRegex(FileNotFoundError, self.missing): util.duplicate(self.missing, self.dst1)
def test_src_not_exists(self): with self.assertRaisesRegex(FileNotFoundError, self.missing): util.duplicate(self.missing, self.dst1)
def test_dst_not_exists(self): util.duplicate(self.src, self.dst1) assert os.path.exists(self.dst1) with open(self.dst1) as fh: self.assertEqual(fh.read(), SECRET)
def test_perm_error_EACCES(self, mocked_link): with self.assertRaisesRegex( PermissionError, "insufficient r/w permissions"): util.duplicate(self.src, self.dst1) assert mocked_link.called
def test_dst_exists(self): with self.assertRaisesRegex(FileExistsError, self.dst2): util.duplicate(self.src, self.dst2)
def test_cross_device_dst_exists(self, mocked_link): with self.assertRaisesRegex(FileExistsError, self.dst2): util.duplicate(self.src, self.dst2)
def test_dst_dir(self): with self.assertRaisesRegex(IsADirectoryError, self.dir): util.duplicate(self.src, self.dir)
def test_cross_device_dst_dir(self, mocked_link): with self.assertRaisesRegex(IsADirectoryError, self.dir): util.duplicate(self.src, self.dir)
def test_dst_not_exists(self): util.duplicate(self.src, self.dst1) assert os.path.exists(self.dst1) with open(self.dst1) as fh: self.assertEqual(fh.read(), SECRET)
def test_cross_device_dst_dir(self, mocked_link): with self.assertRaisesRegex(IsADirectoryError, self.dir): util.duplicate(self.src, self.dir)
def test_perm_error(self, mocked_link): with self.assertRaisesRegex(PermissionError, "find this"): util.duplicate(self.src, self.dst1) assert mocked_link.called