supercat666 commited on
Commit
aea8c18
1 Parent(s): b31c7ac

fix bigwig

Browse files
Files changed (1) hide show
  1. cas9on.py +14 -15
cas9on.py CHANGED
@@ -204,22 +204,21 @@ def process_gene(gene_symbol, model_path):
204
 
205
 
206
  def create_bigwig(df, bigwig_path):
207
- if isinstance(df, list):
208
- df = pd.DataFrame(df, columns=["Chr", "Start Pos", "End Pos", "Strand", "Transcript", "Exon", "Target", "gRNA", "Prediction"])
 
209
 
210
- # Calculate chromosome sizes as the maximum end position per chromosome
211
- chrom_sizes = df.groupby('Chr')['End Pos'].max().astype(int).to_dict()
 
212
 
213
- with pyBigWig.open(bigwig_path, "w") as bw:
214
- # Add chromosome sizes to the header
215
- bw.addHeader([(chr, size) for chr, size in chrom_sizes.items()])
216
 
217
- # Iterate over the DataFrame and add entries for each chromosome
218
- for chrom in df['Chr'].unique():
219
- chrom_df = df[df['Chr'] == chrom]
220
- starts = chrom_df['Start Pos'].astype(int).tolist()
221
- ends = chrom_df['End Pos'].astype(int).tolist()
222
- values = chrom_df['Prediction'].astype(float).tolist()
223
 
224
- if starts and ends and values:
225
- bw.addEntries(chrom, starts, ends=ends, values=values)
 
204
 
205
 
206
  def create_bigwig(df, bigwig_path):
207
+ # Ensure the dataframe has the required columns
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
+ # Prepare the BigWig header using unique chromosomes and their max position
212
+ chr_sizes = df.groupby('Chr')['End Pos'].max().to_dict()
213
+ header = [(chr, size) for chr, size in chr_sizes.items()]
214
 
215
+ # Create a BigWig file
216
+ bw = pyBigWig.open(bigwig_path, "w")
217
+ bw.addHeader(header)
218
 
219
+ # Add entries to the BigWig file
220
+ for _, row in df.iterrows():
221
+ bw.addEntries([row['Chr']], [int(row['Start Pos'])], ends=[int(row['End Pos'])], values=[float(row['Prediction'])])
 
 
 
222
 
223
+ # Close the BigWig file
224
+ bw.close()