-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathseqChopper2.py
More file actions
60 lines (49 loc) · 1.73 KB
/
seqChopper2.py
File metadata and controls
60 lines (49 loc) · 1.73 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
59
60
#!/usr/bin/env python
"""Usage: seqChopper2.py /path/to/fasta /path/to/chopped/fasta window_size
example: python seqChopper.py myFasta.fasta myNewFasta.fasta 50
Created by Kathryn Iverson kiverson@umich.edu"""
import sys
def get_next_fasta (fileObject):
'''usage: for header, seq in get_next_fasta(fileObject):
This is a generator that returns one fasta record's header and
sequence at a time from a multiple fasta file. Return character is removed
from the header. The sequence is returned as one continuous string
with no returns. The returned value is a tuple (header, sequence)
If their is no sequence associated with a header, seq will be an
empty string
Code simplification contributed by Dattatreya Mellacheruvu
01/16/2009, Jeffrey R. de Wet
'''
header = ''
seq = ''
#The following for loop gets the header of the first fasta
#record. Skips any leading junk in the file
for line in fileObject:
if line.startswith('>'):
header = line.strip()
break
for line in fileObject:
if line.startswith('>'):
yield header, seq
header = line.strip()
seq = ''
else:
seq += line.strip()
#yield the last entry
if header:
yield header, seq
infile = open(sys.argv[1], 'r')
outfile = open(sys.argv[2], 'w')
window = int(sys.argv[3])
#while start < maxseqlength:
for header, seq in get_next_fasta(infile):
start = 0
end = window
outfile.write("%s&\n%s\n" %(header, seq))
while end < len(seq)+window:
seqFrag = seq[start:end]
outfile.write("%s@%s-%s\n%s\n"%(header,start, end, seqFrag))
start = end
end = end+window
infile.close()
infile = open(sys.argv[1], 'r')