[26] | 1 | #include "GSEXmlParser.h" |
---|
| 2 | |
---|
| 3 | void Data::Dump() |
---|
| 4 | { |
---|
| 5 | LOG("Sample iid: " << iid); |
---|
| 6 | LOG("Source: " << source); |
---|
| 7 | LOG("Supplementary-Data Type: " << supp_type); |
---|
| 8 | LOG("Supplementary-Data: " << supp_data); |
---|
| 9 | LOG("\n-------- Description --------"); |
---|
| 10 | for(int i = 0; i < desc.GetCount(); i++) |
---|
| 11 | LOG(" " << desc.GetKey(i) << " ..... " << desc[i]); |
---|
| 12 | LOG("-------- Description --------\n"); |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | String Data::ToXML() |
---|
| 16 | { |
---|
| 17 | String descs; //descriptions |
---|
| 18 | for(int i = 0; i < desc.GetCount(); i++) |
---|
| 19 | descs << XmlTag("Description")("name", desc.GetKey(i)).Text(desc[i]); |
---|
| 20 | |
---|
| 21 | return XmlTag("Sample")("iid", iid)( |
---|
| 22 | XmlTag("Source").Text(source) + |
---|
| 23 | XmlTag("Supplementary-Data")("type", supp_type).Text(supp_data) + |
---|
| 24 | XmlTag("Descriptions") (descs) |
---|
| 25 | ); |
---|
| 26 | } |
---|
| 27 | |
---|
[29] | 28 | String Data::ToTSV() |
---|
| 29 | { |
---|
| 30 | String tsv, tab, nl; |
---|
| 31 | tab = "\t"; |
---|
| 32 | nl = "\n"; |
---|
| 33 | tsv = source + tab + "Sample-iid" + tab + iid + nl + |
---|
| 34 | source + tab + "Supplementary-Data:" + supp_type + tab + supp_data + nl; |
---|
| 35 | for(int i = 0; i < desc.GetCount(); i++) |
---|
| 36 | tsv += source + tab + "Description:" + desc.GetKey(i) + tab + desc[i] + nl; |
---|
| 37 | |
---|
| 38 | return tsv; |
---|
| 39 | } |
---|
| 40 | |
---|
[26] | 41 | void GSEParser::Dump() |
---|
| 42 | { |
---|
| 43 | LOG("\n======== GSE XML ========\n"); |
---|
| 44 | for (int i=0; i < data.GetCount(); i++) { |
---|
| 45 | data[i].Dump(); |
---|
| 46 | } |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void GSEParser::LoadXML(String filename) { |
---|
| 50 | //Load XML File |
---|
| 51 | //LOG("\n---- GSE 2109 XML ----"); |
---|
[30] | 52 | |
---|
[26] | 53 | String gse; |
---|
[30] | 54 | /* |
---|
| 55 | Cout() << filename << "\n"; |
---|
| 56 | String fn; |
---|
| 57 | fn = GetDataFile(filename); |
---|
| 58 | Cout() << fn << "\n"; |
---|
| 59 | */ |
---|
| 60 | gse = LoadFile(filename); |
---|
| 61 | //Cout() << gse << "\n"; |
---|
[26] | 62 | XmlNode rnode = ParseXML(gse); |
---|
| 63 | |
---|
| 64 | //Clear Data Array |
---|
| 65 | data.Clear(); |
---|
| 66 | |
---|
| 67 | //Get Root Node |
---|
| 68 | const XmlNode &xnode = rnode["MINiML"]; |
---|
| 69 | |
---|
| 70 | //Save Data Array, one by one |
---|
| 71 | Vector<String> lines, des; |
---|
| 72 | String str, lstr, rstr; |
---|
| 73 | for (int i=0; i<xnode.GetCount(); i++) { |
---|
| 74 | if (xnode[i].GetTag()=="Sample") { |
---|
| 75 | //LOG("Sample(iid):" << xnode[i].Attr("iid")); |
---|
| 76 | //LOG("Source:" << xnode[i]["Channel"]["Source"].GatherText()); |
---|
| 77 | //LOG("\n-------- Description --------"); |
---|
| 78 | Data d; |
---|
| 79 | d.iid = xnode[i].Attr("iid"); |
---|
| 80 | d.source = xnode[i]["Channel"]["Source"].GatherText(); |
---|
| 81 | d.supp_type = xnode[i]["Supplementary-Data"].Attr("type"); |
---|
| 82 | d.supp_data = xnode[i]["Supplementary-Data"].GatherText(); |
---|
| 83 | lines = Split(xnode[i]["Description"].GatherText(),'\n'); |
---|
| 84 | for (int j=0; j<lines.GetCount(); j++) { |
---|
| 85 | if (lines[j].Find("=") > -1) { |
---|
| 86 | des = Split(lines[j], '='); |
---|
| 87 | } else if (lines[j].Find(":") > -1) { |
---|
| 88 | des = Split(lines[j], ':'); |
---|
| 89 | } |
---|
| 90 | if (des.GetCount()>1) { |
---|
| 91 | lstr = TrimSpace(des[0]); |
---|
| 92 | rstr = TrimSpace(des[1]); |
---|
| 93 | //LOG(lstr << ":" << rstr); |
---|
| 94 | String& v = d.desc.GetAdd(lstr); |
---|
| 95 | v = rstr; |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | data.Add(d); |
---|
| 99 | //LOG("-------- Description --------\n"); |
---|
| 100 | } |
---|
| 101 | } |
---|
| 102 | // |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | String GSEParser::ToXML() |
---|
| 106 | { |
---|
| 107 | String datas; // Datas |
---|
| 108 | for (int i = 0; i < data.GetCount(); i++) { |
---|
| 109 | datas << data[i].ToXML(); |
---|
| 110 | } |
---|
| 111 | return XmlHeader() + XmlTag("GSEFAMILY")(datas); |
---|
| 112 | } |
---|
| 113 | |
---|
[29] | 114 | String GSEParser::ToTSV() |
---|
| 115 | { |
---|
| 116 | String tsv, tab; |
---|
| 117 | tab = "\t"; |
---|
| 118 | tsv = "# rowkey" + tab + "columnkey" + tab + "value\n"; |
---|
[30] | 119 | |
---|
[29] | 120 | for (int i = 0; i < data.GetCount(); i++) { |
---|
| 121 | tsv << data[i].ToTSV(); |
---|
| 122 | } |
---|
| 123 | return tsv; |
---|
| 124 | } |
---|
| 125 | |
---|
[37] | 126 | void GSEParser::ToMySQL(String username, String password, String db, String db_ip) |
---|
| 127 | { |
---|
| 128 | // |
---|
| 129 | MySqlSession session; |
---|
[39] | 130 | if(!session.Connect(username, password, db, db_ip)) { |
---|
[37] | 131 | printf("Can't connect with MySql\n"); |
---|
| 132 | return; |
---|
| 133 | } |
---|
| 134 | Sql sql(session); |
---|
| 135 | sql.Execute("use gse"); |
---|
| 136 | String cmd; |
---|
| 137 | // |
---|
| 138 | Cout() << "Total Record Count:" << data.GetCount() << "\n"; |
---|
| 139 | int sid; |
---|
| 140 | int did; |
---|
| 141 | String name; |
---|
| 142 | for (int i = 0; i < data.GetCount(); i++) { |
---|
| 143 | // check gsefamily record |
---|
| 144 | cmd = "select * from gsefamily where iid = \"" + data[i].iid + "\""; |
---|
| 145 | sql.Execute(cmd); |
---|
| 146 | if (sql.GetRowsProcessed()>0) { |
---|
| 147 | Cout() << "Warnning: IID ["+data[i].iid+"] is exist." << "\n"; |
---|
| 148 | } else { |
---|
| 149 | // add a new gsefamily record |
---|
| 150 | cmd = "insert into gsefamily (iid,source) values (\""+data[i].iid+"\",\""+data[i].source+"\");"; |
---|
| 151 | if (!sql.Execute(cmd)) { |
---|
| 152 | Cout() << "Execution Error: " << sql.GetErrorCodeString() << " Cmd: " << sql.GetErrorStatement() << "\n"; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | //Cout() << "IID ["+data[i].iid+"]" << "\n"; |
---|
| 156 | |
---|
| 157 | sid = 0; |
---|
| 158 | // check supplementary |
---|
| 159 | cmd = "select sid from supplementary where type = \"" + data[i].supp_type + "\";"; |
---|
| 160 | sql.Execute(cmd); |
---|
| 161 | if (sql.GetRowsProcessed()==0) { |
---|
| 162 | //new a supplementary record |
---|
| 163 | cmd = "insert into supplementary (type) values (\"" + data[i].supp_type + "\");"; |
---|
| 164 | if (!sql.Execute(cmd)) { |
---|
| 165 | Cout() << "Execution Error: " << sql.GetErrorCode() << " Cmd: " << sql.GetErrorStatement() << "\n"; |
---|
| 166 | } |
---|
| 167 | sid = sql.GetInsertedId(); |
---|
| 168 | } else { |
---|
| 169 | if (sql.Fetch()) |
---|
| 170 | sid = sql[0]; |
---|
| 171 | //Cout() << "sid: " << sid << "\n"; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | //new a supplementary-map record |
---|
| 175 | cmd = "insert into `supplementary-map` (iid,sid,value) values (\""+data[i].iid+"\",\""+AsString(sid)+"\",\""+data[i].supp_data+"\")"; |
---|
| 176 | if (!sql.Execute(cmd)) { |
---|
| 177 | Cout() << "\nExecution Error: " << sql.GetErrorCode() << " Cmd: " << sql.GetErrorStatement() << "\n"; |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | //Cout() << "data[" << i << "].desc.GetCount() = " << data[i].desc.GetCount() << "\n"; |
---|
| 181 | VectorMap<String, String> desc = data[i].desc; |
---|
| 182 | for (int j=0; j<desc.GetCount(); j++) { |
---|
| 183 | |
---|
| 184 | // check description |
---|
| 185 | name = desc.GetKey(j); |
---|
| 186 | cmd = "select did from description where name = \"" + name + "\""; |
---|
| 187 | sql.Execute(cmd); |
---|
| 188 | |
---|
| 189 | did = 0; |
---|
| 190 | if (sql.GetRowsProcessed()==0) { |
---|
| 191 | //new a description record |
---|
| 192 | cmd = "insert into description (name) values (\"" + name + "\");"; |
---|
| 193 | if (!sql.Execute(cmd)) { |
---|
| 194 | Cout() << "Execution Error: " << sql.GetErrorCodeString() << " Cmd: " << sql.GetErrorStatement() << "\n"; |
---|
| 195 | } |
---|
| 196 | did = sql.GetInsertedId(); |
---|
| 197 | } else { |
---|
| 198 | if (sql.Fetch()) |
---|
| 199 | did = sql[0]; |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | cmd = "insert into `description-map` (iid,did,value) values (\"" + data[i].iid + "\",\"" + AsString(did) + "\",\"" + desc.Get(name) + "\")"; |
---|
| 203 | if (!sql.Execute(cmd)) { |
---|
| 204 | Cout() << "Execution Error: " << sql.GetErrorCodeString() << " Cmd: " << sql.GetErrorStatement() << "\n"; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | } |
---|
| 210 | } |
---|
| 211 | |
---|
[26] | 212 | String TrimSpace(String str) { |
---|
| 213 | while (str.StartsWith(" ") && str.GetLength()) { |
---|
| 214 | str = str.Mid(1); |
---|
| 215 | } |
---|
| 216 | while (str.EndsWith(" ") && str.GetLength()) { |
---|
| 217 | str = str.Left(str.GetLength()-1); |
---|
| 218 | } |
---|
| 219 | return str; |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | CONSOLE_APP_MAIN |
---|
| 223 | { |
---|
[37] | 224 | String in, out, fmt, user, pwd, db, ip, arg_flag; |
---|
[29] | 225 | /* |
---|
[26] | 226 | in = "GSE2109_family.xml"; |
---|
[29] | 227 | out = "GSEFamily.tsv"; |
---|
| 228 | fmt = "tsv"; |
---|
| 229 | */ |
---|
[26] | 230 | const Vector<String>& cmdline = CommandLine(); |
---|
[29] | 231 | |
---|
[26] | 232 | for(int i = 0; i < cmdline.GetCount(); i++) { |
---|
| 233 | if (cmdline[i].StartsWith("-")){ |
---|
| 234 | arg_flag = cmdline[i]; |
---|
| 235 | } else if (arg_flag.IsEqual("-i")) { |
---|
[29] | 236 | in = cmdline[i]; |
---|
[26] | 237 | } else if (arg_flag.IsEqual("-o")) { |
---|
[29] | 238 | out = cmdline[i]; |
---|
| 239 | } else if (arg_flag.IsEqual("-f")) { |
---|
| 240 | fmt = cmdline[i]; |
---|
[37] | 241 | } else if (arg_flag.IsEqual("-u")) { |
---|
| 242 | user = cmdline[i]; |
---|
| 243 | } else if (arg_flag.IsEqual("-p")) { |
---|
| 244 | pwd = cmdline[i]; |
---|
| 245 | } else if (arg_flag.IsEqual("-db")) { |
---|
| 246 | db = cmdline[i]; |
---|
| 247 | } else if (arg_flag.IsEqual("-ip")) { |
---|
| 248 | ip = cmdline[i]; |
---|
[29] | 249 | } else if (arg_flag.StartsWith("-")) { |
---|
| 250 | //Error argument |
---|
| 251 | Cout() << "Usage Error: invalid argument "<< arg_flag << "\n"; |
---|
| 252 | exit(0); |
---|
[26] | 253 | } |
---|
| 254 | } |
---|
| 255 | |
---|
[37] | 256 | if (cmdline.GetCount()==0 || |
---|
| 257 | ( (fmt.IsEqual("xml") || fmt.IsEqual("tsv")) && (in.IsEmpty() || out.IsEmpty() || fmt.IsEmpty()) ) || |
---|
| 258 | ( fmt.IsEqual("mysql") && (in.IsEmpty() || user.IsEmpty() || db.IsEmpty() || ip.IsEmpty()) ) |
---|
| 259 | ) { |
---|
[30] | 260 | Cout() << "# Usage #\n"; |
---|
| 261 | Cout() << "For linux: GSEXmlParser -f (xml|tsv) -i (input file) -o (output file)\n"; |
---|
| 262 | Cout() << "For windows: GSEXmlParser.exe -f (xml|tsv) -i (input file) -o (output file)\n"; |
---|
[37] | 263 | Cout() << " GSEXmlParser.exe -f (mysql) -i (input file) -u (username) -p (password) -db (database) -ip (db host)\n"; |
---|
[29] | 264 | exit(0); |
---|
[37] | 265 | } |
---|
[26] | 266 | GSEParser parser; |
---|
| 267 | parser.LoadXML(in); |
---|
[29] | 268 | if (fmt.IsEqual("xml")) |
---|
| 269 | SaveFile(out,parser.ToXML()); |
---|
| 270 | else if (fmt.IsEqual("tsv")) { |
---|
| 271 | SaveFile(out,parser.ToTSV()); |
---|
[37] | 272 | } else if (fmt.IsEqual("mysql")) { |
---|
| 273 | parser.ToMySQL(user,pwd,db,ip); |
---|
| 274 | Cout() << "Completed.\n"; |
---|
[29] | 275 | } |
---|
[26] | 276 | |
---|
[29] | 277 | |
---|
| 278 | |
---|
[26] | 279 | } |
---|
| 280 | |
---|