7 import sqlite3 as sqlite
9 import tempfile, shutil, os, optparse
10 from os import environ
12 if environ.get("CISTEMATIC_TEMP"):
13 cisTemp = environ.get("CISTEMATIC_TEMP")
16 tempfile.tempdir = cisTemp
18 print "version 3.3: %prog"
25 usage = "usage: python %s dbfile snpsfile nr_snps_outfile [--cache numPages] [--repeats]"
27 parser = optparse.OptionParser(usage=usage)
28 parser.add_option("--repeats", action="store_true", dest="repeats")
29 parser.add_option("--cache", type="int", dest="cachePages")
30 parser.set_defaults(repeats=False, cachePages=None)
31 (options, args) = parser.parse_args(argv[1:])
41 chkSNPrmask(dbfile, filename, outfile, options.repeats, options.cachePages)
44 def chkSNPrmask(dbfile, filename, outfile, repeats=False, cachePages=None):
47 if cachePages is not None:
48 if cachePages < 250000:
51 print "caching locally..."
52 cachefile = tempfile.mktemp() + ".db"
53 shutil.copyfile(dbfile, cachefile)
54 db = sqlite.connect(cachefile)
60 db = sqlite.connect(dbfile)
63 sql.execute("PRAGMA CACHE_SIZE = %d" % cachePages)
64 sql.execute("PRAGMA temp_store = MEMORY")
65 sql.execute("ANALYZE")
67 infile = open(filename)
72 if doNotProcessLine(line):
75 fields = line.strip().split("\t")
78 featureList.append((chrom,pos))
79 featureDict[(chrom, pos)] = line.strip()
85 for (chrom, pos) in featureList:
87 if chrom != currentChrom:
93 sql.execute("select family from repeats where chrom = '%s' and %d between start and stop" % (chrom, pos))
94 results = sql.fetchall()
98 if repeats: # if user wants to keep track of the SNPs in repeats
99 featureDict[(chrom,pos)] += "\tN\A"
101 featureDict[(chrom,pos)] += "\t" + str(x)
105 del featureDict[(chrom,pos)]
114 print "removing cache"
118 outFile = open(outfile, "w")
119 for key, value in featureDict.iteritems():
120 outStr = str(value) + "\n"
121 outFile.write(outStr)
126 def doNotProcessLine(line):
127 return line[0] == "#"
130 if __name__ == "__main__":