annotate snp_wind.py @ 3:f959d99d1758 draft default tip

Uploaded
author greg
date Thu, 23 Nov 2023 19:02:52 +0000
parents 6b0a13deae92
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6b0a13deae92 Uploaded
greg
parents:
diff changeset
1 #!/usr/bin/env python
6b0a13deae92 Uploaded
greg
parents:
diff changeset
2
6b0a13deae92 Uploaded
greg
parents:
diff changeset
3 import argparse
6b0a13deae92 Uploaded
greg
parents:
diff changeset
4 import os
6b0a13deae92 Uploaded
greg
parents:
diff changeset
5 from os.path import join as j
6b0a13deae92 Uploaded
greg
parents:
diff changeset
6 from itertools import zip_longest
6b0a13deae92 Uploaded
greg
parents:
diff changeset
7
6b0a13deae92 Uploaded
greg
parents:
diff changeset
8
6b0a13deae92 Uploaded
greg
parents:
diff changeset
9 def setup(base_dir, names=[], fwds=[], revs=[], extension='vcf', pattern="{name}.{orient}.{ext}"):
6b0a13deae92 Uploaded
greg
parents:
diff changeset
10 if fwds and revs and names and len(fwds) != len(revs) != len(names):
6b0a13deae92 Uploaded
greg
parents:
diff changeset
11 raise ValueError('number of forward reads must equal number of reverse reads and names')
6b0a13deae92 Uploaded
greg
parents:
diff changeset
12 elif len(fwds) != len(names) or not fwds or not names:
6b0a13deae92 Uploaded
greg
parents:
diff changeset
13 raise ValueError('number of forward reads must equal number of names')
6b0a13deae92 Uploaded
greg
parents:
diff changeset
14 with open(j(base_dir, 'snp-unwind.sh'), 'w') as unwind:
6b0a13deae92 Uploaded
greg
parents:
diff changeset
15 for i, (name, fwd, rev) in enumerate(zip_longest(names, fwds, revs)):
6b0a13deae92 Uploaded
greg
parents:
diff changeset
16 dir = j(base_dir, str(i))
6b0a13deae92 Uploaded
greg
parents:
diff changeset
17 sample_dir = j(dir, name)
6b0a13deae92 Uploaded
greg
parents:
diff changeset
18 os.makedirs(sample_dir)
6b0a13deae92 Uploaded
greg
parents:
diff changeset
19 target_f = j(sample_dir, pattern.format(name=name, orient=1, ext=extension))
6b0a13deae92 Uploaded
greg
parents:
diff changeset
20 if rev:
6b0a13deae92 Uploaded
greg
parents:
diff changeset
21 target_r = j(sample_dir, pattern.format(name=name, orient=2, ext=extension))
6b0a13deae92 Uploaded
greg
parents:
diff changeset
22 os.symlink(fwd, target_f)
6b0a13deae92 Uploaded
greg
parents:
diff changeset
23 if rev:
6b0a13deae92 Uploaded
greg
parents:
diff changeset
24 os.symlink(rev, target_r)
6b0a13deae92 Uploaded
greg
parents:
diff changeset
25 print(sample_dir)
6b0a13deae92 Uploaded
greg
parents:
diff changeset
26 if rev:
6b0a13deae92 Uploaded
greg
parents:
diff changeset
27 unwind.write('unlink {}\n'.format(target_r))
6b0a13deae92 Uploaded
greg
parents:
diff changeset
28 unwind.write('unlink {}\n'.format(target_f))
6b0a13deae92 Uploaded
greg
parents:
diff changeset
29 unwind.write('rmdir {}\n'.format(sample_dir))
6b0a13deae92 Uploaded
greg
parents:
diff changeset
30 unwind.write('rmdir {}\n'.format(dir))
6b0a13deae92 Uploaded
greg
parents:
diff changeset
31
6b0a13deae92 Uploaded
greg
parents:
diff changeset
32
6b0a13deae92 Uploaded
greg
parents:
diff changeset
33 if __name__ == '__main__':
6b0a13deae92 Uploaded
greg
parents:
diff changeset
34 parser = argparse.ArgumentParser(description="set up vcf symlink directories for snp-pipeline")
6b0a13deae92 Uploaded
greg
parents:
diff changeset
35 parser.add_argument('base_dir')
6b0a13deae92 Uploaded
greg
parents:
diff changeset
36 parser.add_argument('-n', dest='names', type=str, action='append', default=[])
6b0a13deae92 Uploaded
greg
parents:
diff changeset
37 parser.add_argument('-f', dest='fwds', type=str, action='append', default=[])
6b0a13deae92 Uploaded
greg
parents:
diff changeset
38 parser.add_argument('-r', dest='revs', type=str, action='append', default=[])
6b0a13deae92 Uploaded
greg
parents:
diff changeset
39 parser.add_argument('-e', dest='extension', default='vcf')
6b0a13deae92 Uploaded
greg
parents:
diff changeset
40 parser.add_argument('-p', dest='pattern', default='{name}.{orient}.{ext}')
6b0a13deae92 Uploaded
greg
parents:
diff changeset
41 params = parser.parse_args()
6b0a13deae92 Uploaded
greg
parents:
diff changeset
42 setup(**vars(params))