-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbaseCommand.cpp
More file actions
157 lines (135 loc) · 5.9 KB
/
Copy pathbaseCommand.cpp
File metadata and controls
157 lines (135 loc) · 5.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright (C) 2018 Itoh Laboratory, Tokyo Institute of Technology
This file is part of Platanus_B.
Platanus_B 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 2 of the License, or
(at your option) any later version.
Platanus_B 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 Platanus_B; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "baseCommand.h"
#include <string>
#include <fstream>
//////////////////////////////////////////////////////////////////////////////////////
// check input file format either FASTA or FASTQ
//////////////////////////////////////////////////////////////////////////////////////
platanus::FILETYPE BaseCommand::checkFileFormat(const std::string &filename) const
{
FILE* fp = platanus::openFileAllowingCompression(filename, "r");
std::string line[4];
for (unsigned i = 0; i < 4; ++i)
platanus::getlineFILE(line[i], fp);
platanus::closeFileAllowingCompression(fp, filename);
if (line[0][0]=='>' && strspn(line[1].c_str(), "ACGTN")==line[1].size()) {
return platanus::FILETYPE::FASTA;
}
if (line[0][0]=='@'
&& strspn(line[1].c_str(), "ACGTN")==line[1].size()
&& line[2][0]=='+') {
return platanus::FILETYPE::FASTQ;
}
return platanus::FILETYPE::UNKNOWN;
}
//////////////////////////////////////////////////////////////////////////////////////
// parse option argument
//////////////////////////////////////////////////////////////////////////////////////
bool BaseCommand::parseArgs(int argc, char **argv)
{
int optInd = 2;
while (optInd < argc) {
if (strcmp(argv[optInd], "-h") == 0) {
return false;
}
// parse boolean option (has no other argument)
if (optionBool.find(argv[optInd]) != optionBool.end()) {
optionBool[argv[optInd]] = true;
++optInd;
// parse single argument option
} else if (optionMultiArgs.find(argv[optInd]) != optionMultiArgs.end()) {
int optIndNext = optInd + 1;
optionMultiArgs[argv[optInd]].clear();
while (optIndNext < argc && argv[optIndNext][0] != '-') {
std::string tmpArgs = argv[optIndNext];
optionMultiArgs[argv[optInd]].push_back(tmpArgs);
++optIndNext;
}
optInd = optIndNext;
// parse pair file (paired-end or mate-pair read file)
} else if (optionSingleArgs.find(argv[optInd]) != optionSingleArgs.end()) {
optionSingleArgs[argv[optInd]] = argv[optInd + 1];
optInd += 2;
// parse multiple argument option
} else {
auto pairFileIterator = pairedEndSingleFileType.begin();
for (; pairFileIterator != pairedEndSingleFileType.end(); ++pairFileIterator) {
if (strstr(argv[optInd], (*pairFileIterator).c_str()) == argv[optInd]) {
int pairNum = divideArgvInt(argv[optInd]);
if (pairNum == 0) {
return false;
}
int optIndNext = optInd + 1;
while (optIndNext < argc && argv[optIndNext][0] != '-') {
std::string tmpArgs = argv[optIndNext];
PairFile tmpPair(pairNum, *pairFileIterator, tmpArgs);
optionPairFile.push_back(tmpPair);
++optIndNext;
}
optInd = optIndNext;
break;
}
}
if (pairFileIterator == pairedEndSingleFileType.end()) {
pairFileIterator = pairedEndPairFileType.begin();
for (; pairFileIterator != pairedEndPairFileType.end(); ++pairFileIterator) {
if (strstr(argv[optInd], (*pairFileIterator).c_str()) == argv[optInd]) {
int pairNum = divideArgvInt(argv[optInd]);
if (pairNum == 0) {
return false;
}
int optIndNext = optInd + 1;
while (optIndNext < argc && argv[optIndNext][0] != '-') {
std::string tmpArgs1 = argv[optIndNext];
++optIndNext;
if (optIndNext > argc) return false;
if (argv[optIndNext][0] == '-') return false;
std::string tmpArgs2 = argv[optIndNext];
PairFile tmpPair(pairNum, *pairFileIterator, tmpArgs1, tmpArgs2);
optionPairFile.push_back(tmpPair);
++optIndNext;
}
optInd = optIndNext;
break;
}
}
if (pairFileIterator == pairedEndPairFileType.end()) {
int add = checkOtherOption(argv[optInd]);
if (add == 0) {
return false;
} else {
optInd += add;
}
}
}
}
}
return checkFileEnough();
}
//////////////////////////////////////////////////////////////////////////////////////
// recognise option number (ex: -IP1 <- get 1)
//////////////////////////////////////////////////////////////////////////////////////
int BaseCommand::divideArgvInt(char *args) const
{
char *moveArgs = args;
for (;*moveArgs != '\0'; ++moveArgs) {
if (isdigit(*moveArgs)) {
return atoi(moveArgs);
}
}
return 0;
}