C++ 生物信息文件处理模板

#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;

int main(int argc,char *argv[])
{
    /*ifstream testf;
    testf.open("D:\\workspace\\conbination_yongbing\\2011_7_20\\10ksample");
    if(!testf.good())
    {
        cout << "fuck" << endl;
        exit(0);
    }*/
//对命令行下输入参数进行处理
    ifstream COMBINATION;
    ifstream MATRIX;
    ofstream fout;
    if(argc != 4)//三个参数,这个值是4,还有一个是程序名称
    {
        cout << "ERROR: illegal argument number: " << argc << endl;
        cout <<
        "Input format:\n" <<
        "program_name combination_file_path matrix_file_path output_file_path \n" <<
        "i.e.\n" <<
        "test 10ksample cluster.matrix out.txt" << endl;
        exit(0);
    }

    COMBINATION.open(argv[1]);//参数从1开始,0是名称
    MATRIX.open(argv[2]);
    fout.open(argv[3]);
    if(!COMBINATION.good())
    {
        cout << "ERROR: illegal combination file path: " << argv[1] <<endl;
        cout <<
        "Input format:\n" <<
        "program_name combination_file_path matrix_file_path output_file_path \n" <<
        "i.e.\n" <<
        "test 10ksample cluster.matrix out.txt" << endl;
        exit(0);
    }else if(!MATRIX.good())
    {
        cout << "ERROR: illegal matrix file path" << endl;
        cout <<
        "Input format:\n" <<
        "program_name combination_file_path matrix_file_path output_file_path \n" <<
        "i.e.\n" <<
        "test 10ksample cluster.matrix out.txt" << endl;
        exit(0);
    }else if(!fout.good())
    {
        cout << "ERROR: illegal output file path" << endl;
        cout <<
        "Input format:\n" <<
        "program_name combination_file_path matrix_file_path output_file_path \n" <<
        "i.e.\n" <<
        "test 10ksample cluster.matrix out.txt" << endl;
        exit(0);
    }

    cout << "program running..." << endl;

    /*ofstream fout("out.txt");
    ifstream COMBINATION("10ksample");
    ifstream MATRIX("cluster.matrix");
    */


    bool m[19999][31];

    int matrix_line = 0;
    while(MATRIX != NULL)
    {
        string matr;
        getline(MATRIX,matr,'\n');
        //cout << matr.length() << endl;
        for(int j = 0;j < 30;j++)
        {
            if(matr[j] == '0')
            {
                m[matrix_line][j] = false;
            }else if(matr[j] == '1')
            {
                m[matrix_line][j] = true;
            }
        }
        matrix_line++;
    }

    string comb;
    char* num[31];
    while(COMBINATION != NULL)
    {
        int e_num = 0;
        int p = 0;
        int c = 0;
        getline(COMBINATION,comb,'\n');//end reading by '\n'

        char* char_comb=const_cast<char*>(comb.c_str());//chonst char* to char*
        char* temp_num = strtok(char_comb,"\t");//分割字符串的函数

        while(temp_num != NULL)
        {
            num[e_num] = temp_num;
            temp_num = strtok(NULL,"\t");
            e_num++;
        }

        if(e_num == 0)
        {
            break;
        }

        for(int i = 0; i < matrix_line; i++)
        {
            int sum = 0;
            for(int j = 0; j < e_num; j++)
            {
                if(m[i][atoi(num[j])])
                {
                    sum++;
                }
            }
            if(sum > 0)
            {
                p++;
            }
            if(sum == e_num)
            {
                c++;
            }
        }
        fout << p << "\t" << c << endl;
    }
            COMBINATION.close();
            MATRIX.close();
            fout.close();
    return 0;
}
 

猜你喜欢

转载自bbsunchen.iteye.com/blog/1129137