Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
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)
Ejemplo n.º 10
0
 def test_cross_device_dst_exists(self, mocked_link):
     with self.assertRaisesRegex(FileExistsError, self.dst2):
         util.duplicate(self.src, self.dst2)
Ejemplo n.º 11
0
 def test_cross_device_src_not_exists(self, mocked_link):
     with self.assertRaisesRegex(FileNotFoundError, self.missing):
         util.duplicate(self.missing, self.dst1)
Ejemplo n.º 12
0
    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
Ejemplo n.º 13
0
 def test_dst_dir(self):
     with self.assertRaisesRegex(IsADirectoryError, self.dir):
         util.duplicate(self.src, self.dir)
Ejemplo n.º 14
0
    def test_perm_error(self, mocked_link):
        with self.assertRaisesRegex(PermissionError, "find this"):
            util.duplicate(self.src, self.dst1)

        assert mocked_link.called
Ejemplo n.º 15
0
 def test_cross_device_src_not_exists(self, mocked_link):
     with self.assertRaisesRegex(FileNotFoundError, self.missing):
         util.duplicate(self.missing, self.dst1)
Ejemplo n.º 16
0
 def test_dst_exists(self):
     with self.assertRaisesRegex(FileExistsError, self.dst2):
         util.duplicate(self.src, self.dst2)
Ejemplo n.º 17
0
 def test_src_not_exists(self):
     with self.assertRaisesRegex(FileNotFoundError, self.missing):
         util.duplicate(self.missing, self.dst1)
Ejemplo n.º 18
0
 def test_src_not_exists(self):
     with self.assertRaisesRegex(FileNotFoundError, self.missing):
         util.duplicate(self.missing, self.dst1)
Ejemplo n.º 19
0
    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)
Ejemplo n.º 20
0
    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
Ejemplo n.º 21
0
 def test_dst_exists(self):
     with self.assertRaisesRegex(FileExistsError, self.dst2):
         util.duplicate(self.src, self.dst2)
Ejemplo n.º 22
0
 def test_cross_device_dst_exists(self, mocked_link):
     with self.assertRaisesRegex(FileExistsError, self.dst2):
         util.duplicate(self.src, self.dst2)
Ejemplo n.º 23
0
 def test_dst_dir(self):
     with self.assertRaisesRegex(IsADirectoryError, self.dir):
         util.duplicate(self.src, self.dir)
Ejemplo n.º 24
0
 def test_cross_device_dst_dir(self, mocked_link):
     with self.assertRaisesRegex(IsADirectoryError, self.dir):
         util.duplicate(self.src, self.dir)
Ejemplo n.º 25
0
    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)
Ejemplo n.º 26
0
 def test_cross_device_dst_dir(self, mocked_link):
     with self.assertRaisesRegex(IsADirectoryError, self.dir):
         util.duplicate(self.src, self.dir)
Ejemplo n.º 27
0
    def test_perm_error(self,  mocked_link):
        with self.assertRaisesRegex(PermissionError, "find this"):
            util.duplicate(self.src, self.dst1)

        assert mocked_link.called