annotate tools/rgenetics/rgCaCo.py @ 1:cdcb0ce84a1b

Uploaded
author xuebing
date Fri, 09 Mar 2012 19:45:15 -0500
parents 9071e359b9a3
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
1 #!/usr/local/bin/python
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
2 # hack to run and process a plink case control association
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
3 # expects args as
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
4 # bfilepath outname jobname outformat (wig,xls)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
5 # ross lazarus
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
6 # for wig files, we need annotation so look for map file or complain
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
7 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
8 Parameters for wiggle track definition lines
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
9 All options are placed in a single line separated by spaces:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
10
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
11 track type=wiggle_0 name=track_label description=center_label \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
12 visibility=display_mode color=r,g,b altColor=r,g,b \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
13 priority=priority autoScale=on|off \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
14 gridDefault=on|off maxHeightPixels=max:default:min \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
15 graphType=bar|points viewLimits=lower:upper \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
16 yLineMark=real-value yLineOnOff=on|off \
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
17 windowingFunction=maximum|mean|minimum smoothingWindow=off|2-16
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
18 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
20 import sys,math,shutil,subprocess,os,time,tempfile,string
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
21 from os.path import abspath
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
22 from rgutils import timenow, plinke
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
23 imagedir = '/static/rg' # if needed for images
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
24 myversion = 'V000.1 April 2007'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
25 verbose = False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
26
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
27 def makeGFF(resf='',outfname='',logf=None,twd='.',name='track name',description='track description',topn=1000):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
28 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
29 score must be scaled to 0-1000
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
30
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
31 Want to make some wig tracks from each analysis
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
32 Best n -log10(p). Make top hit the window.
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
33 we use our tab output which has
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
34 rs chrom offset ADD_stat ADD_p ADD_log10p
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
35 rs3094315 1 792429 1.151 0.2528 0.597223
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
36
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
37 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
38
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
39 def is_number(s):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
40 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
41 float(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
42 return True
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
43 except ValueError:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
44 return False
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
45 header = 'track name=%s description="%s" visibility=2 useScore=1 color=0,60,120\n' % (name,description)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
46 column_names = [ 'Seqname', 'Source', 'Feature', 'Start', 'End', 'Score', 'Strand', 'Frame', 'Group' ]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
47 halfwidth=100
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
48 resfpath = os.path.join(twd,resf)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
49 resf = open(resfpath,'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
50 resfl = resf.readlines() # dumb but convenient for millions of rows
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
51 resfl = [x.split() for x in resfl]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
52 headl = resfl[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
53 resfl = resfl[1:]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
54 headl = [x.strip().upper() for x in headl]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
55 headIndex = dict(zip(headl,range(0,len(headl))))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
56 whatwewant = ['CHR','RS','OFFSET','LOG10ARMITAGEP']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
57 wewant = [headIndex.get(x,None) for x in whatwewant]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
58 if None in wewant: # missing something
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
59 logf.write('### Error missing a required header from %s in makeGFF - headIndex=%s\n' % (whatwewant,headIndex))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
60 return
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
61 ppos = wewant[3] # last in list
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
62 resfl = [x for x in resfl if x[ppos] > '' and x[ppos] <> 'NA']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
63 resfl = [(float(x[ppos]),x) for x in resfl] # decorate
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
64 resfl.sort()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
65 resfl.reverse() # using -log10 so larger is better
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
66 pvals = [x[0] for x in resfl] # need to scale
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
67 resfl = [x[1] for x in resfl] # drop decoration
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
68 resfl = resfl[:topn] # truncate
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
69 maxp = max(pvals) # need to scale
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
70 minp = min(pvals)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
71 prange = abs(maxp-minp) + 0.5 # fudge
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
72 scalefact = 1000.0/prange
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
73 logf.write('###maxp=%f,minp=%f,prange=%f,scalefact=%f\n' % (maxp,minp,prange,scalefact))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
74 for i,row in enumerate(resfl):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
75 row[ppos] = '%d' % (int(scalefact*pvals[i]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
76 resfl[i] = row # replace
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
77 outf = file(outfname,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
78 outf.write(header)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
79 outres = [] # need to resort into chrom offset order
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
80 for i,lrow in enumerate(resfl):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
81 chrom,snp,offset,p, = [lrow[x] for x in wewant]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
82 gff = ('chr%s' % chrom,'rgCaCo','variation','%d' % (int(offset)-halfwidth),
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
83 '%d' % (int(offset)+halfwidth),p,'.','.','%s logp=%1.2f' % (snp,pvals[i]))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
84 outres.append(gff)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
85 outres = [(x[0],int(x[3]),x) for x in outres] # decorate
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
86 outres.sort() # into chrom offset
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
87 outres=[x[2] for x in outres] # undecorate
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
88 outres = ['\t'.join(x) for x in outres]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
89 outf.write('\n'.join(outres))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
90 outf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
91 outf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
92
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
93
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
94 def plink_assocToGG(plinkout="hm",tag='test'):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
95 """ plink --assoc output looks like this
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
96 # CHR SNP A1 F_A F_U A2 CHISQ P OR
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
97 # 1 rs3094315 G 0.6685 0.1364 A 104.1 1.929e-24 12.77
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
98 # write as a genegraph input file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
99 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
100 inf = file('%s.assoc' % plinkout,'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
101 outf = file('%sassoc.xls' % plinkout,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
102 res = ['rs\tlog10p%s\tFakeInvOR%s\tRealOR%s' % (tag,tag,tag),] # output header for ucsc genome graphs
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
103 head = inf.next()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
104 for l in inf:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
105 ll = l.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
106 if len(ll) >= 8:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
107 p = float(ll[7])
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
108 if p <> 'NA': # eesh
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
109 logp = '%9.9f' % -math.log10(p)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
110 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
111 logp = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
112 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
113 orat = ll[8]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
114 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
115 orat = 'NA'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
116 orat2 = orat
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
117 # invert large negative odds ratios
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
118 if float(orat) < 1 and float(orat) > 0.0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
119 orat2 = '%9.9f' % (1.0/float(orat))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
120 outl = [ll[1],logp, orat2, orat]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
121 res.append('\t'.join(outl))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
122 outf.write('\n'.join(res))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
123 outf.write('\n')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
124 outf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
125 inf.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
126
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
127 def xformModel(infname='',resf='',outfname='',
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
128 name='foo',mapf='/usr/local/galaxy/data/rg/ped/x.bim',flog=None):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
129 """munge a plink .model file into either a ucsc track or an xls file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
130 rerla@meme ~/plink]$ head hmYRI_CEU.model
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
131 CHR SNP TEST AFF UNAFF CHISQ DF P
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
132 1 rs3094315 GENO 41/37/11 0/24/64 NA NA NA
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
133 1 rs3094315 TREND 119/59 24/152 81.05 1 2.201e-19
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
134 1 rs3094315 ALLELIC 119/59 24/152 104.1 1 1.929e-24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
135 1 rs3094315 DOM 78/11 24/64 NA NA NA
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
136
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
137 bim file has
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
138 [rerla@beast pbed]$ head plink_wgas1_example.bim
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
139 1 rs3094315 0.792429 792429 G A
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
140 1 rs6672353 0.817376 817376 A G
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
141 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
142 if verbose:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
143 print 'Rgenetics rgCaCo.xformModel got resf=%s, outfname=%s' % (resf,outfname)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
144 res = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
145 rsdict = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
146 map = file(mapf,'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
147 for l in map: # plink map
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
148 ll = l.strip().split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
149 if len(ll) >= 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
150 rs=ll[1].strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
151 chrom = ll[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
152 if chrom.lower() == 'x':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
153 chrom='23'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
154 elif chrom.lower() == 'y':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
155 chrom = 24
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
156 elif chrom.lower() == 'mito':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
157 chrom = 25
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
158 offset = ll[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
159 rsdict[rs] = (chrom,offset)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
160 res.append('rs\tChr\tOffset\tGenop\tlog10Genop\tArmitagep\tlog10Armitagep\tAllelep\tlog10Allelep\tDomp\tlog10Domp')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
161 f = open(resf,'r')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
162 headl = f.readline()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
163 if headl.find('\t') <> -1:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
164 headl = headl.split('\t')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
165 delim = '\t'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
166 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
167 headl = headl.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
168 delim = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
169 whatwewant = ['CHR','SNP','TEST','AFF','UNAFF','CHISQ','P']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
170 wewant = [headl.index(x) for x in whatwewant]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
171 llen = len(headl)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
172 lnum = anum = 0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
173 lastsnp = None # so we know when to write out a gg line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
174 outl = {}
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
175 f.seek(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
176 for lnum,l in enumerate(f):
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
177 if lnum == 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
178 continue
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
179 ll = l.split()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
180 if delim:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
181 ll = l.split(delim)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
182 if len(ll) >= llen: # valid line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
183 chr,snp,test,naff,nuaff,chi,p = [ll[x] for x in wewant]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
184 snp = snp.strip()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
185 chrom,offset = rsdict.get(snp,(None,None))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
186 anum += 1
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
187 fp = 1.0 # if NA
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
188 lp = 0.0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
189 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
190 fp = float(p)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
191 if fp > 0:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
192 lp = -math.log10(fp)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
193 else:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
194 fp = 9e-100
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
195 flog.write('### WARNING - Plink calculated %s for %s p value!!! 9e-100 substituted!\n' % (p,test))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
196 flog.write('### offending line #%d in %s = %s' % (lnum,l))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
197 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
198 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
199 if snp <> lastsnp:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
200 if len(outl.keys()) > 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
201 sl = [outl.get(x,'?') for x in ('snp','chrom','offset','GENO','TREND','ALLELIC','DOM')]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
202 res.append('\t'.join(sl)) # last snp line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
203 outl = {'snp':snp,'chrom':chrom,'offset':offset} # first 3 cols for gg line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
204 lastsnp = snp # reset for next marker
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
205 #if p == 'NA':
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
206 # p = 1.0
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
207 # let's pass downstream for handling R is fine?
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
208 outl[test] = '%s\t%f' % (p,lp)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
209 if len(outl.keys()) > 3:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
210 l = [outl.get(x,'?') for x in ('snp','chrom','offset','GENO','TREND','ALLELIC','DOM')]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
211 res.append('\t'.join(l)) # last snp line
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
212 f = file(outfname,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
213 res.append('')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
214 f.write('\n'.join(res))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
215 f.close()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
216
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
217
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
218
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
219
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
220 if __name__ == "__main__":
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
221 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
222 # called as
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
223 <command interpreter="python">
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
224 rgCaCo.py '$i.extra_files_path/$i.metadata.base_name' "$name"
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
225 '$out_file1' '$logf' '$logf.files_path' '$gffout'
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
226 </command> </command>
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
227 """
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
228 if len(sys.argv) < 7:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
229 s = 'rgCaCo.py needs 6 params - got %s \n' % (sys.argv)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
230 print >> sys.stdout, s
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
231 sys.exit(0)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
232 bfname = sys.argv[1]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
233 name = sys.argv[2]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
234 killme = string.punctuation + string.whitespace
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
235 trantab = string.maketrans(killme,'_'*len(killme))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
236 name = name.translate(trantab)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
237 outfname = sys.argv[3]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
238 logf = sys.argv[4]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
239 logoutdir = sys.argv[5]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
240 gffout = sys.argv[6]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
241 topn = 1000
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
242 try:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
243 os.makedirs(logoutdir)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
244 except:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
245 pass
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
246 map_file = None
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
247 me = sys.argv[0]
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
248 amapf = '%s.bim' % bfname # to decode map in xformModel
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
249 flog = file(logf,'w')
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
250 logme = []
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
251 cdir = os.getcwd()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
252 s = 'Rgenetics %s http://rgenetics.org Galaxy Tools, rgCaCo.py started %s\n' % (myversion,timenow())
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
253 print >> sys.stdout, s # so will appear as blurb for file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
254 logme.append(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
255 if verbose:
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
256 s = 'rgCaCo.py: bfname=%s, logf=%s, argv = %s\n' % (bfname, logf, sys.argv)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
257 print >> sys.stdout, s # so will appear as blurb for file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
258 logme.append(s)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
259 twd = tempfile.mkdtemp(suffix='rgCaCo') # make sure plink doesn't spew log file into the root!
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
260 tname = os.path.join(twd,name)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
261 vcl = [plinke,'--noweb','--bfile',bfname,'--out',name,'--model']
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
262 p=subprocess.Popen(' '.join(vcl),shell=True,stdout=flog,cwd=twd)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
263 retval = p.wait()
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
264 resf = '%s.model' % tname # plink output is here we hope
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
265 xformModel(bfname,resf,outfname,name,amapf,flog) # leaves the desired summary file
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
266 makeGFF(resf=outfname,outfname=gffout,logf=flog,twd=twd,name='rgCaCo_TopTable',description=name,topn=topn)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
267 flog.write('\n'.join(logme))
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
268 flog.close() # close the log used
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
269 #shutil.copytree(twd,logoutdir)
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
270 shutil.rmtree(twd) # clean up
9071e359b9a3 Uploaded
xuebing
parents:
diff changeset
271