void
Button::Initialize()
{
	DataLoader* loader = DataLoader::GetLoader();
	loader->SetDataPath("Sounds/");

	LoadInterfaceSound(loader, "button.wav",        button_sound);
	LoadInterfaceSound(loader, "click.wav",         click_sound);
	LoadInterfaceSound(loader, "swish.wav",         swish_sound);
	LoadInterfaceSound(loader, "chirp.wav",         chirp_sound);
	LoadInterfaceSound(loader, "accept.wav",        accept_sound);
	LoadInterfaceSound(loader, "reject.wav",        reject_sound);
	LoadInterfaceSound(loader, "confirm.wav",       confirm_sound);
	LoadInterfaceSound(loader, "list_select.wav",   list_select_sound);
	LoadInterfaceSound(loader, "list_scroll.wav",   list_scroll_sound);
	LoadInterfaceSound(loader, "list_drop.wav",     list_drop_sound);
	LoadInterfaceSound(loader, "combo_open.wav",    combo_open_sound);
	LoadInterfaceSound(loader, "combo_close.wav",   combo_close_sound);
	LoadInterfaceSound(loader, "combo_hilite.wav",  combo_hilite_sound);
	LoadInterfaceSound(loader, "combo_select.wav",  combo_select_sound);
	LoadInterfaceSound(loader, "menu_open.wav",     menu_open_sound);
	LoadInterfaceSound(loader, "menu_close.wav",    menu_close_sound);
	LoadInterfaceSound(loader, "menu_select.wav",   menu_select_sound);
	LoadInterfaceSound(loader, "menu_hilite.wav",   menu_hilite_sound);

	loader->SetDataPath(0);
}
void
ModConfig::FindMods()
{
	disabled.destroy();

	DataLoader* loader = DataLoader::GetLoader();

	if (loader) {
		loader->UseFileSystem(true);
		loader->ListFiles("*.dat", disabled, true);
		loader->UseFileSystem(Starshatter::UseFileSystem());

		ListIter<Text> iter = disabled;
		while (++iter) {
			Text* name = iter.value();
			name->setSensitive(false);

			if (*name == "shatter.dat"      || 
					*name == "beta.dat"         || 
					*name == "start.dat"        ||
					*name == "irunin.dat"       ||
					*name == "vox.dat"          ||
					name->contains("uninstall") ||
					enabled.contains(name))
			delete iter.removeItem();
		}
	}
}
void
CmpCompleteDlg::Show()
{
    FormWindow::Show();

    Campaign* c = Campaign::GetCampaign();

    if (img_title && c) {
        DataLoader*    loader = DataLoader::GetLoader();
        Starshatter*   stars  = Starshatter::GetInstance();
        CombatEvent*   event  = c->GetLastEvent();
        char           img_name[256];

        if (event) {
            strcpy_s(img_name, event->ImageFile());

            if (!strstr(img_name, ".pcx")) {
                strcat_s(img_name, ".pcx");
            }

            if (loader) {
                loader->SetDataPath(c->Path());
                loader->LoadBitmap(img_name, banner);
                loader->SetDataPath(0);

                Rect tgt_rect;
                tgt_rect.w = img_title->Width();
                tgt_rect.h = img_title->Height();

                img_title->SetTargetRect(tgt_rect);
                img_title->SetPicture(banner);
            }
        }
    }
}
Beispiel #4
0
void
QuitView::Initialize()
{
    if (!menu_bmp) {
        menu_bmp = new(__FILE__,__LINE__) Bitmap;

        DataLoader* loader = DataLoader::GetLoader();
        loader->SetDataPath("Screens/");
        loader->LoadBitmap("QuitWin.pcx", *menu_bmp, Bitmap::BMP_TRANSPARENT);
        loader->SetDataPath(0);
    }
}
Beispiel #5
0
void Layer::_resetState( const Carta::State::StateInterface& restoreState ){
    m_state.setValue<bool>(Util::VISIBLE, restoreState.getValue<bool>(Util::VISIBLE) );
    m_state.setValue<bool>(Util::SELECTED, restoreState.getValue<bool>( Util::SELECTED) );
    QString layerName = restoreState.getValue<QString>(Util::NAME);
    QString shortName = layerName;
    if ( !layerName.startsWith( GROUP )){
        DataLoader* dLoader = Util::findSingletonObject<DataLoader>();
        shortName = dLoader->getShortName( layerName );
    }
    m_state.setValue<QString>(Util::ID, restoreState.getValue<QString>(Util::ID));
    m_state.setValue<QString>(Util::NAME, shortName);
}
Beispiel #6
0
Hoop::Hoop()
: width(360), height(180), mtl(0)
{
    foreground  = 1;
    radius      = (float) width;

    DataLoader* loader = DataLoader::GetLoader();

    loader->SetDataPath("HUD/");
    loader->LoadTexture("ILS.pcx", hoop_texture, Bitmap::BMP_TRANSLUCENT);
    loader->SetDataPath(0);

    CreatePolys();
}
// ------------------------------------------------------------------------
void DataContainer::LoadData(const std::string & folder)
{
	DataLoader loader;
	loader.SetDataFolder(folder);
	loader.AddFilenameFilter("*.nrrd");
	loader.LoadData(data);

	currentIndex = 0;
	currentTimeStep = 0;
	currentSlice = 0;


	QFileInfo info(QString::fromStdString(folder));
	this->folderName = folder;

}
void
FormWindow::CreateDefEdit(CtrlDef& def)
{
	EditBox* ctrl = CreateEditBox(def.GetText(),
	def.GetX(),
	def.GetY(),
	def.GetW(),
	def.GetH(),
	def.GetID(),
	def.GetParentID());

	ctrl->SetAltText(def.GetAltText());
	ctrl->SetEnabled(def.IsEnabled());
	ctrl->SetBackColor(def.GetBackColor());
	ctrl->SetForeColor(def.GetForeColor());
	ctrl->SetStyle(def.GetStyle());
	ctrl->SetSingleLine(def.GetSingleLine());
	ctrl->SetTextAlign(def.GetTextAlign());
	ctrl->SetTransparent(def.GetTransparent());
	ctrl->SetHidePartial(def.GetHidePartial());
	ctrl->SetPasswordChar(def.GetPasswordChar());

	ctrl->SetMargins(def.GetMargins());
	ctrl->SetTextInsets(def.GetTextInsets());
	ctrl->SetCellInsets(def.GetCellInsets());
	ctrl->SetCells(def.GetCells());
	ctrl->SetFixedWidth(def.GetFixedWidth());
	ctrl->SetFixedHeight(def.GetFixedHeight());

	ctrl->SetLineHeight(def.GetLineHeight());
	ctrl->SetScrollBarVisible(def.GetScrollBarVisible());
	ctrl->SetSmoothScroll(def.GetSmoothScroll());

	if (def.GetTexture().length() > 0) {
		Bitmap*     ctrl_tex = 0;
		DataLoader* loader   = DataLoader::GetLoader();
		loader->SetDataPath("Screens/");
		loader->LoadTexture(def.GetTexture(), ctrl_tex);
		loader->SetDataPath("");

		ctrl->SetTexture(ctrl_tex);
	}

	Font* f = FontMgr::Find(def.GetFont());
	if (f) ctrl->SetFont(f);
}
void
FormWindow::CreateDefRichText(CtrlDef& def)
{
	RichTextBox* ctrl = CreateRichTextBox(def.GetText(),
	def.GetX(),
	def.GetY(),
	def.GetW(),
	def.GetH(),
	def.GetID(),
	def.GetParentID(),
	def.GetStyle());

	ctrl->SetAltText(def.GetAltText());
	ctrl->SetBackColor(def.GetBackColor());
	ctrl->SetForeColor(def.GetForeColor());
	ctrl->SetLineHeight(def.GetLineHeight());
	ctrl->SetLeading(def.GetLeading());
	ctrl->SetScrollBarVisible(def.GetScrollBarVisible());
	ctrl->SetSmoothScroll(def.GetSmoothScroll());
	ctrl->SetTextAlign(def.GetTextAlign());
	ctrl->SetTransparent(def.GetTransparent());
	ctrl->SetHidePartial(def.GetHidePartial());

	ctrl->SetMargins(def.GetMargins());
	ctrl->SetTextInsets(def.GetTextInsets());
	ctrl->SetCellInsets(def.GetCellInsets());
	ctrl->SetCells(def.GetCells());
	ctrl->SetFixedWidth(def.GetFixedWidth());
	ctrl->SetFixedHeight(def.GetFixedHeight());

	if (def.GetTexture().length() > 0) {
		Bitmap*     ctrl_tex = 0;
		DataLoader* loader   = DataLoader::GetLoader();
		loader->SetDataPath("Screens/");
		loader->LoadTexture(def.GetTexture(), ctrl_tex);
		loader->SetDataPath("");

		ctrl->SetTexture(ctrl_tex);
	}

	Font* f = FontMgr::Find(def.GetFont());
	if (f) ctrl->SetFont(f);

	for (int i = 0; i < def.NumTabs(); i++)
	ctrl->SetTabStop(i, def.GetTab(i));
}
void
FormWindow::CreateDefLabel(CtrlDef& def)
{
	ActiveWindow* ctrl = CreateLabel(def.GetText(),
	def.GetX(),
	def.GetY(),
	def.GetW(),
	def.GetH(),
	def.GetID(),
	def.GetParentID(),
	def.GetStyle());

	ctrl->SetAltText(def.GetAltText());
	ctrl->SetBackColor(def.GetBackColor());
	ctrl->SetForeColor(def.GetForeColor());
	ctrl->SetTextAlign(def.GetTextAlign());
	ctrl->SetSingleLine(def.GetSingleLine());
	ctrl->SetTransparent(def.GetTransparent());
	ctrl->SetHidePartial(def.GetHidePartial());

	ctrl->SetMargins(def.GetMargins());
	ctrl->SetTextInsets(def.GetTextInsets());
	ctrl->SetCellInsets(def.GetCellInsets());
	ctrl->SetCells(def.GetCells());
	ctrl->SetFixedWidth(def.GetFixedWidth());
	ctrl->SetFixedHeight(def.GetFixedHeight());

	ctrl->UseLayout(def.GetLayout().x_mins,
	def.GetLayout().y_mins,
	def.GetLayout().x_weights,
	def.GetLayout().y_weights);

	if (def.GetTexture().length() > 0) {
		Bitmap*     ctrl_tex = 0;
		DataLoader* loader   = DataLoader::GetLoader();
		loader->SetDataPath("Screens/");
		loader->LoadTexture(def.GetTexture(), ctrl_tex);
		loader->SetDataPath("");

		ctrl->SetTexture(ctrl_tex);
	}

	Font* f = FontMgr::Find(def.GetFont());
	if (f) ctrl->SetFont(f);
}
Beispiel #11
0
LogisticRegressionProblem::LogisticRegressionProblem(const char* trainFileName) {

	DataLoader * loader = new DataLoader;

    unsigned int featureLen, trainDataCount, trainNumFloats;
    float * pTrainData;
    float * pTrainLabel;

    loader->LoadData(std::string(trainFileName), pTrainData, pTrainLabel);
    loader->ReturnStats(featureLen, trainDataCount, trainNumFloats);

    numFeats = featureLen;

    vector<vector<float> > rowVals(trainDataCount);

    unsigned int count = 0;
    for (unsigned int i=0; i<trainDataCount; i++) {
    	for (unsigned int j=0; j<featureLen; j++) {
			rowVals[i].push_back(pTrainData[count]);
			count ++;
		}
	}

    bool bLabel;
    for (unsigned int i=0; i<trainDataCount; i++) {
		switch (static_cast<int>(pTrainLabel[i])) {
			case 1:
				bLabel = true;
				break;

			case 0:
				bLabel = false;
				break;

			default:
				cerr << "illegal label: must be 1 or 0" << endl;
				exit(1);
		}

		AddInstance(rowVals[i], bLabel);
	}
}
void
FormWindow::CreateDefImage(CtrlDef& def)
{
	ImageBox* ctrl = CreateImageBox(def.GetText(),
	def.GetX(),
	def.GetY(),
	def.GetW(),
	def.GetH(),
	def.GetID(),
	def.GetParentID());

	ctrl->SetAltText(def.GetAltText());
	ctrl->SetBackColor(def.GetBackColor());
	ctrl->SetForeColor(def.GetForeColor());
	ctrl->SetStyle(def.GetStyle());
	ctrl->SetTextAlign(def.GetTextAlign());
	ctrl->SetSingleLine(def.GetSingleLine());
	ctrl->SetTransparent(def.GetTransparent());
	ctrl->SetHidePartial(def.GetHidePartial());

	ctrl->SetMargins(def.GetMargins());
	ctrl->SetTextInsets(def.GetTextInsets());
	ctrl->SetCellInsets(def.GetCellInsets());
	ctrl->SetCells(def.GetCells());
	ctrl->SetFixedWidth(def.GetFixedWidth());
	ctrl->SetFixedHeight(def.GetFixedHeight());

	if (def.GetPicture().length() > 0) {
		Bitmap picture;

		DataLoader* loader = DataLoader::GetLoader();
		loader->SetDataPath("Screens/");
		loader->LoadBitmap(def.GetPicture(), picture);
		loader->SetDataPath("");

		ctrl->SetPicture(picture);
	}

	Font* f = FontMgr::Find(def.GetFont());
	if (f) ctrl->SetFont(f);
}
Beispiel #13
0
int WINAPI WinMain( HINSTANCE hinst, HINSTANCE pinst, LPSTR cmdl, int cmds )
{
	//DataLoader dl("compound.tg4");
	//dl.funkt();                       Marius Style
	screen_interface.open_window( hinst, 1280, 1024 );

	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective( 30, GLdouble( x_res ) / y_res, 0.1f, 10000.0f );
	glMatrixMode( GL_MODELVIEW );
	glPolygonMode( GL_FRONT_AND_BACK, GL_POLYGON);
	glEnable( GL_DEPTH_TEST );
	
	//Für Texturen laden
	glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); 

	GLuint texture_id; //zur Identifikation der Textur
	glGenTextures( 1, &texture_id ); //IdentifikationsID generieren und in texture_id speichern
	glBindTexture( GL_TEXTURE_2D, texture_id ); //Festlegen der aktuellen Textur

	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); //MAGNIFICATION -> vergrößerungsartefakte; unschärfefilter
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); //MINIFICATION -> verkleinerungsartefakte; schärfefilter

	glEnable( GL_TEXTURE_2D ); //Texturprojektion aktivieren (rechenintensiv)
	//Für Texturen laden - Ende

	DataLoader* dl = new DataLoader("compound.tg4");

	while(handle_input() == 0)
	{
		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

		for(int i = 0; i < dl->get_CountOfPolygons() ; i++ )
		{
			(dl->get_Polygon3D()+i)->DrawPolygon();
		}

		screen_interface.swap_buffers();
	}
  return input.msg.wParam;
}
Beispiel #14
0
void
ExitDlg::Show()
{
    if (!IsShown()) {
        Rect r = def_rect;

        if (r.w > screen->Width()) {
            int extra = r.w - screen->Width();
            r.w -= extra;
        }

        if (r.h > screen->Height()) {
            int extra = r.h - screen->Height();
            r.h -= extra;
        }

        r.x = (screen->Width()  - r.w) / 2;
        r.y = (screen->Height() - r.h) / 2;

        MoveTo(r);

        exit_latch = true;
        Button::PlaySound(Button::SND_CONFIRM);
        MusicDirector::SetMode(MusicDirector::CREDITS);

        DataLoader* loader = DataLoader::GetLoader();
        BYTE*       block    = 0;

        loader->SetDataPath(0);
        loader->LoadBuffer("credits.txt", block, true);

        if (block && credits) {
            credits->SetText((const char*) block);
        }

        loader->ReleaseBuffer(block);
    }

    FormWindow::Show();
}
Beispiel #15
0
KBOOL Kylin::Kitbag::AddItem( KUINT uGID )
{
	KSTR sValue;
	if (!DataManager::GetSingletonPtr()->GetGlobalValue("ITEM_DB",sValue))
		return false;

	DataLoader* pLoader = DataManager::GetSingletonPtr()->GetLoaderPtr(sValue);

	// 查询对应的角色信息
	DataItem dbItem;
	if (!pLoader->GetDBPtr()->Query(uGID,dbItem))
		return false;
	
	ItemCell* pItem	 = KNEW ItemCell(this);
	//------------------------------------------------------------------
	DataItem::DataField dbField;
	dbItem.QueryField("MESH",dbField);
	pItem->m_sMesh = boost::any_cast<KSTR>(dbField.m_aValue);

	dbItem.QueryField("TYPE",dbField);
	KSTR sType = boost::any_cast<KSTR>(dbField.m_aValue);
	pItem->m_eType = (ITEM_TYPE)(KUINT)OgreRoot::GetSingletonPtr()->GetScriptVM()->GetGlobalNumber(sType.data());

	dbItem.QueryField("BELONG",dbField);
	pItem->m_uBelong = boost::any_cast<KINT>(dbField.m_aValue);

	dbItem.QueryField("ICON",dbField);
	pItem->m_sIcon = boost::any_cast<KSTR>(dbField.m_aValue);

	dbItem.QueryField("EXPLAIN",dbField);
	pItem->m_sExplain = boost::any_cast<KSTR>(dbField.m_aValue);
	
	dbItem.QueryField("EFFECT",dbField);
	pItem->m_uEffectID = boost::any_cast<KINT>(dbField.m_aValue);
	//------------------------------------------------------------------
	
	return AddItem(pItem);	
}
void
LoadScreen::Setup(Screen* s)
{
	if (!s)
	return;

	screen        = s;

	DataLoader* loader = DataLoader::GetLoader();
	loader->UseFileSystem(true);

	// create windows
	FormDef load_def("LoadDlg", 0);
	load_def.Load("LoadDlg");
	load_dlg = new(__FILE__,__LINE__) LoadDlg(screen, load_def);

	FormDef cmp_load_def("CmpLoadDlg", 0);
	cmp_load_def.Load("CmpLoadDlg");
	cmp_load_dlg = new(__FILE__,__LINE__) CmpLoadDlg(screen, cmp_load_def);

	loader->UseFileSystem(Starshatter::UseFileSystem());
	ShowLoadDlg();
}
Beispiel #17
0
void
Galaxy::Load()
{
    DataLoader* loader = DataLoader::GetLoader();
    loader->SetDataPath("Galaxy/");
    sprintf_s(filename, "%s.def", (const char*) name);
    Load(filename);

    // load mod galaxies:
    List<Text> mod_galaxies;
    loader->SetDataPath("Mods/Galaxy/");
    loader->ListFiles("*.def", mod_galaxies);

    ListIter<Text> iter = mod_galaxies;
    while (++iter) {
        Text* name = iter.value();

        if (!name->contains("/")) {
            loader->SetDataPath("Mods/Galaxy/");
            Load(name->data());
        }
    }
}
CmpSceneDlg::CmpSceneDlg(Screen* s, FormDef& def, CmpnScreen* mgr)
    : FormWindow(s, 0, 0, s->Width(), s->Height()), manager(mgr),
      mov_scene(0), subtitles_box(0), cam_view(0), disp_view(0), old_disp_win(0),
      flare1(0), flare2(0), flare3(0), flare4(0), subtitles_delay(0), subtitles_time(0)
{
    Init(def);

    DataLoader* loader = DataLoader::GetLoader();
    const char* oldpath = loader->GetDataPath();

    loader->SetDataPath(0);
    loader->LoadTexture("flare0+.pcx", flare1, Bitmap::BMP_TRANSLUCENT);
    loader->LoadTexture("flare2.pcx",  flare2, Bitmap::BMP_TRANSLUCENT);
    loader->LoadTexture("flare3.pcx",  flare3, Bitmap::BMP_TRANSLUCENT);
    loader->LoadTexture("flare4.pcx",  flare4, Bitmap::BMP_TRANSLUCENT);
    loader->SetDataPath(oldpath);
}
CombatGroup*
CombatGroup::LoadOrderOfBattle(const char* filename, int team, Combatant* combatant)
{
	CombatGroup* force = 0;
	DataLoader* loader = DataLoader::GetLoader();
	BYTE* block;
	loader->LoadBuffer(filename, block, true);

	Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block));
	Term*  term = parser.ParseTerm();

	if (!term) {
		Print("ERROR: could not parse order of battle '%s'\n", filename);
		return 0;
	}
	else {
		TermText* file_type = term->isText();
		if (!file_type || file_type->value() != "ORDER_OF_BATTLE") {
			Print("ERROR: invalid Order of Battle file '%s'\n", filename);
			term->print(10);
			return 0;
		}
	}


	do {
		delete term; term = 0;
		term = parser.ParseTerm();
		
		if (term) {
			TermDef* def = term->isDef();
			if (def) {
				if (def->name()->value() == "group") {
					if (!def->term() || !def->term()->isStruct()) {
						Print("WARNING: group struct missing in '%s'\n", filename);
					}
					else {
						TermStruct* val = def->term()->isStruct();

						char  name[256];
						char  type[64];
						char  intel[64];
						char  region[64];
						char  system[64];
						char  parent_type[64];
						int   parent_id = 0;
						int   id  = 0;
						int   iff = -1;
						Vec3  loc = Vec3(1.0e9f,0.0f,0.0f);

						List<CombatUnit>  unit_list;
						char              unit_name[64];
						char              unit_regnum[16];
						char              unit_design[64];
						char              unit_skin[64];
						int               unit_class  = 0;
						int               unit_count  = 1;
						int               unit_dead   = 0;
						int               unit_damage = 0;
						int               unit_heading= 0;
						int               unit_index  = 0;
						
						*name          = 0;
						*type          = 0;
						*intel         = 0;
						*region        = 0;
						*system        = 0;
						*parent_type   = 0;
						*unit_name     = 0;
						*unit_regnum   = 0;
						*unit_design   = 0;
						*unit_skin     = 0;

						strcpy_s(intel, "KNOWN");

						// all groups in this OOB default to the IFF of the main force
						if (force)
						iff = force->GetIFF();
						
						for (int i = 0; i < val->elements()->size(); i++) {
							TermDef* pdef = val->elements()->at(i)->isDef();
							if (pdef && (iff < 0 || team < 0 || iff == team)) {
								GET_DEF_TEXT(name);
								else GET_DEF_TEXT(type);
								else GET_DEF_TEXT(intel);
								else GET_DEF_TEXT(region);
								else GET_DEF_TEXT(system);
								else GET_DEF_VEC(loc);
								else GET_DEF_TEXT(parent_type);
								else GET_DEF_NUM(parent_id);
void
MusicDirector::ScanTracks()
{
	DataLoader* loader = DataLoader::GetLoader();

	bool old_file_system = loader->IsFileSystemEnabled();
	loader->UseFileSystem(true);
	loader->SetDataPath("Music/");

	List<Text> files;
	loader->ListFiles("*.ogg", files, true);

	if (files.size() == 0) {
		loader->UseFileSystem(old_file_system);
		no_music = true;
		return;
	}

	no_music = false;

	ListIter<Text> iter = files;
	while (++iter) {
		Text* name = iter.value();
		Text* file = new(__FILE__,__LINE__) Text("Music/");

		name->setSensitive(false);
		file->append(*name);

		if (name->indexOf("Menu") == 0) {
			menu_tracks.append(file);
		}

		else if (name->indexOf("Intro") == 0) {
			intro_tracks.append(file);
		}

		else if (name->indexOf("Brief") == 0) {
			brief_tracks.append(file);
		}

		else if (name->indexOf("Debrief") == 0) {
			debrief_tracks.append(file);
		}

		else if (name->indexOf("Promot") == 0) {
			promote_tracks.append(file);
		}

		else if (name->indexOf("Flight") == 0) {
			flight_tracks.append(file);
		}

		else if (name->indexOf("Combat") == 0) {
			combat_tracks.append(file);
		}

		else if (name->indexOf("Launch") == 0) {
			launch_tracks.append(file);
		}

		else if (name->indexOf("Recovery") == 0) {
			recovery_tracks.append(file);
		}

		else if (name->indexOf("Victory") == 0) {
			victory_tracks.append(file);
		}

		else if (name->indexOf("Defeat") == 0) {
			defeat_tracks.append(file);
		}

		else if (name->indexOf("Credit") == 0) {
			credit_tracks.append(file);
		}

		else {
			menu_tracks.append(file);
		}

		delete iter.removeItem();
	}

	loader->UseFileSystem(old_file_system);

	menu_tracks.sort();
	intro_tracks.sort();
	brief_tracks.sort();
	debrief_tracks.sort();
	promote_tracks.sort();
	flight_tracks.sort();
	combat_tracks.sort();
	launch_tracks.sort();
	recovery_tracks.sort();
	victory_tracks.sort();
	defeat_tracks.sort();
	credit_tracks.sort();
}
Beispiel #21
0
int main() {
	
	// Data loading
    DataLoader dataLoader;
	clock_t begin_time = clock();
	cout << "Reading inverted file to RAM" << endl;
	dataLoader.loadInvFile("data/InvFile.txt");
	std::cout<< "Time elapsed: " << float( clock () - begin_time ) /  CLOCKS_PER_SEC<< " seconds"<< endl;

	cout << "Reading document length to RAM" << endl;
	begin_time = clock();
	dataLoader.loadDocRec("data/file.txt");
	dataLoader.loadDocLen("data/InvFile.doc");
	std::cout<< "Time elapsed: " << float( clock () - begin_time ) /  CLOCKS_PER_SEC<< " seconds"<< endl;


	// Read stopword list
	std::map<std::string, std::string> stopWordList;
	std::string line;
	std::fstream stopwordFile ("data/stopword.txt");
	if (stopwordFile.is_open()) {
		while ( stopwordFile.good() ) {
		  getline (stopwordFile,line);
		  stopWordList[line] = line;
		}
		stopwordFile.close();
	}
	else throw("Unable to open file"); 

	// Retrieval classes
	StopWordProcessor swp(stopWordList);
	VSMRetrieval vsmRetrieval(&dataLoader);
	begin_time = clock();

	// Read query file
	std::vector<std::string> list;
	std::fstream queryFile("data/queryT");
	if (!queryFile.is_open()) throw "File not found";
	while (queryFile.good()) {
		getline(queryFile, line);
		if(line.size()>0){
			list.push_back(line);
		}
	}
	queryFile.close();

	// Retreive
	ofstream ofs("data/TREC/result_queryT_TFIDF_STOPWORD.txt");
	for (int i = 0; i < list.size(); i++) {
		// Stop word processing
		std::string query = swp.processQuery(list[i]);
		vsmRetrieval.retrieve(query, ofs);
	}
	ofs.close();
	std::cout<< "Time elapsed: " << float( clock () - begin_time ) /  CLOCKS_PER_SEC<< " seconds"<< endl;


	system("pause");

	return 0;
}
QuantumFlash::QuantumFlash()
: nverts(64), npolys(16), mtl(0), verts(0), polys(0), beams(0),
texture(quantum_flash_texture), length(8000), width(0),
shade(1.0)
{
    trans    = true;
    luminous = true;

    if (!texture || texture->Width() < 1) {
        DataLoader* loader = DataLoader::GetLoader();
        loader->SetDataPath("Explosions/");
        loader->LoadTexture("quantum.pcx", quantum_flash_texture, Bitmap::BMP_TRANSLUCENT);
        loader->SetDataPath(0);
        texture = quantum_flash_texture;
    }

    loc = Vec3(0.0f, 0.0f, 1000.0f);

    mtl   = new(__FILE__,__LINE__) Material;
    verts = new(__FILE__,__LINE__) VertexSet(nverts);
    polys = new(__FILE__,__LINE__) Poly[npolys];
    beams = new(__FILE__,__LINE__) Matrix[npolys];

    mtl->Kd           = Color::White;
    mtl->tex_diffuse  = texture;
    mtl->tex_emissive = texture;
    mtl->blend        = Video::BLEND_ADDITIVE;
    mtl->luminous     = true;

    verts->nverts = nverts;

    for (int i = 0; i < npolys; i++) {
        verts->loc[4*i+0] = Point( width, 0,       1000);
        verts->loc[4*i+1] = Point( width, -length, 1000);
        verts->loc[4*i+2] = Point(-width, -length, 1000);
        verts->loc[4*i+3] = Point(-width, 0,       1000);

        for (int n = 0; n < 4; n++) {
            verts->diffuse[4*i+n]   = D3DCOLOR_RGBA(255,255,255,255);
            verts->specular[4*i+n]  = D3DCOLOR_RGBA(  0,  0,  0,255);
            verts->tu[4*i+n]        = (n < 2)          ? 0.0f : 1.0f;
            verts->tv[4*i+n]        = (n > 0 && n < 3) ? 1.0f : 0.0f;
        }

        beams[i].Roll( Random(-2*PI, 2*PI));
        beams[i].Pitch(Random(-2*PI, 2*PI));
        beams[i].Yaw(  Random(-2*PI, 2*PI));

        polys[i].nverts      = 4;
        polys[i].visible     = 1;
        polys[i].sortval     = 0;
        polys[i].vertex_set  = verts;
        polys[i].material    = mtl;

        polys[i].verts[0]    = 4*i+0;
        polys[i].verts[1]    = 4*i+1;
        polys[i].verts[2]    = 4*i+2;
        polys[i].verts[3]    = 4*i+3;
    }

    radius = (float) length;
    length = 0;
    strcpy_s(name, "QuantumFlash");
}
Beispiel #23
0
int main(int argc, char** argv) {
  ////////////////////////////////////////////////
  BEGIN_PARAMETER_LIST(pl)
  ADD_PARAMETER_GROUP(pl, "Basic Input/Output")
  ADD_STRING_PARAMETER(pl, inVcf, "--inVcf", "Input VCF File")
  ADD_STRING_PARAMETER(pl, outPrefix, "--out", "Output prefix")
  ADD_BOOL_PARAMETER(pl, outputRaw, "--outputRaw",
                     "Output genotypes, phenotype, covariates(if any) and "
                     "collapsed genotype to tabular files")

  ADD_PARAMETER_GROUP(pl, "Specify Covariate")
  ADD_STRING_PARAMETER(pl, cov, "--covar", "Specify covariate file")
  ADD_STRING_PARAMETER(
      pl, covName, "--covar-name",
      "Specify the column name in covariate file to be included in analysis")
  ADD_BOOL_PARAMETER(pl, sex, "--sex",
                     "Include sex (5th column in the PED file) as a covariate")

  ADD_PARAMETER_GROUP(pl, "Specify Phenotype")
  ADD_STRING_PARAMETER(pl, pheno, "--pheno", "Specify phenotype file")
  ADD_BOOL_PARAMETER(pl, inverseNormal, "--inverseNormal",
                     "Transform phenotype like normal distribution")
  ADD_BOOL_PARAMETER(
      pl, useResidualAsPhenotype, "--useResidualAsPhenotype",
      "Fit covariate ~ phenotype, use residual to replace phenotype")
  ADD_STRING_PARAMETER(pl, mpheno, "--mpheno",
                       "Specify which phenotype column to read (default: 1)")
  ADD_STRING_PARAMETER(pl, phenoName, "--pheno-name",
                       "Specify which phenotype column to read by header")
  ADD_BOOL_PARAMETER(pl, qtl, "--qtl", "Treat phenotype as quantitative trait")
  ADD_STRING_PARAMETER(
      pl, multiplePheno, "--multiplePheno",
      "Specify aa template file for analyses of more than one phenotype")

  ADD_PARAMETER_GROUP(pl, "Specify Genotype")
  ADD_STRING_PARAMETER(pl, dosageTag, "--dosage",
                       "Specify which dosage tag to use. (e.g. EC or DS)")

  ADD_PARAMETER_GROUP(pl, "Chromosome X Options")
  ADD_STRING_PARAMETER(pl, xLabel, "--xLabel",
                       "Specify X chromosome label (default: 23|X)")
  ADD_STRING_PARAMETER(pl, xParRegion, "--xParRegion",
                       "Specify PAR region (default: hg19), can be build "
                       "number e.g. hg38, b37; or specify region, e.g. "
                       "'60001-2699520,154931044-155260560'")

  ADD_PARAMETER_GROUP(pl, "People Filter")
  ADD_STRING_PARAMETER(pl, peopleIncludeID, "--peopleIncludeID",
                       "List IDs of people that will be included in study")
  ADD_STRING_PARAMETER(
      pl, peopleIncludeFile, "--peopleIncludeFile",
      "From given file, set IDs of people that will be included in study")
  ADD_STRING_PARAMETER(pl, peopleExcludeID, "--peopleExcludeID",
                       "List IDs of people that will be included in study")
  ADD_STRING_PARAMETER(
      pl, peopleExcludeFile, "--peopleExcludeFile",
      "From given file, set IDs of people that will be included in study")

  ADD_PARAMETER_GROUP(pl, "Site Filter")
  ADD_STRING_PARAMETER(
      pl, rangeList, "--rangeList",
      "Specify some ranges to use, please use chr:begin-end format.")
  ADD_STRING_PARAMETER(
      pl, rangeFile, "--rangeFile",
      "Specify the file containing ranges, please use chr:begin-end format.")
  ADD_STRING_PARAMETER(pl, siteFile, "--siteFile",
                       "Specify the file containing sites to include, please "
                       "use \"chr pos\" format.")
  ADD_INT_PARAMETER(
      pl, siteDepthMin, "--siteDepthMin",
      "Specify minimum depth(inclusive) to be included in analysis")
  ADD_INT_PARAMETER(
      pl, siteDepthMax, "--siteDepthMax",
      "Specify maximum depth(inclusive) to be included in analysis")
  ADD_INT_PARAMETER(pl, siteMACMin, "--siteMACMin",
                    "Specify minimum Minor Allele Count(inclusive) to be "
                    "included in analysis")
  ADD_STRING_PARAMETER(pl, annoType, "--annoType",
                       "Specify annotation type that is followed by ANNO= in "
                       "the VCF INFO field, regular expression is allowed ")

  ADD_PARAMETER_GROUP(pl, "Genotype Filter")
  ADD_INT_PARAMETER(
      pl, indvDepthMin, "--indvDepthMin",
      "Specify minimum depth(inclusive) of a sample to be included in analysis")
  ADD_INT_PARAMETER(
      pl, indvDepthMax, "--indvDepthMax",
      "Specify maximum depth(inclusive) of a sample to be included in analysis")
  ADD_INT_PARAMETER(
      pl, indvQualMin, "--indvQualMin",
      "Specify minimum depth(inclusive) of a sample to be included in analysis")

  ADD_PARAMETER_GROUP(pl, "Association Model")
  ADD_STRING_PARAMETER(pl, modelSingle, "--single",
                       "Single variant tests, choose from: score, wald, exact, "
                       "famScore, famLrt, famGrammarGamma, firth")
  ADD_STRING_PARAMETER(pl, modelBurden, "--burden",
                       "Burden tests, choose from: cmc, zeggini, mb, exactCMC, "
                       "rarecover, cmat, cmcWald")
  ADD_STRING_PARAMETER(pl, modelVT, "--vt",
                       "Variable threshold tests, choose from: price, analytic")
  ADD_STRING_PARAMETER(
      pl, modelKernel, "--kernel",
      "Kernal-based tests, choose from: SKAT, KBAC, FamSKAT, SKATO")
  ADD_STRING_PARAMETER(pl, modelMeta, "--meta",
                       "Meta-analysis related functions to generate summary "
                       "statistics, choose from: score, cov, dominant, "
                       "recessive")

  ADD_PARAMETER_GROUP(pl, "Family-based Models")
  ADD_STRING_PARAMETER(pl, kinship, "--kinship",
                       "Specify a kinship file for autosomal analysis, use "
                       "vcf2kinship to generate")
  ADD_STRING_PARAMETER(pl, xHemiKinship, "--xHemiKinship",
                       "Provide kinship for the chromosome X hemizygote region")
  ADD_STRING_PARAMETER(pl, kinshipEigen, "--kinshipEigen",
                       "Specify eigen decomposition results of a kinship file "
                       "for autosomal analysis")
  ADD_STRING_PARAMETER(
      pl, xHemiKinshipEigen, "--xHemiKinshipEigen",
      "Specify eigen decomposition results of a kinship file for X analysis")

  ADD_PARAMETER_GROUP(pl, "Grouping Unit ")
  ADD_STRING_PARAMETER(pl, geneFile, "--geneFile",
                       "Specify a gene file (for burden tests)")
  ADD_STRING_PARAMETER(pl, gene, "--gene", "Specify which genes to test")
  ADD_STRING_PARAMETER(pl, setList, "--setList",
                       "Specify a list to test (for burden tests)")
  ADD_STRING_PARAMETER(pl, setFile, "--setFile",
                       "Specify a list file (for burden tests, first 2 "
                       "columns: setName chr:beg-end)")
  ADD_STRING_PARAMETER(pl, set, "--set",
                       "Specify which set to test (1st column)")

  ADD_PARAMETER_GROUP(pl, "Frequency Cutoff")
  /*ADD_BOOL_PARAMETER(pl, freqFromFile, "--freqFromFile", "Obtain frequency
   * from external file")*/
  // ADD_BOOL_PARAMETER(pl, freqFromControl, "--freqFromControl", "Calculate
  // frequency from case samples")
  ADD_DOUBLE_PARAMETER(
      pl, freqUpper, "--freqUpper",
      "Specify upper minor allele frequency bound to be included in analysis")
  ADD_DOUBLE_PARAMETER(
      pl, freqLower, "--freqLower",
      "Specify lower minor allele frequency bound to be included in analysis")

  ADD_PARAMETER_GROUP(pl, "Missing Data")
  ADD_STRING_PARAMETER(
      pl, impute, "--impute",
      "Impute missing genotype (default:mean):  mean, hwe, and drop")
  ADD_BOOL_PARAMETER(
      pl, imputePheno, "--imputePheno",
      "Impute phenotype to mean of those have genotypes but no phenotypes")
  ADD_BOOL_PARAMETER(pl, imputeCov, "--imputeCov",
                     "Impute each covariate to its mean, instead of drop "
                     "samples with missing covariates")

  ADD_PARAMETER_GROUP(pl, "Conditional Analysis")
  ADD_STRING_PARAMETER(pl, condition, "--condition",
                       "Specify markers to be conditions (specify range)")

  ADD_PARAMETER_GROUP(pl, "Auxiliary Functions")
  ADD_BOOL_PARAMETER(pl, noweb, "--noweb", "Skip checking new version")
  ADD_BOOL_PARAMETER(pl, help, "--help", "Print detailed help message")
  END_PARAMETER_LIST(pl);

  pl.Read(argc, argv);

  if (FLAG_help) {
    pl.Help();
    return 0;
  }

  welcome();
  pl.Status();
  if (FLAG_REMAIN_ARG.size() > 0) {
    fprintf(stderr, "Unparsed arguments: ");
    for (unsigned int i = 0; i < FLAG_REMAIN_ARG.size(); i++) {
      fprintf(stderr, " %s", FLAG_REMAIN_ARG[i].c_str());
    }
    exit(1);
  }

  if (!FLAG_outPrefix.size()) FLAG_outPrefix = "rvtest";

  REQUIRE_STRING_PARAMETER(FLAG_inVcf,
                           "Please provide input file using: --inVcf");

  // check new version
  if (!FLAG_noweb) {
    VersionChecker ver;
    if (ver.retrieveRemoteVersion("http://zhanxw.com/rvtests/version") < 0) {
      fprintf(stderr,
              "Retrieve remote version failed, use '--noweb' to skip.\n");
    } else {
      ver.setLocalVersion(VERSION);
      if (ver.isRemoteVersionNewer()) {
        fprintf(stderr, "New version of rvtests is available:");
        ver.printRemoteContent();
      }
    }
  }

  // start logging
  Logger _logger((FLAG_outPrefix + ".log").c_str());
  logger = &_logger;
  logger->info("Program version: %s", VERSION);
  logger->infoToFile("Git Version: %s", GIT_VERSION);
  logger->infoToFile("Parameters BEGIN");
  pl.WriteToFile(logger->getHandle());
  logger->infoToFile("Parameters END");
  logger->sync();

  // start analysis
  time_t startTime = time(0);
  logger->info("Analysis started at: %s", currentTime().c_str());

  GenotypeExtractor ge(FLAG_inVcf);

  // set range filters here
  ge.setRangeList(FLAG_rangeList.c_str());
  ge.setRangeFile(FLAG_rangeFile.c_str());

  // set people filters here
  if (FLAG_peopleIncludeID.size() || FLAG_peopleIncludeFile.size()) {
    ge.excludeAllPeople();
    ge.includePeople(FLAG_peopleIncludeID.c_str());
    ge.includePeopleFromFile(FLAG_peopleIncludeFile.c_str());
  }
  ge.excludePeople(FLAG_peopleExcludeID.c_str());
  ge.excludePeopleFromFile(FLAG_peopleExcludeFile.c_str());

  if (FLAG_siteDepthMin > 0) {
    ge.setSiteDepthMin(FLAG_siteDepthMin);
    logger->info("Set site depth minimum to %d", FLAG_siteDepthMin);
  }
  if (FLAG_siteDepthMax > 0) {
    ge.setSiteDepthMax(FLAG_siteDepthMax);
    logger->info("Set site depth maximum to %d", FLAG_siteDepthMax);
  }
  if (FLAG_siteMACMin > 0) {
    ge.setSiteMACMin(FLAG_siteMACMin);
    logger->info("Set site minimum MAC to %d", FLAG_siteDepthMin);
  }
  if (FLAG_annoType != "") {
    ge.setAnnoType(FLAG_annoType.c_str());
    logger->info("Set annotype type filter to %s", FLAG_annoType.c_str());
  }

  std::vector<std::string> vcfSampleNames;
  ge.getPeopleName(&vcfSampleNames);
  logger->info("Loaded [ %zu ] samples from VCF files", vcfSampleNames.size());

  DataLoader dataLoader;
  dataLoader.setPhenotypeImputation(FLAG_imputePheno);
  dataLoader.setCovariateImputation(FLAG_imputeCov);

  if (FLAG_multiplePheno.empty()) {
    dataLoader.loadPhenotype(FLAG_pheno, FLAG_mpheno, FLAG_phenoName);

    // // load phenotypes
    // std::map<std::string, double> phenotype;
    // if (FLAG_pheno.empty()) {
    //   logger->error("Cannot do association when phenotype is missing!");
    //   return -1;
    // }

    // // check if alternative phenotype columns are used
    // if (!FLAG_mpheno.empty() && !FLAG_phenoName.empty()) {
    //   logger->error("Please specify either --mpheno or --pheno-name");
    //   return -1;
    // }
    // if (!FLAG_mpheno.empty()) {
    //   int col = atoi(FLAG_mpheno);
    //   int ret = loadPedPhenotypeByColumn(FLAG_pheno.c_str(), &phenotype,
    //   col);
    //   if (ret < 0) {
    //     logger->error("Loading phenotype failed!");
    //     return -1;
    //   }
    // } else if (!FLAG_phenoName.empty()) {
    //   int ret = loadPedPhenotypeByHeader(FLAG_pheno.c_str(), &phenotype,
    //                                      FLAG_phenoName.c_str());
    //   if (ret < 0) {
    //     logger->error("Loading phenotype failed!");
    //     return -1;
    //   }
    // } else {
    //   int col = 1;  // default use the first phenotype
    //   int ret = loadPedPhenotypeByColumn(FLAG_pheno.c_str(), &phenotype,
    //   col);
    //   if (ret < 0) {
    //     logger->error("Loading phenotype failed!");
    //     return -1;
    //   }
    // }
    // logger->info("Loaded [ %zu ] sample pheontypes.", phenotype.size());

    // rearrange phenotypes
    // drop samples from phenotype or vcf
    matchPhenotypeAndVCF("missing phenotype", &dataLoader, &ge);

    // // phenotype names (vcf sample names) arranged in the same order as in
    // VCF
    // std::vector<std::string> phenotypeNameInOrder;
    // std::vector<double>
    //     phenotypeInOrder;  // phenotype arranged in the same order as in VCF
    // rearrange(phenotype, vcfSampleNames, &vcfSampleToDrop,
    // &phenotypeNameInOrder,
    //           &phenotypeInOrder, FLAG_imputePheno);
    // if (vcfSampleToDrop.size()) {
    //   // exclude this sample from parsing VCF
    //   ge.excludePeople(vcfSampleToDrop);
    //   // output dropped samples
    //   for (size_t i = 0; i < vcfSampleToDrop.size(); ++i) {
    //     if (i == 0)
    //       logger->warn(
    //           "Total [ %zu ] samples are dropped from VCF file due to missing
    //           "
    //           "phenotype",
    //           vcfSampleToDrop.size());
    //     if (i >= 10) {
    //       logger->warn(
    //           "Skip outputting additional [ %d ] samples with missing "
    //           "phenotypes.",
    //           ((int)vcfSampleToDrop.size() - 10));
    //       break;
    //     }
    //     logger->warn("Drop sample [ %s ] from VCF file due to missing
    //     phenotype",
    //                  (vcfSampleToDrop)[i].c_str());
    //   }
    //   // logger->warn("Drop %zu sample from VCF file since we don't have
    //   their
    //   // phenotypes", vcfSampleToDrop.size());
    // }
    // if (phenotypeInOrder.size() != phenotype.size()) {
    //   logger->warn(
    //       "Drop [ %d ] samples from phenotype file due to missing genotypes
    //       from "
    //       "VCF files",
    //       (int)(phenotype.size() - phenotypeInOrder.size()));
    //   // We may output these samples by comparing keys of phenotype and
    //   // phenotypeNameInOrder
    // }
    dataLoader.loadCovariate(FLAG_cov, FLAG_covName);
    matchCovariateAndVCF("missing covariate", &dataLoader, &ge);

    // // load covariate
    // Matrix covariate;
    // HandleMissingCov handleMissingCov = COVARIATE_DROP;
    // if (FLAG_imputeCov) {
    //   handleMissingCov = COVARIATE_IMPUTE;
    // }
    // if (FLAG_cov.empty() && !FLAG_covName.empty()) {
    //   logger->info("Use phenotype file as covariate file [ %s ]",
    //                FLAG_pheno.c_str());
    //   FLAG_cov = FLAG_pheno;
    // }
    // if (!FLAG_cov.empty()) {
    //   logger->info("Begin to read covariate file.");
    //   std::vector<std::string> columnNamesInCovariate;
    //   std::set<std::string> sampleToDropInCovariate;
    //   int ret = loadCovariate(FLAG_cov.c_str(), phenotypeNameInOrder,
    //                           FLAG_covName.c_str(), handleMissingCov,
    //                           &covariate,
    //                           &columnNamesInCovariate,
    //                           &sampleToDropInCovariate);
    //   if (ret < 0) {
    //     logger->error("Load covariate file failed !");
    //     exit(1);
    //   }

    //   // drop phenotype samples
    //   if (!sampleToDropInCovariate.empty()) {
    //     int idx = 0;
    //     int n = phenotypeNameInOrder.size();
    //     for (int i = 0; i < n; ++i) {
    //       if (sampleToDropInCovariate.count(phenotypeNameInOrder[i]) !=
    //           0) {  // need to drop
    //         continue;
    //       }
    //       phenotypeNameInOrder[idx] = phenotypeNameInOrder[i];
    //       phenotypeInOrder[idx] = phenotypeInOrder[i];
    //       idx++;
    //     }
    //     phenotypeNameInOrder.resize(idx);
    //     phenotypeInOrder.resize(idx);
    //     logger->warn(
    //         "[ %zu ] sample phenotypes are dropped due to lacking
    //         covariates.",
    //         sampleToDropInCovariate.size());
    //   }
    //   // drop vcf samples;
    //   for (std::set<std::string>::const_iterator iter =
    //            sampleToDropInCovariate.begin();
    //        iter != sampleToDropInCovariate.end(); ++iter) {
    //     ge.excludePeople(iter->c_str());
    //   }
    // }
  } else {
    dataLoader.loadMultiplePhenotype(FLAG_multiplePheno, FLAG_pheno, FLAG_cov);

    matchPhenotypeAndVCF("missing phenotype", &dataLoader, &ge);
    matchCovariateAndVCF("missing covariate", &dataLoader, &ge);
  }

  dataLoader.loadSex();
  if (FLAG_sex) {
    dataLoader.useSexAsCovariate();
    matchCovariateAndVCF("missing sex", &dataLoader, &ge);
  }
  // // load sex
  // std::vector<int> sex;
  // if (loadSex(FLAG_pheno, phenotypeNameInOrder, &sex)) {
  //   logger->error("Cannot load sex of samples from phenotype file");
  //   exit(1);
  // }

  // if (FLAG_sex) {            // append sex in covariate
  //   std::vector<int> index;  // mark missing samples
  //   int numMissing = findMissingSex(sex, &index);
  //   logger->info("Futher exclude %d samples with missing sex", numMissing);
  //   removeByIndex(index, &sex);
  //   excludeSamplesByIndex(index, &ge, &phenotypeNameInOrder,
  //   &phenotypeInOrder,
  //                         &covariate);
  //   appendToMatrix("Sex", sex, &covariate);
  // }

  if (!FLAG_condition.empty()) {
    dataLoader.loadMarkerAsCovariate(FLAG_inVcf, FLAG_condition);
    matchCovariateAndVCF("missing in conditioned marker(s)", &dataLoader, &ge);
  }
  // // load conditional markers
  // if (!FLAG_condition.empty()) {
  //   Matrix geno;
  //   std::vector<std::string> rowLabel;
  //   if (loadMarkerFromVCF(FLAG_inVcf, FLAG_condition, &rowLabel, &geno) < 0)
  //   {
  //     logger->error("Load conditional markers [ %s ] from [ %s ] failed.",
  //                   FLAG_condition.c_str(), FLAG_inVcf.c_str());
  //     exit(1);
  //   }
  //   if (appendGenotype(&covariate, phenotypeNameInOrder, geno, rowLabel) < 0)
  //   {
  //     logger->error(
  //         "Failed to combine conditional markers [ %s ] from [ %s ] failed.",
  //         FLAG_condition.c_str(), FLAG_inVcf.c_str());
  //     exit(1);
  //   }
  // }

  dataLoader.checkConstantCovariate();
  // // check if some covariates are constant for all samples
  // // e.g. user may include covariate "1" in addition to intercept
  // //      in such case, we will give a fatal error
  // for (int i = 0; i < covariate.cols; ++i) {
  //   std::set<double> s;
  //   s.clear();
  //   for (int j = 0; j < covariate.rows; ++j) {
  //     s.insert(covariate[j][i]);
  //   }
  //   if (s.size() == 1) {
  //     logger->error(
  //         "Covariate [ %s ] equals [ %g ] for all samples, cannot fit "
  //         "model...\n",
  //         covariate.GetColumnLabel(i), *s.begin());
  //     exit(1);
  //   }
  // }

  g_SummaryHeader = new SummaryHeader;
  g_SummaryHeader->recordCovariate(dataLoader.getCovariate());

  // record raw phenotype
  g_SummaryHeader->recordPhenotype("Trait",
                                   dataLoader.getPhenotype().extractCol(0));

  // adjust phenotype
  // bool binaryPhenotype;
  if (FLAG_qtl) {
    // binaryPhenotype = false;
    dataLoader.setTraitType(DataLoader::PHENOTYPE_QTL);
    logger->info("-- Force quantitative trait mode -- ");
  } else {
    if (dataLoader.detectPhenotypeType() == DataLoader::PHENOTYPE_BINARY) {
      logger->warn("-- Enabling binary phenotype mode -- ");
      dataLoader.setTraitType(DataLoader::PHENOTYPE_BINARY);

    } else {
      dataLoader.setTraitType(DataLoader::PHENOTYPE_QTL);
    }
    // binaryPhenotype = isBinaryPhenotype(phenotypeInOrder);
    // if (binaryPhenotype) {
    //   logger->warn("-- Enabling binary phenotype mode -- ");
    //   convertBinaryPhenotype(&phenotypeInOrder);
    // }
  }

  if (FLAG_useResidualAsPhenotype) {
    dataLoader.useResidualAsPhenotype();
    g_SummaryHeader->recordEstimation(dataLoader.getEstimation());
  }
  // // use residual as phenotype
  // if (FLAG_useResidualAsPhenotype) {
  //   if (binaryPhenotype) {
  //     logger->warn(
  //         "WARNING: Skip transforming binary phenotype, although you want to
  //         "
  //         "use residual as phenotype!");
  //   } else {
  //     if (covariate.cols > 0) {
  //       LinearRegression lr;
  //       Vector pheno;
  //       Matrix covAndInt;
  //       copy(phenotypeInOrder, &pheno);
  //       copyCovariateAndIntercept(covariate.rows, covariate, &covAndInt);
  //       if (!lr.FitLinearModel(covAndInt, pheno)) {
  //         logger->error(
  //             "Cannot fit model: [ phenotype ~ 1 + covariates ], now use the
  //             "
  //             "original phenotype");
  //       } else {
  //         const int n = lr.GetResiduals().Length();
  //         for (int i = 0; i < n; ++i) {
  //           phenotypeInOrder[i] = lr.GetResiduals()[i];
  //         }
  //         covariate.Dimension(0, 0);
  //         logger->info(
  //             "DONE: Fit model [ phenotype ~ 1 + covariates ] and model "
  //             "residuals will be used as responses.");
  //       }
  //     } else {  // no covaraites
  //       centerVector(&phenotypeInOrder);
  //       logger->info("DONE: Use residual as phenotype by centerng it");
  //     }
  //   }
  // }

  if (FLAG_inverseNormal) {
    dataLoader.inverseNormalizePhenotype();
    g_SummaryHeader->setInverseNormalize(FLAG_inverseNormal);
  }
  // // phenotype transformation
  // if (FLAG_inverseNormal) {
  //   if (binaryPhenotype) {
  //     logger->warn(
  //         "WARNING: Skip transforming binary phenotype, although you required
  //         "
  //         "inverse normalization!");
  //   } else {
  //     logger->info("Now applying inverse normalize transformation.");
  //     inverseNormalizeLikeMerlin(&phenotypeInOrder);
  //     g_SummaryHeader->setInverseNormalize(FLAG_inverseNormal);
  //     logger->info("DONE: inverse normal transformation finished.");
  //   }
  // }

  g_SummaryHeader->recordPhenotype("AnalyzedTrait",
                                   dataLoader.getPhenotype().extractCol(0));

  if (dataLoader.getPhenotype().nrow() == 0) {
    logger->fatal("There are 0 samples with valid phenotypes, quitting...");
    exit(1);
  }
  // if (phenotypeInOrder.empty()) {
  //   logger->fatal("There are 0 samples with valid phenotypes, quitting...");
  //   exit(1);
  // }

  logger->info("Analysis begins with [ %d ] samples...",
               dataLoader.getPhenotype().nrow());
  //////////////////////////////////////////////////////////////////////////////
  // prepare each model
  bool singleVariantMode = FLAG_modelSingle.size() || FLAG_modelMeta.size();
  bool groupVariantMode = (FLAG_modelBurden.size() || FLAG_modelVT.size() ||
                           FLAG_modelKernel.size());
  if (singleVariantMode && groupVariantMode) {
    logger->error("Cannot support both single variant and region based tests");
    exit(1);
  }

  ModelManager modelManager(FLAG_outPrefix);
  // set up models in qtl/binary modes
  if (dataLoader.isBinaryPhenotype()) {
    modelManager.setBinaryOutcome();
    matchPhenotypeAndVCF("missing phenotype (not case/control)", &dataLoader,
                         &ge);
  } else {
    modelManager.setQuantitativeOutcome();
  }
  // create models
  modelManager.create("single", FLAG_modelSingle);
  modelManager.create("burden", FLAG_modelBurden);
  modelManager.create("vt", FLAG_modelVT);
  modelManager.create("kernel", FLAG_modelKernel);
  modelManager.create("meta", FLAG_modelMeta);
  if (FLAG_outputRaw) {
    modelManager.create("outputRaw", "dump");
  }

  const std::vector<ModelFitter*>& model = modelManager.getModel();
  const std::vector<FileWriter*>& fOuts = modelManager.getResultFile();
  const size_t numModel = model.size();

  // TODO: optimize this by avoidding data copying
  Matrix phenotypeMatrix;
  Matrix covariate;
  toMatrix(dataLoader.getPhenotype(), &phenotypeMatrix);
  toMatrix(dataLoader.getCovariate(), &covariate);

  // determine VCF file reading pattern
  // current support:
  // * line by line ( including range selection)
  // * gene by gene
  // * range by range
  std::string rangeMode = "Single";
  if (FLAG_geneFile.size() && (FLAG_setFile.size() || FLAG_setList.size())) {
    logger->error("Cannot specify both gene file and set file.");
    exit(1);
  }

  if (!FLAG_gene.empty() && FLAG_geneFile.empty()) {
    logger->error("Please provide gene file for gene bases analysis.");
    exit(1);
  }
  OrderedMap<std::string, RangeList> geneRange;
  if (FLAG_geneFile.size()) {
    rangeMode = "Gene";
    int ret =
        loadGeneFile(FLAG_geneFile.c_str(), FLAG_gene.c_str(), &geneRange);
    if (ret < 0 || geneRange.size() == 0) {
      logger->error("Error loading gene file or gene list is empty!");
      return -1;
    } else {
      logger->info("Loaded [ %zu ] genes.", geneRange.size());
    }
  }

  if (!FLAG_set.empty() && FLAG_setFile.empty()) {
    logger->error("Please provide set file for set bases analysis.");
    exit(1);
  }
  if (FLAG_setFile.size()) {
    rangeMode = "Range";
    int ret = loadRangeFile(FLAG_setFile.c_str(), FLAG_set.c_str(), &geneRange);
    if (ret < 0 || geneRange.size() == 0) {
      logger->error("Error loading set file or set list is empty!");
      return -1;
    } else {
      logger->info("Loaded [ %zu ] set to tests.", geneRange.size());
    }
  }
  if (FLAG_setList.size()) {
    rangeMode = "Range";
    int ret = appendListToRange(FLAG_setList, &geneRange);
    if (ret < 0) {
      logger->error("Error loading set list or set list is empty!");
      return -1;
    }
  }

  DataConsolidator dc;
  dc.setSex(&dataLoader.getSex());
  dc.setFormula(&dataLoader.getFormula());
  dc.setGenotypeCounter(ge.getGenotypeCounter());

  // load kinshp if needed by family models
  if (modelManager.hasFamilyModel() ||
      (!FLAG_modelMeta.empty() && !FLAG_kinship.empty())) {
    logger->info("Family-based model specified. Loading kinship file...");

    // process auto kinship
    if (dc.setKinshipSample(dataLoader.getPhenotype().getRowName()) ||
        dc.setKinshipFile(DataConsolidator::KINSHIP_AUTO, FLAG_kinship) ||
        dc.setKinshipEigenFile(DataConsolidator::KINSHIP_AUTO,
                               FLAG_kinshipEigen) ||
        dc.loadKinship(DataConsolidator::KINSHIP_AUTO)) {
      logger->error(
          "Failed to load autosomal kinship (you may use vcf2kinship to "
          "generate one).");
      exit(1);
    }

    if (dc.setKinshipFile(DataConsolidator::KINSHIP_X, FLAG_xHemiKinship) ||
        dc.setKinshipEigenFile(DataConsolidator::KINSHIP_X,
                               FLAG_xHemiKinshipEigen) ||
        dc.loadKinship(DataConsolidator::KINSHIP_X)) {
      logger->warn(
          "Autosomal kinship loaded, but no hemizygote region kinship "
          "provided, some sex chromosome tests will be skipped.");
      // keep the program going
    }
  } else if (!FLAG_kinship.empty() && FLAG_modelMeta.empty()) {
    logger->info(
        "Family-based model not specified. Options related to kinship will be "
        "ignored here.");
  }

  // set imputation method
  if (FLAG_impute.empty()) {
    logger->info("Impute missing genotype to mean (by default)");
    dc.setStrategy(DataConsolidator::IMPUTE_MEAN);
  } else if (FLAG_impute == "mean") {
    logger->info("Impute missing genotype to mean");
    dc.setStrategy(DataConsolidator::IMPUTE_MEAN);
  } else if (FLAG_impute == "hwe") {
    logger->info("Impute missing genotype by HWE");
    dc.setStrategy(DataConsolidator::IMPUTE_HWE);
  } else if (FLAG_impute == "drop") {
    logger->info("Drop missing genotypes");
    dc.setStrategy(DataConsolidator::DROP);
  }
  dc.setPhenotypeName(dataLoader.getPhenotype().getRowName());

  // set up par region
  ParRegion parRegion(FLAG_xLabel, FLAG_xParRegion);
  dc.setParRegion(&parRegion);

  // genotype will be extracted and stored
  Matrix& genotype = dc.getOriginalGenotype();
  if (FLAG_freqUpper > 0) {
    ge.setSiteFreqMax(FLAG_freqUpper);
    logger->info("Set upper minor allele frequency limit to %g",
                 FLAG_freqUpper);
  }
  if (FLAG_freqLower > 0) {
    ge.setSiteFreqMin(FLAG_freqLower);
    logger->info("Set lower minor allele frequency limit to %g",
                 FLAG_freqLower);
  }

  // handle sex chromosome
  ge.setParRegion(&parRegion);
  ge.setSex(&dataLoader.getSex());

  // use dosage instead GT
  if (!FLAG_dosageTag.empty()) {
    ge.setDosageTag(FLAG_dosageTag);
    logger->info("Use dosage genotype from VCF flag %s.",
                 FLAG_dosageTag.c_str());
  }

  // genotype QC options
  if (FLAG_indvDepthMin > 0) {
    ge.setGDmin(FLAG_indvDepthMin);
    logger->info("Minimum GD set to %d (or marked as missing genotype).",
                 FLAG_indvDepthMin);
  }
  if (FLAG_indvDepthMax > 0) {
    ge.setGDmax(FLAG_indvDepthMax);
    logger->info("Maximum GD set to %d (or marked as missing genotype).",
                 FLAG_indvDepthMax);
  }
  if (FLAG_indvQualMin > 0) {
    ge.setGQmin(FLAG_indvQualMin);
    logger->info("Minimum GQ set to %d (or marked as missing genotype).",
                 FLAG_indvQualMin);
  }

  dc.preRegressionCheck(phenotypeMatrix, covariate);

  logger->info("Analysis started");
  Result& buf = dc.getResult();

  // we have three modes:
  // * single variant reading, single variant test
  // * range variant reading, single variant test
  // * range variant reading, group variant test
  if (rangeMode == "Single" && singleVariantMode) {  // use line by line mode
    buf.addHeader("CHROM");
    buf.addHeader("POS");
    buf.addHeader("REF");
    buf.addHeader("ALT");
    buf.addHeader("N_INFORMATIVE");

    // output headers
    for (size_t m = 0; m < model.size(); m++) {
      model[m]->writeHeader(fOuts[m], buf);
    }

    int variantProcessed = 0;
    while (true) {
      buf.clearValue();
      int ret = ge.extractSingleGenotype(&genotype, &buf);

      if (ret == GenotypeExtractor::FILE_END) {  // reach file end
        break;
      }
      if (ret == GenotypeExtractor::FAIL_FILTER) {
        continue;
      }
      if (ret != GenotypeExtractor::SUCCEED) {
        logger->error("Extract genotype failed at site: %s:%s!",
                      buf["CHROM"].c_str(), buf["POS"].c_str());
        continue;
      }
      if (genotype.cols == 0) {
        logger->warn("Extract [ %s:%s ] has 0 variants, skipping",
                     buf["CHROM"].c_str(), buf["POS"].c_str());
        continue;
      }

      ++variantProcessed;
      dc.consolidate(phenotypeMatrix, covariate, genotype);

      buf.updateValue("N_INFORMATIVE", toString(genotype.rows));

      // fit each model
      for (size_t m = 0; m != numModel; m++) {
        model[m]->reset();
        model[m]->fit(&dc);
        model[m]->writeOutput(fOuts[m], buf);
      }
    }
    logger->info("Analyzed [ %d ] variants", variantProcessed);
  } else if (rangeMode != "Single" &&
             singleVariantMode) {  // read by gene/range model, single variant
    // test
    buf.addHeader(rangeMode);
    buf.addHeader("CHROM");
    buf.addHeader("POS");
    buf.addHeader("REF");
    buf.addHeader("ALT");
    buf.addHeader("N_INFORMATIVE");

    // output headers
    for (size_t m = 0; m < numModel; m++) {
      model[m]->writeHeader(fOuts[m], buf);
    }
    std::string geneName;
    RangeList rangeList;
    int variantProcessed = 0;
    for (size_t i = 0; i < geneRange.size(); ++i) {
      geneRange.at(i, &geneName, &rangeList);
      ge.setRange(rangeList);

      while (true) {
        buf.clearValue();
        int ret = ge.extractSingleGenotype(&genotype, &buf);
        if (ret == GenotypeExtractor::FILE_END) {  // reach end of this region
          break;
        }
        if (ret == GenotypeExtractor::FAIL_FILTER) {
          continue;
        }
        if (ret != GenotypeExtractor::SUCCEED) {
          logger->error("Extract genotype failed for gene %s!",
                        geneName.c_str());
          continue;
        }
        if (genotype.cols == 0) {
          logger->warn("Gene %s has 0 variants, skipping", geneName.c_str());
          continue;
        }

        ++variantProcessed;
        dc.consolidate(phenotypeMatrix, covariate, genotype);

        buf.updateValue(rangeMode, geneName);
        buf.updateValue("N_INFORMATIVE", genotype.rows);

        // #pragma omp parallel for
        for (size_t m = 0; m != numModel; m++) {
          model[m]->reset();
          model[m]->fit(&dc);
          model[m]->writeOutput(fOuts[m], buf);
        }
      }
    }
    logger->info("Analyzed [ %d ] variants from [ %d ] genes/regions",
                 variantProcessed, (int)geneRange.size());
  } else if (rangeMode != "Single" &&
             groupVariantMode) {  // read by gene/range mode, group variant
                                  // test
    buf.addHeader(rangeMode);
    buf.addHeader("RANGE");
    buf.addHeader("N_INFORMATIVE");
    buf.addHeader("NumVar");
    buf.addHeader("NumPolyVar");

    // output headers
    for (size_t m = 0; m < numModel; m++) {
      model[m]->writeHeader(fOuts[m], buf);
    }
    std::string geneName;
    RangeList rangeList;
    int variantProcessed = 0;
    ge.enableAutoMerge();
    for (size_t i = 0; i < geneRange.size(); ++i) {
      geneRange.at(i, &geneName, &rangeList);
      ge.setRange(rangeList);

      buf.clearValue();
      int ret = ge.extractMultipleGenotype(&genotype);

      if (ret != GenotypeExtractor::SUCCEED) {
        logger->error("Extract genotype failed for gene %s!", geneName.c_str());
        continue;
      }
      if (genotype.cols == 0) {
        logger->info("Gene %s has 0 variants, skipping", geneName.c_str());
        continue;
      }

      variantProcessed += genotype.cols;  // genotype is people by marker
      dc.consolidate(phenotypeMatrix, covariate, genotype);

      buf.updateValue(rangeMode, geneName);
      buf.updateValue("RANGE", rangeList.toString());
      buf.updateValue("N_INFORMATIVE", genotype.rows);
      buf.updateValue("NumVar", genotype.cols);
      buf.updateValue("NumPolyVar",
                      dc.getFlippedToMinorPolymorphicGenotype().cols);

      // #ifdef _OPENMP
      // #pragma omp parallel for
      // #endif
      for (size_t m = 0; m != numModel; m++) {
        model[m]->reset();
        model[m]->fit(&dc);
        model[m]->writeOutput(fOuts[m], buf);
      }
    }
    logger->info("Analyzed [ %d ] variants from [ %d ] genes/regions",
                 variantProcessed, (int)geneRange.size());
  } else {
    logger->error(
        "Unsupported reading mode and test modes! (need more parameters?)");
    exit(1);
  }

  // Resource cleaning up
  modelManager.close();
  delete g_SummaryHeader;

  time_t endTime = time(0);
  logger->info("Analysis ends at: %s", currentTime().c_str());
  int elapsedSecond = (int)(endTime - startTime);
  logger->info("Analysis took %d seconds", elapsedSecond);

  return 0;
}
Beispiel #24
0
int main(int argc, char** argv) {
  PARSE_PARAMETER(argc, argv);

  if (FLAG_help) {
    PARAMETER_HELP();
    return 0;
  }

  welcome();
  PARAMETER_STATUS();
  if (FLAG_REMAIN_ARG.size() > 0) {
    fprintf(stderr, "Unparsed arguments: ");
    for (unsigned int i = 0; i < FLAG_REMAIN_ARG.size(); i++) {
      fprintf(stderr, " %s", FLAG_REMAIN_ARG[i].c_str());
    }
    exit(1);
  }

  if (!FLAG_outPrefix.size()) FLAG_outPrefix = "rvtest";

  if ((FLAG_inVcf.empty() ? 0 : 1) + (FLAG_inBgen.empty() ? 0 : 1) +
          (FLAG_inKgg.empty() ? 0 : 1) !=
      1) {
    fprintf(stderr,
            "Please provide one type of input file using: --inVcf, --inBgen or "
            "--inKgg\n");
    exit(1);
  }

  // check new version
  if (!FLAG_noweb) {
    VersionChecker ver;
    if (ver.retrieveRemoteVersion("http://zhanxw.com/rvtests/version") < 0) {
      fprintf(stderr,
              "Retrieve remote version failed, use '--noweb' to skip.\n");
    } else {
      ver.setLocalVersion(VERSION);
      if (ver.isRemoteVersionNewer()) {
        fprintf(stderr, "New version of rvtests is available:");
        ver.printRemoteContent();
      }
    }
  }

  // start logging
  Logger _logger((FLAG_outPrefix + ".log").c_str());
  logger = &_logger;
  logger->info("Program version: %s", VERSION);
  logger->infoToFile("Git Version: %s", GIT_VERSION);
  logger->infoToFile("Parameters BEGIN");
  PARAMETER_INSTANCE().WriteToFile(logger->getHandle());
  logger->infoToFile("Parameters END");
  logger->sync();

// set up multithreading
#ifdef _OPENMP
  if (FLAG_numThread <= 0) {
    fprintf(stderr, "Invalid number of threads [ %d ], reset to single thread",
            FLAG_numThread);
    omp_set_num_threads(1);
  } else if (FLAG_numThread > omp_get_max_threads()) {
    int maxThreads = omp_get_max_threads();
    fprintf(stderr,
            "Reduced your specified number of threads to the maximum of system "
            "limit [ %d ]",
            maxThreads);
    omp_set_num_threads(maxThreads);
  } else if (FLAG_numThread == 1) {
    // need to set to one thread, otherwise all CPUs may be used
    omp_set_num_threads(1);
  } else {
    logger->info("Set number of threads = [ %d ]", FLAG_numThread);
    omp_set_num_threads(FLAG_numThread);
  }
#endif

  // start analysis
  time_t startTime = time(0);
  logger->info("Analysis started at: %s", currentTime().c_str());

  GenotypeExtractor* ge = NULL;
  if (!FLAG_inVcf.empty()) {
    ge = new VCFGenotypeExtractor(FLAG_inVcf);
  } else if (!FLAG_inBgen.empty()) {
    ge = new BGenGenotypeExtractor(FLAG_inBgen, FLAG_inBgenSample);
  } else if (!FLAG_inKgg.empty()) {
    ge = new KGGGenotypeExtractor(FLAG_inKgg);
  } else {
    assert(false);
  }

  // set range filters here
  ge->setRangeList(FLAG_rangeList.c_str());
  ge->setRangeFile(FLAG_rangeFile.c_str());

  // set people filters here
  if (FLAG_peopleIncludeID.size() || FLAG_peopleIncludeFile.size()) {
    ge->excludeAllPeople();
    ge->includePeople(FLAG_peopleIncludeID.c_str());
    ge->includePeopleFromFile(FLAG_peopleIncludeFile.c_str());
  }
  ge->excludePeople(FLAG_peopleExcludeID.c_str());
  ge->excludePeopleFromFile(FLAG_peopleExcludeFile.c_str());

  if (!FLAG_siteFile.empty()) {
    ge->setSiteFile(FLAG_siteFile);
    logger->info("Restrict analysis based on specified site file [ %s ]",
                 FLAG_siteFile.c_str());
  }
  if (FLAG_siteDepthMin > 0) {
    ge->setSiteDepthMin(FLAG_siteDepthMin);
    logger->info("Set site depth minimum to %d", FLAG_siteDepthMin);
  }
  if (FLAG_siteDepthMax > 0) {
    ge->setSiteDepthMax(FLAG_siteDepthMax);
    logger->info("Set site depth maximum to %d", FLAG_siteDepthMax);
  }
  if (FLAG_siteMACMin > 0) {
    ge->setSiteMACMin(FLAG_siteMACMin);
    logger->info("Set site minimum MAC to %d", FLAG_siteDepthMin);
  }
  if (FLAG_annoType != "") {
    ge->setAnnoType(FLAG_annoType.c_str());
    logger->info("Set annotype type filter to %s", FLAG_annoType.c_str());
  }

  std::vector<std::string> vcfSampleNames;
  ge->getPeopleName(&vcfSampleNames);
  logger->info("Loaded [ %zu ] samples from genotype files",
               vcfSampleNames.size());

  DataLoader dataLoader;
  dataLoader.setPhenotypeImputation(FLAG_imputePheno);
  dataLoader.setCovariateImputation(FLAG_imputeCov);

  if (FLAG_multiplePheno.empty()) {
    dataLoader.loadPhenotype(FLAG_pheno, FLAG_mpheno, FLAG_phenoName);
    // // load phenotypes
    // std::map<std::string, double> phenotype;
    // if (FLAG_pheno.empty()) {
    //   logger->error("Cannot do association when phenotype is missing!");
    //   return -1;
    // }

    // // check if alternative phenotype columns are used
    // if (!FLAG_mpheno.empty() && !FLAG_phenoName.empty()) {
    //   logger->error("Please specify either --mpheno or --pheno-name");
    //   return -1;
    // }
    // if (!FLAG_mpheno.empty()) {
    //   int col = atoi(FLAG_mpheno);
    //   int ret = loadPedPhenotypeByColumn(FLAG_pheno.c_str(), &phenotype,
    //   col);
    //   if (ret < 0) {
    //     logger->error("Loading phenotype failed!");
    //     return -1;
    //   }
    // } else if (!FLAG_phenoName.empty()) {
    //   int ret = loadPedPhenotypeByHeader(FLAG_pheno.c_str(), &phenotype,
    //                                      FLAG_phenoName.c_str());
    //   if (ret < 0) {
    //     logger->error("Loading phenotype failed!");
    //     return -1;
    //   }
    // } else {
    //   int col = 1;  // default use the first phenotype
    //   int ret = loadPedPhenotypeByColumn(FLAG_pheno.c_str(), &phenotype,
    //   col);
    //   if (ret < 0) {
    //     logger->error("Loading phenotype failed!");
    //     return -1;
    //   }
    // }
    // logger->info("Loaded [ %zu ] sample phenotypes.", phenotype.size());

    // rearrange phenotypes
    // drop samples from phenotype or vcf
    matchPhenotypeAndVCF("missing phenotype", &dataLoader, ge);
    // // phenotype names (vcf sample names) arranged in the same order as in
    // VCF
    // std::vector<std::string> phenotypeNameInOrder;
    // std::vector<double>
    //     phenotypeInOrder;  // phenotype arranged in the same order as in VCF
    // rearrange(phenotype, vcfSampleNames, &vcfSampleToDrop,
    // &phenotypeNameInOrder,
    //           &phenotypeInOrder, FLAG_imputePheno);
    // if (vcfSampleToDrop.size()) {
    //   // exclude this sample from parsing VCF
    //   ge->excludePeople(vcfSampleToDrop);
    //   // output dropped samples
    //   for (size_t i = 0; i < vcfSampleToDrop.size(); ++i) {
    //     if (i == 0)
    //       logger->warn(
    //           "Total [ %zu ] samples are dropped from VCF file due to missing
    //           "
    //           "phenotype",
    //           vcfSampleToDrop.size());
    //     if (i >= 10) {
    //       logger->warn(
    //           "Skip outputting additional [ %d ] samples with missing "
    //           "phenotypes.",
    //           ((int)vcfSampleToDrop.size() - 10));
    //       break;
    //     }
    //     logger->warn("Drop sample [ %s ] from VCF file due to missing
    //     phenotype",
    //                  (vcfSampleToDrop)[i].c_str());
    //   }
    //   // logger->warn("Drop %zu sample from VCF file since we don't have
    //   their
    //   // phenotypes", vcfSampleToDrop.size());
    // }
    // if (phenotypeInOrder.size() != phenotype.size()) {
    //   logger->warn(
    //       "Drop [ %d ] samples from phenotype file due to missing genotypes
    //       from "
    //       "VCF files",
    //       (int)(phenotype.size() - phenotypeInOrder.size()));
    //   // We may output these samples by comparing keys of phenotype and
    //   // phenotypeNameInOrder
    // }
    dataLoader.loadCovariate(FLAG_cov, FLAG_covName);
    matchCovariateAndVCF("missing covariate", &dataLoader, ge);
    // // load covariate
    // Matrix covariate;
    // HandleMissingCov handleMissingCov = COVARIATE_DROP;
    // if (FLAG_imputeCov) {
    //   handleMissingCov = COVARIATE_IMPUTE;
    // }
    // if (FLAG_cov.empty() && !FLAG_covName.empty()) {
    //   logger->info("Use phenotype file as covariate file [ %s ]",
    //                FLAG_pheno.c_str());
    //   FLAG_cov = FLAG_pheno;
    // }
    // if (!FLAG_cov.empty()) {
    //   logger->info("Begin to read covariate file.");
    //   std::vector<std::string> columnNamesInCovariate;
    //   std::set<std::string> sampleToDropInCovariate;
    //   int ret = loadCovariate(FLAG_cov.c_str(), phenotypeNameInOrder,
    //                           FLAG_covName.c_str(), handleMissingCov,
    //                           &covariate,
    //                           &columnNamesInCovariate,
    //                           &sampleToDropInCovariate);
    //   if (ret < 0) {
    //     logger->error("Load covariate file failed !");
    //     exit(1);
    //   }

    //   // drop phenotype samples
    //   if (!sampleToDropInCovariate.empty()) {
    //     int idx = 0;
    //     int n = phenotypeNameInOrder.size();
    //     for (int i = 0; i < n; ++i) {
    //       if (sampleToDropInCovariate.count(phenotypeNameInOrder[i]) !=
    //           0) {  // need to drop
    //         continue;
    //       }
    //       phenotypeNameInOrder[idx] = phenotypeNameInOrder[i];
    //       phenotypeInOrder[idx] = phenotypeInOrder[i];
    //       idx++;
    //     }
    //     phenotypeNameInOrder.resize(idx);
    //     phenotypeInOrder.resize(idx);
    //     logger->warn(
    //         "[ %zu ] sample phenotypes are dropped due to lacking
    //         covariates.",
    //         sampleToDropInCovariate.size());
    //   }
    //   // drop vcf samples;
    //   for (std::set<std::string>::const_iterator iter =
    //            sampleToDropInCovariate.begin();
    //        iter != sampleToDropInCovariate.end(); ++iter) {
    //     ge->excludePeople(iter->c_str());
    //   }
    // }
  } else {
    dataLoader.loadMultiplePhenotype(FLAG_multiplePheno, FLAG_pheno, FLAG_cov);

    matchPhenotypeAndVCF("missing phenotype", &dataLoader, ge);
    matchCovariateAndVCF("missing covariate", &dataLoader, ge);
  }

  dataLoader.loadSex();
  if (FLAG_sex) {
    dataLoader.useSexAsCovariate();
    matchCovariateAndVCF("missing sex", &dataLoader, ge);
  }
  // // load sex
  // std::vector<int> sex;
  // if (loadSex(FLAG_pheno, phenotypeNameInOrder, &sex)) {
  //   logger->error("Cannot load sex of samples from phenotype file");
  //   exit(1);
  // }

  // if (FLAG_sex) {            // append sex in covariate
  //   std::vector<int> index;  // mark missing samples
  //   int numMissing = findMissingSex(sex, &index);
  //   logger->info("Futher exclude %d samples with missing sex", numMissing);
  //   removeByIndex(index, &sex);
  //   excludeSamplesByIndex(index, &ge, &phenotypeNameInOrder,
  //   &phenotypeInOrder,
  //                         &covariate);
  //   appendToMatrix("Sex", sex, &covariate);
  // }

  if (!FLAG_condition.empty()) {
    dataLoader.loadMarkerAsCovariate(FLAG_inVcf, FLAG_condition);
    matchCovariateAndVCF("missing in conditioned marker(s)", &dataLoader, ge);
  }
  // // load conditional markers
  // if (!FLAG_condition.empty()) {
  //   Matrix geno;
  //   std::vector<std::string> rowLabel;
  //   if (loadMarkerFromVCF(FLAG_inVcf, FLAG_condition, &rowLabel, &geno) < 0)
  //   {
  //     logger->error("Load conditional markers [ %s ] from [ %s ] failed.",
  //                   FLAG_condition.c_str(), FLAG_inVcf.c_str());
  //     exit(1);
  //   }
  //   if (appendGenotype(&covariate, phenotypeNameInOrder, geno, rowLabel) < 0)
  //   {
  //     logger->error(
  //         "Failed to combine conditional markers [ %s ] from [ %s ] failed.",
  //         FLAG_condition.c_str(), FLAG_inVcf.c_str());
  //     exit(1);
  //   }
  // }

  dataLoader.checkConstantCovariate();
  // // check if some covariates are constant for all samples
  // // e.g. user may include covariate "1" in addition to intercept
  // //      in such case, we will give a fatal error
  // for (int i = 0; i < covariate.cols; ++i) {
  //   std::set<double> s;
  //   s.clear();
  //   for (int j = 0; j < covariate.rows; ++j) {
  //     s.insert(covariate(j,i));
  //   }
  //   if (s.size() == 1) {
  //     logger->error(
  //         "Covariate [ %s ] equals [ %g ] for all samples, cannot fit "
  //         "model...\n",
  //         covariate.GetColumnLabel(i), *s.begin());
  //     exit(1);
  //   }
  // }

  g_SummaryHeader = new SummaryHeader;
  g_SummaryHeader->recordCovariate(dataLoader.getCovariate());

  // record raw phenotype
  g_SummaryHeader->recordPhenotype("Trait",
                                   dataLoader.getPhenotype().extractCol(0));
  // adjust phenotype
  // bool binaryPhenotype;
  if (FLAG_qtl) {
    // binaryPhenotype = false;
    dataLoader.setTraitType(DataLoader::PHENOTYPE_QTL);
    logger->info("-- Force quantitative trait mode -- ");
  } else {
    if (dataLoader.detectPhenotypeType() == DataLoader::PHENOTYPE_BINARY) {
      logger->warn("-- Enabling binary phenotype mode -- ");
      dataLoader.setTraitType(DataLoader::PHENOTYPE_BINARY);

    } else {
      dataLoader.setTraitType(DataLoader::PHENOTYPE_QTL);
    }
    // binaryPhenotype = isBinaryPhenotype(phenotypeInOrder);
    // if (binaryPhenotype) {
    //   logger->warn("-- Enabling binary phenotype mode -- ");
    //   convertBinaryPhenotype(&phenotypeInOrder);
    // }
  }

  if (FLAG_useResidualAsPhenotype) {
    dataLoader.useResidualAsPhenotype();
    g_SummaryHeader->recordEstimation(dataLoader.getEstimation());
  }
  // // use residual as phenotype
  // if (FLAG_useResidualAsPhenotype) {
  //   if (binaryPhenotype) {
  //     logger->warn(
  //         "WARNING: Skip transforming binary phenotype, although you want to
  //         "
  //         "use residual as phenotype!");
  //   } else {
  //     if (covariate.cols > 0) {
  //       LinearRegression lr;
  //       Vector pheno;
  //       Matrix covAndInt;
  //       copy(phenotypeInOrder, &pheno);
  //       copyCovariateAndIntercept(covariate.rows, covariate, &covAndInt);
  //       if (!lr.FitLinearModel(covAndInt, pheno)) {
  //         logger->error(
  //             "Cannot fit model: [ phenotype ~ 1 + covariates ], now use the
  //             "
  //             "original phenotype");
  //       } else {
  //         const int n = lr.GetResiduals().Length();
  //         for (int i = 0; i < n; ++i) {
  //           phenotypeInOrder[i] = lr.GetResiduals()[i];
  //         }
  //         covariate.Dimension(0, 0);
  //         logger->info(
  //             "DONE: Fit model [ phenotype ~ 1 + covariates ] and model "
  //             "residuals will be used as responses.");
  //       }
  //     } else {  // no covaraites
  //       centerVector(&phenotypeInOrder);
  //       logger->info("DONE: Use residual as phenotype by centerng it");
  //     }
  //   }
  // }

  if (FLAG_inverseNormal) {
    dataLoader.inverseNormalizePhenotype();
    g_SummaryHeader->setInverseNormalize(FLAG_inverseNormal);
  }
  // // phenotype transformation
  // if (FLAG_inverseNormal) {
  //   if (binaryPhenotype) {
  //     logger->warn(
  //         "WARNING: Skip transforming binary phenotype, although you required
  //         "
  //         "inverse normalization!");
  //   } else {
  //     logger->info("Now applying inverse normalize transformation.");
  //     inverseNormalizeLikeMerlin(&phenotypeInOrder);
  //     g_SummaryHeader->setInverseNormalize(FLAG_inverseNormal);
  //     logger->info("DONE: inverse normalization transformation finished.");
  //   }
  // }

  g_SummaryHeader->recordPhenotype("AnalyzedTrait",
                                   dataLoader.getPhenotype().extractCol(0));

  if (dataLoader.getPhenotype().nrow() == 0) {
    logger->fatal("There are 0 samples with valid phenotypes, quitting...");
    exit(1);
  }
  // if (phenotypeInOrder.empty()) {
  //   logger->fatal("There are 0 samples with valid phenotypes, quitting...");
  //   exit(1);
  // }
  logger->info("Analysis begins with [ %d ] samples...",
               dataLoader.getPhenotype().nrow());
  //////////////////////////////////////////////////////////////////////////////
  // prepare each model
  bool singleVariantMode = FLAG_modelSingle.size() || FLAG_modelMeta.size();
  bool groupVariantMode = (FLAG_modelBurden.size() || FLAG_modelVT.size() ||
                           FLAG_modelKernel.size());
  if (singleVariantMode && groupVariantMode) {
    logger->error("Cannot support both single variant and region based tests");
    exit(1);
  }

  ModelManager modelManager(FLAG_outPrefix);
  // set up models in qtl/binary modes
  if (dataLoader.isBinaryPhenotype()) {
    modelManager.setBinaryOutcome();
    matchPhenotypeAndVCF("missing phenotype (not case/control)", &dataLoader,
                         ge);
  } else {
    modelManager.setQuantitativeOutcome();
  }
  // create models
  modelManager.create("single", FLAG_modelSingle);
  modelManager.create("burden", FLAG_modelBurden);
  modelManager.create("vt", FLAG_modelVT);
  modelManager.create("kernel", FLAG_modelKernel);
  modelManager.create("meta", FLAG_modelMeta);
  if (FLAG_outputRaw) {
    modelManager.create("outputRaw", "dump");
  }

  const std::vector<ModelFitter*>& model = modelManager.getModel();
  const std::vector<FileWriter*>& fOuts = modelManager.getResultFile();
  const size_t numModel = model.size();

  // TODO: optimize this to avoid data copying
  Matrix phenotypeMatrix;
  Matrix covariate;
  toMatrix(dataLoader.getPhenotype(), &phenotypeMatrix);
  toMatrix(dataLoader.getCovariate(), &covariate);

  // determine VCF file reading pattern
  // current support:
  // * line by line ( including range selection)
  // * gene by gene
  // * range by range
  std::string rangeMode = "Single";
  if (FLAG_geneFile.size() && (FLAG_setFile.size() || FLAG_setList.size())) {
    logger->error("Cannot specify both gene file and set file.");
    exit(1);
  }

  if (!FLAG_gene.empty() && FLAG_geneFile.empty()) {
    logger->error("Please provide gene file for gene bases analysis.");
    exit(1);
  }
  OrderedMap<std::string, RangeList> geneRange;
  if (FLAG_geneFile.size()) {
    rangeMode = "Gene";
    int ret =
        loadGeneFile(FLAG_geneFile.c_str(), FLAG_gene.c_str(), &geneRange);
    if (ret < 0 || geneRange.size() == 0) {
      logger->error("Error loading gene file or gene list is empty!");
      return -1;
    } else {
      logger->info("Loaded [ %zu ] genes.", geneRange.size());
    }
  }

  if (!FLAG_set.empty() && FLAG_setFile.empty()) {
    logger->error("Please provide set file for set bases analysis.");
    exit(1);
  }
  if (FLAG_setFile.size()) {
    rangeMode = "Range";
    int ret = loadRangeFile(FLAG_setFile.c_str(), FLAG_set.c_str(), &geneRange);
    if (ret < 0 || geneRange.size() == 0) {
      logger->error("Error loading set file or set list is empty!");
      return -1;
    } else {
      logger->info("Loaded [ %zu ] set to tests.", geneRange.size());
    }
  }
  if (FLAG_setList.size()) {
    rangeMode = "Range";
    int ret = appendListToRange(FLAG_setList, &geneRange);
    if (ret < 0) {
      logger->error("Error loading set list or set list is empty!");
      return -1;
    }
  }

  DataConsolidator dc;
  dc.setSex(&dataLoader.getSex());
  dc.setFormula(&dataLoader.getFormula());
  dc.setGenotypeCounter(ge->getGenotypeCounter());

  // load kinshp if needed by family models
  if (modelManager.hasFamilyModel() ||
      (!FLAG_modelMeta.empty() && !FLAG_kinship.empty())) {
    logger->info("Family-based model specified. Loading kinship file...");

    // process auto kinship
    if (dc.setKinshipSample(dataLoader.getPhenotype().getRowName()) ||
        dc.setKinshipFile(DataConsolidator::KINSHIP_AUTO, FLAG_kinship) ||
        dc.setKinshipEigenFile(DataConsolidator::KINSHIP_AUTO,
                               FLAG_kinshipEigen) ||
        dc.loadKinship(DataConsolidator::KINSHIP_AUTO)) {
      logger->error(
          "Failed to load autosomal kinship (you may use vcf2kinship to "
          "generate one).");
      exit(1);
    }

    if (dc.setKinshipFile(DataConsolidator::KINSHIP_X, FLAG_xHemiKinship) ||
        dc.setKinshipEigenFile(DataConsolidator::KINSHIP_X,
                               FLAG_xHemiKinshipEigen) ||
        dc.loadKinship(DataConsolidator::KINSHIP_X)) {
      logger->warn(
          "Autosomal kinship loaded, but no hemizygote region kinship "
          "provided, some sex chromosome tests will be skipped.");
      // keep the program going
    }
  } else if (!FLAG_kinship.empty() && FLAG_modelMeta.empty()) {
    logger->info(
        "Family-based model not specified. Options related to kinship will be "
        "ignored here.");
  }

  // set imputation method
  if (FLAG_impute.empty()) {
    logger->info("Impute missing genotype to mean (by default)");
    dc.setStrategy(DataConsolidator::IMPUTE_MEAN);
  } else if (FLAG_impute == "mean") {
    logger->info("Impute missing genotype to mean");
    dc.setStrategy(DataConsolidator::IMPUTE_MEAN);
  } else if (FLAG_impute == "hwe") {
    logger->info("Impute missing genotype by HWE");
    dc.setStrategy(DataConsolidator::IMPUTE_HWE);
  } else if (FLAG_impute == "drop") {
    logger->info("Drop missing genotypes");
    dc.setStrategy(DataConsolidator::DROP);
  }
  dc.setPhenotypeName(dataLoader.getPhenotype().getRowName());

  // set up par region
  ParRegion parRegion(FLAG_xLabel, FLAG_xParRegion);
  dc.setParRegion(&parRegion);

  // genotype will be extracted and stored
  if (FLAG_freqUpper > 0) {
    ge->setSiteFreqMax(FLAG_freqUpper);
    logger->info("Set upper minor allele frequency limit to %g",
                 FLAG_freqUpper);
  }
  if (FLAG_freqLower > 0) {
    ge->setSiteFreqMin(FLAG_freqLower);
    logger->info("Set lower minor allele frequency limit to %g",
                 FLAG_freqLower);
  }

  // handle sex chromosome
  ge->setParRegion(&parRegion);
  ge->setSex(&dataLoader.getSex());

  // use dosage instead GT
  if (!FLAG_dosageTag.empty()) {
    ge->setDosageTag(FLAG_dosageTag);
    logger->info("Use dosage genotype from VCF flag %s.",
                 FLAG_dosageTag.c_str());
  }

  // multi-allelic sites will be treats as ref/alt1, ref/alt2, ref/alt3..
  // instead of ref/alt1 (biallelic)
  if (FLAG_multiAllele) {
    ge->enableMultiAllelicMode();
    logger->info("Enable analysis using multiple allelic models");
  }

  // genotype QC options
  if (FLAG_indvDepthMin > 0) {
    ge->setGDmin(FLAG_indvDepthMin);
    logger->info("Minimum GD set to %d (or marked as missing genotype).",
                 FLAG_indvDepthMin);
  }
  if (FLAG_indvDepthMax > 0) {
    ge->setGDmax(FLAG_indvDepthMax);
    logger->info("Maximum GD set to %d (or marked as missing genotype).",
                 FLAG_indvDepthMax);
  }
  if (FLAG_indvQualMin > 0) {
    ge->setGQmin(FLAG_indvQualMin);
    logger->info("Minimum GQ set to %d (or marked as missing genotype).",
                 FLAG_indvQualMin);
  }

  // e.g. check colinearity and correlations between predictors
  dc.preRegressionCheck(phenotypeMatrix, covariate);

  // prepare PLINK files for BoltLMM model
  if (!FLAG_boltPlink.empty()) {
    if (dc.prepareBoltModel(FLAG_boltPlink,
                            dataLoader.getPhenotype().getRowName(),
                            dataLoader.getPhenotype())) {
      logger->error(
          "Failed to prepare inputs for BOLT-LMM association test model with "
          "this prefix [ %s ]!",
          FLAG_boltPlink.c_str());
      exit(1);
    }
  }

  logger->info("Analysis started");
  Result& buf = dc.getResult();
  Matrix& genotype = dc.getOriginalGenotype();

  // we have three modes:
  // * single variant reading, single variant test
  // * range variant reading, single variant test
  // * range variant reading, group variant test
  if (rangeMode == "Single" && singleVariantMode) {  // use line by line mode
    buf.addHeader("CHROM");
    buf.addHeader("POS");
    if (FLAG_outputID) {
      buf.addHeader("ID");
    }
    buf.addHeader("REF");
    buf.addHeader("ALT");
    buf.addHeader("N_INFORMATIVE");

    // output headers
    for (size_t m = 0; m < model.size(); m++) {
      model[m]->writeHeader(fOuts[m], buf);
    }

    int variantProcessed = 0;
    while (true) {
      buf.clearValue();
      int ret = ge->extractSingleGenotype(&genotype, &buf);

      if (ret == GenotypeExtractor::FILE_END) {  // reach file end
        break;
      }
      if (ret == GenotypeExtractor::FAIL_FILTER) {
        continue;
      }
      if (ret != GenotypeExtractor::SUCCEED) {
        logger->error("Extract genotype failed at site: %s:%s!",
                      buf["CHROM"].c_str(), buf["POS"].c_str());
        continue;
      }
      if (genotype.cols == 0) {
        logger->warn("Extract [ %s:%s ] has 0 variants, skipping",
                     buf["CHROM"].c_str(), buf["POS"].c_str());
        continue;
      }

      ++variantProcessed;
      dc.consolidate(phenotypeMatrix, covariate, genotype);

      buf.updateValue("N_INFORMATIVE", toString(genotype.rows));

      // logger->info("Test variant at site: %s:%s!",
      //               buf["CHROM"].c_str(), buf["POS"].c_str());

      // fit each model
      for (size_t m = 0; m != numModel; m++) {
        model[m]->reset();
        model[m]->fit(&dc);
        model[m]->writeOutput(fOuts[m], buf);
      }
    }
    logger->info("Analyzed [ %d ] variants", variantProcessed);
  } else if (rangeMode != "Single" &&
             singleVariantMode) {  // read by gene/range model, single variant
    // test
    buf.addHeader(rangeMode);
    buf.addHeader("CHROM");
    buf.addHeader("POS");
    if (FLAG_outputID) {
      buf.addHeader("ID");
    }
    buf.addHeader("REF");
    buf.addHeader("ALT");
    buf.addHeader("N_INFORMATIVE");

    // output headers
    for (size_t m = 0; m < numModel; m++) {
      model[m]->writeHeader(fOuts[m], buf);
    }
    std::string geneName;
    RangeList rangeList;
    int variantProcessed = 0;
    for (size_t i = 0; i < geneRange.size(); ++i) {
      geneRange.at(i, &geneName, &rangeList);
      ge->setRange(rangeList);

      while (true) {
        buf.clearValue();
        int ret = ge->extractSingleGenotype(&genotype, &buf);
        if (ret == GenotypeExtractor::FILE_END) {  // reach end of this region
          break;
        }
        if (ret == GenotypeExtractor::FAIL_FILTER) {
          continue;
        }
        if (ret != GenotypeExtractor::SUCCEED) {
          logger->error("Extract genotype failed for gene %s!",
                        geneName.c_str());
          continue;
        }
        if (genotype.cols == 0) {
          logger->warn("Gene %s has 0 variants, skipping", geneName.c_str());
          continue;
        }

        ++variantProcessed;
        dc.consolidate(phenotypeMatrix, covariate, genotype);

        buf.updateValue(rangeMode, geneName);
        buf.updateValue("N_INFORMATIVE", genotype.rows);

        // #pragma omp parallel for
        for (size_t m = 0; m != numModel; m++) {
          model[m]->reset();
          model[m]->fit(&dc);
          model[m]->writeOutput(fOuts[m], buf);
        }
      }
    }
    logger->info("Analyzed [ %d ] variants from [ %d ] genes/regions",
                 variantProcessed, (int)geneRange.size());
  } else if (rangeMode != "Single" &&
             groupVariantMode) {  // read by gene/range mode, group variant
                                  // test
    buf.addHeader(rangeMode);
    buf.addHeader("RANGE");
    buf.addHeader("N_INFORMATIVE");
    buf.addHeader("NumVar");
    buf.addHeader("NumPolyVar");

    // output headers
    for (size_t m = 0; m < numModel; m++) {
      model[m]->writeHeader(fOuts[m], buf);
    }
    std::string geneName;
    RangeList rangeList;
    int variantProcessed = 0;
    ge->enableAutoMerge();
    for (size_t i = 0; i < geneRange.size(); ++i) {
      geneRange.at(i, &geneName, &rangeList);
      ge->setRange(rangeList);
      buf.clearValue();
      int ret = ge->extractMultipleGenotype(&genotype);

      if (ret != GenotypeExtractor::SUCCEED) {
        logger->error("Extract genotype failed for gene %s!", geneName.c_str());
        continue;
      }
      if (genotype.cols == 0) {
        logger->info("Gene %s has 0 variants, skipping", geneName.c_str());
        continue;
      }

      variantProcessed += genotype.cols;  // genotype is people by marker
      dc.consolidate(phenotypeMatrix, covariate, genotype);

      buf.updateValue(rangeMode, geneName);
      buf.updateValue("RANGE", rangeList.toString());
      buf.updateValue("N_INFORMATIVE", genotype.rows);
      buf.updateValue("NumVar", genotype.cols);
      buf.updateValue("NumPolyVar",
                      dc.getFlippedToMinorPolymorphicGenotype().cols);

      // #ifdef _OPENMP
      // #pragma omp parallel for
      // #endif
      for (size_t m = 0; m != numModel; m++) {
        model[m]->reset();
        model[m]->fit(&dc);
        model[m]->writeOutput(fOuts[m], buf);
      }
    }
    logger->info("Analyzed [ %d ] variants from [ %d ] genes/regions",
                 variantProcessed, (int)geneRange.size());
  } else {
    logger->error(
        "Unsupported reading mode and test modes! (need more parameters?)");
    exit(1);
  }

  // Resource cleaning up
  modelManager.close();
  delete g_SummaryHeader;

  time_t endTime = time(0);
  logger->info("Analysis ends at: %s", currentTime().c_str());
  int elapsedSecond = (int)(endTime - startTime);
  logger->info("Analysis took %d seconds", elapsedSecond);

  fputs("RVTESTS finished successfully\n", stdout);
  return 0;
}
void
FormWindow::CreateDefList(CtrlDef& def)
{
	ListBox* ctrl = CreateListBox(def.GetText(),
	def.GetX(),
	def.GetY(),
	def.GetW(),
	def.GetH(),
	def.GetID(),
	def.GetParentID());

	ctrl->SetAltText(def.GetAltText());
	ctrl->SetEnabled(def.IsEnabled());
	ctrl->SetBackColor(def.GetBackColor());
	ctrl->SetForeColor(def.GetForeColor());
	ctrl->SetStyle(def.GetStyle());
	ctrl->SetTextAlign(def.GetTextAlign());
	ctrl->SetTransparent(def.GetTransparent());
	ctrl->SetHidePartial(def.GetHidePartial());

	ctrl->SetLineHeight(def.GetLineHeight());
	ctrl->SetShowHeadings(def.GetShowHeadings());
	ctrl->SetLeading(def.GetLeading());
	ctrl->SetMultiSelect(def.GetMultiSelect());
	ctrl->SetDragDrop(def.GetDragDrop());
	ctrl->SetScrollBarVisible(def.GetScrollBarVisible());
	ctrl->SetSmoothScroll(def.GetSmoothScroll());
	ctrl->SetItemStyle(def.GetItemStyle());
	ctrl->SetSelectedStyle(def.GetSelectedStyle());

	ctrl->SetMargins(def.GetMargins());
	ctrl->SetTextInsets(def.GetTextInsets());
	ctrl->SetCellInsets(def.GetCellInsets());
	ctrl->SetCells(def.GetCells());
	ctrl->SetFixedWidth(def.GetFixedWidth());
	ctrl->SetFixedHeight(def.GetFixedHeight());

	if (def.GetTexture().length() > 0) {
		Bitmap*     ctrl_tex = 0;
		DataLoader* loader   = DataLoader::GetLoader();
		loader->SetDataPath("Screens/");
		loader->LoadTexture(def.GetTexture(), ctrl_tex);
		loader->SetDataPath("");

		ctrl->SetTexture(ctrl_tex);
	}

	int ncols = def.NumColumns();
	for (int i = 0; i < ncols; i++) {
		ColumnDef* col = def.GetColumn(i);
		ctrl->AddColumn(col->title, col->width, col->align, col->sort);

		if (col->use_color)
		ctrl->SetColumnColor(i, col->color);
	}

	int nitems = def.NumItems();
	for (int i = 0; i < nitems; i++)
	ctrl->AddItem(def.GetItem(i));

	Font* f = FontMgr::Find(def.GetFont());
	if (f) ctrl->SetFont(f);
}
void
FormWindow::CreateDefButton(CtrlDef& def)
{
	Button* ctrl = CreateButton(def.GetText(),
	def.GetX(),
	def.GetY(),
	def.GetW(),
	def.GetH(),
	def.GetID(),
	def.GetParentID());

	if (def.GetStandardImage().length()) {
		DataLoader* loader = DataLoader::GetLoader();
		loader->SetDataPath("Screens/");

		Bitmap* bmp = 0;
		loader->LoadTexture(def.GetStandardImage(), bmp);
		ctrl->SetStandardImage(bmp);

		if (def.GetActivatedImage().length()) {
			loader->LoadTexture(def.GetActivatedImage(), bmp);
			ctrl->SetActivatedImage(bmp);
		}

		if (def.GetTransitionImage().length()) {
			loader->LoadTexture(def.GetTransitionImage(), bmp);
			ctrl->SetTransitionImage(bmp);
		}

		loader->SetDataPath("");
	}

	ctrl->SetAltText(def.GetAltText());
	ctrl->SetEnabled(def.IsEnabled());
	ctrl->SetBackColor(def.GetBackColor());
	ctrl->SetForeColor(def.GetForeColor());
	ctrl->SetTextAlign(def.GetTextAlign());
	ctrl->SetSingleLine(def.GetSingleLine());

	ctrl->SetBevelWidth(def.GetBevelWidth());
	ctrl->SetDropShadow(def.GetDropShadow());
	ctrl->SetSticky(def.GetSticky());
	ctrl->SetTransparent(def.GetTransparent());
	ctrl->SetHidePartial(def.GetHidePartial());
	ctrl->SetPictureLocation(def.GetPictureLocation());

	ctrl->SetMargins(def.GetMargins());
	ctrl->SetTextInsets(def.GetTextInsets());
	ctrl->SetCellInsets(def.GetCellInsets());
	ctrl->SetCells(def.GetCells());
	ctrl->SetFixedWidth(def.GetFixedWidth());
	ctrl->SetFixedHeight(def.GetFixedHeight());

	if (def.GetPicture().length() > 0) {
		Bitmap pict;
		int    type = def.GetPictureType();

		DataLoader* loader = DataLoader::GetLoader();
		loader->SetDataPath("Screens/");
		loader->LoadBitmap(def.GetPicture(), pict, type);
		loader->SetDataPath("");

		ctrl->SetPicture(pict);
	}

	Font* f = FontMgr::Find(def.GetFont());
	if (f) ctrl->SetFont(f);
}
void
MissionEvent::Execute(bool silent)
{
	Starshatter*   stars    = Starshatter::GetInstance();
	HUDView*       hud      = HUDView::GetInstance();
	Sim*           sim      = Sim::GetSim();
	Ship*          player   = sim->GetPlayerShip();
	Ship*          ship     = 0;
	Ship*          src      = 0;
	Ship*          tgt      = 0;
	Element*       elem     = 0;
	int            pan      = 0;
	bool           end_mission = false;

	if (event_ship.length())
	ship = sim->FindShip(event_ship);
	else
	ship = player;

	if (event_source.length())
	src = sim->FindShip(event_source);

	if (event_target.length())
	tgt = sim->FindShip(event_target);

	if (ship)
	elem = ship->GetElement();

	else if (event_ship.length()) {
		elem = sim->FindElement(event_ship);

		if (elem)
		ship = elem->GetShip(1);
	}

	// expire the delay, if any remains
	delay = 0;

	// fire the event action
	switch (event) {
	case MESSAGE:
		if (event_message.length() > 0) {
			if (ship) {
				RadioMessage* msg = new(__FILE__,__LINE__) RadioMessage(ship, src, event_param[0]);
				msg->SetInfo(event_message);
				msg->SetChannel(ship->GetIFF());
				if (tgt)
				msg->AddTarget(tgt);
				RadioTraffic::Transmit(msg);
			}

			else if (elem) {
				RadioMessage* msg = new(__FILE__,__LINE__) RadioMessage(elem, src, event_param[0]);
				msg->SetInfo(event_message);
				msg->SetChannel(elem->GetIFF());
				if (tgt)
				msg->AddTarget(tgt);
				RadioTraffic::Transmit(msg);
			}
		}

		if (event_sound.length() > 0) {
			pan = event_param[0];
		}
		break;

	case OBJECTIVE: 
		if (elem) {
			if (event_param[0]) {
				elem->ClearInstructions();
				elem->ClearObjectives();
			}

			Instruction* obj = new(__FILE__,__LINE__) Instruction(event_param[0], 0);
			obj->SetTarget(event_target);
			elem->AddObjective(obj);

			if (elem->Contains(player)) {
				HUDView* hud = HUDView::GetInstance();

				if (hud)
				hud->ShowHUDInst();
			}
		}
		break;

	case INSTRUCTION:
		if (elem) {
			if (event_param[0])
			elem->ClearInstructions();

			elem->AddInstruction(event_message);

			if (elem->Contains(player) && event_message.length() > 0) {
				HUDView* hud = HUDView::GetInstance();

				if (hud)
				hud->ShowHUDInst();
			}
		}
		break;

	case IFF:
		if (elem) {
			elem->SetIFF(event_param[0]);
		}

		else if (ship) {
			ship->SetIFF(event_param[0]);
		}
		break;

	case DAMAGE:
		if (ship) {
			ship->InflictDamage(event_param[0]);

			if (ship->Integrity() < 1) {
				NetUtil::SendObjKill(ship, 0, NetObjKill::KILL_MISC);
				ship->DeathSpiral();
				Print("    %s Killed By Scripted Event %d (%s)\n", (const char*) ship->Name(), id, FormatGameTime());
			}
		}
		else {
			Print("   EVENT %d: Could not apply damage to ship '%s' (not found).\n", id, (const char*) event_ship);
		}
		break;

	case JUMP:
		if (ship) {
			SimRegion* rgn = sim->FindRegion(event_target);

			if (rgn && ship->GetRegion() != rgn) {
				if (rgn->IsOrbital()) {
					QuantumDrive* quantum_drive = ship->GetQuantumDrive();
					if (quantum_drive) {
						quantum_drive->SetDestination(rgn, Point(0,0,0));
						quantum_drive->Engage(true); // request immediate jump
					}

					else if (ship->IsAirborne()) {
						ship->MakeOrbit();
					}
				}

				else {
					ship->DropOrbit();
				}
			}

		}
		break;

	case HOLD:
		if (elem)
		elem->SetHoldTime(event_param[0]);
		break;

	case SKIP: {
			for (int i = 0; i < event_nparams; i++) {
				int skip_id = event_param[i];

				ListIter<MissionEvent> iter = sim->GetEvents();
				while (++iter) {
					MissionEvent* e = iter.value();
					if (e->EventID() == skip_id) {
						if (e->status != COMPLETE)
						e->status = SKIPPED;
					}
				}
			}
		}
		break;

	case END_MISSION:
		Print("    END MISSION By Scripted Event %d (%s)\n", id, FormatGameTime());
		end_mission = true;
		break;

		//
		// NOTE: CUTSCENE EVENTS DO NOT APPLY IN MULTIPLAYER
		//
	case BEGIN_SCENE:
		Print("    ------------------------------------\n");
		Print("    Begin Cutscene '%s'\n", event_message.data());
		stars->BeginCutscene();
		break;

	case END_SCENE:
		Print("    End Cutscene '%s'\n", event_message.data());
		Print("    ------------------------------------\n");
		stars->EndCutscene();
		break;

	case CAMERA:
		if (stars->InCutscene()) {
			CameraDirector* cam_dir = CameraDirector::GetInstance();

			if (!cam_dir->GetShip())
			cam_dir->SetShip(player);

			switch (event_param[0]) {
			case 1:  
				if (cam_dir->GetMode() != CameraDirector::MODE_COCKPIT)
				cam_dir->SetMode(CameraDirector::MODE_COCKPIT, event_rect.x);
				break;

			case 2:
				if (cam_dir->GetMode() != CameraDirector::MODE_CHASE)
				cam_dir->SetMode(CameraDirector::MODE_CHASE, event_rect.x);
				break;

			case 3:
				if (cam_dir->GetMode() != CameraDirector::MODE_ORBIT)
				cam_dir->SetMode(CameraDirector::MODE_ORBIT, event_rect.x);
				break;

			case 4:
				if (cam_dir->GetMode() != CameraDirector::MODE_TARGET)
				cam_dir->SetMode(CameraDirector::MODE_TARGET, event_rect.x);
				break;
			}

			if (event_target.length()) {
				::Print("Mission Event %d: setting camera target to %s\n", id, (const char*) event_target);
				Ship* s_tgt = 0;
				
				if (event_target.indexOf("body:") < 0)
				s_tgt = sim->FindShip(event_target);

				if (s_tgt) {
					::Print("   found ship %s\n", s_tgt->Name());
					cam_dir->SetViewOrbital(0);

					if (cam_dir->GetViewObject() != s_tgt) {

						if (event_param[0] == 6) {
							s_tgt->DropCam(event_param[1], event_param[2]);
							cam_dir->SetShip(s_tgt);
							cam_dir->SetMode(CameraDirector::MODE_DROP, 0);
						}
						else {
							Ship* cam_ship = cam_dir->GetShip();

							if (cam_ship && cam_ship->IsDropCam()) {
								cam_ship->CompleteTransition();
							}

							if (cam_dir->GetShip() != sim->GetPlayerShip())
							cam_dir->SetShip(sim->GetPlayerShip());
							cam_dir->SetViewObject(s_tgt, true); // immediate, no transition
						}
					}
				}

				else {
					const char* body_name = event_target.data();

					if (!strncmp(body_name, "body:", 5))
					body_name += 5;

					Orbital* orb = sim->FindOrbitalBody(body_name);

					if (orb) {
						::Print("   found body %s\n", orb->Name());
						cam_dir->SetViewOrbital(orb);
					}
				}
			}

			if (event_param[0] == 3) {
				cam_dir->SetOrbitPoint(event_point.x, event_point.y, event_point.z);
			}

			else if (event_param[0] == 5) {
				cam_dir->SetOrbitRates(event_point.x, event_point.y, event_point.z);
			}
		}
		break;

	case VOLUME:
		if (stars->InCutscene()) {
			AudioConfig* audio_cfg = AudioConfig::GetInstance();

			audio_cfg->SetEfxVolume(event_param[0]);
			audio_cfg->SetWrnVolume(event_param[0]);
		}
		break;

	case DISPLAY:
		if (stars->InCutscene()) {
			DisplayView* disp_view = DisplayView::GetInstance();

			if (disp_view) {
				Color color;
				color.Set(event_param[0]);

				if (event_message.length() && event_source.length()) {

					if (event_message.contains('$')) {
						Campaign*      campaign = Campaign::GetCampaign();
						Player*        user     = Player::GetCurrentPlayer();
						CombatGroup*   group    = campaign->GetPlayerGroup();

						if (user) {
							event_message = FormatTextReplace(event_message, "$NAME",  user->Name().data());
							event_message = FormatTextReplace(event_message, "$RANK",  Player::RankName(user->Rank()));
						}

						if (group) {
							event_message = FormatTextReplace(event_message, "$GROUP", group->GetDescription());
						}

						if (event_message.contains("$TIME")) {
							char timestr[32];
							FormatDayTime(timestr, campaign->GetTime(), true);
							event_message = FormatTextReplace(event_message, "$TIME", timestr);
						}
					}

					disp_view->AddText(  event_message,
					FontMgr::Find(event_source),
					color,
					event_rect,
					event_point.y,
					event_point.x,
					event_point.z);

				}

				else if (event_target.length()) {
					DataLoader* loader = DataLoader::GetLoader();

					if (loader) {
						loader->SetDataPath(0);
						loader->LoadBitmap(event_target, image, 0, true);
					}

					if (image.Width() && image.Height())
					disp_view->AddImage( &image,
					color,
					Video::BLEND_ALPHA,
					event_rect,
					event_point.y,
					event_point.x,
					event_point.z);
				}
			}
		}
		break;

	case FIRE_WEAPON:
		if (ship) {
			// fire single weapon:
			if (event_param[0] >= 0) {
				ship->FireWeapon(event_param[0]);
			}

			// fire all weapons:
			else {
				ListIter<WeaponGroup> g_iter = ship->Weapons();
				while (++g_iter) {
					ListIter<Weapon> w_iter = g_iter->GetWeapons();
					while (++w_iter) {
						Weapon* w = w_iter.value();
						w->Fire();
					}
				}
			}
		}
		break;

	default:
		break;
	}

	sim->ProcessEventTrigger(TRIGGER_EVENT, id);

	if (!silent && !sound && event_sound.length()) {
		DataLoader* loader = DataLoader::GetLoader();
		bool        use_fs = loader->IsFileSystemEnabled();
		DWORD       flags  = pan ? Sound::LOCKED|Sound::LOCALIZED : 
		Sound::LOCKED|Sound::AMBIENT;

		loader->UseFileSystem(true);
		loader->SetDataPath("Sounds/");
		loader->LoadSound(event_sound, sound, flags);
		loader->SetDataPath(0);

		if (!sound) {
			loader->SetDataPath("Mods/Sounds/");
			loader->LoadSound(event_sound, sound, flags);
			loader->SetDataPath(0);
		}

		if (!sound) {
			loader->LoadSound(event_sound, sound, flags);
		}

		loader->UseFileSystem(use_fs);

		// fire and forget:
		if (sound) {
			if (sound->GetFlags() & Sound::STREAMED) {
				sound->SetFlags(flags | sound->GetFlags());
				sound->SetVolume(AudioConfig::VoxVolume());
				sound->Play();
			}
			else {
				sound->SetFlags(flags);
				sound->SetVolume(AudioConfig::VoxVolume());
				sound->SetPan(pan);
				sound->SetFilename(event_sound);
				sound->AddToSoundCard();
				sound->Play();
			}
		}
	}

	status = COMPLETE;

	if (end_mission) {
		StarServer*  server = StarServer::GetInstance();

		if (stars) {
			stars->EndMission();
		}

		else if (server) {
			// end mission event uses event_target member
			// to forward server to next mission in the chain:
			if (event_target.length())
			server->SetNextMission(event_target);

			server->SetGameMode(StarServer::MENU_MODE);
		}
	}
}
void
FormWindow::Init(const FormDef& def)
{
	if (def.GetRect().w > 0 && def.GetRect().h > 0) {
		// if form size is specified in def, and it is 
		// smaller than the current screen size,
		// center the form on the display:

		Rect r = def.GetRect();

		if (r.w < screen->Width()) {
			r.x = (screen->Width()  - r.w) / 2;
		}
		else {
			r.x = 0;
			r.w = screen->Width();
		}

		if (r.h < screen->Height()) {
			r.y = (screen->Height() - r.h) / 2;
		}
		else {
			r.y = 0;
			r.h = screen->Height();
		}

		MoveTo(r);
	}

	SetMargins(def.GetMargins());
	SetTextInsets(def.GetTextInsets());
	SetCellInsets(def.GetCellInsets());
	SetCells(def.GetCells());
	SetFixedWidth(def.GetFixedWidth());
	SetFixedHeight(def.GetFixedHeight());

	UseLayout(def.GetLayout().x_mins,
	def.GetLayout().y_mins,
	def.GetLayout().x_weights,
	def.GetLayout().y_weights);

	if (def.GetTexture().length() > 0) {
		DataLoader* loader = DataLoader::GetLoader();
		loader->SetDataPath("Screens/");
		loader->LoadTexture(def.GetTexture(), texture);
		loader->SetDataPath("");
	}

	SetBackColor(def.GetBackColor());
	SetForeColor(def.GetForeColor());

	Font* f = FontMgr::Find(def.GetFont());
	if (f) SetFont(f);

	SetTransparent(def.GetTransparent());

	ListIter<CtrlDef> ctrl = def.GetControls();
	while (++ctrl) {
		switch (ctrl->GetType()) {
		case WIN_DEF_FORM:
		case WIN_DEF_LABEL:
		default:
			CreateDefLabel(*ctrl);
			break;

		case WIN_DEF_BUTTON:
			CreateDefButton(*ctrl);
			break;

		case WIN_DEF_COMBO:
			CreateDefCombo(*ctrl);
			break;

		case WIN_DEF_IMAGE:
			CreateDefImage(*ctrl);
			break;

		case WIN_DEF_EDIT:
			CreateDefEdit(*ctrl);
			break;

		case WIN_DEF_LIST:
			CreateDefList(*ctrl);
			break;

		case WIN_DEF_SLIDER:
			CreateDefSlider(*ctrl);
			break;

		case WIN_DEF_RICH:
			CreateDefRichText(*ctrl);
			break;
		}
	}

	RegisterControls();
	DoLayout();
	CalcGrid();
}
void
SystemDesign::Initialize(const char* filename)
{
    Print("Loading System Designs '%s'\n", filename);

    // Load Design File:
    DataLoader* loader = DataLoader::GetLoader();
    BYTE* block;

    int blocklen = loader->LoadBuffer(filename, block, true);
    Parser parser(new(__FILE__,__LINE__) BlockReader((const char*) block, blocklen));
    Term*  term = parser.ParseTerm();

    if (!term) {
        Print("ERROR: could not parse '%s'\n", filename);
        exit(-3);
    }
    else {
        TermText* file_type = term->isText();
        if (!file_type || file_type->value() != "SYSTEM") {
            Print("ERROR: invalid system design file '%s'\n", filename);
            exit(-4);
        }
    }

    int type = 1;

    do {
        delete term;

        term = parser.ParseTerm();
        
        if (term) {
            TermDef* def = term->isDef();
            if (def) {
                if (def->name()->value() == "system") {

                    if (!def->term() || !def->term()->isStruct()) {
                        Print("WARNING: system structure missing in '%s'\n", filename);
                    }
                    else {
                        TermStruct*   val    = def->term()->isStruct();
                        SystemDesign* design = new(__FILE__,__LINE__) SystemDesign;
                        
                        for (int i = 0; i < val->elements()->size(); i++) {
                            TermDef* pdef = val->elements()->at(i)->isDef();
                            if (pdef) {
                                GET_DEF_TEXT(pdef, design, name);

                                else if (pdef->name()->value()==("component")) {
                                    if (!pdef->term() || !pdef->term()->isStruct()) {
                                        Print("WARNING: component structure missing in system '%s' in '%s'\n", (const char*) design->name, filename);
                                    }
                                    else {
                                        TermStruct*   val2    = pdef->term()->isStruct();
                                        ComponentDesign* comp_design = new(__FILE__,__LINE__) ComponentDesign;
                                        
                                        for (int i = 0; i < val2->elements()->size(); i++) {
                                            TermDef* pdef2 = val2->elements()->at(i)->isDef();
                                            if (pdef2) {
                                                GET_DEF_TEXT(pdef2, comp_design, name);
                                                else GET_DEF_TEXT(pdef2, comp_design, abrv);
                                                else GET_DEF_NUM (pdef2, comp_design, repair_time);
                                                else GET_DEF_NUM (pdef2, comp_design, replace_time);
                                                else GET_DEF_NUM (pdef2, comp_design, spares);
                                                else GET_DEF_NUM (pdef2, comp_design, affects);

                                                else {
                                                    Print("WARNING: parameter '%s' ignored in '%s'\n",
                                                    pdef2->name()->value().data(), filename);
                                                }
                                            }
                                        }

                                        design->components.append(comp_design);
                                    }
                                }

                                else {
                                    Print("WARNING: parameter '%s' ignored in '%s'\n",
                                    pdef->name()->value().data(), filename);
                                }
                            }
void MainWindow::loadAllObjectRecords()
{
    DataLoader* dl = ct->loader;

    QMessageBox msgBox;
    int amountOfObjectRecords = dl->getAmountOfObjectRecords(objectsData[choosenObjectId].first);
    msgBox.setInformativeText(QString::fromUtf8("Czy chcesz załadować ") + QString::number(amountOfObjectRecords) + QString::fromUtf8(" rekordów?"));
    msgBox.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
    msgBox.setDefaultButton(QMessageBox::Yes);
    msgBox.setIcon(QMessageBox::Question);
    msgBox.setWindowTitle(QString::fromUtf8("Pobieranie danych"));

    // workaround for not working setMinimumWidth:
    QSpacerItem* horizontalSpacer = new QSpacerItem(350, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
    QGridLayout* layout = (QGridLayout*)msgBox.layout();
    layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());

    if(msgBox.exec()==QMessageBox::Yes)
    {
        ct->loader->objectId = objectsData[choosenObjectId].first;
        QProgressDialog progress(QString::fromUtf8("Pobieranie rekordów dla wybranego obiektu..."), "Anuluj", 0, amountOfObjectRecords, this);
        progress.setWindowModality(Qt::WindowModal);
        dl->progessBar = &progress;
        progress.show();
        progress.setValue(0);
        dl->initDataRecordTable();
        dl->loadAllRecords();

        if(progress.wasCanceled())
            return;

        progress.setLabelText(QString::fromUtf8("Pobieranie danych anomalii dla wybranego obiektu..."));
        progress.setCancelButtonText("Anuluj");
        progress.setMinimum(0);
        progress.setValue(0);

        dl->loadMeasurementInfo();
        dl->setAlarmFlagToRecords();
        ct->dataset->setMinMaxFromDataset();

        if(!progress.wasCanceled())
        {
            this->statusOfObjectDataLoad = true;
            ui->livelogTab->setEnabled(true);
            ui->changeRefreshTimeBtn->setEnabled(false);
            ui->refreshIntervalSpin->setEnabled(false);
            ui->filterValuesLivelogBtn->setEnabled(false);
            ui->startLivelogBtn->setEnabled(true);
            ui->stopLivelogBtn->setEnabled(false);
            ui->linearScaleLivelogRadioButton->setEnabled(false);
            ui->logScaleLivelogRadioButton->setEnabled(false);

            ui->anomalyDetectionTab->setEnabled(true);
            ui->leftBtn->setEnabled(false);
            ui->rightBtn->setEnabled(false);
            ui->linearScaleRadioButton->setEnabled(false);
            ui->logScaleRadioButton->setEnabled(false);

            ui->dataOverviewTab->setEnabled(true);
            ui->refreshDatabaseTable->setEnabled(true);
            ui->datasetTableView->setEnabled(true);

            ui->reportsTab->setEnabled(true);
            ui->subscriptionTab->setEnabled(true);

            ui->reportDataRangeFrom->setMaximum(this->ct->dataset->datasetControler->dataset->dataTable->getLength()-2);
            ui->reportDataRangeTo->setMaximum(this->ct->dataset->datasetControler->dataset->dataTable->getLength()-1);
            ui->reportDataRangeTo->setValue(this->ct->dataset->datasetControler->dataset->dataTable->getLength()-1);

            ct->dataRecordSize = ct->dataset->datasetControler->dataset->dataTable->dataNames.size();

            ui->webView->initFilter(ct->dataRecordSize);

            registerMethods();

            QMessageBox msgBoxOK;
            msgBoxOK.setInformativeText(QString::fromUtf8("Dane zostały pobrane."));
            msgBoxOK.setStandardButtons(QMessageBox::Ok);
            msgBoxOK.setDefaultButton(QMessageBox::Ok);
            msgBoxOK.setIcon(QMessageBox::Information);
            msgBoxOK.setWindowTitle(QString::fromUtf8("Pobrano"));

            // workaround for not working setMinimumWidth:
            QSpacerItem* horizontalSpacer = new QSpacerItem(350, 0, QSizePolicy::Minimum, QSizePolicy::Expanding);
            QGridLayout* layout = (QGridLayout*)msgBoxOK.layout();
            layout->addItem(horizontalSpacer, layout->rowCount(), 0, 1, layout->columnCount());

            msgBoxOK.exec();
        }
    }
}