Got it! You want to:
- Read
positions-filtered.tsv which has two columns: Positions and PositionsPeaks.
- Read
180-avg-ste-pos-gaussian-ddaverage.tsv which has a column oldAverages.
- For each position in
Positions, check if the value in oldAverages at that position is at least 10 less than the value in oldAverages at the nearest downstream position in PositionsPeaks.
- If the condition is not met, filter out that position.
- Output the remaining positions to
positions-filtered2.tsv.
Assumptions:
- Positions are integers and correspond to row indices or keys in
oldAverages.
- "Nearest downstream position" means the smallest position in
PositionsPeaks that is greater than the current Positions value.
oldAverages is indexed by position (or can be accessed by position).
Here's a Python script to do this:
import pandas as pd
import numpy as np
# Load the files
df_positions = pd.read_csv('positions-filtered.tsv', sep='\t')
df_averages = pd.read_csv('180-avg-ste-pos-gaussian-ddaverage.tsv', sep='\t')
# Assuming df_averages has a column 'position' or index corresponds to position
# If not, you need to clarify how to map positions to oldAverages rows.
# For now, let's assume df_averages has a 'position' column:
# If not, you can create one if positions correspond to row indices:
# df_averages['position'] = df_averages.index
# Check if 'position' column exists, if not, create it as index
if 'position' not in df_averages.columns:
df_averages['position'] = df_averages.index
# Create a dictionary for quick lookup of oldAverages by position
old_avg_dict = dict(zip(df_averages['position'], df_averages['oldAverages']))
# Sort PositionsPeaks for efficient downstream search
positions_peaks_sorted = sorted(df_positions['PositionsPeaks'].unique())
def find_nearest_downstream(pos):
# Find the smallest position in positions_peaks_sorted greater than pos
for peak_pos in positions_peaks_sorted:
if peak_pos > pos:
return peak_pos
return None
filtered_positions = []
for idx, row in df_positions.iterrows():
pos = row['Positions']
downstream_pos = find_nearest_downstream(pos)
if downstream_pos is None:
# No downstream position, skip this position
continue
val_pos = old_avg_dict.get(pos, np.nan)
val_downstream = old_avg_dict.get(downstream_pos, np.nan)
# Check if values exist and condition is met
if not np.isnan(val_pos) and not np.isnan(val_downstream):
if val_downstream - val_pos >= 10:
filtered_positions.append(row)
# Create a DataFrame from filtered positions
df_filtered = pd.DataFrame(filtered_positions)
# Save to file
df_filtered.to_csv('positions-filtered2.tsv', sep='\t', index=False)
print(f"Filtered positions saved to positions-filtered2.tsv")
Explanation:
- We load both files.
- We assume
oldAverages can be accessed by position.
- For each position in
Positions, find the nearest downstream position in PositionsPeaks.
- Check if
oldAverages[downstream] - oldAverages[pos] >= 10.
- Keep only those positions that satisfy this.
- Save the filtered positions to a new TSV file.
If your data structure is different (e.g., no position column in averages file), please provide a sample of your files or clarify how to map positions to oldAverages. I can adjust the script accordingly.