Skip to content

Commit

Permalink
read date format year-month-date for options -a and -z
Browse files Browse the repository at this point in the history
  • Loading branch information
Thu-Hien To committed Apr 16, 2020
1 parent 69e260a commit 3baa26d
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 24 deletions.
Binary file modified bin/lsd2_mac
Binary file not shown.
Binary file modified bin/lsd2_unix
Binary file not shown.
10 changes: 9 additions & 1 deletion src/confidence_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,15 @@ void output(double br,int y, Pr* pr,Node** nodes,ostream& f,ostream& tree1,ostre
}
if (pr->relative) {
std::ostringstream oss;
oss<<"- The results correspond to the estimation of relative dates when T[mrca]="<<pr->mrca<<" and T[tips]="<<pr->leaves<<"\n";
ostringstream tMRCA,tLeaves;
if (pr->outDateFormat==2){
tMRCA<<realToYearMonthDay(pr->mrca);
tLeaves<<realToYearMonthDay(pr->leaves);
} else {
tMRCA<<pr->mrca;
tLeaves<<pr->leaves;
}
oss<<"- The results correspond to the estimation of relative dates when T[mrca]="<<tMRCA.str()<<" and T[tips]="<<tLeaves.str()<<"\n";
pr->warningMessage.push_back(oss.str());
}
ostringstream tMRCA;
Expand Down
89 changes: 69 additions & 20 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Pr* getOptions( int argc, char** argv )

Pr* getCommandLine( int argc, char** argv)
{
const string VERSION="v1.6.4";
const string VERSION="v1.6.5";
Pr* opt = new Pr();
int c;
string s;
Expand All @@ -37,7 +37,8 @@ Pr* getCommandLine( int argc, char** argv)
fflag=false,
lflag=false,
uflag=false,
vflag=false;
vflag=false,
validDate = true;
while ( (c = getopt(argc, argv, ":i:d:D:o:s:n:g:r:v:ct:w:b:ha:z:f:kje:m:p:q:u:l:U:R:S:V")) != -1 )
{
switch (c)
Expand Down Expand Up @@ -165,15 +166,29 @@ Pr* getCommandLine( int argc, char** argv)
opt->givenRate[0] = true;
break;
case 'a':
if( !isInteger(optarg) )
myExit("Argument of option -a must be an integer.\n");
opt->mrca=atof(optarg);
if( isReal(optarg) ){
opt->mrca=atof(optarg);
validDate=true;
}
else{
validDate=readDateFromString(optarg,opt->mrca);
opt->inDateFormat=2;
}
if (!validDate)
myExit("Argument of option -a must be a real or a date format year-month-day.\n");
flagA=true;
break;
case 'z':
if( !isInteger(optarg) )
myExit("Argument of option -z must be an integer.\n");
opt->leaves=atof(optarg);
if( isReal(optarg) ){
opt->leaves=atof(optarg);
validDate=true;
}
else{
validDate=readDateFromString(optarg,opt->leaves);
opt->inDateFormat=2;
}
if (!validDate)
myExit("Argument of option -z must be a real or a date format year-month-day.\n");
flagZ=true;
break;
case 'h':
Expand Down Expand Up @@ -258,6 +273,7 @@ Pr* getCommandLine( int argc, char** argv)
opt->leaves=1;
}
if (!dflag && (flagA && flagZ)){
if (opt->inDateFormat==2 && opt->outDateFormat==0) opt->outDateFormat=2;
if (opt->mrca >= opt->leaves)
myExit("The root date must be strictly smaller than the tips date.\n");
opt->relative=true;
Expand Down Expand Up @@ -291,14 +307,17 @@ Pr* getInterface()
do{
fgets(letter,3,stdin);
if (*letter=='n' || *letter=='N') {
cout<<"There is no date file, so the program will estimate relative dates with root date = 0 and tips date = 1. Type 'y' to continue or 'n' to modify the root date and the tips date"<<endl;
cout<<"There is no date file, so the program will estimate relative dates with root date = 0 and tips date = 1.\n Type 'y' to continue or 'n' to modify the root date and the tips date"<<endl;
char letter1[3];
do {
fgets( letter1, 3, stdin );
if (*letter1=='n' || *letter1=='N'){
do {
opt->mrca = getInputReal("Enter the root date (default=0)> ");
opt->leaves = getInputReal("Enter the tips date (default=1)> ");
int dateTypeA, dateTypeZ;
opt->mrca = getInputDate("Enter the root date (default=0)> ",dateTypeA);
opt->leaves = getInputDate("Enter the tips date (default=1)> ",dateTypeZ);
if (dateTypeA == 2 || dateTypeZ==2) opt->inDateFormat=2;
else opt->inDateFormat=1;
if (opt->leaves <= opt->mrca) cout<<"Root date must be smaller than the tips date."<<endl;
} while (opt->leaves <= opt->mrca);
}
Expand Down Expand Up @@ -338,19 +357,29 @@ Pr* getInterface()
if (opt->nullblen<0){
opt->nullblen = 0.5/opt->seqLength;
}
if (opt->inDateFormat==2 && opt->outDateFormat==0) opt->outDateFormat=2;
return opt;
}


void printInterface(ostream& in, Pr* opt)
{
const string VERSION = "v1.6.4";
const string VERSION = "v1.6.5";

in<<"\nLEAST-SQUARE METHODS TO ESTIMATE RATES AND DATES - "<<VERSION<<" \n\n";
in<<"\nInput files:\n";
in<<" i Input tree file : "<<opt->inFile.c_str()<<"\n";
if (opt->relative==true)
in<<" d Estimate relative dates : mrca date = "<<opt->mrca<<", tips date ="<<opt->leaves<<"\n";
if (opt->relative==true){
ostringstream tMRCA,tLeaves;
if (opt->inDateFormat==2){
tMRCA<<realToYearMonthDay(opt->mrca);
tLeaves<<realToYearMonthDay(opt->leaves);
} else {
tMRCA<<opt->mrca;
tLeaves<<opt->leaves;
}
in<<" d Estimate relative dates : mrca date = "<<tMRCA.str()<<", tips date = "<<tLeaves.str()<<"\n";
}
else
in<<" d Input date file : "<<opt->inDateFile.c_str()<<"\n";
in<<" p Partition file : ";
Expand Down Expand Up @@ -475,7 +504,7 @@ void printHelp( void )
const string BOLD = "\033[00;01m";
const string LINE = "\033[00;04m";
const string FLAT = "\033[00;00m";
const string VERSION = "v1.6.4";
const string VERSION = "v1.6.5";

cout<<BOLD<<"LSD: LEAST-SQUARES METHODS TO ESTIMATE RATES AND DATES - "<<VERSION<<"\n\n";
cout<<BOLD<<"DESCRIPTION\n"
Expand Down Expand Up @@ -693,6 +722,23 @@ double getInputReal( string msg )
return atof( word.c_str() );
}

double getInputDate( string msg ,int& type)
{
string word = getInputString( msg );
double date;
bool validDate = true;
if( isReal(word.c_str()) ){
date = atof( word.c_str() );
type = 1;
validDate = true;
} else{
validDate = readDateFromString(word.c_str(), date);
type = 2;
}
if (!validDate) myErrorMsg("Your word is not recognized as a real or a valid date format year-month-day.\n");
return date;
}

double getInputPositiveReal( string msg )
{
string word;
Expand Down Expand Up @@ -799,14 +845,17 @@ void setOptionsWithLetter( Pr* opt, char letter)
do{
fgets(letter,3,stdin);
if (*letter=='n' || *letter=='N') {
cout<<"There is no date file, so the program will estimate relative dates with root date = 0 and tips date = 1. Type 'y' to continue or 'n' to modify the root date and the tips date"<<endl;
cout<<"There is no date file, so the program will estimate relative dates with root date = 0 and tips date = 1.\n Type 'y' to continue or 'n' to modify the root date and the tips date"<<endl;
char letter1[3];
do {
fgets( letter1, 3, stdin );
if (*letter1=='n' || *letter1=='N'){
do {
opt->mrca = getInputReal("Enter the root date (default=0)> ");
opt->leaves = getInputReal("Enter the tips date (default=1)> ");
int dateTypeA, dateTypeZ;
opt->mrca = getInputDate("Enter the root date (default=0)> ",dateTypeA);
opt->leaves = getInputDate("Enter the tips date (default=1)> ",dateTypeZ);
if (dateTypeA == 2 || dateTypeZ==2) opt->inDateFormat=2;
else opt->inDateFormat=1;
if (opt->leaves <= opt->mrca) cout<<"Root date must be smaller than the tips date."<<endl;
} while (opt->leaves <= opt->mrca);
}
Expand All @@ -828,8 +877,8 @@ void setOptionsWithLetter( Pr* opt, char letter)
break;
case 'D':
if (opt->outDateFormat==0) opt->outDateFormat=1;
if (opt->outDateFormat==1) opt->outDateFormat=2;
if (opt->outDateFormat==2) opt->outDateFormat=0;
else if (opt->outDateFormat==1) opt->outDateFormat=2;
else if (opt->outDateFormat==2) opt->outDateFormat=0;
break;
case 'p':
opt->partitionFile = getInputFileName("Enter your Partition File name> ");
Expand Down
1 change: 1 addition & 0 deletions src/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ string getOutgroupFileName( string );
void printInterface(ostream& result, Pr*);
void setOptionsWithLetter( Pr* , char);
double getInputReal( string );
double getInputDate( string msg, int& type );
double getInputPositiveReal( string );
int getInputInteger( string );
int getPositiveInputInteger( string );
Expand Down
68 changes: 65 additions & 3 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,33 @@ double readDate1(istream& f,string fn,char c,Pr* pr){
exit(EXIT_FAILURE);
}

bool readDateFromString(const char* str,double& f){
string y;
while( *str!='\0' && *str>='0' && *str<='9'){
y = y+(*str);
str++;
}
if (*str=='-'){
str++;
string m;
while( *str!='\0' && *str>='0' && *str<='9'){
m = m+(*str);
str++;
}
if(*str=='-'){
str++;
string d;
while( *str!='\0' && *str>='0' && *str<='9'){
d = d+(*str);
str++;
}
f = (atoi(y.c_str())+monthDayToReal(atoi(m.c_str()),atoi(d.c_str())));
return true;
}
}
return false;
}

vector<double> read_double_from_line(string line){
stringstream ss(line);
vector<double> results;
Expand Down Expand Up @@ -822,12 +849,47 @@ void myErrorMsg( string msg, ... )

bool isReal( const char* str )
{
while( *str!='\0' )
if( *str=='-' )
str++;
while( *str!='\0' && *str!='e' && *str!= 'E' && *str!='.')
{
if( !( ('0'<=*str && *str<='9') || *str=='e' || *str=='E' || *str=='.' || *str=='-' ) )
if( !( ('0'<=*str && *str<='9')))
return false;
str++;
}
if (*str=='.'){
str++;
while( *str!='\0' && *str!='e' && *str!= 'E'){
if( !( ('0'<=*str && *str<='9')))
return false;
str++;
}
if (*str=='e' || *str=='E'){
str++;
if (*str=='-' || *str=='+'){
str++;
while( *str!='\0'){
if( !( ('0'<=*str && *str<='9'))) return false;
str++;
}
} else {
return false;
}
} else {
return true;
}
} else if (*str=='e' || *str=='E'){
str++;
if (*str=='-' || *str=='+'){
str++;
while( *str!='\0'){
if( !( ('0'<=*str && *str<='9'))) return false;
str++;
}
} else {
return false;
}
}
return true;
}

Expand Down Expand Up @@ -2342,7 +2404,7 @@ void imposeMinBlen(ostream& file,Pr* pr, Node** nodes, double median_rate,bool m
if (round_time==365) unit=" days";
if (round_time==52) unit=" weeks";
if (medianRateOK && pr->minblen<0){
if (!pr->relative){
if (!pr->relative || pr->inDateFormat==2 || pr->round_time!=-1){
minblen = round(round_time*m)/(double)round_time;
if (pr->minblenL < 0){
cout<<"Minimum branch length of time scaled tree (settable via option -u and -U): "<<m<<", rounded to "<<minblen<<" ("<<round(round_time*m)<<unit<<"/"<<round_time<<") using factor "<<round_time<<" (settable via option -R)"<<endl;
Expand Down
2 changes: 2 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ double readDate(istream& f,string fn,Pr* pr);

double readDate1(istream& f,string fn,char c,Pr* pr);

bool readDateFromString(const char* st,double& f);

vector<double> read_double_from_line(string line);

int readInt(istream& f,string msg);
Expand Down

0 comments on commit 3baa26d

Please sign in to comment.