annotate tools/rgenetics/rgHaploView.py @ 0:9071e359b9a3

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:37:19 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 released under the terms of the LGPL
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 copyright ross lazarus August 2007
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 for the rgenetics project
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 Special galaxy tool for the camp2007 data
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 Allows grabbing genotypes from an arbitrary region and estimating
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 ld using haploview
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10 stoopid haploview won't allow control of dest directory for plots - always end
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 up where the data came from - need to futz to get it where it belongs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 Needs a mongo results file in the location hardwired below or could be passed in as
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 a library parameter - but this file must have a very specific structure
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 rs chrom offset float1...floatn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 import sys, array, os, string, tempfile, shutil, subprocess, glob
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 from rgutils import galhtmlprefix
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 progname = os.path.split(sys.argv[0])[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26 javabin = 'java'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 #hvbin = '/usr/local/bin/Haploview.jar'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 #hvbin = '/home/universe/linux-i686/haploview/Haploview.jar'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 # get this from tool as a parameter - can use
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 atrandic = {'A':'1','C':'2','G':'3','T':'4','N':'0','-':'0','1':'1','2':'2','3':'3','4':'4','0':'0'}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 class NullDevice:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36 """ a dev/null for ignoring output
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38 def write(self, s):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 class ldPlot:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 def __init__(self, argv=[]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 setup
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 self.args=argv
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 self.parseArgs(argv=self.args)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 self.setupRegions()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 def parseArgs(self,argv=[]):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 ts = '%s%s' % (string.punctuation,string.whitespace)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 ptran = string.maketrans(ts,'_'*len(ts))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 ### Figure out what genomic region we are interested in
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 self.region = argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 self.orslist = argv[2].replace('X',' ').lower() # galaxy replaces newlines with XX - go figure
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 self.title = argv[3].translate(ptran)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 # for outputs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 self.outfile = argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 self.logfn = 'Log_%s.txt' % (self.title)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 self.histextra = argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 self.base_name = argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 self.pedFileBase = os.path.join(self.histextra,self.base_name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 print 'pedfilebase=%s' % self.pedFileBase
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 self.minMaf=argv[7]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 if self.minMaf:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 self.minMaf = float(self.minMaf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 self.minMaf = 0.0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 self.maxDist=argv[8] or None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 self.ldType=argv[9] or 'RSQ'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 self.hiRes = (argv[10].lower() == 'hi')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 self.memSize= argv[11] or '1000'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 self.memSize = int(self.memSize)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 self.outfpath = argv[12]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 self.infotrack = False # note that otherwise this breaks haploview in headless mode
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 #infotrack = argv[13] == 'info'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 # this fails in headless mode as at april 2010 with haploview 4.2
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 self.tagr2 = argv[14] or '0.8'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 hmpanels = argv[15] # eg "['CEU','YRI']"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 if hmpanels:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 hmpanels = hmpanels.replace('[','')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 hmpanels = hmpanels.replace(']','')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 hmpanels = hmpanels.replace("'",'')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 hmpanels = hmpanels.split(',')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 self.hmpanels = hmpanels
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 self.hvbin = argv[16] # added rml june 2008
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 self.bindir = os.path.split(self.hvbin)[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92 # jan 2010 - always assume utes are on path to avoid platform problems
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93 self.pdfjoin = 'pdfjoin' # os.path.join(bindir,'pdfjoin')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 self.pdfnup = 'pdfnup' # os.path.join(bindir,'pdfnup')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 self.mogrify = 'mogrify' # os.path.join(bindir,'mogrify')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 self.convert = 'convert' # os.path.join(bindir,'convert')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 self.log_file = os.path.join(self.outfpath,self.logfn)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 self.MAP_FILE = '%s.map' % self.pedFileBase
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 self.DATA_FILE = '%s.ped' % self.pedFileBase
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 os.makedirs(self.outfpath)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 s = '## made new path %s\n' % self.outfpath
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 self.lf = file(self.log_file,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 s = 'PATH=%s\n' % os.environ.get('PATH','?')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 self.lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 def getRs(self):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 if self.region > '':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 useRs = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 useRsdict={}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 try: # TODO make a regexp?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 c,rest = self.region.split(':')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 chromosome = c.replace('chr','')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 rest = rest.replace(',','') # remove commas
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 spos,epos = rest.split('-')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 spos = int(spos)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 epos = int(epos)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 s = '## %s parsing chrom %s from %d to %d\n' % (progname,chromosome,spos,epos)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 self.lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 self.lf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 print >> sys.stdout, s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 s = '##! %s unable to parse region %s - MUST look like "chr8:10,000-100,000\n' % (progname,self.region)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126 print >> sys.stdout, s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 self.lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 self.lf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 self.lf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 useRs = self.orslist.split() # galaxy replaces newlines with XX - go figure
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 useRsdict = dict(zip(useRs,useRs))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 return useRs, useRsdict
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 def setupRegions(self):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 This turns out to be complex because we allow the user
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 flexibility - paste a list of rs or give a region.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 In most cases, some subset has to be generated correctly before running Haploview
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 chromosome = ''
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 spos = epos = -9
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 rslist = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 rsdict = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 useRs,useRsdict = self.getRs()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 self.useTemp = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 dfile = open(self.DATA_FILE, 'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 except: # bad input file name?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 s = '##! RGeno unable to open file %s\n' % (self.DATA_FILE)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 self.lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 self.lf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 self.lf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 print >> sys.stdout, s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 raise
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 mfile = open(self.MAP_FILE, 'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 except: # bad input file name?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 s = '##! RGeno unable to open file %s' % (self.MAP_FILE)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 lf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 lf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 print >> sys.stdout, s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 raise
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 if len(useRs) > 0 or spos <> -9 : # subset region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 self.useTemp = True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 ### Figure out which markers are in this region
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 markers = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 snpcols = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 chroms = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 minpos = 2**32
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 maxpos = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 for lnum,row in enumerate(mfile):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 line = row.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 if not line: continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 chrom, snp, genpos, abspos = line.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 ic = int(chrom)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 ic = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 if ic and ic <= 23:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 abspos = int(abspos)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 if abspos > maxpos:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 maxpos = abspos
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 if abspos < minpos:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 minpos = abspos
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 abspos = epos + 999999999 # so next test fails
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 if useRsdict.get(snp,None) or (spos <> -9 and chrom == chromosome and (spos <= abspos <= epos)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 if chromosome == '':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 chromosome = chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 chroms.setdefault(chrom,chrom)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 markers.append((chrom,abspos,snp)) # decorate for sort into genomic
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 snpcols[snp] = lnum # so we know which col to find genos for this marker
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 markers.sort()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 rslist = [x[2] for x in markers] # drop decoration
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 rsdict = dict(zip(rslist,rslist))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 if len(rslist) == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 s = '##! %s: Found no rs numbers matching %s' % (progname,self.args[1:3])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 self.lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 self.lf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 self.lf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 print >> sys.stdout, s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 if spos == -9:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 spos = minpos
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 epos = maxpos
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 s = '## %s looking for %d rs (%s)' % (progname,len(rslist),rslist[:5])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 self.lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 print >> sys.stdout, s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216 wewant = [(6+(2*snpcols[x])) for x in rslist] #
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217 # column indices of first geno of each marker pair to get the markers into genomic
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218 ### ... and then parse the rest of the ped file to pull out
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219 ### the genotypes for all subjects for those markers
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 # /usr/local/galaxy/data/rg/1/lped/
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 self.tempMapName = os.path.join(self.outfpath,'%s.info' % self.title)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 self.tempMap = file(self.tempMapName,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 self.tempPedName = os.path.join(self.outfpath,'%s.ped' % self.title)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 self.tempPed = file(self.tempPedName,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 self.pngpath = '%s.LD.PNG' % self.tempPedName
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 map = ['%s\t%s' % (x[2],x[1]) for x in markers] # snp,abspos in genomic order for haploview
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 self.tempMap.write('%s\n' % '\n'.join(map))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 self.tempMap.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 nrows = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 for line in dfile:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 line = line.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 if not line:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 fields = line.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 preamble = fields[:6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 g = ['%s %s' % (fields[snpcol], fields[snpcol+1]) for snpcol in wewant]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 g = ' '.join(g)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 g = g.split() # we'll get there
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 g = [atrandic.get(x,'0') for x in g] # numeric alleles...
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 self.tempPed.write('%s %s\n' % (' '.join(preamble), ' '.join(g)))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 nrows += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 self.tempPed.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 s = '## %s: wrote %d markers, %d subjects for region %s\n' % (progname,len(rslist),nrows,self.region)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 self.lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 self.lf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 print >> sys.stdout,s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 else: # even if using all, must set up haploview info file instead of map
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 markers = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 chroms = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 spos = sys.maxint
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 epos = -spos
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 for lnum,row in enumerate(mfile):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 line = row.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 if not line: continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 chrom, snp, genpos, abspos = line.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 ic = int(chrom)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 ic = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 if ic and ic <= 23:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 if chromosome == '':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 chromosome = chrom
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 chroms.setdefault(chrom,chrom)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 p = int(abspos)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 if p < spos and p <> 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 spos = p
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 if p > epos and p <> 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 epos = p
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
272 markers.append('%s %s' % (snp,abspos)) # no sort - pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
273 # now have spos and epos for hapmap if hmpanels
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
274 self.tempMapName = os.path.join(self.outfpath,'%s.info' % self.title)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
275 self.tempMap = file(self.tempMapName,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
276 self.tempMap.write('\n'.join(markers))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
277 self.tempMap.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
278 self.tempPedName = os.path.join(self.outfpath,'%s.ped' % self.title)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
279 try: # will fail on winblows!
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
280 os.symlink(self.DATA_FILE,self.tempPedName)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
281 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
282 shutil.copy(self.DATA_FILE,self.tempPedName) # wasteful but..
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
283 self.nchroms = len(chroms) # if > 1 can't really do this safely
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
284 dfile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
285 mfile.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
286 self.spos = spos
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
287 self.epos = epos
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
288 self.chromosome = chromosome
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
289 if self.nchroms > 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
290 s = '## warning - multiple chromosomes found in your map file - %s\n' % ','.join(chroms.keys())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
291 self.lf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
292 print >> sys.stdout,s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
293 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
294
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
295 def run(self,vcl):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
296 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
297 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
298 p=subprocess.Popen(vcl,shell=True,cwd=self.outfpath,stderr=self.lf,stdout=self.lf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
299 retval = p.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
300 self.lf.write('## executing %s returned %d\n' % (vcl,retval))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
301
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
302 def plotHmPanels(self,ste):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
303 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
304 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
305 sp = '%d' % (self.spos/1000.) # hapmap wants kb
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
306 ep = '%d' % (self.epos/1000.)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
307 fnum=0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
308 for panel in self.hmpanels:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
309 if panel > '' and panel.lower() <> 'none': # in case someone checks that option too :)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
310 ptran = panel.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
311 ptran = ptran.replace('+','_')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
312 fnum += 1 # preserve an order or else we get sorted
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
313 vcl = [javabin,'-jar',self.hvbin,'-n','-memory','%d' % self.memSize,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
314 '-chromosome',self.chromosome, '-panel',panel.strip(),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
315 '-hapmapDownload','-startpos',sp,'-endpos',ep,
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
316 '-ldcolorscheme',self.ldType]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
317 if self.minMaf:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
318 vcl += ['-minMaf','%f' % self.minMaf]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
319 if self.maxDist:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
320 vcl += ['-maxDistance',self.maxDist]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
321 if self.hiRes:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
322 vcl.append('-png')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
323 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
324 vcl.append('-compressedpng')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
325 if self.infotrack:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
326 vcl.append('-infoTrack')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
327 p=subprocess.Popen(' '.join(vcl),shell=True,cwd=self.outfpath,stderr=ste,stdout=self.lf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
328 retval = p.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
329 inpng = 'Chromosome%s%s.LD.PNG' % (self.chromosome,panel)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
330 inpng = inpng.replace(' ','') # mysterious spaces!
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
331 outpng = '%d_HapMap_%s_%s.png' % (fnum,ptran,self.chromosome)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
332 # hack for stupid chb+jpt
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
333 outpng = outpng.replace(' ','')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
334 tmppng = '%s.tmp.png' % self.title
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
335 tmppng = tmppng.replace(' ','')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
336 outpng = os.path.split(outpng)[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
337 vcl = [self.convert, '-resize 800x400!', inpng, tmppng]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
338 self.run(' '.join(vcl))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
339 s = "text 10,300 'HapMap %s'" % ptran.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
340 vcl = [self.convert, '-pointsize 25','-fill maroon',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
341 '-draw "%s"' % s, tmppng, outpng]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
342 self.run(' '.join(vcl))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
343 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
344 os.remove(os.path.join(self.outfpath,tmppng))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
345 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
346 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
347
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
348 def doPlots(self):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
349 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
350 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
351 DATA_FILE = self.tempPedName # for haploview
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
352 INFO_FILE = self.tempMapName
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
353 fblog,blog = tempfile.mkstemp()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
354 ste = open(blog,'w') # to catch the blather
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
355 # if no need to rewrite - set up names for haploview call
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
356 vcl = [javabin,'-jar',self.hvbin,'-n','-memory','%d' % self.memSize,'-pairwiseTagging',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
357 '-pedfile',DATA_FILE,'-info',INFO_FILE,'-tagrsqcounts',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
358 '-tagrsqcutoff',self.tagr2, '-ldcolorscheme',self.ldType]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
359 if self.minMaf:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
360 vcl += ['-minMaf','%f' % self.minMaf]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
361 if self.maxDist:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
362 vcl += ['-maxDistance',self.maxDist]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
363 if self.hiRes:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
364 vcl.append('-png')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
365 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
366 vcl.append('-compressedpng')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
367 if self.nchroms == 1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
368 vcl += ['-chromosome',self.chromosome]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
369 if self.infotrack:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
370 vcl.append('-infoTrack')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
371 self.run(' '.join(vcl))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
372 vcl = [self.mogrify, '-resize 800x400!', '*.PNG']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
373 self.run(' '.join(vcl))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
374 inpng = '%s.LD.PNG' % DATA_FILE # stupid but necessary - can't control haploview name mangle
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
375 inpng = inpng.replace(' ','')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
376 inpng = os.path.split(inpng)[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
377 tmppng = '%s.tmp.png' % self.title
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
378 tmppng = tmppng.replace(' ','')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
379 outpng = '1_%s.png' % self.title
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
380 outpng = outpng.replace(' ','')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
381 outpng = os.path.split(outpng)[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
382 vcl = [self.convert, '-resize 800x400!', inpng, tmppng]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
383 self.run(' '.join(vcl))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
384 s = "text 10,300 '%s'" % self.title[:40]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
385 vcl = [self.convert, '-pointsize 25','-fill maroon',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
386 '-draw "%s"' % s, tmppng, outpng]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
387 self.run(' '.join(vcl))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
388 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
389 os.remove(os.path.join(self.outfpath,tmppng))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
390 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
391 pass # label all the plots then delete all the .PNG files before munging
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
392 fnum=1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
393 if self.hmpanels:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
394 self.plotHmPanels(ste)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
395 nimages = len(glob.glob(os.path.join(self.outfpath,'*.png'))) # rely on HaploView shouting - PNG @!
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
396 self.lf.write('### nimages=%d\n' % nimages)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
397 if nimages > 0: # haploview may fail?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
398 vcl = '%s -format pdf -resize 800x400! *.png' % self.mogrify
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
399 self.run(vcl)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
400 vcl = '%s *.pdf --fitpaper true --outfile alljoin.pdf' % self.pdfjoin
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
401 self.run(vcl)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
402 vcl = '%s alljoin.pdf --nup 1x%d --outfile allnup.pdf' % (self.pdfnup,nimages)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
403 self.run(vcl)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
404 vcl = '%s -resize x300 allnup.pdf allnup.png' % (self.convert)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
405 self.run(vcl)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
406 ste.close() # temp file used to catch haploview blather
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
407 hblather = open(blog,'r').readlines() # to catch the blather
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
408 os.unlink(blog)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
409 if len(hblather) > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
410 self.lf.write('## In addition, Haploview complained:')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
411 self.lf.write(''.join(hblather))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
412 self.lf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
413 self.lf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
414
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
415 def writeHtml(self):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
416 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
417 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
418 flist = glob.glob(os.path.join(self.outfpath, '*'))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
419 flist.sort()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
420 ts = '!"#$%&\'()*+,-/:;<=>?@[\\]^_`{|}~' + string.whitespace
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
421 ftran = string.maketrans(ts,'_'*len(ts))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
422 outf = file(self.outfile,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
423 outf.write(galhtmlprefix % progname)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
424 s = '<h4>rgenetics for Galaxy %s, wrapping HaploView</h4>' % (progname)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
425 outf.write(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
426 mainthumb = 'allnup.png'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
427 mainpdf = 'allnup.pdf'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
428 if os.path.exists(os.path.join(self.outfpath,mainpdf)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
429 if not os.path.exists(os.path.join(self.outfpath,mainthumb)):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
430 outf.write('<table><tr><td colspan="3"><a href="%s">Main combined LD plot</a></td></tr></table>\n' % (mainpdf))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
431 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
432 outf.write('<table><tr><td><a href="%s"><img src="%s" title="Main combined LD image" hspace="10" align="middle">' % (mainpdf,mainthumb))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
433 outf.write('</td><td>Click the thumbnail at left to download the main combined LD image <a href=%s>%s</a></td></tr></table>\n' % (mainpdf,mainpdf))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
434 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
435 outf.write('(No main image was generated - this usually means a Haploview error connecting to Hapmap site - please try later)<br/>\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
436 outf.write('<br><div><hr><ul>\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
437 for i, data in enumerate( flist ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
438 dn = os.path.split(data)[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
439 if dn[:3] <> 'all':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
440 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
441 newdn = dn.translate(ftran)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
442 if dn <> newdn:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
443 os.rename(os.path.join(self.outfpath,dn),os.path.join(self.outfpath,newdn))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
444 dn = newdn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
445 dnlabel = dn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
446 ext = dn.split('.')[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
447 if dn == 'allnup.pdf':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
448 dnlabel = 'All pdf plots on a single page'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
449 elif dn == 'alljoin.pdf':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
450 dnlabel = 'All pdf plots, each on a separate page'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
451 outf.write('<li><a href="%s">%s - %s</a></li>\n' % (dn,dn,dnlabel))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
452 for i, data in enumerate( flist ):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
453 dn = os.path.split(data)[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
454 if dn[:3] == 'all':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
455 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
456 newdn = dn.translate(ftran)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
457 if dn <> newdn:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
458 os.rename(os.path.join(self.outfpath,dn),os.path.join(self.outfpath,newdn))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
459 dn = newdn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
460 dnlabel = dn
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
461 ext = dn.split('.')[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
462 if dn == 'allnup.pdf':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
463 dnlabel = 'All pdf plots on a single page'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
464 elif dn == 'alljoin.pdf':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
465 dnlabel = 'All pdf plots, each on a separate page'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
466 elif ext == 'info':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
467 dnlabel = '%s map data for Haploview input' % self.title
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
468 elif ext == 'ped':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
469 dnlabel = '%s genotype data for Haploview input' % self.title
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
470 elif dn.find('CEU') <> -1 or dn.find('YRI') <> -1 or dn.find('CHB_JPT') <> -1: # is hapmap
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
471 dnlabel = 'Hapmap data'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
472 if ext == 'TAGS' or ext == 'TESTS' or ext == 'CHAPS':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
473 dnlabel = dnlabel + ' Tagger output'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
474 outf.write('<li><a href="%s">%s - %s</a></li>\n' % (dn,dn,dnlabel))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
475 outf.write('</ol><br>')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
476 outf.write("</div><div><hr>Job Log follows below (see %s)<pre>" % self.logfn)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
477 s = file(self.log_file,'r').readlines()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
478 s = '\n'.join(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
479 outf.write('%s</pre><hr></div>' % s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
480 outf.write('</body></html>')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
481 outf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
482 if self.useTemp:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
483 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
484 os.unlink(self.tempMapName)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
485 os.unlink(self.tempPedName)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
486 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
487 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
488
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
489 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
490 """ ### Sanity check the arguments
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
491
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
492 <command interpreter="python">
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
493 rgHaploView.py "$ucsc_region" "$rslist" "$title" "$out_file1"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
494 "$lhistIn.extra_files_path" "$lhistIn.metadata.base_name"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
495 "$minmaf" "$maxdist" "$ldtype" "$hires" "$memsize" "$out_file1.files_path"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
496 "$infoTrack" "$tagr2" "$hmpanel" ${GALAXY_DATA_INDEX_DIR}/rg/bin/haploview.jar
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
497 </command>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
498
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
499 remember to figure out chromosome and complain if > 1?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
500 and use the -chromosome <1-22,X,Y> parameter to haploview
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
501 skipcheck?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
502 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
503 progname = os.path.split(sys.argv[0])[-1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
504 if len(sys.argv) < 16:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
505 s = '##!%s: Expected 16 params in sys.argv, got %d (%s)' % (progname,len(sys.argv), sys.argv)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
506 print s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
507 sys.exit(1)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
508 ld = ldPlot(argv = sys.argv)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
509 ld.doPlots()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
510 ld.writeHtml()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
511
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
512
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
513