# check the file number make sure no other files in the command line.

if($ARGV[0])
{
  die("\n!!!!!WARNING: you shouldn't have any parameter after the program name!!!!!\n perl changeflagandbadspot.pl");
}
$flagfile; #this is flag informatin file;
$normdatafile; # this file is from RMAANOVA normalization.
@allfilecutoff; #hold the cutoffvalue

while(1)  #######################input and check the flag file name typing.
{
    print "Please enter the flag information file(.txt): \n";
    $flagfile=<STDIN>;
    chomp $flagfile;
    $flagfile=~s/\s*//;
    if($flagfile=~m/.+\.txt$/)
    {
             last;
    }
}
while(1) #################input and check normalized data file.
{
    print "\n\nPlease Enter the normalized data file name which is from Rmaanova\n";
    print "Normalized data file name(Extension is .txt)\n\n";
    $normdatafile=<STDIN>;
    chomp $normdatafile;
    $normdatafile=~s/\s*//;
#check the output file formatf
    if($normdatafile=~m/.+\.txt$/)
    {
             last;
    }
}

@allfilecutoff;
$filenumber=0;
$ifornotfoundcontrol=0; 
###########################";
########################################determine the cutoff values
open NORMDATAFILE, "$normdatafile" or die "Cannot open $normdatafile for read\n";
open FLAG, "$flagfile" or die "cannot open $flagfile for reading\n";
$firstlinefromflagfile=<FLAG>; # this line pass the control information and replicates information.
chomp $firstlinefromflagfile;
$firstlinefromflagfile=removebothendspace($firstlinefromflagfile);
print "the 1st line is $firstlinefromflagfile\n";
$controlgene;
$arbitraryvalue;
$replicates;
($controlgene, $arbitraryvalue, $replicates)=split/and/, $firstlinefromflagfile;
print "control gene is $controlgene dddd\n";
if(!($arbitraryvalue==0))
{
	$arbitraryvalue=log_base(2,$arbitraryvalue);
	print "the arbitrary value is $arbitraryvalue\n";
}
print "rep is $replicates\n";
while($line=<NORMDATAFILE>)
{
	if($filenumber==0)
	{
		$line=~m/^(\d+\t+\d+\t+\d+\t+\d+\t+\S+)(.+)/; # $1 will contains the spot location information. $2 will contain the real data information.
		$realdata=$2;
		$realdata=removebothendspace($realdata);
		@realdataarray=split/\t/, $realdata;
		$filenumber=@realdataarray/2; #because the file doesn't contain the flag information.
	}
	if($line=~m/$controlgene/ and $arbitraryvalue==0)
	{
		$line=~m/^(\d+\t+\d+\t+\d+\t+\d+\t+\S+)(.+)/; # $1 will contains the spot location information. $2 will contain the real data information.

		$realdata=$2;
		$realdata=removebothendspace($realdata);
		@humancontroldataarray=split/\t/, $realdata;

		$filenumber=@humancontroldataarray/2; #because the file doesn't contain the flag information.
		print "file number is $filenumber\n";
		for($i=0; $i<$filenumber; $i++)
		{
			push @{$humancontrol[$i]}, $humancontroldataarray[$i*2];
			push @{$humancontrol[$i]}, $humancontroldataarray[$i*2+1];
		}
		$ifornotfoundcontrol=1; # indicate we found the control spot;
	}
	if(!($arbitraryvalue==0))
	{
		$ifornotfoundcontrol=1;
	}
}
if(!$ifornotfoundcontrol)
{
	die("\nyou don't have the control $controlgene in your slides\n");
}
for($i = 0; $i < $filenumber;  $i++)
{
	if(!($arbitraryvalue==0))
	{
		push @allfilecutoff, $arbitraryvalue;
	}
	else
	{
		$medianoffile=median(@{$humancontrol[$i]});
		push @allfilecutoff, $medianoffile;
	}
}
print "\nthe cutoff value of the file is @allfilecutoff\n";
close(NORMDATAFILE);
close(FLAG);
########################################end of cutoff values

###open output file for writing.

open NORMDATAFILE, "$normdatafile" or die "Cannot open $normdatafile for read\n";
open FLAG, "$flagfile" or die "cannot open $flagfile for reading\n";
open OUTFILE, ">forsasinput.txt" or die "Cannot open forsasinput.txt for write\n";
open SUMMARY, ">summary.txt" or die "can't open summary.txt to write\n";

@allusefulcolumn;          #this will hold the all column number of F635, F532 and flags for each file.
@stringforinputfile;

#####################title line writing###############
$firstlinefromflagfile=<FLAG>; # this line pass the control information and replicates information.
$secondlinefromflagfile=<FLAG>;
$firstlinefromnormdatafile=<NORMDATAFILE>;
chomp $firstlinefromflagfile;
chomp $secondlinefromflagfile;
chomp $firstlinefromnormdatafile;
@columnname_newfile=split(/\t/, $secondlinefromflagfile);

for($i=0; $i<@columnname_newfile; $i++)
{
	if($columnname_newfile[$i]=~m/[fF]lag/)
	{
		splice(@columnname_newfile, $i, 1);
		$i--;
	}
}
#print "@columnname_newfile\n";
print OUTFILE join("\t",@columnname_newfile), "\n";
print scalar(@columnname_newfile);
#####################end of title line writing############

#################################################################
while($linefromnormdatafile=<NORMDATAFILE> and $linefromflag=<FLAG>)
{
	chomp $linefromnormdatafile;
	chomp $linefromflag;
	
	@flag_columns=split/\t/, $linefromflag;
	@data_columns=split/\t/, $linefromnormdatafile;
	
	for($i=0; $i<$filenumber; $i++)
	{
		$sum=$data_columns[$i*2+5]+$data_columns[$i*2+5+1];
		$doublelog=2*$allfilecutoff[$i];
		if($sum<$doublelog)
		{
			#$data_columns[$i*2+5]=$allfilecutoff[$i];   ####change the low value to the cutofvalue.
			#$data_columns[$i*2+5+1]=$allfilecutoff[$i];
			$data_columns[$i*2+5]=".";         ###change the low value to "."
			$data_columns[$i*2+5+1]=".";
		}

		if(!($flag_columns[$i+5]==0)) # change the flagged spot to ".";
		{
			$data_columns[$i*2+5]=".";
			$data_columns[$i*2+5+1]=".";
		}

	}

	$inputlinefornewfile="";
	for($i=0; $i<@data_columns; $i++)
	{
		if($inputlinefornewfile eq "")
		{
			$inputlinefornewfile=$data_columns[$i];
		}
		else
		{
			$inputlinefornewfile=$inputlinefornewfile."\t".$data_columns[$i];
		}
	}
	print OUTFILE $inputlinefornewfile, "\n";
}
close(OUTFILE);
#################################################################


######read the newly generated files and check how many replicated each gene have after imputation and write to a new file called replicatesforeachgene.txt.
#$replicates=4;
open REPLICATE, ">replicatesforeachgene.txt" or die "can't open replicatesforeachgene.txt for writing\n";
open newlygenerated, "forsasinput.txt" or die "can't open forsasinput.txt for reading.\n";
$line=<newlygenerated>;
chomp $line;
$formatnumberofcolumns=5; # this is row column metarow ect
@arrayoftitlecolumn=split/\t/, $line;
$slidenumber=(@arrayoftitlecolumn-$formatnumberofcolumns)/(2*$replicates);
print "slidenumber is $slidenumber\n";
##build the title for the REPLICATE file
$title="Meta Row\tMeta Column\tGrid Row\tGrid Column\tGB ID";
for($i=0; $i<$slidenumber; $i++)
{
	$j=$i+1;
	$title=$title."\t"."Array".$j;
	
}
##end of title building of REPLICATE.
writetoreplicate($title);
while($line=<newlygenerated>)
{
	chomp $line;
	#extract the first 5 columns
	@newlinearray=split/\t/, $line;
	$newinputline=$newlinearray[0]."\t".$newlinearray[1]."\t".$newlinearray[2]."\t".$newlinearray[3]."\t".$newlinearray[4];
	
	for($i=0; $i<$slidenumber; $i++)
	{
		$currentslidereplicates=0;

		for($j=0; $j<$replicates; $j++)
		{
			$currentintensityindex=$formatnumberofcolumns+$i*2+$j*2*$slidenumber;
			if(!($newlinearray[$currentintensityindex] eq "."))
			{
				$currentslidereplicates++;	
			}
			
		}
		$newinputline=$newinputline."\t".$currentslidereplicates;
	}
		

		writetoreplicate($newinputline);
	
}######################################################


sub mean
{
	(@data)=@_;
	my $sum;
	foreach $item (@data) {
		$sum+=$item;
	}
	return($sum/@data);
}
sub median
{
	my(@data)=sort { $a <=> $b} @_;
	if (scalar(@data)%2) {
			return($data[@data/2]);
	} else {
			my($upper, $lower);
			$lower=$data[@data/2];
			$upper=$data[@data/2 - 1];
			return(mean($lower, $upper));
	}
}
sub removebothendspace
{
	my $item=$_[0];
	$item=~s/^\s+//;
	$item=~s/\s+$//;
	return $item;
}
sub log_base {
    my ($base, $value) = @_;
    return log($value)/log($base);
}
sub writetoreplicate
{
	$newline=$_[0];
	print REPLICATE $newline, "\n"; 
}