<commit_before>/*
 * Copyright (c) 2015 The Jackson Laboratory
 *
 * This software was developed by <NAME>'s Lab at The Jackson
 * Laboratory (see http://research.jax.org/faculty/churchill).
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software. If not, see <http://www.gnu.org/licenses/>.
 */


//
//  populase.cpp
//
//
//  Created by <NAME> on 8/20/14.
//
//

#include <iostream>

#include <getopt.h>

#include "alignment_incidence_matrix.h"
#include "sample_allelic_expression.h"
#include "python_interface.h"
#include "kallisto_import.h"

void print_help();


int main(int argc, char **argv)
{
    AlignmentIncidenceMatrix *aim;

    bool binary_input = false;

    int num_iterations;
    int max_iterations = 200;
    int read_length = 100;

    SampleAllelicExpression::model model = SampleAllelicExpression::MODEL_2;
    int m;

    clock_t t1, t2;
    float diff;

    std::string input_filename;
    std::string output_filename;
    std::string transcript_length_file;
    std::string extension = ".pcl.bz2";
    std::string gene_file;

    //getopt related variables
    int c;
    int option_index = 0;
    int bad_args = 0;

    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"model", required_argument, 0, 'm'},
        {"output", required_argument, 0, 'o'},
        {"read-length", required_argument, 0, 'k'},
        {"transcript-lengths", required_argument, 0, 'l'},
        {"max-iterations", required_argument, 0, 'i'},
        {"bin", no_argument, 0, 'b'},
        {"gene-mappings", required_argument, 0, 'g'},
        {0, 0, 0, 0}
    };

    while ((c = getopt_long(argc, argv, "hm:o:k:l:i:bg:", long_options, &option_index)) != -1) {
        switch (c) {
            case 'h':
                print_help();
                return 0;

            case 'm':
                m = std::stoi(optarg);
                if (m < 1 || m > 4) {
                    std::cerr << "Invalid model number specified. Valid options: 1, 2, 3, 4\n";
                }

                switch (m) {
                    case 1:
                        model = SampleAllelicExpression::MODEL_1;
                        break;
                    case 2:
                        model = SampleAllelicExpression::MODEL_2;
                        break;
                    case 3:
                        std::cerr << "Model 3 is currently unimplemented, please specify a different model\n";
                        return 1;
                    case 4:
                        model = SampleAllelicExpression::MODEL_4;
                        break;
                }

                break;

            case 'o':
                output_filename = std::string(optarg);
                break;

            case 'k':
                read_length = std::stoi(optarg);
                break;

            case 'l':
                transcript_length_file = std::string(optarg);
                break;

            case 'i':
                max_iterations = std::stoi(optarg);
                break;

            case 'b':
                binary_input = true;
                break;

            case 'g':
                gene_file = std::string(optarg);
                break;

            case '?':
                bad_args++;

        }
    }

    if (bad_args) {
        print_help();
        return 1;
    }


    if (argc - optind == 1) {
        input_filename = argv[optind];
    }
    else {
        // TODO print error message
        print_help();
        return 1;
    }

    if (!binary_input && (extension.size() >= input_filename.size() || !std::equal(extension.rbegin(), extension.rend(), input_filename.rbegin()))) {
        std::cerr << "Error, expected file with .pcl.bz2 extension. Input file should be prepared with bam_to_pcl.py script.\n";
        return 1;
    }

    if (output_filename.empty()) {
        //use default, based on the input file name but placed in current working directdory
        output_filename = input_filename.substr(0, input_filename.size() - extension.size()).append(".stacksum.tsv");

        //check to see if there was a path in the input file name.  If so, trim it off
        std::size_t found = output_filename.rfind('/');
        if (found != std::string::npos) {
            output_filename = output_filename.substr(found+1);
        }

    }


    if (binary_input) {
        aim = loadFromBin(input_filename);

        if (!aim) {
            std::cerr << "Error loading binary input file\n";
            return 1;
        }
    }
    else {
        PythonInterface pi = PythonInterface();
        if (pi.init()){
            std::cerr << "Error importing TranscriptHits Python module.\n";
            std::cerr << '\t' << pi.getErrorString() << std::endl;
            return 1;
        }

        std::cout << "Loading " << input_filename << ". This may take a while..." << std::endl;
        aim = pi.load(input_filename);

        if (!aim) {
            std::cerr << "Error loading pcl file\n";
            std::cerr << '\t' << pi.getErrorString() << std::endl;
            return 1;
        }
    }


    std::cout << "Alignment Incidence file " << input_filename << std::endl;
    std::vector<std::string> hap_names = aim->get_haplotype_names();

    std::cout << "File had the following haplotype names:\n";
    for (std::vector<std::string>::iterator it = hap_names.begin(); it != hap_names.end(); ++it) {
        std::cout << *it << "\t";
    }
    std::cout << std::endl;<commit_msg>fix bug generating default output filename when file is not a .pcl.bz file<commit_after>/*
 * Copyright (c) 2015 The Jackson Laboratory
 *
 * This software was developed by <NAME>'s Lab at The Jackson
 * Laboratory (see http://research.jax.org/faculty/churchill).
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software. If not, see <http://www.gnu.org/licenses/>.
 */


//
//  populase.cpp
//
//
//  Created by <NAME> on 8/20/14.
//
//

#include <iostream>

#include <getopt.h>

#include "alignment_incidence_matrix.h"
#include "sample_allelic_expression.h"
#include "python_interface.h"
#include "kallisto_import.h"

void print_help();


int main(int argc, char **argv)
{
    AlignmentIncidenceMatrix *aim;

    bool binary_input = false;

    int num_iterations;
    int max_iterations = 200;
    int read_length = 100;

    SampleAllelicExpression::model model = SampleAllelicExpression::MODEL_2;
    int m;

    clock_t t1, t2;
    float diff;

    std::string input_filename;
    std::string output_filename;
    std::string transcript_length_file;
    std::string extension = ".pcl.bz2";
    std::string gene_file;

    //getopt related variables
    int c;
    int option_index = 0;
    int bad_args = 0;

    static struct option long_options[] = {
        {"help", no_argument, 0, 'h'},
        {"model", required_argument, 0, 'm'},
        {"output", required_argument, 0, 'o'},
        {"read-length", required_argument, 0, 'k'},
        {"transcript-lengths", required_argument, 0, 'l'},
        {"max-iterations", required_argument, 0, 'i'},
        {"bin", no_argument, 0, 'b'},
        {"gene-mappings", required_argument, 0, 'g'},
        {0, 0, 0, 0}
    };

    while ((c = getopt_long(argc, argv, "hm:o:k:l:i:bg:", long_options, &option_index)) != -1) {
        switch (c) {
            case 'h':
                print_help();
                return 0;

            case 'm':
                m = std::stoi(optarg);
                if (m < 1 || m > 4) {
                    std::cerr << "Invalid model number specified. Valid options: 1, 2, 3, 4\n";
                }

                switch (m) {
                    case 1:
                        model = SampleAllelicExpression::MODEL_1;
                        break;
                    case 2:
                        model = SampleAllelicExpression::MODEL_2;
                        break;
                    case 3:
                        std::cerr << "Model 3 is currently unimplemented, please specify a different model\n";
                        return 1;
                    case 4:
                        model = SampleAllelicExpression::MODEL_4;
                        break;
                }

                break;

            case 'o':
                output_filename = std::string(optarg);
                break;

            case 'k':
                read_length = std::stoi(optarg);
                break;

            case 'l':
                transcript_length_file = std::string(optarg);
                break;

            case 'i':
                max_iterations = std::stoi(optarg);
                break;

            case 'b':
                binary_input = true;
                break;

            case 'g':
                gene_file = std::string(optarg);
                break;

            case '?':
                bad_args++;

        }
    }

    if (bad_args) {
        print_help();
        return 1;
    }


    if (argc - optind == 1) {
        input_filename = argv[optind];
    } else {
        // TODO print error message
        print_help();
        return 1;
    }

    if (!binary_input && (extension.size() >= input_filename.size() || !std::equal(extension.rbegin(), extension.rend(), input_filename.rbegin()))) {
        std::cerr << "Error, expected file with .pcl.bz2 extension. Input file should be prepared with bam_to_pcl.py script.\n";
        return 1;
    }

    if (output_filename.empty()) {
        //use default, based on the input file name but placed in current working directdory

        if (!binary_input) {
            output_filename = input_filename.substr(0, input_filename.size() - extension.size()).append(".stacksum.tsv");
        } else {
           output_filename = input_filename;
           output_filename.append(".stacksum.tsv");
        }
        //check to see if there was a path in the input file name.  If so, trim it off
        std::size_t found = output_filename.rfind('/');
        if (found != std::string::npos) {
            output_filename = output_filename.substr(found+1);
        }

    }


    if (binary_input) {
        aim = loadFromBin(input_filename);

        if (!aim) {
            std::cerr << "Error loading binary input file\n";
            return 1;
        }
    } else {
        PythonInterface pi = PythonInterface();
        if (pi.init()){
            std::cerr << "Error importing TranscriptHits Python module.\n";
            std::cerr << '\t' << pi.getErrorString() << std::endl;
            return 1;
        }

        std::cout << "Loading " << input_filename << ". This may take a while..." << std::endl;
        aim = pi.load(input_filename);

        if (!aim) {
            std::cerr << "Error loading pcl file\n";
            std::cerr << '\t' << pi.getErrorString() << std::endl;
            return 1;
        }
    }


    std::cout << "Alignment Incidence file " << input_filename << std::endl;
    std::vector<std::string> hap_names = aim->get_haplotype_names();

    std::cout << "File had the following haplotype names:\n";
    for (std::vector<std::string>::iterator it = hap_names.begin(); it != hap_names.end(); ++it) {
        std::cout << *it << "\t";
    }
    std::cout << std::endl;
{'fraction_non_alphanumeric': 0.12400130441871841, 'fraction_numerical': 0.009946192727865644, 'mean_word_length': 1.8789016662755222, 'pattern_counts': {'":': 0, '<': 111, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '174598', 'n_tokens_mistral': 3717, 'n_tokens_neox': 3444, 'n_words': 1019}