annotate tools/chasm/chasm_web.py @ 3:89407d4da3ca

fix for closing tag
author Saket Choudhary <saketkc@gmail.com>
date Fri, 01 Nov 2013 02:46:48 +0530
parents 8eaaa7f6b619
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
1 #!/usr/bin/python
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
2 """
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
3 The MIT License (MIT)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
4
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
5 Copyright (c) 2013 Saket Choudhary, <saketkc@gmail.com>
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
6
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
8 of this software and associated documentation files (the "Software"), to deal
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
9 in the Software without restriction, including without limitation the rights
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
11 copies of the Software, and to permit persons to whom the Software is
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
12 furnished to do so, subject to the following conditions:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
13
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
14 The above copyright notice and this permission notice shall be included in
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
15 all copies or substantial portions of the Software.
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
16
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
23 THE SOFTWARE.
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
24
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
25 """
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
26 import requests
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
27 import argparse
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
28 import sys
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
29 import time
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
30 from functools import wraps
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
31 import simplejson as json
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
32 import zipfile
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
33 import tempfile, ntpath, shutil
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
34 import xlrd
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
35 import csv
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
36 import os
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
37 sheet_map = {0:"Variant_Analysis.csv",1:"Amino_Acid_Level_Analysis.csv",2:"Gene_Level_Analysis.csv"}
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
38 def retry(ExceptionToCheck, tries=4, delay=3, backoff=2, logger=None):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
39 """Retry calling the decorated function using an exponential backoff.
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
40
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
41 http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
42 original from: http://wiki.python.org/moin/PythonDecoratorLibrary#Retry
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
43
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
44 :param ExceptionToCheck: the exception to check. may be a tuple of
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
45 exceptions to check
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
46 :type ExceptionToCheck: Exception or tuple
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
47 :param tries: number of times to try (not retry) before giving up
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
48 :type tries: int
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
49 :param delay: initial delay between retries in seconds
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
50 :type delay: int
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
51 :param backoff: backoff multiplier e.g. value of 2 will double the delay
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
52 each retry
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
53 :type backoff: int
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
54 :param logger: logger to use. If None, print
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
55 :type logger: logging.Logger instance
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
56 """
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
57 def deco_retry(f):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
58
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
59 @wraps(f)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
60 def f_retry(*args, **kwargs):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
61 mtries, mdelay = tries, delay
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
62 while mtries > 1:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
63 try:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
64 return f(*args, **kwargs)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
65 except ExceptionToCheck, e:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
66 #msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
67 msg = "Retrying in %d seconds..." % (mdelay)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
68 if logger:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
69 logger.warning(msg)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
70 else:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
71 print msg
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
72 time.sleep(mdelay)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
73 mtries -= 1
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
74 mdelay *= backoff
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
75 return f(*args, **kwargs)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
76
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
77 return f_retry # true decorator
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
78
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
79 return deco_retry
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
80 CANCERTYPES =["Bladder","Blood-Lymphocyte","Blood-Myeloid","Brain-Cerebellum","Brain-Glioblastoma_Multiforme","Brain-Lower_Grade_Glioma","Breast","Cervix","Colon","Head_and_Neck","Kidney-Chromophobe","Kidney-Clear_Cell","Kidney-Papiallary_Cell","Liver-Nonviral","Liver-Viral","Lung-Adenocarcinoma","Lung-Squamous_Cell","Melanoma","Other","Ovary","Pancreas","Prostate-Adenocarcinoma","Rectum","Skin","Stomach","Thyroid","Uterus"]
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
81 URL="http://www.cravat.us/ClassifierSelect1"
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
82 def stop_err( msg ):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
83 sys.stderr.write( '%s\n' % msg )
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
84 sys.exit()
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
85
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
86 class CHASMWeb:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
87 def __init__( self, mutationbox=None, filepath=None,is_hg_18=None, analysis_type=None, analysis_program=None, chosendb=None, cancer_type=None, email=None, annotate_genes=None, text_reports=None, mupit_out=None ):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
88 self.mutationbox = mutationbox
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
89 self.filepath = filepath
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
90 self.is_hg_18 = is_hg_18
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
91 self.analysis_type = analysis_type
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
92 self.analysis_program = analysis_program
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
93 self.chosendb = chosendb
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
94 self.email = email
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
95 self.annotate_genes = annotate_genes
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
96 self.cancer_type = cancer_type
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
97 self.email = email
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
98 self.annotate_genes = annotate_genes
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
99 self.text_reports=text_reports
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
100 self.mupit_input= mupit_out
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
101 def make_request( self ):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
102 data = {
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
103 "mutationbox":self.mutationbox,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
104 "hg18": self.is_hg_18,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
105 "analysistype": self.analysis_type,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
106 "chosendb": self.analysis_program,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
107 "cancertype": self.cancer_type,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
108 "geneannotcheckbox": self.annotate_genes,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
109 "emailbox": self.email,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
110 "tsvreport": self.text_reports,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
111 "mupitinput": self.mupit_input,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
112 }
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
113 stripped_data = {}
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
114
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
115 for key,value in data.iteritems():
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
116 if value==True:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
117 value="on"
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
118 if value!=None and value!=False:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
119 stripped_data[key]=value
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
120 #print stripped_data
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
121 if not self.mutationbox:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
122 file_payload={"inputfile":open(self.filepath)}
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
123 request = requests.post(URL, data=stripped_data, files=file_payload)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
124 else:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
125 request = requests.post(URL, data=stripped_data, files=dict(foo='bar'))
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
126 job_id = json.loads(request.text)["jobId"]
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
127 return job_id
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
128 @retry(requests.exceptions.HTTPError)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
129 def zip_exists(self,job_id ):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
130 url="http://www.cravat.us/results/%s/%s.zip" %(job_id,job_id)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
131 zip_download_request = requests.request("GET", url)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
132 if zip_download_request.status_code==404:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
133 raise requests.HTTPError()
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
134 else:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
135 return url
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
136 def download_zip( self, url, job_id):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
137 self.tmp_dir = tempfile.mkdtemp()
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
138 r = requests.get( url, stream=True )
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
139 if r.status_code == 200:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
140 self.path = os.path.join( self.tmp_dir,job_id+".zip" )
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
141 with open(self.path, 'wb') as f:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
142 for chunk in r.iter_content(128):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
143 f.write(chunk)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
144 else:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
145 self.path = None
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
146 return self.path
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
147
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
148 def move_files( self, file_map ):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
149 fh = open(self.path,"rb")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
150 zip_files = zipfile.ZipFile(fh)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
151 for name in zip_files.namelist():
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
152 filename = ntpath.basename(name)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
153 extension = ntpath.splitext(filename)[-1]
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
154 source_file = zip_files.open(name)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
155 if extension==".txt":
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
156 target_file = open(file_map["error.txt"],"wb")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
157 elif filename!="SnvGet Feature Description.xls" and extension!=".xls":
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
158 target_file = open(file_map[filename],"wbb")#file(os.path.join(output_dir,filename),"wb")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
159 else:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
160 target_file=None
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
161 if target_file:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
162 with source_file,target_file:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
163 shutil.copyfileobj(source_file,target_file)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
164 if filename=="SnvGet Feature Description.xls":
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
165 with xlrd.open_workbook(source_file) as wb:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
166 sheet_names = wb.sheet_names()
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
167 for name in sheet_names:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
168 sh=wb.sheet_by_name(name)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
169 name_shortened= name.replace(" ").strip()+".csv"
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
170 with open(name_shortened,'wb') as f:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
171 c = csv.writer(f)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
172 for r in range(sh.nrows):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
173 c.writerow(sh.row_values(r))
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
174 shutil.rmtree(self.tmp_dir)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
175 fh.close()
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
176 def main(params):
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
177
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
178 parser = argparse.ArgumentParser()
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
179 parser.add_argument("-i","--input", type=str, dest="mutationbox", help="Input variants")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
180 parser.add_argument("--path",type=str,dest="input_file_location", help="Input file location")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
181 parser.add_argument("--hg18", dest="hg18", action="store_true")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
182 parser.add_argument("--analysis_type",dest="analysis_type",type=str,choices=["driver","functional","geneannotationonly"], default="driver")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
183 parser.add_argument("--chosendb",dest="chosendb",type=str,nargs="*",choices=["CHASM","SnvGet"], default="CHASM")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
184 parser.add_argument("--cancertype", dest="cancer_type", type=str, choices=CANCERTYPES, required=True)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
185 parser.add_argument("--email", dest="email", required=True, type=str)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
186 parser.add_argument("--annotate", dest="annotate", action="store_true", default=None )
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
187 parser.add_argument("--tsv_report", dest="tsv_report", action="store_true", default=None )
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
188 parser.add_argument("--mupit_out", dest="mupit_out", action="store_true", default=None )
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
189 parser.add_argument("--gene_analysis_out", dest="gene_analysis_out", type=str, required=True)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
190 parser.add_argument("--variant_analysis_out", dest="variant_analysis_out", type=str, required=True)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
191 parser.add_argument("--amino_acid_level_analysis_out", dest="amino_acid_level_analysis_out", type=str, required=True,)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
192 parser.add_argument("--error_file", dest="error_file_out", type=str, required=True)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
193 parser.add_argument("--snv_box_out", dest="snv_box_out", type=str, required=False)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
194 parser.add_argument("--snv_features", dest="snv_features_out", type=str, required=False)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
195 args = (parser.parse_args(params))
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
196 chasm_web = CHASMWeb(mutationbox=args.mutationbox,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
197 filepath=args.input_file_location,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
198 is_hg_18 = args.hg18,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
199 analysis_type=args.analysis_type,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
200 chosendb = args.chosendb,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
201 cancer_type = args.cancer_type,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
202 email=args.email,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
203 annotate_genes=args.annotate,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
204 text_reports=args.tsv_report,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
205 mupit_out=args.mupit_out)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
206 job_id=chasm_web.make_request()
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
207 file_map = {"Amino_Acid_Level_Analysis.tsv":args.amino_acid_level_analysis_out,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
208 "SNVBox.tsv":args.snv_box_out,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
209 "Variant_Analysis.tsv":args.variant_analysis_out,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
210 "Gene_Level_Analysis.tsv":args.gene_analysis_out,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
211 "SnvGet Feature Description.xls":args.snv_features_out,
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
212 "error.txt":args.error_file_out
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
213 }
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
214 url = chasm_web.zip_exists(job_id)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
215 download = chasm_web.download_zip(url,job_id)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
216 if download:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
217 move = chasm_web.move_files(file_map=file_map)
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
218 else:
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
219 stop_err("Unable to download from the server")
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
220
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
221 if __name__=="__main__":
8eaaa7f6b619 Move files to tools
Saket Choudhary <saketkc@gmail.com>
parents:
diff changeset
222 main(sys.argv[1:])