supercat666 commited on
Commit
114492c
1 Parent(s): ba0ca5b
Files changed (1) hide show
  1. cas9on.py +20 -14
cas9on.py CHANGED
@@ -213,25 +213,31 @@ def create_bigwig(df, bigwig_path):
213
  df['End Pos'] = df['End Pos'].astype(int)
214
  df['Prediction'] = df['Prediction'].astype(float)
215
 
216
- # Sort the DataFrame by chromosome and start position to ensure order
217
- df = df.sort_values(by=['Chr', 'Start Pos'])
218
 
219
  # Calculate chromosome sizes for the BigWig header
220
- chr_sizes = df.groupby('Chr')['End Pos'].max().to_dict()
221
- header = [(chr, size) for chr, size in chr_sizes.items()]
 
 
 
222
 
223
  # Create the BigWig file and add the header
224
  bw = pyBigWig.open(bigwig_path, "w")
225
- bw.addHeader(header)
226
-
227
- # Create separate lists for chromosomes, starts, ends, and values
228
- chromosomes = df['Chr'].tolist()
229
- starts = df['Start Pos'].tolist()
230
- ends = df['End Pos'].tolist()
231
- values = df['Prediction'].astype(float).tolist()
232
-
233
- # Add entries to the BigWig file
234
- bw.addEntries(chromosomes, starts, ends=ends, values=values)
 
 
 
235
 
236
  # Close the BigWig file
237
  bw.close()
 
213
  df['End Pos'] = df['End Pos'].astype(int)
214
  df['Prediction'] = df['Prediction'].astype(float)
215
 
216
+ # Get the list of all chromosomes present in the DataFrame
217
+ all_chromosomes = df['Chr'].unique().tolist()
218
 
219
  # Calculate chromosome sizes for the BigWig header
220
+ chr_sizes = []
221
+ for chr in all_chromosomes:
222
+ chr_group = df[df['Chr'] == chr]
223
+ max_end_pos = chr_group['End Pos'].max()
224
+ chr_sizes.append((chr, max_end_pos))
225
 
226
  # Create the BigWig file and add the header
227
  bw = pyBigWig.open(bigwig_path, "w")
228
+ bw.addHeader(chr_sizes)
229
+
230
+ # Add entries for each chromosome
231
+ for chr in all_chromosomes:
232
+ chr_group = df[df['Chr'] == chr]
233
+ if not chr_group.empty:
234
+ starts = chr_group['Start Pos'].tolist()
235
+ ends = chr_group['End Pos'].tolist()
236
+ values = chr_group['Prediction'].astype(float).tolist()
237
+ bw.addEntries([chr] * len(starts), starts, ends=ends, values=values)
238
+ else:
239
+ # Add empty entries for the missing chromosome
240
+ bw.addEntries([chr], [0], ends=[1], values=[0.0])
241
 
242
  # Close the BigWig file
243
  bw.close()