annotate seg2matrix/segToProbeMap.py @ 58:3e5680fecd7a

accept chr1 and 1
author jingchunzhu
date Tue, 22 Sep 2015 15:16:19 -0700
parents 61f03b481b0d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
1 #!/usr/bin/env python
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
2
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
3 import sys,string
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
4 sys.path.insert(0,"../CGDataNew")
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
5
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
6 import CGData.GenomicSegment
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
7 import CGData.SegToMatrix
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
8 import CGData.RefGene
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
9 import CGData.GeneMap
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
10 import optparse
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
11
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
12 class segs:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
13 def __init__(self):
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
14 self.probes = []
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
15
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
16 def load (self, handle): #handle bed6
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
17 fin =open(handle,'r')
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
18 while 1:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
19 line =string.strip(fin.readline())
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
20 if line =="":
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
21 break
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
22 if line[0]=="#":
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
23 continue
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
24 tmp = string.split(line,"\t")
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
25 if len(tmp)== 5:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
26 p = probeseg(tmp[0], tmp[1], int(tmp[3]), int(tmp[4]),tmp[2])
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
27 self.probes.append(p)
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
28 fin.close()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
29
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
30
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
31 class probeseg:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
32 def __init__(self, name, chrom, chrom_start, chrom_end, strand):
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
33 self.name = name
58
3e5680fecd7a accept chr1 and 1
jingchunzhu
parents: 39
diff changeset
34 if chrom[0:3] in ["chr","CHR","Chr"]:
3e5680fecd7a accept chr1 and 1
jingchunzhu
parents: 39
diff changeset
35 chrom = "chr"+chrom[3:]
3e5680fecd7a accept chr1 and 1
jingchunzhu
parents: 39
diff changeset
36 else:
3e5680fecd7a accept chr1 and 1
jingchunzhu
parents: 39
diff changeset
37 chrom = "chr"+chrom
39
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
38 self.chrom = chrom
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
39 self.chrom_start = chrom_start
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
40 self.chrom_end = chrom_end
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
41 self.strand = strand
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
42
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
43
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
44 if __name__ == "__main__":
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
45 def printUsage():
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
46 print "python segToProbeMap.py segInput(name,chr,strand,start,end) refGene(eg hg18) probeMapOut --mode=cnv|exp\n"
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
47
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
48 if len(sys.argv) != 5:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
49 printUsage()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
50 sys.exit()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
51
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
52 parser = optparse.OptionParser()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
53 parser.add_option("--mode", action="store", type="string", dest="mode")
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
54 (options, args) = parser.parse_args()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
55
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
56 if (not options.mode) or (options.mode not in ["cnv","exp"]):
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
57 printUsage()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
58 sys.exit()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
59
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
60 probes=segs()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
61 probes.load(sys.argv[1])
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
62
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
63 refgene = CGData.RefGene.RefGene()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
64 refgene.load(sys.argv[2])
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
65
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
66 handle = open(sys.argv[3], "w")
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
67 if options.mode=="cnv":
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
68 probeMapper = CGData.GeneMap.ProbeMapper('b')
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
69 if options.mode=="exp":
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
70 probeMapper = CGData.GeneMap.ProbeMapper('g')
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
71
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
72 handle.write("%s\t%s\t%s\t%s\t%s\t%s\n" % ("#id", "gene","chrom","chromStart","chromEnd","strand"))
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
73 for probe in probes.probes:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
74 hits = []
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
75 for hit in probeMapper.find_overlap( probe, refgene ):
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
76 if hit.name not in hits:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
77 hits.append(hit.name)
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
78 handle.write("%s\t%s\t%s\t%s\t%s\t%s\n" % (probe.name, ",".join(hits), probe.chrom, probe.chrom_start, probe.chrom_end, probe.strand))
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
79 handle.close()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
80
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
81