1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
#!/usr/bin/env python3
"""
script to filter plasmids and chromosomes from full genome assemblies
"""
import os
import sys
import gzip
import builtins
import argparse
from glob import glob
sys.path.insert(0, os.path.abspath('.')) # gross hack
from pangraph.utils import parse_fasta, breakpoint
def open(path, *args, **kwargs):
if path.endswith('.gz'):
return gzip.open(path, *args, **kwargs)
else:
return builtins.open(path, *args, **kwargs)
def main(dirs, plasmids=True):
for d in dirs:
in_dir = f"data/{d}/assemblies"
if not os.path.exists(in_dir):
print(f"{in_dir} doesn't exist. skipping...")
continue
if plasmids:
out_dir = f"data/{d}-plasmid/assemblies"
else:
out_dir = f"data/{d}-chromosome/assemblies"
if not os.path.exists(out_dir):
os.makedirs(out_dir)
for path in glob(f"{in_dir}/*.f?a*"):
with open(path, 'rt') as fd, open(f"{out_dir}/{os.path.basename(path).replace('.gz', '')}", 'w') as wtr:
for i, rec in enumerate(parse_fasta(fd)):
if i == 0:
if not plasmids:
wtr.write(str(rec))
wtr.write('\n')
break
continue
wtr.write(str(rec))
wtr.write('\n')
parser = argparse.ArgumentParser(description='seperate plasmids from chromosomes')
parser.add_argument('directories', metavar='dirs', nargs='+')
parser.add_argument('--chromosomes', default=False, action='store_true')
if __name__ == "__main__":
args = parser.parse_args()
main(args.directories, plasmids=not args.chromosomes)
|