-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsplit_by_size.py
More file actions
executable file
·58 lines (47 loc) · 1.25 KB
/
Copy pathsplit_by_size.py
File metadata and controls
executable file
·58 lines (47 loc) · 1.25 KB
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
58
#! /usr/bin/env python
"""
Split fasta file into files, <int> max per file.
Originally made for web-based eggnog-mapper submissions.
"""
from sys import argv, exit
from os import path
from Bio import SeqIO
from math import ceil
def popslice(lrecs, i):
lcurr = lrecs[:i]
del(lrecs[:i])
return(lcurr)
def write_split_faa(pnew, lrecs, i):
lcurr = popslice(lrecs, i)
with open(pnew, "w") as fnew:
for o in lcurr:
SeqIO.write(o, fnew, "fasta")
def main(pin, pout, i):
orecs = SeqIO.parse(pin, "fasta")
lrecs = list(orecs)
ilen = len(lrecs)
if ilen <= i:
print("No need to split.")
exit()
else:
k = len(str(ceil(ilen/i)))
sfile, sext = path.splitext(pin)
samp = path.basename(sfile)
j = 1
while lrecs:
sj = f"sub{j:0{k}}"
pnew = f"{pout}/{samp}_{sj}{sext}"
write_split_faa(pnew, lrecs, i)
j += 1
if __name__ == "__main__":
# Checkpoint
try:
pin = path.abspath(argv[1])
pout = path.abspath(argv[2])
i = int(argv[3])
except IndexError:
ppy = path.basename(__file__)
shelp = f"Usage:\t{ppy} <input_fasta> <output_dir> <max_records>"
print(shelp)
exit()
main(pin, pout, i)