TreeViewClasses.cpp
上传用户:szb0815
上传日期:2007-06-13
资源大小:338k
文件大小:38k
- /*
- Software and source code Copyright (C) 1998-2000 Stanford University
- Written by Michael Eisen (eisen@genome.stanford.edu)
- This software is copyright under the following conditions:
- Permission to use, copy, and modify this software and its documentation
- is hereby granted to all academic and not-for-profit institutions
- without fee, provided that the above copyright notice and this permission
- notice appear in all copies of the software and related documentation.
- Permission to distribute the software or modified or extended versions
- thereof on a not-for-profit basis is explicitly granted, under the above
- conditions. However, the right to use this software in conjunction with
- for profit activities, and the right to distribute the software or modified or
- extended versions thereof for profit are *NOT* granted except by prior
- arrangement and written consent of the copyright holders.
- Use of this source code constitutes an agreement not to criticize, in any
- way, the code-writing style of the author, including any statements regarding
- the extent of documentation and comments present.
- The software is provided "AS-IS" and without warranty of ank kind, express,
- implied or otherwise, including without limitation, any warranty of
- merchantability or fitness for a particular purpose.
- In no event shall Stanford University or the authors be liable for any special,
- incudental, indirect or consequential damages of any kind, or any damages
- whatsoever resulting from loss of use, data or profits, whether or not
- advised of the possibility of damage, and on any theory of liability,
- arising out of or in connection with the use or performance of this software.
- This code was written using Borland C++ Builder 4 (Inprise Inc., www.inprise.com)
- and may be subject to certain additional restrictions as a result.
- */
- //---------------------------------------------------------------------------
- #include <vcl.h>
- #pragma hdrstop
- #include <stdlib.h>
- #include <math.h>
- #include <dir.h>
- #include "TreeViewMain.h"
- #include "TreeViewClasses.h"
- //---------------------------------------------------------------------------
- #pragma package(smart_init)
- AnsiString NextString(AnsiString* String)
- {
- AnsiString Field;
- int delim = (*String).LastDelimiter("t");
- if (delim != 0)
- {
- Field = (*String).SubString(delim+1,(*String).Length()-delim);
- (*String) = (*String).SubString(0,delim-1);
- }
- else
- {
- Field = (*String);
- (*String) = (*String).SubString(0,0);
- }
- if ((Field.Length() == 0) && (delim == 0) )
- {
- return AnsiString("DONE");
- }
- else
- {
- return Field;
- }
- }
- /*__fastcall TMyWrapper::TMyWrapper() : TObject()
- {
- } */
- __fastcall TGeneTreeNode::TGeneTreeNode() : TObject()
- {
- IsNode = true;
- Color = clBlack;
- Child1 = NULL;
- Child2 = NULL;
- Elements = 1;
- }
- __fastcall TGeneTreeNode::~TGeneTreeNode()
- {
- delete Data;
- }
- void __fastcall TGeneTreeNode::SetParent(TGeneTreeNode *pParent)
- {
- Parent = pParent;
- if (IsNode == true)
- {
- Child1->SetParent(this);
- Child2->SetParent(this);
- }
- else
- {
- Child1 = NULL;
- Child2 = NULL;
- }
- }
- void __fastcall TGeneTreeNode::SetCoor()
- {
- if (IsNode == true)
- {
- Child1->SetCoor();
- Child2->SetCoor();
- XCoor = 1.0 - ((1.0 - Corr)/2);
- XCoor = min(XCoor, Child1->XCoor);
- XCoor = min(XCoor, Child2->XCoor);
- YCoor = (Child1->YCoor + Child2->YCoor)/2;
- MinCoor = min(Child1->MinCoor,Child2->MinCoor);
- MaxCoor = max(Child1->MaxCoor,Child2->MaxCoor);
- }
- else
- {
- XCoor = 1.0;
- MinCoor = YCoor;
- MaxCoor = YCoor;
- }
- }
- void __fastcall TGeneTreeNode::SetResist()
- {
- if (IsNode == true)
- {
- Child1->SetResist();
- Child2->SetResist();
- double Resist1 = max(0.0,Child1->Corr - Corr) + Child1->Resist;
- double Resist2 = max(0.0,Child2->Corr - Corr) + Child2->Resist;
- if ( (Resist1 > 0 ) && (Resist2 > 0) )
- {
- Resist = (Resist1 * Resist2) / (Resist1 + Resist2);
- }
- else if (Resist1 > 0)
- {
- Resist = Resist1;
- }
- else if (Resist2 > 0)
- {
- Resist = Resist2;
- }
- else
- {
- Resist = 0;
- }
- }
- else
- {
- Resist = 0;
- }
- }
- void __fastcall TGeneTreeNode::WriteList(double Cutoff)
- {
- if ((IsNode == true) && (Corr > Cutoff))
- {
- AnsiString OutFileName = Data->ID + ".txt";
- TStringList *IDList = new TStringList();
- Child1->GetIDs(IDList);
- Child2->GetIDs(IDList);
- if ( (IDList->Count > 5) && (IDList->Count < 50) )
- {
- IDList->SaveToFile(OutFileName);
- }
- delete IDList;
- }
- if (IsNode == true)
- {
- Child1->WriteList(Cutoff);
- Child2->WriteList(Cutoff);
- }
- }
- void __fastcall TGeneTreeNode::GetIDs(TStringList *IDList)
- {
- if (IsNode == true)
- {
- Child1->GetIDs(IDList);
- Child2->GetIDs(IDList);
- }
- else
- {
- IDList->Add(Data->ID);
- }
- }
- TGeneTreeNode* __fastcall TGeneTreeNode::FindNode(AnsiString NodeName)
- {
- if (IsNode == true)
- {
- if (Data->ID == NodeName)
- {
- return this;
- }
- else
- {
- TGeneTreeNode *NodeMatched1 = Child1->FindNode(NodeName);
- TGeneTreeNode *NodeMatched2 = Child2->FindNode(NodeName);
- if (NodeMatched1 != NULL)
- {
- return NodeMatched1;
- }
- else
- {
- return NodeMatched2;
- }
- }
- }
- else
- {
- return NULL;
- }
- }
- void __fastcall TGeneTreeNode::SetWeight(double Current)
- {
- Weight = Current;
- if (IsNode == true)
- {
- double Current1, Current2;
- double Resist1, Resist2;
- Resist1 = max(0.0,Child1->Corr - Corr) + Child1->Resist;
- Resist2 = max(0.0,Child2->Corr - Corr) + Child2->Resist;
- if ( (Resist1 > 0) || (Resist2 > 0) )
- {
- Current1 = Current * Resist2 / (Resist1 + Resist2);
- Current2 = Current * Resist1 / (Resist1 + Resist2);
- }
- else
- {
- Current1 = Current/2;
- Current2 = Current/2;
- }
- Child1->SetWeight(Current1);
- Child2->SetWeight(Current2);
- }
- }
- void __fastcall TGeneTreeNode::SetData()
- {
- if (IsNode == true)
- {
- Child1->SetData();
- Child2->SetData();
- Elements = Child1->Elements + Child2->Elements;
- int Count;
- double Val;
- int i;
- for (i=0;i<Data->Columns;i++)
- {
- Val = 0;
- Count = 0;
- Data->Mask[i] = false;
- if (Child1->Data->Mask[i])
- {
- Val += Child1->Data->Data[i] * Child1->Elements;
- Count += Child1->Elements;
- }
- if (Child2->Data->Mask[i])
- {
- Val += Child2->Data->Data[i] * Child2->Elements;
- Count += Child2->Elements;
- }
- if (Count > 0)
- {
- Data->Mask[i] = true;
- Data->Data[i] = Val / Count;
- }
- }
-
- }
- }
- void __fastcall TGeneTreeNode::Draw(TImage *Image, double Scale)
- {
- if (IsNode == true)
- {
- Image->Canvas->MoveTo(Child1->XCoor*Image->Width,Child1->YCoor * Scale);
- Image->Canvas->LineTo(XCoor*Image->Width,Child1->YCoor * Scale);
- Image->Canvas->LineTo(XCoor*Image->Width,Child2->YCoor * Scale);
- Image->Canvas->LineTo(Child2->XCoor*Image->Width,Child2->YCoor * Scale);
- Child1->Draw(Image, Scale);
- Child2->Draw(Image, Scale);
- XPoint = XCoor * Image->Width;
- YPoint = YCoor * Scale;
- }
- }
- /*void __fastcall TGeneTreeNode::AddNote(TStrings *NewNote)
- {
- if (IsNode == true)
- {
- Child1->AddNote(NewNote);
- Child2->AddNote(NewNote);
- }
- else
- {
- AnsiString Key1 = TreeViewMainForm->Cluster->UniqueID;
- AnsiString Key2 = Data->ID;
- TVarRec Keys[] = {Key1,Key2};
- if (!TreeViewMainForm->Table1->FindKey(Keys,1))
- {
- TreeViewMainForm->Table1->AppendRecord(Keys,1);
- TreeViewMainForm->Table1->FindKey(Keys,1);
- }
- TreeViewMainForm->DataSource1->Edit();
- AnsiString NewString = TreeViewMainForm->DBMemo1->Field->AsString;
- for (int i=0;i<NewNote->Count;i++)
- {
- NewString += "rn" + NewNote->Strings[i];
- }
- TreeViewMainForm->DBMemo1->Field->AsString = NewString;
- }
- } */
- void __fastcall TGeneTreeNode::DrawPS(TStringList *PSFile, int ScaleX, int ScaleY, int StartYCoor)
- {
- if (IsNode == true)
- {
- AnsiString TempString;
- int X1,Y1,X2,Y2;
- X1 = Child1->XCoor*ScaleX;
- Y1 = (ScaleY/2) + (StartYCoor - Child1->YCoor)*ScaleY;
- X2 = XCoor * ScaleX;
- Y2 = (ScaleY/2)+ (StartYCoor - Child1->YCoor)*ScaleY;
- TempString = AnsiString(X1) + " " + AnsiString(Y1) + " " +
- AnsiString(X2) + " " + AnsiString(Y2) + " ln";
- PSFile->Add(TempString);
- X1 = Child2->XCoor*ScaleX;
- Y1 = (ScaleY/2) + (StartYCoor - Child2->YCoor)*ScaleY;
- X2 = XCoor * ScaleX;
- Y2 = (ScaleY/2) + (StartYCoor - Child2->YCoor)*ScaleY;
- TempString = AnsiString(X1) + " " + AnsiString(Y1) + " " +
- AnsiString(X2) + " " + AnsiString(Y2) + " ln";
- PSFile->Add(TempString);
- X1 = XCoor * ScaleX;
- Y1 = (ScaleY/2) + (StartYCoor - Child1->YCoor)*ScaleY;
- X2 = XCoor * ScaleX;
- Y2 = (ScaleY/2) + (StartYCoor - Child2->YCoor)*ScaleY;
- TempString = AnsiString(X1) + " " + AnsiString(Y1) + " " +
- AnsiString(X2) + " " + AnsiString(Y2) + " ln";
- PSFile->Add(TempString);
- Child1->DrawPS(PSFile, ScaleX, ScaleY, StartYCoor);
- Child2->DrawPS(PSFile, ScaleX, ScaleY, StartYCoor);
- }
- }
- void __fastcall TGeneTreeNode::Limits(double *mincoor, double *maxcoor)
- {
- if (IsNode == true)
- {
- double mincoor1,mincoor2,maxcoor1,maxcoor2;
- Child1->Limits(&mincoor1,&maxcoor1);
- Child2->Limits(&mincoor2,&maxcoor2);
- *mincoor = min(mincoor1,mincoor2);
- *maxcoor = max(maxcoor1,maxcoor2);
- }
- else
- {
- *mincoor = YCoor;
- *maxcoor = YCoor;
- }
- }
- TGeneTreeNode* __fastcall TGeneTreeNode::Dist(int X, int Y, double *Dist)
- {
- TGeneTreeNode *Best;
- if (IsNode == true)
- {
- double Dist1, Dist2;
- TGeneTreeNode *Best1,*Best2;
- Best1 = Child1->Dist(X,Y,&Dist1);
- Best2 = Child2->Dist(X,Y,&Dist2);
- *Dist = sqrt( (X-XPoint)*(X-XPoint) + (Y-YPoint)*(Y-YPoint) );
- if (*Dist < min((Dist1),(Dist2)))
- {
- Best = this;
- }
- else
- {
- if (Dist1 < Dist2)
- {
- Best = Best1;
- *Dist = Dist1;
- }
- else
- {
- Best = Best2;
- *Dist = Dist2;
- }
- }
- }
- else
- {
- Best = NULL;
- *Dist = 100000.0;
- }
- return Best;
- }
- TGeneTreeNode * __fastcall TGeneTreeNode::Include(int X, int Y, bool *Includes)
- {
- TGeneTreeNode *Best;
- Best = NULL;
- if (IsNode == true)
- {
- bool Includes1, Includes2;
- TGeneTreeNode *Best1,*Best2;
- Best1 = Child1->Include(X,Y,&Includes1);
- Best2 = Child2->Include(X,Y,&Includes2);
- if (Best1 != NULL)
- {
- Best = Best1;
- }
- if (Best2 != NULL)
- {
- Best = Best2;
- }
- *Includes = (Includes1 || Includes2);
- if (Includes1 && Includes2)
- {
- Best = this;
- }
- }
- else
- {
- Best = NULL;
- *Includes = false;
- if ( (X == (int) YCoor) || (Y == (int) YCoor) )
- {
- *Includes = true;
- }
- if ( (X == (int) YCoor) && (Y == (int) YCoor) )
- {
- *Includes = true;
- Best = this;
- }
- }
- return Best;
- }
- void __fastcall TGeneTreeNode::SaveData(TStringList *FileList, double LowCut,
- double HighCut, int MinElems, bool SaveChildren, bool SaveList)
- {
- if ( (Corr >= LowCut) && (Corr <= HighCut) && (Elements > MinElems) )
- {
- int i,j;
- AnsiString Line =
- Data->ID + "t" + Data->ID + " " + AnsiString(Corr) + " " + AnsiString(Elements);
- for (j=0;j<Data->Columns;j++)
- {
- Line += "t";
- if (Data->Mask[j])
- {
- Line += AnsiString(Data->Data[j]);
- }
- }
- FileList->Add(Line);
- if (SaveChildren && IsNode)
- {
- Child1->SaveData(FileList,LowCut,HighCut, MinElems,SaveChildren,SaveList);
- Child2->SaveData(FileList,LowCut,HighCut, MinElems,SaveChildren,SaveList);
- }
- if (SaveList)
- {
- TStringList *GeneList = new TStringList();
- GetList(GeneList);
- AnsiString ListFileName = Data->ID + ".lst";
- GeneList->SaveToFile(ListFileName);
- delete GeneList;
- }
- }
- else if (IsNode)
- {
- Child1->SaveData(FileList,LowCut,HighCut,MinElems,SaveChildren,SaveList);
- Child2->SaveData(FileList,LowCut,HighCut,MinElems,SaveChildren,SaveList);
- }
- }
- void __fastcall TGeneTreeNode::GetList(TStringList *List)
- {
- if (IsNode)
- {
- Child1->GetList(List);
- Child2->GetList(List);
- }
- else
- {
- List->Add(Data->ID);
- }
- }
- __fastcall TGeneCluster::TGeneCluster() : TObject()
- {
- Arrays = new TStringList();
- Genes = new TStringList();
- Nodes = new TStringList();
- ArrayNodes = new TStringList();
- GeneNames = new TStringList();
- Loaded = false;
- Columns = 0;
- }
- __fastcall TGeneCluster::~TGeneCluster()
- {
- int i;
- delete Bitmap;
- delete Arrays;
- for (i=0; i<Genes->Count;i++)
- {
- delete (TGeneTreeNode *)Genes->Objects[i];
- }
- delete Genes;
- for (i=0; i<Nodes->Count;i++)
- {
- delete (TGeneTreeNode *)Nodes->Objects[i];
- }
- delete Nodes;
- for (i=0; i<ArrayNodes->Count;i++)
- {
- delete (TArrayTreeNode *)ArrayNodes->Objects[i];
- }
- delete ArrayNodes;
- }
- void __fastcall TGeneCluster::Dist(int X, int Y)
- {
- BestNode = TopNode->Dist(X,Y,&BestDist);
- BestNode->Limits(&MinCoor,&MaxCoor);
- }
- void __fastcall TGeneCluster::Include(int X, int Y)
- {
- bool Includes;
- if (TopNode != NULL)
- {
- BestNode = TopNode->Include(X,Y,&Includes);
- BestNode->Limits(&MinCoor,&MaxCoor);
- }
- else
- {
- MinCoor = X;
- MaxCoor = Y;
- }
- }
- double __fastcall TGeneCluster::Load
- (AnsiString DataFile)
- {
- TStringList *DataFileList = new TStringList();
- char Extension[5];
- char FileName[256];
- fnsplit(DataFile.c_str(),NULL,NULL,FileName,Extension);
- DataFileList->LoadFromFile(DataFile);
- bool LoadGeneTree;
- AnsiString GeneTreeFile = AnsiString(FileName) + ".gtr";
- AnsiString ArrayTreeFile = AnsiString(FileName) + ".atr";
- AnsiString Line;
- AnsiString Field;
- int i,j;
- TStringList *Headers = new TStringList();
- bool *IsData;
- long double Sum = 0;
- long double Sum2 = 0;
- double DCount = 0;
- // Remove Comment Lines
- for (i=DataFileList->Count-1;i>=0;i--)
- {
- if (DataFileList->Strings[i].SubString(1,6) == "REMARK")
- {
- DataFileList->Delete(i);
- }
- if (DataFileList->Strings[i].Length() < 3)
- {
- DataFileList->Delete(i);
- }
- }
- // Parse First Line
- Line = DataFileList->Strings[0];
- DataFileList->Delete(0);
- while ((Field = NextString(&Line)) != "DONE")
- {
- Headers->Insert(0,Field);
- }
-
- IsData = new bool [Headers->Count];
- for (i=0;i<Headers->Count;i++)
- {
- IsData[i] = true;
- }
- int GIDIndex = Headers->IndexOf("GID");
- int UniqueIDIndex;
- if (GIDIndex > -1)
- {
- LoadGeneTree = true;
- UniqueIDIndex = GIDIndex + 1;
- IsData[GIDIndex] = false;
- }
- else
- {
- LoadGeneTree = false;
- UniqueIDIndex = 0;
- }
- UniqueID = Headers->Strings[UniqueIDIndex];
- IsData[UniqueIDIndex] = false;
- int NameIndex = Headers->IndexOf("NAME");
- if (NameIndex > -1)
- {
- IsData[NameIndex] = false;
- }
- int GeneWeightIndex = Headers->IndexOf("GWEIGHT");
- if (GeneWeightIndex > -1)
- {
- IsData[GeneWeightIndex] = false;
- }
- for (i=0;i<Headers->Count;i++)
- {
- if (IsData[i] == true)
- {
- Columns++;
- Arrays->Add(Headers->Strings[i]);
- }
- }
- ListIndex = new long[DataFileList->Count * 2];
- TGeneTreeNode *GeneTreeNode;
- AnsiString GID, ID, Name;
- int index;
- int nGID;
- bool LoadArrayTree = false;
- AnsiString ArrayHeaderString;
- AnsiString ArrayWeightString;
- double Weight;
- TStringList *LineList = new TStringList();
- i=0;
- Rows = 0;
- while (DataFileList->Count > 0)
- {
- Line = DataFileList->Strings[0];
- LineList->Clear();
- while ((Field = NextString(&Line)) != "DONE")
- {
- LineList->Insert(0,Field);
- }
- if (LineList->Strings[0] == "AID")
- {
- LoadArrayTree = true;
- ArrayHeaderString = DataFileList->Strings[0];
- }
- else if (LineList->Strings[0] == "EWEIGHT")
- {
- ArrayWeightString = DataFileList->Strings[0];
- }
- else
- {
- GID = "";
- if ( (GIDIndex > -1) && (GIDIndex < LineList->Count) )
- {
- GID = LineList->Strings[GIDIndex];
- }
- Name = "";
- if ( (NameIndex > -1) && (NameIndex < LineList->Count) )
- {
- Name = LineList->Strings[NameIndex];
- for (int Pos=Name.Length();Pos>=0;Pos--)
- {
- if (Name.SubString(Pos,1) == """)
- {
- Name.Delete(Pos,1);
- }
- }
- }
- ID = "";
- if ( (UniqueIDIndex > -1) && (UniqueIDIndex < LineList->Count) )
- {
- ID = LineList->Strings[UniqueIDIndex];
- }
- Weight = 1.0;
- if ( (GeneWeightIndex > -1) && (GeneWeightIndex < LineList->Count) )
- {
- try
- {
- Weight = (LineList->Strings[GeneWeightIndex]).ToDouble();
- }
- catch (EConvertError &E)
- {
- Weight = 1.0;
- }
- }
- index = Genes->Add(ID);
- if (LoadGeneTree == true)
- {
- try
- {
- nGID = (GID.SubString(5,GID.Length()-5)).ToInt();
- ListIndex[nGID] = index;
- }
- catch (EConvertError &E)
- {
- }
- }
- Genes->Objects[index] = new TGeneTreeNode();
- GeneNames->Add(Name);
- GeneTreeNode = (TGeneTreeNode *) Genes->Objects[index];
- GeneTreeNode->Data = new TGeneData(Columns);
- GeneTreeNode->IsNode = false;
- GeneTreeNode->Corr = 1.0;
- GeneTreeNode->Weight = Weight;
- GeneTreeNode->Data->ID = ID;
- GeneTreeNode->Data->Name = Name;
- index = 0;
- for (j=0;j<min(LineList->Count,Headers->Count);j++)
- {
- if (IsData[j] == true)
- {
- try
- {
- double Val = LineList->Strings[j].ToDouble();
- GeneTreeNode->Data->Data[index] = Val;
- GeneTreeNode->Data->Mask[index] = true;
- Sum += Val;
- Sum2 += pow(Val,2.0);
- DCount += 1.0;
- }
- catch (EConvertError &E)
- {
- GeneTreeNode->Data->Mask[index] = false;
- }
- index++;
- }
- }
- /* Handle case where there are too few lines */
- for (j=index; j<Columns;j++)
- {
- GeneTreeNode->Data->Mask[j] = false;
- }
- i++;
- Rows++;
- }
- DataFileList->Delete(0);
- }
- for (i=0;i<Genes->Count;i++)
- {
- ((TGeneTreeNode *)Genes->Objects[i])->YCoor = i;
- }
- if (LoadGeneTree == true)
- {
- TStringList *TreeFileList = new TStringList();
- TreeFileList->LoadFromFile(GeneTreeFile);
- if (TreeFileList->Count == 0)
- {
- ShowMessage("Could Not Load Gene Tree File");
- }
- else
- {
- AnsiString NodeID, Child1, Child2;
- double Corr;
- int nGID1,nGID2;
- for (i=0;i<TreeFileList->Count;i++)
- {
- Line = TreeFileList->Strings[i];
- LineList->Clear();
- while ((Field = NextString(&Line)) != "DONE")
- {
- LineList->Insert(0,Field);
- }
- NodeID = LineList->Strings[0];
- Child1 = LineList->Strings[1];
- Child2 = LineList->Strings[2];
- Corr = LineList->Strings[3].ToDouble();
- nGID1 = (Child1.SubString(5,Child1.Length()-5)).ToInt();
- nGID2 = (Child2.SubString(5,Child2.Length()-5)).ToInt();
- GeneTreeNode = new TGeneTreeNode();
- GeneTreeNode->IsNode = true;
- if (Child1.SubString(1,4) == "NODE")
- {
- GeneTreeNode->Child1 = (TGeneTreeNode *) Nodes->Objects[nGID1-1];
- }
- else
- {
- GeneTreeNode->Child1 = (TGeneTreeNode *) Genes->Objects[ListIndex[nGID1]];
- }
- if (Child2.SubString(1,4) == "NODE")
- {
- GeneTreeNode->Child2 = (TGeneTreeNode *) Nodes->Objects[nGID2-1];
- }
- else
- {
- GeneTreeNode->Child2 = (TGeneTreeNode *) Genes->Objects[ListIndex[nGID2]];
- }
- if ( (GeneTreeNode->Child1 == NULL) || (GeneTreeNode->Child2 == NULL) )
- {
- GeneTreeNode->IsNode = false;
- }
- GeneTreeNode->Corr = Corr;
- index = Nodes->Add(NodeID);
- Nodes->Objects[index] = GeneTreeNode;
- GeneTreeNode->Data = new TGeneData(Columns);
- GeneTreeNode->Data->ID = NodeID;
- }
- TopNode = GeneTreeNode;
- TopNode->SetParent(NULL);
- TopNode->SetData();
- //Nodes->Clear();
- }
- delete TreeFileList;
- }
- else
- {
- TopNode = NULL;
- }
- if (LoadArrayTree == true)
- {
- TStringList *TreeFileList = new TStringList();
- TreeFileList->LoadFromFile(ArrayTreeFile);
- TArrayTreeNode *ArrayTreeNode;
- long *ArrayListIndex = new long[Columns];
- LineList->Clear();
- while ((Field = NextString(&ArrayHeaderString)) != "DONE")
- {
- LineList->Insert(0,Field);
- }
- index = 0;
- for (i=0;i<LineList->Count;i++)
- {
- if (IsData[i] == true)
- {
- ID = LineList->Strings[i];
- Arrays->Objects[index] = new TArrayTreeNode();
- ArrayTreeNode = (TArrayTreeNode *) Arrays->Objects[index];
- nGID = (ID.SubString(5,ID.Length()-5)).ToInt();
- ArrayListIndex[nGID] = index;
- ArrayTreeNode->Data = new TArrayData();
- ArrayTreeNode->IsNode = false;
- ArrayTreeNode->Corr = 1.0;
- ArrayTreeNode->Weight = 1.0;
- ArrayTreeNode->Data->ID = ID;
- ArrayTreeNode->Data->Name = "";
- index++;
- }
- }
- LineList->Clear();
- while ((Field = NextString(&ArrayWeightString)) != "DONE")
- {
- LineList->Insert(0,Field);
- }
- index = 0;
- for (i=0;i<LineList->Count;i++)
- {
- if (IsData[i] == true)
- {
- try
- {
- Weight = (LineList->Strings[i]).ToDouble();
- ArrayTreeNode = (TArrayTreeNode *) Arrays->Objects[index];
- ArrayTreeNode->Weight = Weight;
- }
- catch (EConvertError &E)
- {
- }
- index ++;
- }
- }
- AnsiString NodeID, Child1, Child2;
- double Corr;
- int nGID1,nGID2;
- for (i=0;i<TreeFileList->Count;i++)
- {
- Line = TreeFileList->Strings[i];
- LineList->Clear();
- while ((Field = NextString(&Line)) != "DONE")
- {
- LineList->Insert(0,Field);
- }
- NodeID = LineList->Strings[0];
- Child1 = LineList->Strings[1];
- Child2 = LineList->Strings[2];
- Corr = LineList->Strings[3].ToDouble();
- nGID1 = (Child1.SubString(5,Child1.Length()-5)).ToInt();
- nGID2 = (Child2.SubString(5,Child2.Length()-5)).ToInt();
- ArrayTreeNode = new TArrayTreeNode();
- ArrayTreeNode->IsNode = true;
- if (Child1.SubString(1,4) == "NODE")
- {
- ArrayTreeNode->Child1 = (TArrayTreeNode *) ArrayNodes->Objects[nGID1-1];
- }
- else
- {
- ArrayTreeNode->Child1 = (TArrayTreeNode *) Arrays->Objects[ArrayListIndex[nGID1]];
- }
- if (Child2.SubString(1,4) == "NODE")
- {
- ArrayTreeNode->Child2 = (TArrayTreeNode *) ArrayNodes->Objects[nGID2-1];
- }
- else
- {
- ArrayTreeNode->Child2 = (TArrayTreeNode *) Arrays->Objects[ArrayListIndex[nGID2]];
- }
- if ( (ArrayTreeNode->Child1 == NULL) || (ArrayTreeNode->Child2 == NULL) )
- {
- ArrayTreeNode->IsNode = false;
- }
- ArrayTreeNode->Corr = Corr;
- index = ArrayNodes->Add(NodeID);
- ArrayNodes->Objects[index] = ArrayTreeNode;
- ArrayTreeNode->Data = new TArrayData();
- ArrayTreeNode->Data->ID = NodeID;
- }
- TopArrayNode = ArrayTreeNode;
- TopArrayNode->SetParent(NULL);
- for (i=0;i<Arrays->Count;i++)
- {
- ((TArrayTreeNode *)Arrays->Objects[i])->XCoor = i;
- }
- //Nodes->Clear();
- delete TreeFileList;
- delete ArrayListIndex;
- }
-
- Loaded = true;
- delete ListIndex;
- delete DataFileList;
- delete LineList;
- delete IsData;
- return (sqrt(Sum2/DCount));
- }
- __fastcall TGeneData::TGeneData() : TObject()
- {
- }
- __fastcall TGeneData::TGeneData(int nCols) : TObject()
- {
- Columns = nCols;
- try
- {
- fMask = new bool[nCols];
- fData = new double[nCols];
- }
- catch (EAccessViolation &E)
- {
- }
- }
- __fastcall TGeneData::~TGeneData()
- {
- delete fMask;
- delete fData;
- }
- void __fastcall TGeneCluster::MakeThumbnail(TImage *Image,
- double ScaleX, double ScaleY, double Contrast, double MaskVal,
- TColor PositiveColor, TColor ZeroColor, TColor NegativeColor,
- TColor MissingColor)
- {
- int i,j,k;
- TRect Rect;
- if (Bitmap != NULL)
- {
- delete Bitmap;
- }
- Bitmap = new Graphics::TBitmap();
- Bitmap->Height = Rows;
- Bitmap->Width = Columns;
- Bitmap->PixelFormat = 6;
- unsigned char *ScanLine;
- unsigned char *Color;
- Color = new unsigned char[3];
- DWORD DPositiveColor = ColorToRGB(PositiveColor);
- unsigned char *Positive;
- //Positive = new unsigned char[3];
- Positive = (unsigned char *)&DPositiveColor;
- DWORD DZeroColor = ColorToRGB(ZeroColor);
- unsigned char *Zero;
- //Zero = new unsigned char[3];
- Zero = (unsigned char *)&DZeroColor;
- DWORD DNegativeColor = ColorToRGB(NegativeColor);
- unsigned char *Negative;
- //Negative = new unsigned char[3];
- Negative = (unsigned char *)&DNegativeColor;
- DWORD DMissingColor = ColorToRGB(MissingColor);
- unsigned char *Missing;
- //Missing = new unsigned char[3];
- Missing = (unsigned char *)&DMissingColor;
- double Factor;
- TGeneTreeNode *GeneTreeNode;
- for (i=0;i<Genes->Count;i++)
- {
- ScanLine = (unsigned char *) Bitmap->ScanLine[i];
- GeneTreeNode = (TGeneTreeNode *) Genes->Objects[i];
- if ( (GeneTreeNode->Data->ID == "NULL") || (GeneTreeNode->Data->ID == "NONE") )
- {
- for (j=0;j<Columns;j++)
- {
- ScanLine[3*j] = 255; //Blue
- ScanLine[3*j+1] = 255; //Green
- ScanLine[3*j+2] = 255; //Red
- }
- }
- else
- {
- for (j=0;j<Columns;j++)
- {
- if (GeneTreeNode->Data->Mask[j] == true)
- {
- if (GeneTreeNode->Data->Data[j] >= MaskVal)
- {
- Factor = min(1.0, GeneTreeNode->Data->Data[j] / Contrast);
- for (k=0;k<3;k++)
- {
- Color[k] = Factor * Positive[k] + (1.0 - Factor) * Zero[k];
- }
- }
- else if (GeneTreeNode->Data->Data[j] <= -MaskVal)
- {
- Factor = min(1.0, -GeneTreeNode->Data->Data[j] / Contrast);
- for (k=0;k<3;k++)
- {
- Color[k] = Factor * Negative[k] + (1.0 - Factor) * Zero[k];
- }
- }
- else
- {
- Color[0] = 0;
- Color[1] = 0;
- Color[2] = 0;
- }
- ScanLine[3*j] = Color[2]; //Blue
- ScanLine[3*j+1] = Color[1]; //Green
- ScanLine[3*j+2] = Color[0]; //Red
- }
- else
- {
- ScanLine[3*j] = Missing[2]; //Blue
- ScanLine[3*j+1] = Missing[1]; //Green
- ScanLine[3*j+2] = Missing[0]; //Red
- }
- }
- }
- }
- /*
- unsigned char *ScanLine;
- TGeneTreeNode *GeneTreeNode;
- for (i=0;i<Genes->Count;i++)
- {
- ScanLine = (unsigned char *) Bitmap->ScanLine[i];
- for (j=0;j<Columns;j++)
- {
- Red = 0;
- Green = 0;
- Blue = 0;
- GeneTreeNode = (TGeneTreeNode *) Genes->Objects[i];
- if (GeneTreeNode->Data->Mask[j] == true)
- {
- if (GeneTreeNode->Data->Data[j] > 0)
- {
- Red = max(0,min(255,(int) (256.0 * GeneTreeNode->Data->Data[j] / Contrast)));
- }
- else
- {
- Green = max(0,min(255,(int) (-256.0 * GeneTreeNode->Data->Data[j] / Contrast)));
- }
- }
- else
- {
- Red = 100;
- Green = 100;
- Blue = 100;
- }
- ScanLine[3*j] = Blue; //Blue
- ScanLine[3*j+1] = Green; //Green
- ScanLine[3*j+2] = Red; //Red
- }
- } */
- Image->Width = Columns * ScaleX;
- Image->Height = Rows * ScaleY;
- Image->Picture->Bitmap = Bitmap;
- delete Color;
- }
- __fastcall TArrayTreeNode::TArrayTreeNode() : TObject()
- {
- IsNode = true;
- Color = clBlack;
- Child1 = NULL;
- Child2 = NULL;
- }
- __fastcall TArrayTreeNode::~TArrayTreeNode()
- {
- delete Data;
- }
- void __fastcall TArrayTreeNode::SetParent(TArrayTreeNode *pParent)
- {
- Parent = pParent;
- if (IsNode == true)
- {
- Child1->SetParent(this);
- Child2->SetParent(this);
- }
- else
- {
- Child1 = NULL;
- Child2 = NULL;
- }
- }
- void __fastcall TArrayTreeNode::SetCoor()
- {
- if (IsNode == true)
- {
- Child1->SetCoor();
- Child2->SetCoor();
- YCoor = 1.0 - ((1.0 - Corr)/2);
- YCoor = min(YCoor, Child1->YCoor);
- YCoor = min(YCoor, Child2->YCoor);
- XCoor = (Child1->XCoor + Child2->XCoor)/2;
- MinCoor = min(Child1->MinCoor,Child2->MinCoor);
- MaxCoor = max(Child1->MaxCoor,Child2->MaxCoor);
- }
- else
- {
- YCoor = 1.0;
- MinCoor = XCoor;
- MaxCoor = XCoor;
- }
- }
- void __fastcall TArrayTreeNode::Draw(TImage *Image, double Scale)
- {
- if (IsNode == true)
- {
- Image->Canvas->MoveTo(Scale/2 + Child1->XCoor * Scale, Child1->YCoor*Image->Height);
- Image->Canvas->LineTo(Scale/2 + Child1->XCoor * Scale, YCoor*Image->Height);
- Image->Canvas->LineTo(Scale/2 + Child2->XCoor * Scale, YCoor*Image->Height);
- Image->Canvas->LineTo(Scale/2 + Child2->XCoor * Scale, Child2->YCoor*Image->Height);
- Child1->Draw(Image, Scale);
- Child2->Draw(Image, Scale);
- YPoint = YCoor * Image->Height;
- XPoint = XCoor * Scale;
- }
- }
- void __fastcall TArrayTreeNode::DrawPS(TStringList *PSFile, int XScale, int YScale, int XOrigin, int YOrigin)
- {
- if (IsNode == true)
- {
- AnsiString TempString;
- int X1,Y1,X2,Y2;
- X1 = XOrigin + (XScale/2) + Child1->XCoor*XScale;
- Y1 = YOrigin + (1.0 - Child1->YCoor) * YScale;
- X2 = XOrigin + (XScale/2) + Child1->XCoor*XScale;
- Y2 = YOrigin + (1.0 - YCoor) * YScale;
- TempString = AnsiString(X1) + " " + AnsiString(Y1) + " " +
- AnsiString(X2) + " " + AnsiString(Y2) + " ln";
- PSFile->Add(TempString);
- X1 = XOrigin + (XScale/2) + Child2->XCoor*XScale;
- Y1 = YOrigin + (1.0 - Child2->YCoor) * YScale;
- X2 = XOrigin + (XScale/2) + Child2->XCoor*XScale;
- Y2 = YOrigin + (1.0 - YCoor) * YScale;
- TempString = AnsiString(X1) + " " + AnsiString(Y1) + " " +
- AnsiString(X2) + " " + AnsiString(Y2) + " ln";
- PSFile->Add(TempString);
- X1 = XOrigin + (XScale/2) + Child1->XCoor*XScale;
- Y1 = YOrigin + (1.0 - YCoor) * YScale;
- X2 = XOrigin + (XScale/2) + Child2->XCoor*XScale;
- Y2 = YOrigin + (1.0 - YCoor) * YScale;
- TempString = AnsiString(X1) + " " + AnsiString(Y1) + " " +
- AnsiString(X2) + " " + AnsiString(Y2) + " ln";
- PSFile->Add(TempString);
- Child1->DrawPS(PSFile, XScale, YScale, XOrigin, YOrigin);
- Child2->DrawPS(PSFile, XScale, YScale, XOrigin, YOrigin);
- }
- }
- __fastcall TArrayData::TArrayData() : TObject()
- {
- }
- __fastcall TArrayData::~TArrayData()
- {
- }
- void __fastcall TGeneTreeNode::MakeBarcode(TImage *Image, int ScaleX, int ScaleY,
- double Contrast, TColor PositiveColor, TColor ZeroColor, TColor NegativeColor,
- TColor MissingColor)
- {
- int i,j,k;
- unsigned char *Color;
- Color = new unsigned char[3];
- DWORD DPositiveColor = ColorToRGB(PositiveColor);
- unsigned char *Positive;
- //Positive = new unsigned char[3];
- Positive = (unsigned char *)&DPositiveColor;
- DWORD DZeroColor = ColorToRGB(ZeroColor);
- unsigned char *Zero;
- //Zero = new unsigned char[3];
- Zero = (unsigned char *)&DZeroColor;
- DWORD DNegativeColor = ColorToRGB(NegativeColor);
- unsigned char *Negative;
- //Negative = new unsigned char[3];
- Negative = (unsigned char *)&DNegativeColor;
- DWORD DMissingColor = ColorToRGB(MissingColor);
- unsigned char *Missing;
- //Missing = new unsigned char[3];
- Missing = (unsigned char *)&DMissingColor;
- double Factor;
- TRect Rect;
- Rect.Top = 0;
- Rect.Bottom = ScaleY;
- Image->Canvas->Pen->Style = psClear;
- Image->Canvas->Brush->Style = bsSolid;
- for (j=0;j<Data->Columns;j++)
- {
- Color[0] = Missing[0];
- Color[1] = Missing[1];
- Color[2] = Missing[2];
- if (Data->Mask[j] == true)
- {
- if (Data->Data[j] > 0)
- {
- Factor = min(1.0, Data->Data[j] / Contrast);
- for (k=0;k<3;k++)
- {
- Color[k] = Factor * Positive[k] + (1.0 - Factor) * Zero[k];
- }
- }
- else
- {
- Factor = min(1.0, -Data->Data[j] / Contrast);
- for (k=0;k<3;k++)
- {
- Color[k] = Factor * Negative[k] + (1.0 - Factor) * Zero[k];
- }
- }
- }
- Image->Canvas->Brush->Color = RGB(Color[0],Color[1],Color[2]);
- Rect.Left = j * ScaleX;
- Rect.Right = (j + 1) * ScaleX;
- Image->Canvas->FillRect(Rect);
- }
- delete Color;
- }