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