supercat666 commited on
Commit
45a0b21
1 Parent(s): fe2a0c6
Files changed (1) hide show
  1. cas9on.py +12 -3
cas9on.py CHANGED
@@ -208,9 +208,14 @@ def create_bigwig(df, bigwig_path):
208
  if not all(column in df.columns for column in ["Chr", "Start Pos", "End Pos", "Prediction"]):
209
  raise ValueError("DataFrame must contain 'Chr', 'Start Pos', 'End Pos', and 'Prediction' columns.")
210
 
211
- # Convert positions to integers and prepare the BigWig header
212
  df['Start Pos'] = df['Start Pos'].astype(int)
213
  df['End Pos'] = df['End Pos'].astype(int)
 
 
 
 
 
214
  chr_sizes = df.groupby('Chr')['End Pos'].max().to_dict()
215
  header = [(chr, int(size)) for chr, size in chr_sizes.items()]
216
 
@@ -219,8 +224,12 @@ def create_bigwig(df, bigwig_path):
219
  bw.addHeader(header)
220
 
221
  # Add entries to the BigWig file
222
- for _, row in df.iterrows():
223
- bw.addEntries([row['Chr']], [row['Start Pos']], ends=[row['End Pos']], values=[float(row['Prediction'])])
 
 
 
224
 
225
  # Close the BigWig file
226
  bw.close()
 
 
208
  if not all(column in df.columns for column in ["Chr", "Start Pos", "End Pos", "Prediction"]):
209
  raise ValueError("DataFrame must contain 'Chr', 'Start Pos', 'End Pos', and 'Prediction' columns.")
210
 
211
+ # Convert positions to integers
212
  df['Start Pos'] = df['Start Pos'].astype(int)
213
  df['End Pos'] = df['End Pos'].astype(int)
214
+
215
+ # Sort the DataFrame by chromosome and start position
216
+ df = df.sort_values(by=['Chr', 'Start Pos'])
217
+
218
+ # Prepare the BigWig header
219
  chr_sizes = df.groupby('Chr')['End Pos'].max().to_dict()
220
  header = [(chr, int(size)) for chr, size in chr_sizes.items()]
221
 
 
224
  bw.addHeader(header)
225
 
226
  # Add entries to the BigWig file
227
+ for chr, group in df.groupby('Chr'):
228
+ starts = group['Start Pos'].tolist()
229
+ ends = group['End Pos'].tolist()
230
+ values = group['Prediction'].astype(float).tolist()
231
+ bw.addEntries(chr, starts, ends=ends, values=values)
232
 
233
  # Close the BigWig file
234
  bw.close()
235
+