-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONSequenceAnalyzer.cpp
More file actions
executable file
·181 lines (134 loc) · 4.66 KB
/
JSONSequenceAnalyzer.cpp
File metadata and controls
executable file
·181 lines (134 loc) · 4.66 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* JSONSequenceAnalyzer.cpp
* Written by: Mark Koh
* DARwIn-OP Dance Synthesis Project
* 06/04/2013
*
* Description: This class takes a file with either a JSON or proprietary sequence list and loads them
* into a SequenceTable.
*
*/
#include "JSONSequenceAnalyzer.h"
using namespace std;
//-------------------------------------------------
//-------------- Constructors ---------------------
//-------------------------------------------------
JSONSequenceAnalyzer::JSONSequenceAnalyzer() {
//Initialize anything that we need to initialize here
//The user will need to load a file now using the load function
}
JSONSequenceAnalyzer::JSONSequenceAnalyzer(string fileName) {
parse(fileName);
}
//-------------------------------------------------
//-------------- Destructor ---------------------
//-------------------------------------------------
JSONSequenceAnalyzer::~JSONSequenceAnalyzer() {
}
//-------------------------------------------------
//-------------- Inspectors - ---------------------
//-------------------------------------------------
/*int JSONSequenceAnalyzer::getNextPosition() const {
}*/
//-------------------------------------------------
//-------------- Mutators -------------------------
//-------------------------------------------------
//This file will load a new file into the Sequence Analyzer and parse it, directing the values into the markov map
//It will return true if the file was successfully loaded and the map was filled
bool JSONSequenceAnalyzer::parse(string filename) {
//Open the file and
_file.open(filename.c_str(), ifstream::in);
//Make sure we didn't run into any errors openeing the file
if (! _file.is_open())
{
return false;
}
_file.close();
//Decalare our json types
json_t *root, *bases, *sequences;
json_error_t error;
//Read the root from the file
root = json_load_file(filename.c_str(),0,&error);
//Do some error checking
if (!root) {
cout<<"Error loading JSON file: "<<error.text<<endl;
cout<<"Line: "<<error.line<<", Column: "<<error.column<<endl;
return false;
}
if (!json_is_object(root)){
cout<<"Error in JSON formatting."<<endl;
return false;
}
//Grab our bases and sequences
bases = json_object_get(root, "bases");
sequences = json_object_get(root, "sequences");
//Some more error checking (just make sure everything is as expected)
if(!json_is_array(bases) || !json_is_array(sequences)){
cout<<"Base positions unable to be read as an array."<<endl;
return false;
}
//Handle the base positions
int numBases = json_array_size(bases);
//cout<<"Base positions read: "<<endl;
for (int i=0; i<numBases;i++){
json_t *pos = json_array_get(bases,i);
if (json_is_integer(pos)){
int unpacked;
json_unpack(pos,"i",&unpacked);
//cout<<unpacked<<",";
_table.addBase(unpacked);
}
}
cout<<endl;
//Handle the base positions
int numSequences = json_array_size(sequences);
for (int i=0; i<numSequences;i++){
json_t *bundle = json_array_get(sequences,i);
if (json_is_object(bundle)){
const char *name;
json_t *json_name = json_object_get(bundle,"name");
json_t *sequence = json_object_get(bundle,"sequence");
if (json_is_string(json_name) && json_is_array(sequence)){
json_unpack(json_name,"s",&name);
int length = json_array_size(sequence);
//Analyze sequence with NULL for prefix
analyzeSequence(sequence);
//cout<<"Sequence read: "<<name<<", Sequence length of "<<length<<endl;
}
}
}
cout<<endl;
//The file was successfully loaded! Return true.
return true;
}
//Given a sequence (with potentialy nested arrays), this function will add moves to the
//sequence table
void JSONSequenceAnalyzer::analyzeSequence(json_t* sequence) {
//from i=0 to length-1
//Add the current element and the next element
if (json_is_array(sequence)){
//Call linkpair on the rest of the array
int size = json_array_size(sequence);
for (int i=0; i<size-1; i++) {
json_t *pre = json_array_get(sequence, i);
json_t *post = json_array_get(sequence, i+1);
linkPair(pre,post);
}
}
}
//This will check to see if the prefix and postfixes are various types
void JSONSequenceAnalyzer::linkPair(json_t* prefix, json_t* postfix){
if (json_is_integer(prefix) && json_is_integer(postfix)) {
//Link the prefix and postfix
int pre, post;
json_unpack(prefix,"i",&pre);
json_unpack(postfix,"i",&post);
_table.addMove(pre,post);
}
else {
cout<<"Error: Non-integer value found in sequence."<<endl;
}
}
void JSONSequenceAnalyzer::printTable(ostream &out){
_table.print(cout);
}