view KinaMine-Galaxy-7-7/src/kinamine/Reporter.java @ 0:67635b462045 draft

Uploaded
author jfb
date Tue, 20 Feb 2018 14:31:15 -0500
parents
children
line wrap: on
line source

/**
 *****************************************************************************
 * <p>
 * Copyright (c) Regents of the University of Minnesota. All Rights Reserved.
 * <p>
 * Author: Kevin Murray University of Minnesota - (murra668@umn.edu)
 * <p>
 *****************************************************************************
 */
package kinamine;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map.Entry;
import java.util.Set;
import javafx.util.Pair;

/**
 * Reporter class to write reports. Currently writes out in .csv format.
 *
 * @version 1.0
 * @author murra668
 * new author: blank121@umn.edu as of August 2017
 */
public class Reporter {

    /**
     * Write reports for run.
     *
     * @param run
     * @param outPath
     * @param outGroup
     */
    static void writeReports(Run run, String outPath, String outGroup) {

        /** Write substrates report. */
        String outputFileSub = outPath + outGroup + "_Substrates.csv";
        Reporter.substrates(run, outputFileSub);

        /** Write substrate background frequency report. */
        String outputFileFreq = outPath + outGroup + "_SubBackFreq.csv";
        Reporter.frequencies(run, outputFileFreq);

    }

    /**
     * Writes substrates report from the ids and motifs of each peptide.
     *
     * @param run
     * @param outputFileName
     */
    private static void substrates(Run run, String outputFileName) {

        /** Format header */
//        String header = "Substrates," + "Species," + "Reference," + "-7," + "-6,"
//                + "-5," + "-4," + "-3," + "-2," + "-1," + "0," + "1,"
//                + "2," + "3," + "4," + "5," + "6," + "7," + "Phosphite"
//                + "\n";
        String header = "Substrates," + "Species," + "Reference," + "-7," + "-6,"
                + "-5," + "-4," + "-3," + "-2," + "-1," + "0," + "1,"
                + "2," + "3," + "4," + "5," + "6," + "7," 
                +" ," +" ,"+ " ,"+ "Phosphite"
                + "\n";

        /** Initialize details */
        String detail = null;

        try (FileWriter writer = new FileWriter(outputFileName)) {

            /* Write the column headers */
            writer.append(header);

           Collection<Motif> motifs = run.motifs.values();

            /** Loop through each motif */
            for (Motif motif : motifs) {

                /** Format ID and blanks */
                detail = "," + "," + motif.ref + ",";

                String seq = motif.seq;
                int index = motif.index;
//changing the numbers for index only changed where in the excel doc these motifs
//showed up
//I should fuck with this and try to reallign it
//I should do that now
                if (index < 8) {
                    for (int i = index; i < 8; i++) {
                        detail += ",";
                    }
                    for (int j = 0; j < seq.length(); j++) {
                        detail += seq.charAt(j) + ",";
                    }
                    if (seq.length() - index < 7) {
                        for (int i = seq.length() - index; i < 7; i++) {
                            detail += ",";
                        }
                    }
                } else if (seq.length() < 15) {
                    for (int j = 0; j < seq.length(); j++) {
                        detail += seq.charAt(j) + ",";
                    }
                    for (int i = seq.length(); i < 15; i++) {
                        detail += ",";
                    }

                } else {
                    for (int j = 0; j < seq.length(); j++) {
                        detail += seq.charAt(j) + ",";
                    }
                }

//
//                if (index < 5) {
//                    for (int i = index; i < 5; i++) {
//                        detail += ",";
//                    }
//                    for (int j = 0; j < seq.length(); j++) {
//                        detail += seq.charAt(j) + ",";
//                    }
//                    if (seq.length() - index < 4) {
//                        for (int i = seq.length() - index; i < 4; i++) {
//                            detail += ",";
//                        }
//                    }
//                } else if (seq.length() < 9) {
//                    for (int j = 0; j < seq.length(); j++) {
//                        detail += seq.charAt(j) + ",";
//                    }
//                    for (int i = seq.length(); i < 9; i++) {
//                        detail += ",";
//                    }
//
//                } else {
//                    for (int j = 0; j < seq.length(); j++) {
//                        detail += seq.charAt(j) + ",";
//                    }
//                }
////////////////////////////////////////////////////////////////////////////////
                /** Format trailing blanks */
                detail += "," + "," + "," + seq + ",";
                
                for (String id : motif.regenSeqs){
                    detail += id + ",";
                }
                detail += "\n";

                /** Append each line of the report. */
                writer.append(detail);
            }

            writer.flush();
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * Write substrates background frequency report from each protein in the
     * database.
     *
     * @param run
     * @param outputFileName
     */
    private static void frequencies(Run run, String outputFileName) {

        /** Initialize the header */
        StringBuffer header = new StringBuffer();
        header.append("Amino Acids,");

        /** Write each protein accession. */
        Object[] prots = run.database.keySet().toArray();
        for (Object ref : prots) {
            header.append(ref).append(",");
        }
        header.append("\n");

        try (FileWriter writer = new FileWriter(outputFileName)) {

            /* Write the column headers. */
            writer.append(header);

            Collection<Protein> proteins = run.database.values();

            /** Write frequency of each amino acid. */
            for (char acid : AminoAcid.ACIDS) {
                StringBuffer detail = new StringBuffer();
                detail.append(acid).append(",");
                for (Protein protein : proteins) {
                    detail.append(protein.comp.get(acid)).append(",");
                }
                detail.append("\n");
                writer.append(detail);
            }

            writer.append("Properties\n");

            /** Write the property frequency of each amino acid. */
            for (String prop : AminoAcid.PROPS) {
                StringBuffer props = new StringBuffer();
                props.append(prop).append(",");
                for (Protein protein : proteins) {
                    props.append(protein.props.get(prop)).append(",");
                }
                props.append("\n");
                writer.append(props);
            }

            writer.append("\n");

            StringBuffer tyr = new StringBuffer("Number of Y,");
            StringBuffer phosphTyr = new StringBuffer("Number of pY,");
            StringBuffer aa = new StringBuffer("Total AAs,");

            /** Write the number of tyrosine, phospho-tyrosine, and length. */
            for (Protein protein : proteins) {
                tyr.append(protein.numTyr).append(",");
                phosphTyr.append(protein.phosphoTyr).append(",");
                aa.append(protein.seq.length()).append(",");
            }

            writer.append(tyr + "\n");
            writer.append(phosphTyr + "\n");
            writer.append(aa + "\n");

            writer.flush();
            writer.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}