comparison COBRAxy/flux_to_map.py @ 246:06e3b644de83 draft default tip

Uploaded
author francesco_lapi
date Wed, 15 Jan 2025 11:16:30 +0000
parents 58037c24c716
children
comparison
equal deleted inserted replaced
245:58037c24c716 246:06e3b644de83
840 missing_reactions = [reaction for reaction in required_reactions if reaction not in dataset.index] 840 missing_reactions = [reaction for reaction in required_reactions if reaction not in dataset.index]
841 841
842 if missing_reactions: 842 if missing_reactions:
843 sys.exit(f'Execution aborted: Missing required reactions {missing_reactions} in {datasetName}\n') 843 sys.exit(f'Execution aborted: Missing required reactions {missing_reactions} in {datasetName}\n')
844 844
845
846 # Calculate new rows using safe division 845 # Calculate new rows using safe division
847 lact_glc = np.divide( 846 lact_glc = np.divide(
848 np.clip(dataset.loc['EX_lac__L_e'].to_numpy(), a_min=0, a_max=None), 847 np.clip(dataset.loc['EX_lac__L_e'].to_numpy(), a_min=0, a_max=None),
849 np.clip(dataset.loc['EX_glc__D_e'].to_numpy(), a_min=None, a_max=0), 848 np.clip(dataset.loc['EX_glc__D_e'].to_numpy(), a_min=None, a_max=0),
850 out=np.full_like(dataset.loc['EX_lac__L_e'].to_numpy(), np.nan), # Prepara un array con NaN come output di default 849 out=np.full_like(dataset.loc['EX_lac__L_e'].to_numpy(), np.nan), # Prepara un array con NaN come output di default
868 out=np.full_like(dataset.loc['EX_lac__L_e'].to_numpy(), np.nan), 867 out=np.full_like(dataset.loc['EX_lac__L_e'].to_numpy(), np.nan),
869 where=dataset.loc['EX_gln__L_e'].to_numpy() != 0 868 where=dataset.loc['EX_gln__L_e'].to_numpy() != 0
870 ) 869 )
871 870
872 # Controllo e sostituzione dei NaN con 0 se necessario 871 # Controllo e sostituzione dei NaN con 0 se necessario
873 vectors = {'lact_glc': lact_glc, 'lact_gln': lact_gln, 'lact_o2': lact_o2, 'glu_gln': glu_gln} 872 values = {'lact_glc': lact_glc, 'lact_gln': lact_gln, 'lact_o2': lact_o2, 'glu_gln': glu_gln}
874 873
875 for idx, g in enumerate(glu_gln): 874 # Sostituzione di inf e NaN con 0 se necessario
876 if g == np.inf: 875 for key, value in values.items():
877 print(dataset.loc['EX_gln__L_e'][idx]) 876 values[key] = np.where(np.isinf(value), np.nan, value)
878 877
879 for name, vector in vectors.items(): 878 # Creazione delle nuove righe da aggiungere al dataset
880 if np.any(np.isinf(vector)): # Controlla se ci sono inf o -inf
881 vectors[name] = np.where(np.isinf(vector), np.nan, vector) # Sostituisci inf con NaN
882 if np.all(np.isnan(vector)): # Se tutto il vettore รจ NaN
883 vectors[name] = np.zeros_like(vector) # Sostituisci con 0
884
885
886 # Riassegna i vettori aggiornati
887 lact_glc, lact_gln, lact_o2, glu_gln = vectors['lact_glc'], vectors['lact_gln'], vectors['lact_o2'], vectors['glu_gln']
888
889 print(vectors)
890 # Create a DataFrame for the new rows
891 new_rows = pd.DataFrame({ 879 new_rows = pd.DataFrame({
892 dataset.index.name: ['LactGlc', 'LactGln','LactO2', 'GluGln'], 880 dataset.index.name: ['LactGlc', 'LactGln', 'LactO2', 'GluGln'],
893 **{col: [lact_glc[i], lact_gln[i],lact_o2[i], glu_gln[i]] for i, col in enumerate(dataset.columns)} 881 **{col: [values['lact_glc'][i], values['lact_gln'][i], values['lact_o2'][i], values['glu_gln'][i]]
882 for i, col in enumerate(dataset.columns)}
894 }) 883 })
895 884
896 # Reset the index of the original dataset and append new rows 885 # Ritorna il dataset originale con le nuove righe
897 dataset.reset_index(inplace=True) 886 dataset.reset_index(inplace=True)
898 dataset = pd.concat([dataset, new_rows], ignore_index=True) 887 dataset = pd.concat([dataset, new_rows], ignore_index=True)
899 888
900 IDs = pd.Series.tolist(dataset.iloc[:, 0].astype(str)) 889 IDs = pd.Series.tolist(dataset.iloc[:, 0].astype(str))
901 890