@@ -97,6 +97,28 @@ def ODF(odf_obj):
9797############################
9898
9999
100+ def write_odf (df , file_path , headers = None ):
101+ """
102+ Writes the provided DataFrame to a ODF file.
103+
104+ Assumes that the DataFrame matches the structure of those produced
105+ by the ODF() function in this library
106+
107+ :param df: the DataFrame to write to ODF
108+ :param file_path: path to which to write the ODF file
109+ :param headers: A dict of ODF headers, if none are provided will attempt to read them from the ODF file
110+ :return:
111+ """
112+ if headers is None and hasattr (df , 'headers' ):
113+ headers = df .headers
114+ else :
115+ raise AttributeError ('ODF headers not provided' )
116+
117+ with open (file_path , 'w' ) as file :
118+ file .write (_header_dict_to_str (headers ))
119+ df .to_csv (file , sep = '\t ' , header = False , index = False , mode = 'w+' )
120+
121+
100122def write_gct (df , file_path ):
101123 """
102124 Writes the provided DataFrame to a GCT file.
@@ -191,6 +213,41 @@ def _obtain_io(init_obj):
191213#########################
192214
193215
216+ def _header_dict_to_str (headers ):
217+ # Define the list of headers to handle as special cases
218+ special = ['HeaderLines' , 'COLUMN_NAMES' , 'COLUMN_TYPES' , 'Model' , 'DataLines' ]
219+
220+ # Add the initial ODF version line
221+ combined = 'ODF 1.0\n '
222+
223+ # Add HeaderLines
224+ combined += 'HeaderLines=' + str (len (headers )) + '\n '
225+
226+ # Add column names, if available
227+ if 'COLUMN_NAMES' in headers :
228+ combined += 'COLUMN_NAMES:' + str (headers ['COLUMN_NAMES' ]) + '\n '
229+
230+ # Add column types, if available
231+ if 'COLUMN_TYPES' in headers :
232+ combined += 'COLUMN_TYPES:' + str (headers ['COLUMN_TYPES' ]) + '\n '
233+
234+ # Add model, if available
235+ if 'Model' in headers :
236+ combined += 'Model=' + str (headers ['Model' ]) + '\n '
237+
238+ # Add remaining headers
239+ for key , value in sorted (headers .items ()):
240+ if key not in special :
241+ combined += str (key ) + '=' + str (value ) + '\n '
242+
243+ # Add data lines, if available
244+ if 'DataLines' in headers :
245+ combined += 'DataLines=' + str (headers ['DataLines' ]) + '\n '
246+
247+ # Return the combined header string
248+ return combined
249+
250+
194251def _apply_odf_properties (df , headers , model ):
195252 """
196253 Attach properties to the Dataframe to carry along ODF metadata
0 commit comments