Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce molten output to unique pairs #47

Open
amilesj opened this issue Sep 10, 2021 · 2 comments
Open

Reduce molten output to unique pairs #47

amilesj opened this issue Sep 10, 2021 · 2 comments

Comments

@amilesj
Copy link

amilesj commented Sep 10, 2021

Making a new request for the enhancement suggested in a previous comment (#39 (comment)) to make molten output only unique pairs of isolates.

@idolawoye
Copy link

Hi @amilesj I have the same issue. Were you able to find a way around getting only unique pair combinations in the molten output?

@slbai01
Copy link

slbai01 commented Jul 20, 2023

I write a python script, maybe you can try.

import argparse
from os import sep
import pandas as pd

def process_molten_file(molten_file, output_file):
    # Read the molten file into a DataFrame
    df = pd.read_csv(molten_file, sep = "\t", header=None)
    df.columns = ["Sample", "Pair", "Value"]
    # Ensure that the Pair column contains unique pairs
    df['Pair2'] = df.apply(lambda row: tuple(sorted([row['Sample'], row['Pair']])), axis=1)

    # Sort the DataFrame based on 'Value' column in descending order
    df = df.sort_values(by='Value', ascending=False)

    # Drop duplicates based on 'Pair' column, keeping the row with the maximum 'Value'
    unique_pairs_df = df.drop_duplicates(subset='Pair2', keep='first')

    # If you want to reset the index of the resulting DataFrame:
    unique_pairs_df = unique_pairs_df.reset_index(drop=True)

    # Save the output DataFrame to a TSV file
    unique_pairs_df.to_csv(output_file, sep='\t', index=False)
    print(f"Contents of the reduced molten file are saved to: {output_file}")


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Reduce molten output to unique pairs and keep max value.")
    parser.add_argument("molten_file", help="Path to the molten output file to be processed")
    parser.add_argument("output_file", help="Path to save the output in TSV format")

    args = parser.parse_args()
    molten_file_path = args.molten_file
    output_file_path = args.output_file

    process_molten_file(molten_file_path, output_file_path)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants