snapshot of 4.0a development. initial git repo commit
[erange.git] / listGeneFeatures.py
1 #
2 #  listGeneFeatures.py
3 #  ENRAGE
4 #
5
6 import sys
7 from cistematic.genomes import Genome
8 from commoncode import getMergedRegions, getFeaturesByChromDict
9
10 print "%s: version 1.1" % sys.argv[0]
11
12
13 def main(argv=None):
14     if not argv:
15         argv = sys.argv
16
17     if len(argv) < 4:
18         print "usage: python %s genome [acceptFile] gid outfile\n" % argv[0]
19         sys.exit(1)
20
21     genome = argv[1]
22
23     if len(argv) == 4:
24         gid = argv[2]
25         outfile = argv[3]
26     else:
27         acceptFileName = argv[2]
28         gid = argv[3]
29         outfile = argv[4]
30
31     listGeneFeatures(genome, gid, outfile, acceptFileName)
32
33
34 def listGeneFeatures(genome, gid, outFileName, acceptFileName=""):
35     hg = Genome(genome)
36     outfile = open(outFileName, "w")
37     if acceptFileName:
38         additionalDict = getMergedRegions(acceptFileName, maxDist = 0, keepLabel = True, verbose = True)
39     else:
40         additionalDict = {}
41
42     featuresDict = getFeaturesByChromDict(hg, additionalDict, restrictList=[gid])
43     outfile.write('track name="LOC%s"\n' % gid)
44
45     senseDict = {"F": "+",
46                  "R": "-",
47                  "+": "+",
48                  "-": "-"
49     }
50
51     for chrom in featuresDict:
52         for (start, stop, fgid, sense, ftype) in featuresDict[chrom]:
53             outfile.write("chr%s\t%d\t%d\t%s\t0\t%s\n" % (chrom, start, stop, ftype, senseDict[sense]))
54
55     outfile.close()
56
57
58 if __name__ == "__main__":
59     main(sys.argv)