-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildsa.cpp
88 lines (68 loc) · 1.5 KB
/
buildsa.cpp
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
#include<cassert>
#include<cmath>
#include<string>
#include<iostream>
#include<sstream>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<list>
#include<queue>
#include<cstdarg>
#include<algorithm>
//#include<mysql++.h>
//#include <boost/foreach.hpp>
using namespace std;
ostringstream oMsg;
string sbuf;
string filename, baseFilename;
string genome;
int genSize;
void open_file(ifstream & inFile, string filename) {
inFile.open(filename.c_str());
if (!inFile) {
cerr << "Cannot open input file: " << filename << endl;
assert(0);
exit(1);
}
}
string read_genome(string filename) {
string genome;
ifstream inFile;
open_file(inFile, filename);
char buf[257];
//read the file into genome
inFile.get(buf, 256);
char c;
while ((c = inFile.rdbuf()->sbumpc()) != EOF) {
if (c != '\n') {
genome.push_back(c);
}
}
inFile.close();
return genome;
}
bool complt(int x, int y) {
if (x==y) return false;
int i =0;
while ((i < genSize) && (genome[x+i] == genome[y+i])) i++;
return genome[x+i] < genome[y+i];
}
void usage(int argc, char * argv[]) {
cerr << "Usage: " << argv[0] << " <genome> " << endl;
exit(1);
}
int main(int argc, char * argv[]) {
if (argc != 2) usage(argc, argv);
vector<int> sa;
genome = read_genome(argv[1]);
genSize= genome.size();
genome += genome; //to support circularity
for (int i = 0; i < genSize; i++) {
sa.push_back(i);
}
sort(sa.begin(), sa.end(), complt);
for (int i = 0; i < sa.size() ; i++) cout << sa[i] << endl;
return 0;
}