source: GSEXmlParser/GSEXmlParser.cpp @ 30

Last change on this file since 30 was 30, checked in by sunny, 16 years ago

1.remove GetDataFile? function for linux version
2.add usage message for linux

File size: 4.9 KB
Line 
1#include "GSEXmlParser.h"
2
3void   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
15String   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
28String 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
41void   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
49void GSEParser::LoadXML(String filename) {
50  //Load XML File
51    //LOG("\n---- GSE 2109 XML ----");
52   
53    String gse;
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";
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
105String   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
114String   GSEParser::ToTSV()
115{
116  String tsv, tab; 
117  tab = "\t";
118  tsv = "# rowkey" + tab + "columnkey" + tab + "value\n";
119 
120  for (int i = 0; i < data.GetCount(); i++) {
121    tsv << data[i].ToTSV();
122  }
123  return tsv;
124}
125
126String TrimSpace(String str) {
127  while (str.StartsWith(" ") && str.GetLength()) {
128    str = str.Mid(1);
129  }
130  while (str.EndsWith(" ") && str.GetLength()) {
131    str = str.Left(str.GetLength()-1);
132  }
133  return str;
134}
135
136CONSOLE_APP_MAIN
137{
138  String in, out, fmt, arg_flag;
139  /*
140  in = "GSE2109_family.xml";
141  out = "GSEFamily.tsv";
142  fmt = "tsv";
143  */
144  const Vector<String>& cmdline = CommandLine();
145   
146  for(int i = 0; i < cmdline.GetCount(); i++) {
147    if (cmdline[i].StartsWith("-")){
148      arg_flag = cmdline[i];     
149    } else if (arg_flag.IsEqual("-i")) {
150      in = cmdline[i];   
151    } else if (arg_flag.IsEqual("-o")) {
152      out = cmdline[i];     
153    } else if (arg_flag.IsEqual("-f")) {
154      fmt = cmdline[i];
155    } else if (arg_flag.StartsWith("-")) {
156      //Error argument
157      Cout() << "Usage Error: invalid argument "<< arg_flag << "\n";
158      exit(0);
159    }
160  }
161 
162  if (cmdline.GetCount()==0 || in.IsEmpty() || out.IsEmpty() || fmt.IsEmpty()) {
163    Cout() << "# Usage #\n";
164    Cout() << "For linux:   GSEXmlParser -f (xml|tsv) -i (input file) -o (output file)\n";
165    Cout() << "For windows: GSEXmlParser.exe -f (xml|tsv) -i (input file) -o (output file)\n";
166    exit(0); 
167  }
168 
169  GSEParser parser;
170  parser.LoadXML(in);
171  if (fmt.IsEqual("xml"))
172    SaveFile(out,parser.ToXML());
173  else if (fmt.IsEqual("tsv"))  {
174    SaveFile(out,parser.ToTSV());
175  }
176 
177 
178 
179}
180
Note: See TracBrowser for help on using the repository browser.