forked from dib-lab/khmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simgenome.py
executable file
·70 lines (54 loc) · 1.52 KB
/
simgenome.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
#!/usr/bin/python
"""
simreads.py
Creates short reads from a given genome.
"""
import sys
import random
import math
import screed
def getRandomNucleotide():
'''
Returns a nucleotide with even chance for all four.
'''
nucleotides = ['A', 'C', 'G', 'T']
return nucleotides[random.randint(0,3)]
def generateChromosome(length):
'''
Generates a simple chromosome with random nucleotides.
'''
chrList = []
for i in range(length):
chrList.append(getRandomNucleotide())
return ''.join(chrList)
def generateGenome(genomeSize, chrs):
'''
Generates a collection of chromosomes and returns genome as a list of
chromosome strings.
'''
chrSize = int(genomeSize / chrs)
genome = []
for i in range(chrs):
genome.append(generateChromosome(chrSize))
return genome
def writeGenomeFASTA(filename, genome, prefix):
'''
Writes the genome to a file in FASTA format.
'''
fd = open(filename, "w")
for i in range(len(genome)):
fd.write(">" + prefix + ":chr" + str(i+1) + "\n")
lines = int(math.ceil(len(genome[i])/80.0))
for j in range(lines):
fd.write(genome[i][(j*80):((j*80)+80)] + "\n")
def main():
if len(sys.argv) != 5:
print "Usage: ./simgenome.py filename genome_size n_chromosomes prefix"
return
filename = sys.argv[1]
genome_size = int(sys.argv[2])
n_chromosomes = int(sys.argv[3])
prefix = sys.argv[4]
genome = generateGenome(genome_size, n_chromosomes)
writeGenomeFASTA(filename, genome, prefix)
main()