annotateMain.cpp 5.15 KB
Newer Older
1
/*****************************************************************************
Aaron's avatar
Aaron committed
2
  annotateMain.cpp
3
4
5
6
7
8
9

  (c) 2009 - Aaron Quinlan
  Hall Laboratory
  Department of Biochemistry and Molecular Genetics
  University of Virginia
  aaronquinlan@gmail.com

Aaron's avatar
Aaron committed
10
  Licenced under the GNU General Public License 2.0 license.
11
******************************************************************************/
Aaron's avatar
Aaron committed
12
#include "annotateBed.h"
13
14
15
16
17
#include "version.h"

using namespace std;

// define the version
Aaron's avatar
Aaron committed
18
#define PROGRAM_NAME "annotateBed"
19
20
21
22
23
24
25
26
27

// define our parameter checking macro
#define PARAMETER_CHECK(param, paramLen, actualLen) (strncmp(argv[i], param, min(actualLen, paramLen))== 0) && (actualLen == paramLen)

// function declarations
void ShowHelp(void);

int main(int argc, char* argv[]) {

28
29
30
31
32
33
34
35
36
37
38
    // our configuration variables
    bool showHelp = false;

    // input file
    string mainFile;

    // parm flags
    bool forceStrand    = false;
    bool haveBed        = false;
    bool haveFiles      = false;
    bool haveTitles     = false;
Aaron's avatar
Aaron committed
39
    bool reportCounts   = false;
40
    bool reportBoth     = false;
Aaron's avatar
Aaron committed
41
42

    // list of annotation files / names
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
    vector<string> inputFiles;
    vector<string> inputTitles;

    // check to see if we should print out some help
    if(argc <= 1) showHelp = true;

    for(int i = 1; i < argc; i++) {
        int parameterLength = (int)strlen(argv[i]);

        if((PARAMETER_CHECK("-h", 2, parameterLength)) ||
        (PARAMETER_CHECK("--help", 5, parameterLength))) {
            showHelp = true;
        }
    }

    if(showHelp) ShowHelp();

    // do some parsing (all of these parameters require 2 strings)
    for(int i = 1; i < argc; i++) {

        int parameterLength = (int)strlen(argv[i]);

        if(PARAMETER_CHECK("-i", 2, parameterLength)) {
            if ((i+1) < argc) {
                haveBed  = true;
                mainFile = argv[i + 1];
                i++;
            }
        }
        else if(PARAMETER_CHECK("-files", 6, parameterLength)) {
            if ((i+1) < argc) {
                haveFiles = true;
Aaron's avatar
Aaron committed
75
76
                i = i+1;
                string file = argv[i];
77
                while (file[0] != '-' && i < argc) {
Aaron's avatar
Aaron committed
78
79
80
81
                    inputFiles.push_back(file);
                    i++;
                    if (i < argc)
                        file = argv[i];
82
                }
Aaron's avatar
Aaron committed
83
                i--;
84
85
86
87
88
            }
        }
        else if(PARAMETER_CHECK("-names", 6, parameterLength)) {
            if ((i+1) < argc) {
                haveTitles = true;
Aaron's avatar
Aaron committed
89
90
                i = i+1;
                string title = argv[i];
91
                while (title[0] != '-' && i < argc) {
Aaron's avatar
Aaron committed
92
93
94
95
                    inputTitles.push_back(title);
                    i++;
                    if (i < argc)
                        title = argv[i];
96
                }
Aaron's avatar
Aaron committed
97
                i--;
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
            }
        }
        else if(PARAMETER_CHECK("-counts", 7, parameterLength)) {
            reportCounts = true;
        }
        else if(PARAMETER_CHECK("-both", 5, parameterLength)) {
            reportBoth = true;
        }
        else if (PARAMETER_CHECK("-s", 2, parameterLength)) {
            forceStrand = true;
        }
        else {
            cerr << endl << "*****ERROR: Unrecognized parameter: " << argv[i] << " *****" << endl << endl;
            showHelp = true;
        }
    }

    // make sure we have both input files
    if (!haveBed || !haveFiles) {
        cerr << endl << "*****" << endl << "*****ERROR: Need -i and -files files. " << endl << "*****" << endl;
        showHelp = true;
    }

    if (!showHelp) {
        BedAnnotate *ba = new BedAnnotate(mainFile, inputFiles, inputTitles, forceStrand, reportCounts, reportBoth);
Aaron's avatar
Aaron committed
123
        ba->AnnotateBed();
124
125
126
127
128
129
        delete ba;
        return 0;
    }
    else {
        ShowHelp();
    }
130
131
132
}

void ShowHelp(void) {
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

    cerr << endl << "Program: " << PROGRAM_NAME << " (v" << VERSION << ")" << endl;

    cerr << "Author:  Aaron Quinlan (aaronquinlan@gmail.com)" << endl;

    cerr << "Summary: Annotates the depth & breadth of coverage of features from multiple files" << endl;
    cerr << "\t on the intervals in -i." << endl << endl;

    cerr << "Usage:   " << PROGRAM_NAME << " [OPTIONS] -i <bed/gff/vcf> -files FILE1 FILE2 .. FILEn" << endl << endl;

    cerr << "Options: " << endl;

    cerr << "\t-names\t"        << "A list of names (one / file) to describe each file in -i." << endl;
    cerr                        << "\t\tThese names will be printed as a header line." << endl << endl;

    cerr << "\t-counts\t"       << "Report the count of features in each file that overlap -i." << endl;
    cerr                        << "\t\t- Default is to report the fraction of -i covered by each file." << endl << endl;

    cerr << "\t-both\t"         << "Report the counts followed by the % coverage." << endl;
    cerr                        << "\t\t- Default is to report the fraction of -i covered by each file." << endl << endl;

    cerr << "\t-s\t"            << "Force strandedness.  That is, only include hits in A that" << endl;
    cerr                        << "\t\toverlap B on the same strand." << endl;
    cerr                        << "\t\t- By default, hits are included without respect to strand." << endl << endl;

    exit(1);
159
}