annotate Split_ExcelTabs_IntoFiles.py @ 1:f1bcd79cd923 draft default tip

Uploaded
author insilico-bob
date Tue, 27 Nov 2018 14:20:40 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
1 import sys
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
2 import os
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
3 #import MySQLdb
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
4 #import config
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
5 import subprocess
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
6 import re
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
7 import shutil
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
8 import traceback
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
9 #import xlsxwriter
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
10 import xlrd
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
11
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
12 #http://www.blog.pythonlibrary.org/2014/04/30/reading-excel-spreadsheets-with-python-and-xlrd/
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
13
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
14 def File_From_Tab(infileName, outfileName, tabName,tabNumber):
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
15 """
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
16 Open and read an Excel file
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
17 """
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
18 book = xlrd.open_workbook(infileName)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
19 # print number of sheets
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
20 #print book.nsheets
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
21
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
22 # print sheet names
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
23 tabList= book.sheet_names()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
24 #print tabList
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
25 #print book.sheet_names()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
26 if tabName == "" and (tabNumber <1 or tabNumber > len(tabList)):
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
27 sys.stderr.write("\n>>>ERROR illegal tab number "+str(tabNumber)+" input when no tab name was specified\n")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
28 sys.stderr.write("\n>>>Allowed tab numbers, or tab names, for this file with "+str(len(tabList))+" total tabs are:")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
29
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
30 for i in range(len(tabList)):
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
31 sys.stderr.write("\n>>> tab number "+str(i+1)+" is named "+str(tabList[i]))
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
32 sys.exit(-1)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
33
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
34 if tabName != "": # use name instead of tab number
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
35 found = False
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
36 i = 0
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
37 while (i < len(tabList)) and not found:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
38 i += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
39 if tabName == str(tabList[i-1]):
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
40 tabNumber = i
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
41 found = True
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
42 if not found:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
43 sys.stderr("\n>>> ERROR -- Input Tab name "+tabName+" was not found\n")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
44 sys.exit(-1)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
45 # get the first worksheet
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
46 #first_sheet = book.sheet_by_index(0)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
47 worksheet = book.sheet_by_index(tabNumber-1)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
48
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
49 outFile = open(outfileName+str(tabList[tabNumber-1]+".tsv"), 'w')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
50
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
51 #https://stackoverflow.com/questions/14944623/python-xrld-read-rows-and-columns
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
52 #workbook = xlrd.open_workbook('my_workbook.xls')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
53 #worksheet = workbook.sheet_by_name('Sheet1')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
54 num_rows = worksheet.nrows - 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
55 num_cells = worksheet.ncols - 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
56 curr_row = -1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
57 while curr_row < num_rows:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
58 curr_row += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
59 row = worksheet.row(curr_row)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
60
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
61 if curr_row == 0:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
62 endOfLine= False
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
63 allRowNumCols= len(row)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
64 i= len(row)-1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
65 # find length of matrix and covariates using first row
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
66 # Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
67 while i <= len(row)-1 and not endOfLine:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
68 cell_type = worksheet.cell_type(curr_row, i)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
69 #temp = str(worksheet.cell_value(curr_row, i))
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
70 #print( " pos and cell type row one ",cell_type, i)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
71
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
72 if cell_type == 0 or cell_type == 6:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
73 allRowNumCols -= 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
74 i -= 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
75 else:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
76 endOfLine= True
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
77
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
78 if allRowNumCols < 5:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
79 sys.stderr.write("\nERROR First row number of columns= "+str(allRowNumCols)+" is too short, so all rows will be ignored\n")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
80 sys.exit(-1)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
81 elif curr_row == 0:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
82 sys.stdout.write("\nALL Rows must all have the same number of columns as the First row's number columns = "+ str(allRowNumCols) +"\n")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
83
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
84 temp= ''
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
85 rowLen= 0
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
86 endOfLine= False
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
87
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
88 while rowLen < allRowNumCols and not endOfLine:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
89 temp += str(worksheet.cell_value(curr_row, rowLen))+"\t"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
90 #temp += str(row[rowLen])+"\t"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
91 rowLen += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
92
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
93 temp = temp[:-1]+"\n"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
94 #print 'Row:', curr_row, len(row), rowLen
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
95 outFile.write(temp) #TODO check if rows are all same length
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
96
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
97 sys.stdout.write("File created with "+str(curr_row)+" rows and "+str(allRowNumCols)+" columns\n")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
98 # curr_cell = -1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
99 # while curr_cell < num_cells:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
100 # curr_cell += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
101 # # Cell Types: 0=Empty, 1=Text, 2=Number, 3=Date, 4=Boolean, 5=Error, 6=Blank
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
102 # cell_type = worksheet.cell_type(curr_row, curr_cell)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
103 # cell_value = worksheet.cell_value(curr_row, curr_cell)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
104 # print ' ', cell_type, ':', cell_value
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
105 # # read a row
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
106 # print first_sheet.row_values(0)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
107 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
108 # # read a cell
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
109 # cell = first_sheet.cell(0,0)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
110 # print cell
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
111 # print cell.value
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
112 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
113 # # read a row slice
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
114 # print first_sheet.row_slice(rowx=0,
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
115 # start_colx=0,
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
116 # end_colx=2)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
117
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
118 return tabList
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
119
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
120
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
121 #======================
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
122 # from RPPA callInSilicoReportWriter.py
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
123 # def write_xlsx_for_report(directory_for_reports, report_name, report_id, dict_cf2_values):
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
124 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
125 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
126 # error_write_xlsx = ""
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
127 # error_occurred = 0
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
128 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
129 # try:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
130 # path_to_dir_when_writing = os.path.join(directory_for_reports, report_name)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
131 # header_path = os.path.join(directory_for_reports, report_name, "header.csv")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
132 # raw_log_2_path = os.path.join(directory_for_reports, report_name, "RawLog2.csv")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
133 # norm_linear_path = os.path.join(directory_for_reports, report_name, "NormLinear.csv")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
134 # norm_log_2_path = os.path.join(directory_for_reports, report_name, "NormLog2.csv")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
135 # norm_log_2_median_centered_path = os.path.join(directory_for_reports, report_name, "NormLog2_MedianCentered.csv")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
136 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
137 # # put the cf2 values in the NormLinear file
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
138 # error_put_cf2_in_normLinear = write_new_normLinear_csv_file_with_cf2_values(path_to_dir_when_writing, norm_linear_path, dict_cf2_values)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
139 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
140 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
141 # excel_workBook = xlsxwriter.Workbook(os.path.join(directory_for_reports, report_name,report_name + ".xlsx"), {'strings_to_numbers': True})
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
142 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
143 # rawLog2_worksheet = excel_workBook.add_worksheet("RawLog2")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
144 # error_rawLog2 = construct_worksheet_for_xlsx(rawLog2_worksheet, header_path, "RawLog2", raw_log_2_path)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
145 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
146 # norm_linear_worksheet = excel_workBook.add_worksheet("NormLinear")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
147 # error_norm_linear = construct_worksheet_for_xlsx(norm_linear_worksheet, header_path, "NormLinear", norm_linear_path)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
148 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
149 # norm_log_2_worksheet = excel_workBook.add_worksheet("NormLog2")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
150 # error_norm_log_2 = construct_worksheet_for_xlsx(norm_log_2_worksheet, header_path, "NormLog2", norm_log_2_path)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
151 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
152 # norm_log_2_median_centered_worksheet = excel_workBook.add_worksheet("NormLog2_MedianCentered")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
153 # error_norm_log_2_median_centered = construct_worksheet_for_xlsx(norm_log_2_median_centered_worksheet, header_path, "Median-Centered", norm_log_2_median_centered_path)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
154 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
155 # errors_array = [error_put_cf2_in_normLinear, error_rawLog2, error_norm_linear, error_norm_log_2, error_norm_log_2_median_centered]
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
156 # for error in errors_array:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
157 # if error != "":
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
158 # error_write_xlsx = error_write_xlsx + error
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
159 # error_occurred = 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
160 # if error_occurred == 1:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
161 # error_write_xlsx + "\nThe excel workbook for the report "+report_name+" was not written successfully.\n\n"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
162 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
163 # excel_workBook.close()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
164 # except Exception, e:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
165 # error_occurred = 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
166 # error_write_xlsx += str(repr(e)) + "\n\n"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
167 # error_write_xlsx + "\nThe excel workbook for the report "+report_name+" was not written successfully.\n\n"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
168 # try:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
169 # excel_workBook.close()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
170 # except Exception, f:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
171 # sys.stderr.write("An unforeseen problem has occurred in write_xlsx_for_report()\n")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
172 # sys.stderr.write(str(repr(f)) + "\n\n")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
173 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
174 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
175 # return error_occurred, error_write_xlsx
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
176 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
177 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
178 # def write_new_normLinear_csv_file_with_cf2_values(path_to_dir, norm_linear_path, dict_cf2_values):
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
179 # errors = ""
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
180 # try:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
181 # titles = {}
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
182 # new_lines_normLinear_with_cf2 = []
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
183 # # read old norm linear file
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
184 # rf_normLinear = open(norm_linear_path, 'rU')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
185 # line_num = 0
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
186 # for line in rf_normLinear:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
187 # line = strip_new_line_from_right_side(line)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
188 # toks = line.split(",")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
189 # line_num += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
190 # if line_num == 1:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
191 # line += "1,CF2"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
192 # new_lines_normLinear_with_cf2.append(line)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
193 # titles = toks
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
194 # continue
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
195 # pos_rf = int(toks[titles.index('Order')])
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
196 # line += "," + str(dict_cf2_values[pos_rf])
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
197 # new_lines_normLinear_with_cf2.append(line)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
198 # rf_normLinear.close()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
199 # # rename the old normLinear file
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
200 # os.rename(norm_linear_path, os.path.join(path_to_dir, 'before_cf2_NormLinear.csv'))
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
201 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
202 # # write new normLinear with cf2
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
203 # wf_new_normLinear = open(norm_linear_path, 'w')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
204 # for line_writing in new_lines_normLinear_with_cf2:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
205 # wf_new_normLinear.write(line_writing + "\n")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
206 # wf_new_normLinear.close()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
207 # except Exception, err_write_normLinear_with_cf2_values:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
208 # errors = str(repr(err_write_normLinear_with_cf2_values))
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
209 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
210 # return errors
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
211 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
212 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
213 # # This function constructs the worksheet for each tab in the excel file for a report
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
214 # # It puts these things in this order:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
215 # # 1. Title of the tab
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
216 # # 2. Header for the tab
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
217 # # 3. Content of the tab
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
218 # def construct_worksheet_for_xlsx(worksheet, header_path, title_top_of_tab, tab_input_path):
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
219 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
220 # reload(sys)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
221 # sys.setdefaultencoding('utf8')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
222 # errors = ""
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
223 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
224 # try:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
225 # # Write the title at the top of the tab
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
226 # worksheet.write(0,0,title_top_of_tab)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
227 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
228 # # Variable to keep track of the rows
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
229 # row_num = 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
230 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
231 # # Write the header stuff
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
232 # header_file = open(header_path, 'rU')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
233 # for head_line in header_file:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
234 # head_line = strip_new_line_from_right_side(head_line)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
235 # head_toks = head_line.split(",")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
236 # col_num = 0
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
237 # for tok in head_toks:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
238 # worksheet.write(row_num, col_num, tok)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
239 # col_num += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
240 # row_num += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
241 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
242 # # Write the content stuff
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
243 # tab_input_file = open(tab_input_path, 'rU')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
244 # for tab_line in tab_input_file:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
245 # tab_line = strip_new_line_from_right_side(tab_line)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
246 # tab_toks = tab_line.split(",")
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
247 # col_num = 0
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
248 # for tok in tab_toks:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
249 # tok = tok.decode('iso-8859-1').encode('utf-8')
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
250 # worksheet.write(row_num, col_num, tok)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
251 # col_num += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
252 # row_num += 1
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
253 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
254 # header_file.close()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
255 # tab_input_file.close()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
256 # except Exception, e:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
257 # errors = errors + "\n\nAn error occurred while constructing the "+title_top_of_tab+" tab for the excel file.\n"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
258 # errors = errors + "The error was :\n\t" + str(e) + "\n\n"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
259 # try:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
260 # header_file.close()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
261 # tab_input_file.close()
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
262 # except NameError:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
263 # x = 5
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
264 #
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
265 return errors
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
266
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
267 #----------------------------------------------------------------------
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
268 if __name__ == "__main__":
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
269
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
270 #try:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
271 if len(sys.argv) > 4:
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
272 infileName = '"'+sys.argv[1]+'"'
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
273 tabName = '"'+sys.argv[2]+'"'
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
274 tabNumber = 0
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
275 if tabName == '': tabNumber = int(sys.argv[3])
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
276 outfileName = '"'+sys.argv[4]+'"' #TODO Later multiple outputs one per tab
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
277
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
278 sys.stdout.write( "\nInput parameters ",str(sys.argv[1:4]),"\n" )
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
279
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
280 #infileName = "/Users/bobbrown/Desktop/01_Gordon_Mills__Zhiyong_Ding.xlsx"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
281 #outfileName= "/Users/bobbrown/Desktop/01_Gordon_Mills__Zhiyong_Ding-Tab-Out-"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
282 #tabName ="NormLog2"
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
283 #tabName =""
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
284 #tabNumber= 10
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
285
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
286 status= File_From_Tab(infileName, outfileName, tabName, tabNumber )
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
287 #except
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
288 #sys.exit(-1)
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
289
f1bcd79cd923 Uploaded
insilico-bob
parents:
diff changeset
290 sys.exit(0)