88"""
99
1010import argparse
11+ import os
1112
1213import numpy as np
1314import pandas as pd
@@ -51,21 +52,23 @@ def curve_fitting(seq, degree=2, penalty='abs'):
5152 return compute_curve (xx , result .x )
5253
5354
54- def curve_fitting_io (fn_in , fn_out , degree = 2 , penalty = 'abs' , alpha = 0.01 ):
55- # read all sheets
56- xl = pd . ExcelFile ( fn_in )
57- nSpots = len (xl . sheet_names )
58- data_all = []
55+ def curve_fitting_io (input_list , output , degree = 2 , penalty = 'abs' , alpha = 0.01 ):
56+
57+ # read all inputs
58+ nSpots = len (input_list )
59+ df_all , data_all = [], []
5960 for i in range (nSpots ):
60- df = pd .read_excel (xl , xl .sheet_names [i ])
61+ df = pd .read_csv (input_list [i ], delimiter = '\t ' )
62+ df .columns = df .columns .str .strip () # remove whitespaces from header names
63+ df_all .append (df )
6164 data_all .append (np .array (df ))
6265 col_names = df .columns .tolist ()
6366 ncols_ori = len (col_names )
6467
6568 # curve_fitting
6669 diff = np .array ([], dtype = ('float64' ))
6770 for i in range (nSpots ):
68- seq = data_all [i ][:, - 1 ]
71+ seq = data_all [i ][:, col_names . index ( 'intensity' ) ]
6972 seq_fit = seq .copy ()
7073 idx_valid = ~ np .isnan (seq )
7174 seq_fit [idx_valid ] = curve_fitting (seq [idx_valid ], degree = degree , penalty = penalty )
@@ -82,25 +85,21 @@ def curve_fitting_io(fn_in, fn_out, degree=2, penalty='abs', alpha=0.01):
8285 seq_assist = data_all [i ][:, - 1 ] + sig3
8386 data_all [i ] = np .concatenate ((data_all [i ], seq_assist .reshape (- 1 , 1 )), axis = 1 )
8487
85- # write to file
86- with pd .ExcelWriter (fn_out ) as writer :
87- for i in range (nSpots ):
88- df = pd .DataFrame ()
89- for c in range (ncols_ori ):
90- df [col_names [c ]] = data_all [i ][:, c ]
91- df ['CURVE' ] = data_all [i ][:, ncols_ori ]
92- if alpha > 0 :
93- df ['CURVE_A' ] = data_all [i ][:, ncols_ori + 1 ]
94- df .to_excel (writer , sheet_name = xl .sheet_names [i ], index = False , float_format = '%.2f' )
95- writer .save ()
88+ # write to files
89+ for i in range (nSpots ):
90+ df = df_all [i ]
91+ df ['curve' ] = data_all [i ][:, ncols_ori ]
92+ if alpha > 0 :
93+ df ['curve_a' ] = data_all [i ][:, ncols_ori + 1 ]
94+ df .to_csv (os .path .join (output , f'curve{ i + 1 } .tsv' ), sep = '\t ' , lineterminator = '\n ' , index = False )
9695
9796
9897if __name__ == "__main__" :
9998 parser = argparse .ArgumentParser (description = "Fit (1st- or 2nd-degree) polynomial curves to data points" )
100- parser .add_argument ("fn_in " , help = "File name of input data points (xlsx)" )
101- parser .add_argument ("fn_out " , help = "File name of output fitted curves (xlsx) " )
99+ parser .add_argument ("--input " , help = "File name of input data points (tsv)" , action = 'append' , type = str , required = True )
100+ parser .add_argument ("output " , help = "Name of output directory " )
102101 parser .add_argument ("degree" , type = int , help = "Degree of the polynomial function" )
103102 parser .add_argument ("penalty" , help = "Optimization objective for fitting" )
104103 parser .add_argument ("alpha" , type = float , help = "Significance level for generating assistive curves" )
105104 args = parser .parse_args ()
106- curve_fitting_io (args .fn_in , args .fn_out , args .degree , args .penalty , args .alpha )
105+ curve_fitting_io (args .input , args .output , args .degree , args .penalty , args .alpha )
0 commit comments