-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_background.py
executable file
·77 lines (59 loc) · 2.16 KB
/
make_background.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
'''
Copyright © 2018 Anton Tsukanov. Contacts: [email protected]
License: http://www.gnu.org/licenses/gpl.txt
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
'''
from random import sample
import argparse
import sys
def read_fasta(path):
with open(path, 'r') as fasta:
out = []
for line in fasta:
if line.startswith('>'):
continue
else:
out.append(line.strip().upper())
fasta.close()
return(out)
def shuffle_fasta(fasta, times):
out = []
for seq in fasta:
shuffled = [''.join(sample(seq, k=len(seq))) for i in range(times)]
out += shuffled
return(out)
def write(fasta, path):
with open(path, 'w') as file:
for index, seq in enumerate(fasta):
file.write('>Background_' + str(index) + '\n')
file.write(seq + '\n')
file.close
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', action='store', dest='input',
required=True, help='path to FASTA file')
parser.add_argument('-t', '--times', action='store', type=int, dest='times',
required=False, default=2, help='times bigger, default = 2')
parser.add_argument('-o', '--output', action='store', dest='output',
required=True, help='path to write output file with shuffled sequences')
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
return(parser.parse_args())
def main():
args = parse_args()
ifasta = args.input
ofasta = args.output
times = args.times
fasta = read_fasta(ifasta)
res = shuffle_fasta(fasta, times)
write(res, ofasta)
if __name__ == '__main__':
main()