annotate seg2matrix/segToProbeMap.py @ 42:03b7b1cf78ce

fix
author jingchunzhu
date Thu, 06 Aug 2015 00:30:49 -0700
parents 61f03b481b0d
children 3e5680fecd7a
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
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
34 self.chrom = chrom
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
35 self.chrom_start = chrom_start
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
36 self.chrom_end = chrom_end
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
37 self.strand = strand
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
38
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
39
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
40 if __name__ == "__main__":
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
41 def printUsage():
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
42 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
43
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
44 if len(sys.argv) != 5:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
45 printUsage()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
46 sys.exit()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
47
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
48 parser = optparse.OptionParser()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
49 parser.add_option("--mode", action="store", type="string", dest="mode")
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
50 (options, args) = parser.parse_args()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
51
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
52 if (not options.mode) or (options.mode not in ["cnv","exp"]):
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
53 printUsage()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
54 sys.exit()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
55
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
56 probes=segs()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
57 probes.load(sys.argv[1])
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
58
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
59 refgene = CGData.RefGene.RefGene()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
60 refgene.load(sys.argv[2])
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
61
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
62 handle = open(sys.argv[3], "w")
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
63 if options.mode=="cnv":
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
64 probeMapper = CGData.GeneMap.ProbeMapper('b')
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
65 if options.mode=="exp":
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
66 probeMapper = CGData.GeneMap.ProbeMapper('g')
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
67
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
68 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
69 for probe in probes.probes:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
70 hits = []
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
71 for hit in probeMapper.find_overlap( probe, refgene ):
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
72 if hit.name not in hits:
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
73 hits.append(hit.name)
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
74 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
75 handle.close()
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
76
61f03b481b0d new tool
jingchunzhu
parents:
diff changeset
77