-
Notifications
You must be signed in to change notification settings - Fork 42
/
fasta_digest.py
executable file
·54 lines (43 loc) · 1.27 KB
/
fasta_digest.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
#!/usr/bin/env python
"""Digest sequences from a fasta file in sequences of a specified length.
Usage:
%program <input_file> <fragment_length> <move> <output_file>
input_file: a fasta file
fragment_length: length of digested fragments
move: number of non-overlapping nucleotides between two consecutive fragments"""
import sys
try:
from Bio import SeqIO
except:
print("This program requires the Biopython library")
sys.exit(0)
try:
in_file = open(sys.argv[1], "rU")
nb_nuc = int(sys.argv[2])
move = int(sys.argv[3])
out_file = sys.argv[4]
except:
print(__doc__)
sys.exit(0)
sequences = ([seq.id, seq.seq.tostring()]
for seq in SeqIO.parse(in_file, 'fasta'))
def digest(seq, nb_nuc):
fragments = []
n = seq[0]
n_counter = 0
s = seq[1]
start = nb_nuc/2
while len(s) > nb_nuc/2:
frag = s[:nb_nuc - start]
start -= move
if start < 0:
start = 0
s = s[move:]
n_counter +=1
fragments.append([n + "_" + str("%06i" % n_counter), frag])
return fragments
with open(out_file, "w") as out_file:
for seq in sequences:
fragments = digest(seq, nb_nuc)
for frag in fragments:
out_file.write(">" + frag[0] + "\n" + frag[1] + "\n")