<gh_stars>1-10
"""factor_plot.py: This script generates a factor plot from the dataframe generated by generate_factor_plot_df.py. This script will write the plot to figure.pdf by default."""

__author__      = "<NAME>"

from sys import argv, exit
import generate_point_plot_df as gpp

if len(argv) != 3:
    print("\npython {0} counts.matrix genes.txt\n".format(argv[0]))
    exit(1)
else:
    import pandas as pd
    import seaborn as sns
    import matplotlib.pyplot as plt
    import numpy as np

def plot_data():
    """Plot data"""
    with open(argv[2]) as f:
        genes = [i.rstrip() for i in f.readlines()]
    df = pd.read_csv(argv[1])
    plotting_df = gpp.generate_df(df, genes)
    plotting_df["species"] = plotting_df["Sample"].apply(lambda x: "$\itAcomys$" if "aco" in x else "$\itMus$")
    plotting_df["Day"] = plotting_df["Sample"].apply(lambda x: x[3:])
    plotting_df["Gene Symbol"]=plotting_df["Gene Symbol"].apply(lambda x: "$\it" + x.title() + "$")
    plotting_df.sort_values(by=["Gene Symbol"], inplace=True)
    sns.set(style="whitegrid")
    plt.switch_backend('Agg')
    g = sns.factorplot(x="Day", 
                       y="log2(Expression+1)", 
                       hue="Gene Symbol", 
                       col="species", 
                       data=plotting_df, 
                       order=["0","7","14"],
                       col_order=["$\itAcomys$","$\itMus$"])
    g.despine(left=True)
    print("Saving plot")
    plt.savefig("figure.pdf", bbox_inches='tight')


if __name__ == "__main__":
    plot_data()
