supercat666 commited on
Commit
f067f02
1 Parent(s): 9999544
Files changed (2) hide show
  1. app.py +2 -2
  2. cas9on.py +25 -21
app.py CHANGED
@@ -287,8 +287,8 @@ if selected_model == 'Cas9':
287
  # cas9on.create_bed_file_from_df(df, bed_file_path)
288
  # cas9on.create_csv_from_df(df, csv_file_path)
289
 
290
- # Assuming create_bigwig_from_df is a function that generates a BigWig file from the DataFrame
291
- cas9on.create_bigwig_from_df(df, bigwig_file_path)
292
 
293
  # Prepare an in-memory buffer for the ZIP file
294
  zip_buffer = io.BytesIO()
 
287
  # cas9on.create_bed_file_from_df(df, bed_file_path)
288
  # cas9on.create_csv_from_df(df, csv_file_path)
289
 
290
+ # Assuming create_bigwig is a function that generates a BigWig file from the DataFrame
291
+ cas9on.create_bigwig(df, bigwig_file_path)
292
 
293
  # Prepare an in-memory buffer for the ZIP file
294
  zip_buffer = io.BytesIO()
cas9on.py CHANGED
@@ -203,24 +203,28 @@ def process_gene(gene_symbol, model_path):
203
  # df.to_csv(output_path, index=False)
204
 
205
 
206
- def create_bigwig(df, bigwig_path, chrom_sizes_path):
207
- bw = pyBigWig.open(bigwig_path, "w")
208
-
209
- # Load chromosome sizes
210
- chrom_sizes = {}
211
- with open(chrom_sizes_path, 'r') as f:
212
- for line in f:
213
- chrom, size = line.strip().split()
214
- chrom_sizes[chrom] = int(size)
215
-
216
- bw.addHeader(list(chrom_sizes.items()))
217
-
218
- for chrom in df['Chr'].unique():
219
- chrom_df = df[df['Chr'] == chrom]
220
- bw.addEntries(
221
- chrom,
222
- chrom_df['Start Pos'].astype(int).tolist(),
223
- ends=chrom_df['End Pos'].astype(int).tolist(),
224
- values=chrom_df['Prediction'].astype(float).tolist()
225
- )
226
- bw.close()
 
 
 
 
 
203
  # df.to_csv(output_path, index=False)
204
 
205
 
206
+ def create_bigwig(predictions, bigwig_path):
207
+ # Convert predictions to DataFrame if it's a list of lists
208
+ if isinstance(predictions, list):
209
+ import pandas as pd
210
+ df = pd.DataFrame(predictions, columns=["Chr", "Start Pos", "End Pos", "Strand", "Transcript", "Exon", "Target", "gRNA", "Prediction"])
211
+ else:
212
+ df = predictions # Assuming predictions is already a DataFrame
213
+
214
+ # Calculate chromosome sizes as the maximum end position per chromosome
215
+ chrom_sizes = df.groupby('Chr')['End Pos'].max().to_dict()
216
+
217
+ # Create a BigWig file
218
+ with pyBigWig.open(bigwig_path, "w") as bw:
219
+ # Add chromosome sizes to the header
220
+ bw.addHeader(list(chrom_sizes.items()))
221
+
222
+ # Add entries for each prediction
223
+ for index, row in df.iterrows():
224
+ chrom = row['Chr']
225
+ start = int(row['Start Pos']) - 1 # BigWig positions are 0-based
226
+ end = int(row['End Pos'])
227
+ score = float(row['Prediction'])
228
+
229
+ # Add the entry to the BigWig file
230
+ bw.addEntries([chrom], [start], ends=[end], values=[score])