snapshot of 4.0a development. initial git repo commit
[erange.git] / featureIntersects.py
1 #
2 #  featureIntersects.py
3 #  ENRAGE
4 #
5
6 try:
7     import psyco
8     psyco.full()
9 except:
10     pass
11
12 import sys, optparse
13 from cistematic.core import featuresIntersecting
14
15 print "%prog: version 1.0"
16
17
18 def main(argv=None):
19     if not argv:
20         argv = sys.argv
21
22     usage = "usage: python %s tabfile [--cistype type] [--radius radius]"
23
24     parser = optparse.OptionParser(usage=usage)
25     parser.add_option("--cistype", action="store_false", dest="cistype")
26     parser.add_option("--radius", type="int", dest="radius")
27     parser.set_defaults(cistype="TFBSCONSSITES", radius=100)
28     (options, args) = parser.parse_args(argv[1:])
29
30     if len(args) < 1:
31         print usage
32         sys.exit(1)
33
34     tabfile = args[0]
35
36     featureIntersects(tabfile, options.cistype, options.radius)
37
38
39 def featureIntersects(tabFileName, cistype="TFBSCONSSITES", radius=100):
40     tabfile = open(tabFileName)
41     previous = ""
42
43     posList = []
44     for line in tabfile:
45         fields = line.split("\t")
46         current = fields[0]
47         if previous == current:
48             continue
49
50         previous = current
51         chrom = fields[1][3:]
52         posList.append((chrom, (int(fields[2]) + int(fields[3]))/2))
53
54     feats = featuresIntersecting("human", posList, radius, cistype)
55     featkeys = feats.keys()
56     featkeys.sort()
57     for (chrom, pos) in featkeys:
58         print "chr%s:%d-%d\t%s" % (chrom, pos, pos + 20, str(feats[(chrom, pos)]))
59
60
61 if __name__ == "__main__":
62     main(sys.argv)