-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextractRandomFastaSites.py
More file actions
41 lines (34 loc) · 965 Bytes
/
extractRandomFastaSites.py
File metadata and controls
41 lines (34 loc) · 965 Bytes
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
#!/usr/bin/python
import random
N = 1000 #Number of sites to randomly generate
maxIndex = 7594374 #The length of the sequences in the fasta file
fout = open("subSequence.fa", "w")
fin = open("input/seq.fa", "r")
randomNums = random.sample(range(1, maxIndex), N)
randomNums.sort()
curSequence = ""
subSequence = ""
start = 0
for line in fin:
if line[0] == '>':
if start != 0:
for i in randomNums:
subSequence += curSequence[i-1]
fout.write(subSequence + '\n')
curSequence = ""
subSequence = ""
else:
start = 1
fout.write(line)
else:
curSequence += line.rstrip()
#This clears the final line (since there isn't a final '>')
for i in randomNums:
subSequence += curSequence[i-1]
fout.write(subSequence)
fSiteList = open("ranSiteList.txt", "w")
for i in randomNums:
fSiteList.write(str(i) + '\n')
fout.close()
fin.close()
fSiteList.close()