supercat666 commited on
Commit
475fe8c
1 Parent(s): f067f02
Files changed (1) hide show
  1. cas9on.py +18 -18
cas9on.py CHANGED
@@ -203,28 +203,28 @@ def process_gene(gene_symbol, model_path):
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])
 
203
  # df.to_csv(output_path, index=False)
204
 
205
 
206
+ def create_bigwig(df, bigwig_path):
207
+ import pandas as pd
208
+ import pyBigWig
209
+
210
+ if isinstance(df, list):
211
+ df = pd.DataFrame(df, columns=["Chr", "Start Pos", "End Pos", "Strand", "Transcript", "Exon", "Target", "gRNA", "Prediction"])
 
212
 
213
  # Calculate chromosome sizes as the maximum end position per chromosome
214
+ # Ensure the sizes are integers
215
+ chrom_sizes = df.groupby('Chr')['End Pos'].max().astype(int).to_dict()
216
 
217
  # Create a BigWig file
218
  with pyBigWig.open(bigwig_path, "w") as bw:
219
+ # Add chromosome sizes to the header, ensuring sizes are integers
220
+ bw.addHeader([(chr, size) for chr, size in chrom_sizes.items()])
221
 
222
  # Add entries for each prediction
223
+ for chrom in df['Chr'].unique():
224
+ chrom_df = df[df['Chr'] == chrom]
225
+ bw.addEntries(
226
+ chrom,
227
+ chrom_df['Start Pos'].astype(int).tolist(),
228
+ ends=chrom_df['End Pos'].astype(int).tolist(),
229
+ values=chrom_df['Prediction'].astype(float).tolist()
230
+ )