|
| 1 | +import csv |
| 2 | +import statistics |
| 3 | +import cobra |
| 4 | +import pandas |
| 5 | +import sklearn.linear_model |
| 6 | +from matplotlib import pyplot as plt |
| 7 | +from sklearn.model_selection import LeaveOneOut |
| 8 | + |
| 9 | +model=cobra.io.read_sbml_model("iJO1366.xml") |
| 10 | + |
| 11 | +expression_values=pandas.read_csv("expression values.csv",index_col='genes') |
| 12 | + |
| 13 | +flux=pandas.read_csv("Rnx-Flux relationship modified for MLR for each reaction.csv") |
| 14 | + |
| 15 | +data=pandas.read_csv("D:/semesters/11/3/list of genes added-Rnx-Flux relationship modified for MLR for each reaction .csv") |
| 16 | + |
| 17 | + |
| 18 | +rxns_f_list=pandas.read_csv("D:/semesters/11/first data ref 17/reactions.csv") |
| 19 | + |
| 20 | +list_of_systematic_names_of_needed_genes=[] |
| 21 | + |
| 22 | +for item in rxns_f_list: |
| 23 | + g_r_rule=model.reactions.get_by_id(item).gene_reaction_rule |
| 24 | + if 'or' in g_r_rule and 'and' in g_r_rule: |
| 25 | + or_splitted = g_r_rule.split(' or ') |
| 26 | + for i in or_splitted: |
| 27 | + if not 'and' in i: |
| 28 | + list_of_systematic_names_of_needed_genes.append(i) |
| 29 | + |
| 30 | + else: |
| 31 | + new_i = i.replace("( ", '') |
| 32 | + newer_i = new_i.replace(" )", '') |
| 33 | + |
| 34 | + list_of_systematic_names_of_needed_genes.extend(newer_i.split(' and ')) |
| 35 | + |
| 36 | + elif g_r_rule=="": |
| 37 | + pass |
| 38 | + #print(item ,'has no gene and',g_r_rule) |
| 39 | + elif 'or' in g_r_rule: |
| 40 | + list_of_systematic_names_of_needed_genes.extend(g_r_rule.split(' or ')) |
| 41 | + |
| 42 | + |
| 43 | + elif 'and' in g_r_rule: |
| 44 | + # print(item,'=',g_r_rule) |
| 45 | + list_of_systematic_names_of_needed_genes.extend(g_r_rule.split(' and ')) |
| 46 | + |
| 47 | + |
| 48 | + else: |
| 49 | + |
| 50 | + list_of_systematic_names_of_needed_genes.append(g_r_rule) |
| 51 | + |
| 52 | +'length= 126 ' |
| 53 | + |
| 54 | +#removing repetetive files: |
| 55 | +list_of_systematic_names_of_needed_genes=list(dict.fromkeys |
| 56 | + (list_of_systematic_names_of_needed_genes)) |
| 57 | + |
| 58 | + |
| 59 | + |
| 60 | +list_of_systematic_names_of_needed_genes.to_csv("genes needed.csv") |
| 61 | + |
| 62 | +column_of_reactions=[] |
| 63 | + |
| 64 | +column_of_genes=[] |
| 65 | +for n in range(flux.shape[0]): |
| 66 | + rxns=flux.iloc[n]["Rnx-Flux relationship"] |
| 67 | + rxns = rxns.replace("(", '') |
| 68 | + rxns = rxns.replace(")", '') |
| 69 | + rxns = rxns.replace(" ", '') |
| 70 | + |
| 71 | + list_of_rxns=[] |
| 72 | + if "+" in rxns: |
| 73 | + list_of_rxns=rxns.split("+") |
| 74 | + elif '-' in rxns: |
| 75 | + list_of_rxns=rxns.split("-") |
| 76 | + elif ',' in rxns: |
| 77 | + list_of_rxns=rxns.split(",") |
| 78 | + else: |
| 79 | + list_of_rxns.append(rxns) |
| 80 | + |
| 81 | + column_of_reactions.append(list_of_rxns) |
| 82 | + |
| 83 | + genes=[] |
| 84 | + for rxn in list_of_rxns: |
| 85 | + #print("list_of_rxns: ",list_of_rxns) |
| 86 | + #print("rxn: ",rxn) |
| 87 | + gene_rule=model.reactions.get_by_id(rxn).gene_reaction_rule |
| 88 | + |
| 89 | + if "or" in gene_rule: |
| 90 | + genes.extend(gene_rule.split(" or ")) |
| 91 | + elif "and" in gene_rule: |
| 92 | + genes.extend(gene_rule.split(" and ")) |
| 93 | + else: |
| 94 | + genes.append(gene_rule) |
| 95 | + genes=list(set(genes)) |
| 96 | + |
| 97 | + column_of_genes.append(genes) |
| 98 | + |
| 99 | +flux_and_genes=flux |
| 100 | +flux_and_genes["list of reactions"]=column_of_reactions |
| 101 | +flux_and_genes["list of genes"]=column_of_genes |
| 102 | + |
| 103 | +#flux_and_genes.to_csv("list of genes added-Rnx-Flux relationship modified for MLR for each reaction .csv") |
| 104 | +flux_and_genes.set_index("Flux Module Name (short)",inplace=True) |
| 105 | + |
| 106 | +conditions=['Acetate','Fructose','Galactose','Glucose','Glycerol','Gluconate', |
| 107 | + 'Pyruvate','Succinate'] |
| 108 | + |
| 109 | +pearson_for_reactions= {} |
| 110 | +for reaction in flux_and_genes.index: |
| 111 | + |
| 112 | + Y = flux_and_genes.loc[reaction,conditions] |
| 113 | + X = expression_values.loc[flux_and_genes.loc[reaction,"list of genes"],conditions] |
| 114 | + X=X.T |
| 115 | + |
| 116 | + loocv = LeaveOneOut() |
| 117 | + |
| 118 | + y_test_for_pearson = [] |
| 119 | + y_predicted_for_pearson = [] |
| 120 | + |
| 121 | + for train_index, test_index in loocv.split(X): |
| 122 | + x_train = X.iloc[train_index] |
| 123 | + y_train = Y.iloc[train_index] |
| 124 | + |
| 125 | + x_test = X.iloc[test_index] |
| 126 | + y_test = Y.iloc[test_index] |
| 127 | + MLR_model = sklearn.linear_model.LinearRegression() |
| 128 | + |
| 129 | + |
| 130 | + MLR_model.fit(x_train, y_train) |
| 131 | + |
| 132 | + |
| 133 | + predicted = MLR_model.predict(x_test) |
| 134 | + y_test_for_pearson.append(y_test.iloc[0]) |
| 135 | + y_predicted_for_pearson.append(predicted) |
| 136 | + |
| 137 | + y_test_for_pearson_df = pandas.DataFrame(y_test_for_pearson) |
| 138 | + y_predicted_for_pearson_df = pandas.DataFrame(y_predicted_for_pearson) |
| 139 | + |
| 140 | + #print('model score: ' , MLR_model.score(X,Y)) |
| 141 | + #print("y_test_for_pearson_df: ",y_test_for_pearson_df) |
| 142 | + pearson = y_test_for_pearson_df.corrwith(y_predicted_for_pearson_df, axis=0) |
| 143 | + pearson_for_reactions[reaction] = pearson.iloc[0] |
| 144 | + |
| 145 | + |
| 146 | +pearson_for_reactions_df=pandas.DataFrame(pearson_for_reactions,index=["pearson"]) |
| 147 | +pearson_for_reactions_df=pearson_for_reactions_df.T |
| 148 | + |
| 149 | +#pearson_for_reactions_df.columns=["Flux Module Name (short)"] |
| 150 | + |
| 151 | +#print("pearson_for_reactions as df: ",pearson_for_reactions_df) |
| 152 | + |
0 commit comments