-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathqueryTestGenerator.py
More file actions
executable file
·177 lines (143 loc) · 5.12 KB
/
queryTestGenerator.py
File metadata and controls
executable file
·177 lines (143 loc) · 5.12 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#! /usr/bin/env python
# RLZ index
# Copyright (C) 2013 Shanika Kuruppu
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# queryTestGenerator.py generates tests for the display, search and locate
# queries.
#
# Author: Shanika Kuruppu
# Email: shanika.kuruppu@gmail.com
# Date: 17/03/2013
import sys, optparse, random
# Read in the input sequences and their lengths
def readSequences(files):
sequences = []
sequencelens = []
for filename in files:
file = open(filename)
sequence = file.read()
file.close()
sequences.append(sequence)
sequencelens.append(len(sequence))
return (sequences, sequencelens)
def generateDisplayTests(sequences, sequencelens, numqueries, querylen,
testfile, expoutfile):
numseqs = len(sequences)
i = 0
while i < numqueries:
# Get the sequence number
s = random.randint(0, numseqs - 1)
if sequencelens[s] < querylen:
continue
# Set the boundaries
min = 0
max = sequencelens[s]
# Get the left boundary for the query
x = random.randint(min, max - querylen)
y = x + querylen
# Write to file
testfile.write("%d %d %d\n" % (s, x, y))
expoutfile.write(sequences[s][x:x + querylen] + '\n')
i += 1
def generatePattern(querylen):
alpha = ['a','c','g','t']
pattern = ''
for j in range(0, querylen):
pattern += alpha[random.randint(0, len(alpha) - 1)]
return pattern
def generateCountTests(sequences, sequencelens, numqueries, querylen,
testfile, expoutfile):
for i in range(0, numqueries):
pattern = generatePattern(querylen)
testfile.write("%s\n" % (pattern))
expocc = 0
for sequence in sequences:
start = 0
while start < len(sequence):
pos = sequence.find(pattern, start)
if pos != -1:
expocc += 1
start = pos+1
else:
break
expoutfile.write("%s : %d\n" % (pattern, expocc))
def generateLocateTests(sequences, sequencelens, numqueries, querylen,
testfile, expoutfile):
for i in range(0, numqueries):
pattern = generatePattern(querylen)
testfile.write("%s\n" % (pattern))
j = 0
for sequence in sequences:
start = 0
while start < len(sequence):
pos = sequence.find(pattern, start)
if pos != -1:
expoutfile.write("%d %d\n" % (j, pos))
start = pos+1
else:
break
j += 1
# Main function
def main(args=None):
usage = """%prog query queries querylen prefix ref file ...
query: Query to execute ('d' for display, 'c' for count, 'l' for locate)
queries: Number of queries to output
querylen: Length of the query
out_file_prefix: Prefix of output files
ref: Reference file name
file ...: Input file names"""
if args is None:
args = sys.argv[1:]
# Parse arguments and options
parser = optparse.OptionParser(usage)
(options, args) = parser.parse_args(args)
# Check if there's at least four arguments
if len(args) < 6:
parser.print_help()
parser.exit()
# Get query type
qrytype = args[0]
if qrytype not in ['d', 'c', 'l']:
parser.print_help()
parser.exit()
# Get number of queries to output
numqueries = int(args[1])
# Length of queries
querylen = int(args[2])
# Output file prefix
prefix = args[3]
# Get file names where the first file is the reference
files = args[4:]
# Read in the contents of the files
sequences, sequencelens = readSequences(files)
# Open output files
testfile = open(prefix + '.test', 'w')
expoutfile = open(prefix + '.exp', 'w')
# Seed the random number generator with the output file prefix
random.seed(hash(prefix))
# Generate tests for display query
if qrytype == 'd':
generateDisplayTests(sequences, sequencelens, numqueries, querylen,
testfile, expoutfile)
elif qrytype == 'c':
generateCountTests(sequences, sequencelens, numqueries, querylen,
testfile, expoutfile)
elif qrytype == 'l':
generateLocateTests(sequences, sequencelens, numqueries, querylen,
testfile, expoutfile)
testfile.close()
expoutfile.close()
if __name__ == "__main__":
sys.exit(main())