File: CSVInputDataItem.C

package info (click to toggle)
ball 1.4.3~beta1-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 318,984 kB
  • sloc: cpp: 346,579; ansic: 4,097; python: 2,664; yacc: 1,778; lex: 1,099; xml: 964; sh: 688; sql: 316; awk: 118; makefile: 108
file content (218 lines) | stat: -rw-r--r-- 5,466 bytes parent folder | download | duplicates (2)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <CSVInputDataItem.h>
#include <mainWindow.h>

#include <BALL/QSAR/exception.h>
#include <BALL/VIEW/KERNEL/iconLoader.h>
#include <exception.h>
#include <inputDataDialog.h>

#include <QtGui/QDialog>
#include <QtGui/QDrag>
#include <QtCore/QMimeData>
#include <QtGui/QApplication>
#include <QtGui/QMessageBox>

using namespace BALL::QSAR;

namespace BALL
{
	namespace VIEW
	{

		CSVInputDataItem::CSVInputDataItem(QString filename, DataItemView* view):
			InputDataItem(filename, view)
		{
			setPixmap(findPixmap("csv_icon"));

			QStringList list = filename_.split("/");
			setName(list[list.size()-1]);
			append_ = 0;

			data_ = new QSARData();
		}

		CSVInputDataItem::CSVInputDataItem(QSARData* data, DataItemView* view)
		{
			view_ = view;
			append_ = true;
			setPixmap(findPixmap("csv_icon"));
			data_ = data;
		}

		CSVInputDataItem::~CSVInputDataItem()
		{
			if (view_ && view_->name == "view")
			{
				//if the item was connected to others, delete it from its respective pipeline
				if (!removeDisconnectedItem())
				{
					removeFromPipeline();
				}
			}
		}

		CSVInputDataItem::CSVInputDataItem(CSVInputDataItem& item):
			InputDataItem(filename_, center_data_, center_y_ ,item.view_)
		{
			no_y_ = item. no_y_;
			x_labels_ = item.x_labels_;
			y_labels_ = item.y_labels_;
			append_ = item.append_;
		}

		void CSVInputDataItem::setSeperator(string sep)
		{
			if(sep=="tabulator") {sep_="	";}
			else sep_=sep;
		}


		bool CSVInputDataItem::execute()
		{
			if(done_) return 0; // do nothing twice...
			
			if(append_) appendData();
			else readData();
			
			return 1;	
		}

		void CSVInputDataItem::readData()
		{
			if(done_) return; // do nothing twice...
			
			string st = filename_.toStdString();
			
			data_->readCSVFile(st.c_str(), no_y_, x_labels_, y_labels_, sep_.c_str(), 0, nonnumeric_class_names_);

			if (center_data_)
			{
				data_->centerData(center_y_);
			}
			
			done_ = 1;
		}

		void CSVInputDataItem::appendData()
		{
			if(done_) return; // do nothing twice..
			
			string st = filename_.toStdString();

			data_->readCSVFile(st.c_str(), no_y_, x_labels_, y_labels_, sep_.c_str(), 1, nonnumeric_class_names_);
			
			if (center_data_)
			{
				data_->centerData(center_y_);
			}
			
			done_ = 1;
		}

		void CSVInputDataItem::setXLabelFlag(bool x)
		{
			x_labels_ = x;
		}

		void CSVInputDataItem::setYLabelFlag(bool y)
		{
			y_labels_ = y;
		}

		void CSVInputDataItem::setNumOfActivities(int num)
		{
			no_y_ = num;
		}

		bool CSVInputDataItem::checkForDiscreteY()
		{
			if(checked_for_discrete_y_) return discrete_y_;
			
			try
			{
				if(done_) // if data has already been read, check within data-structure
				{
					discrete_y_=data_->checkforDiscreteY();
				}
				else // if data has not been read, check within file
				{
					//discrete_y_=data_->checkforDiscreteY(filename_.toStdString().c_str(), activity_values_);
					return true;
				}
			}
			catch(BALL::Exception::GeneralException e)
			{	
				// e.g. input file has not been found
				QMessageBox::critical(view_,e.getName(),e.getMessage());
				return 0;
			}
			
			checked_for_discrete_y_ = 1;
			return discrete_y_;
		}

		void CSVInputDataItem::contextMenuEvent(QGraphicsSceneContextMenuEvent* /*event*/)
		{
			InputDataDialog inputDataDialog(this);
			inputDataDialog.exec();
		}

		void CSVInputDataItem::addToPipeline()
		{
			view_->data_scene->main_window->csv_input_pipeline_.insert(this);
			view_->data_scene->main_window->all_items_pipeline_.insert(this);
		}

		void CSVInputDataItem::removeFromPipeline()
		{
			view_->data_scene->main_window->csv_input_pipeline_.erase(this);
			view_->data_scene->main_window->all_items_pipeline_.erase(this);
		}


		void CSVInputDataItem::replaceItem(InputDataItem* old_item)
		{
			transferEdges(old_item);  // steal the old item's edges
			
			// put the new item into the the pipeline at the same position
			CSVInputDataItem* old_csv = dynamic_cast<CSVInputDataItem*>(old_item);
			SDFInputDataItem* old_sdf = dynamic_cast<SDFInputDataItem*>(old_item); 
			if(old_csv)
			{
				Pipeline<CSVInputDataItem*>::iterator csv_it=view_->data_scene->main_window->csv_input_pipeline_.find(old_csv);
				if(csv_it!=view_->data_scene->main_window->csv_input_pipeline_.end())
				{
					view_->data_scene->main_window->csv_input_pipeline_.insert(csv_it,this);
					view_->data_scene->main_window->csv_input_pipeline_.erase(csv_it);
				}
				else view_->data_scene->main_window->csv_input_pipeline_.insert(this);
			}
			else if(old_sdf)
			{
				// delete all features that were read from CSV-files appended to the old SDF-item
				for(list<CSVInputDataItem*>::iterator csv_it=old_sdf->getConnectedCSVItems()->begin(); csv_it!=old_sdf->getConnectedCSVItems()->end(); csv_it++)
				{
					delete *csv_it;
				}
				view_->data_scene->main_window->csv_input_pipeline_.insert(this);
			}
			else view_->data_scene->main_window->csv_input_pipeline_.insert(this);	
			
			Pipeline<DataItem*>::iterator it = view_->data_scene->main_window->all_items_pipeline_.find(old_item);
			if(it!=view_->data_scene->main_window->all_items_pipeline_.end())
			{
				view_->data_scene->main_window->all_items_pipeline_.insert(it,this);
			}
			else
			{
				view_->data_scene->main_window->all_items_pipeline_.insert(this);	
			}
			
			// kill the old item
			delete old_item;	
			
			// finially, make sure that the entire pipeline created by use of this input is reset
			change();
		}
	}
}