#!/bin/sh

echo "This script will compare inter-individual variability in gene expression in mouse neutrophils, to miR-223-guided repression of TargetScan-predicted mRNA targets for that mRNA (as measured by Baek et al., 2008)."
echo ""


###Below: if input data is not transformed (not our case)
#log=F
###Above: if input data is not transformed (not our case)
###Below: if input data is log-transformed (our case)
log=T
###Above: if input data is log-transformed (our case)

echo "library(MASS)" > R_variability_vs_miR-guided_every_quantified_mRNA
for sample in `grep ' pooled$' 00Sample_nomenclature | awk '{print $1}'`
do echo "sample"$sample"=read.table('"$sample"_RMA\ -\ Group 1.TXT',sep='\\\t',header=T)"
done >> R_variability_vs_miR-guided_every_quantified_mRNA

for sample in `grep ' pooled$' 00Sample_nomenclature | awk '{print $1}'`
do echo "sample"$sample"\$X"$sample".ga.rma.Signal,"
done | sed -e '1 s|^|all_rep=array(c(|' -e '$ s|,$|),dim=c(length(sample'`grep ' pooled$' 00Sample_nomenclature | awk '{print $1}' | head -1`'\$Probe.Set.ID),'`grep -c ' pooled$' 00Sample_nomenclature`'))|' >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "means=apply(all_rep,1,mean)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "tmp_tech=list(sample"`grep -m 1 ' pooled$' 00Sample_nomenclature | awk '{print $1}'`"\$Probe.Set.ID,means," >> R_variability_vs_miR-guided_every_quantified_mRNA
for sample in `grep ' pooled$' 00Sample_nomenclature | awk '{print $1}'`
do echo "sample"$sample"\$X"$sample".ga.rma.Signal,"
done | sed '$ s|,$|)|' >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "names(tmp_tech)=c('Probeset','Mean'," >> R_variability_vs_miR-guided_every_quantified_mRNA
for sample in `grep ' pooled$' 00Sample_nomenclature | awk '{print $1}'`
do echo "'Sample"$sample"',"
done | sed '$ s|,$|)|' >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "tech=as.data.frame(tmp_tech)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "for_ordering=order(tech\$Mean[substr(as.character(tech\$Probeset),1,5)!='AFFX-'])" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "ordered_tech_names=tech\$Probeset[substr(as.character(tech\$Probeset),1,5)!='AFFX-'][for_ordering]" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "ordered_tech_means=tech\$Mean[substr(as.character(tech\$Probeset),1,5)!='AFFX-'][for_ordering]" >> R_variability_vs_miR-guided_every_quantified_mRNA
for sample in `grep ' pooled$' 00Sample_nomenclature | awk '{print $1}'`
do echo "ordered_tech_sample"$sample"=tech\$Sample"$sample"[substr(as.character(tech\$Probeset),1,5)!='AFFX-'][for_ordering]"
done >> R_variability_vs_miR-guided_every_quantified_mRNA
### Below: in order to extract the decile of probesets centered on the probeset of interest, and ranked by mean signal among technical replicates (we'll consider the 5% of probesets below the probeset of interest, and the 5% of probesets above it)
echo "interval=round(length(ordered_tech_names)/20)" >> R_variability_vs_miR-guided_every_quantified_mRNA
### Above: in order to extract the decile of probesets centered on the probeset of interest, and ranked by mean signal among technical replicates (we'll consider the 5% of probesets below the probeset of interest, and the 5% of probesets above it)
for sample in `grep ' pooled$' 00Sample_nomenclature | awk '{print $1}'`
do echo "ordered_tech_sample"$sample","
done | sed -e '1 s|^|technical_replicates=cbind(|' -e '$ s|,$|)|' >> R_variability_vs_miR-guided_every_quantified_mRNA

for sample in `grep ' individual$' 00Sample_nomenclature | awk '{print $1}'`
do echo "sample"$sample"=read.table('"$sample"_RMA\ -\ Group 1.TXT',sep='\\\t',header=T)"
done >> R_variability_vs_miR-guided_every_quantified_mRNA
for sample in `grep ' individual$' 00Sample_nomenclature | awk '{print $1}'`
do echo "sample"$sample"\$X"$sample".ga.rma.Signal,"
done | sed -e '1 s|^|all_rep=array(c(|' -e '$ s|,$|),dim=c(length(sample'`grep ' individual$' 00Sample_nomenclature | awk '{print $1}' | head -1`'\$Probe.Set.ID),'`grep -c ' individual$' 00Sample_nomenclature`'))|' >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "means=apply(all_rep,1,mean)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "tmp_biol=list(sample"`grep -m 1 ' individual$' 00Sample_nomenclature | awk '{print $1}'`"\$Probe.Set.ID,means," >> R_variability_vs_miR-guided_every_quantified_mRNA
for sample in `grep ' individual$' 00Sample_nomenclature | awk '{print $1}'`
do echo "sample"$sample"\$X"$sample".ga.rma.Signal,"
done | sed '$ s|,$|)|' >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "names(tmp_biol)=c('Probeset','Mean'," >> R_variability_vs_miR-guided_every_quantified_mRNA
for sample in `grep ' individual$' 00Sample_nomenclature | awk '{print $1}'`
do echo "'Sample"$sample"',"
done | sed '$ s|,$|)|' >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "biol=as.data.frame(tmp_biol)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "significance_of_measured_distances <- function(probeset,sigma,d)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
for sample in `grep ' individual$' 00Sample_nomenclature | awk '{print $1}'`
do echo "biol\$Sample"$sample"[biol\$Probeset==probeset],"
done | sed -e '1 s|^|measured=c(|' -e '$ s|,$|)|' >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "distances=c()" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "minimal=1" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "maximal=2" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "for (i in 1:(length(measured)-1))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "for (j in (i+1):length(measured))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "if (abs(measured[i]-measured[j])>abs(measured[minimal]-measured[maximal]))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "if (measured[i]<measured[j])" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "minimal=i" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "maximal=j" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "} else" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "minimal=j" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "maximal=i" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "truc=list(measured[maximal]-measured[minimal],deconvolutes_variability(measured[minimal],measured[maximal],sigma,d),minimal,maximal)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "names(truc)=c('Expr_variability','p_value','Lowest_expr','Highest_expr')" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "return(as.data.frame(truc))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "technical_variability <- function(probeset)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "interval1=max(1,c(1:length(ordered_tech_names))[ordered_tech_names==probeset]-interval)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "interval2=min(length(ordered_tech_names),c(1:length(ordered_tech_names))[ordered_tech_names==probeset]+interval)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "technical_normalized=c()" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "for (i in (interval1:interval2))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "technical_normalized=c(technical_normalized,as.numeric(technical_replicates[i,])/mean(as.numeric(technical_replicates[i,])))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "return(fitdistr(technical_normalized,'normal')\$estimate[2]*mean(as.numeric(technical_replicates[c(1:length(ordered_tech_names))[ordered_tech_names==probeset],])))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "deconvolutes_variability <- function(x1,x2,sigma,d)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "picked_x=rnorm(100000,x1,sigma)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "picked_y=rnorm(100000,x2,sigma)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "return(length(picked_x[abs(picked_y-picked_x)<=d])/length(picked_x))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "" >> R_variability_vs_miR-guided_every_quantified_mRNA
### Below: to analyze every TargetScan 6.1 - predicted target whose miR-223-guided repression of mRNA abundance was quantified by Baek et al., 2008
echo "Baek_data=read.table('mRNA_fold-change_in_Baek_2008.dat',header=T)" >> R_variability_vs_miR-guided_every_quantified_mRNA
### Above: to analyze every TargetScan 6.1 - predicted target whose miR-223-guided repression of mRNA abundance was quantified by Baek et al., 2008
echo "Probeset_annotation=read.csv('Probeset_annotation.csv',header=T)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "for (gene in Baek_data\$Gene)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA

echo "probes=Probeset_annotation\$Probe.Set.ID[Probeset_annotation\$Gene.Symbol==gene]" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "for (probe in probes)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "sigma=technical_variability(probe)" >> R_variability_vs_miR-guided_every_quantified_mRNA
###Below: use the mean of probeset fold-changes in Baek et al's experiment as a measurement of miR-233-guided repression
echo "for (d in Baek_data\$log2FC_means[Baek_data\$Gene==gene])" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
###Below: 'abs(d)' and not 'd', so that potential miRNA-guided up-regulation is properly handled
echo "output=significance_of_measured_distances(probe,sigma,abs(d))" >> R_variability_vs_miR-guided_every_quantified_mRNA
###Above: 'abs(d)' and not 'd', so that potential miRNA-guided up-regulation is properly handled
echo "sink('Variability_vs_miR-guided_every_quantified_mRNA.txt',append=T)" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(c('Gene: Probeset: miRNA-guided fold-change in mRNA abundance (mean-based):'))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(c(gene,probe,signif(d,digits=4)))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(signif(output,digits=4))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(c('Minimal p-value:',signif(min(output\$p_value),digits=4)))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(c('Median p-value:',signif(median(output\$p_value),digits=4)))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print('')" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
###Below: use the median of probeset fold-changes in Baek et al's experiment as a measurement of miR-233-guided repression
echo "for (d in Baek_data\$log2FC_medians[Baek_data\$Gene==gene])" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "{" >> R_variability_vs_miR-guided_every_quantified_mRNA
###Below: 'abs(d)' and not 'd', so that potential miRNA-guided up-regulation is properly handled
echo "output=significance_of_measured_distances(probe,sigma,abs(d))" >> R_variability_vs_miR-guided_every_quantified_mRNA
###Above: 'abs(d)' and not 'd', so that potential miRNA-guided up-regulation is properly handled
echo "print(c('Gene: Probeset: miRNA-guided fold-change in mRNA abundance (median-based):'))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(c(gene,probe,signif(d,digits=4)))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(signif(output,digits=4))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(c('Minimal p-value:',signif(min(output\$p_value),digits=4)))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print(c('Median p-value:',signif(median(output\$p_value),digits=4)))" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "print('')" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "sink()" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
###Below: use Baek et al's measurement of target mRNA fold-change
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA
echo "}" >> R_variability_vs_miR-guided_every_quantified_mRNA

R CMD BATCH R_variability_vs_miR-guided_every_quantified_mRNA

echo "Gene Probeset Quantification_of_miRNA_effect Quantified_miRNA_effect p-value"
for gene in `grep -A 1 '^\[1\] "Gene: Probeset: miRNA-guided fold-change in ' Variability_vs_miR-guided_every_quantified_mRNA.txt | grep -v '^\[1\] "Gene: Probeset: miRNA-guided fold-change in ' | grep -v '\-\-' | awk '{print $2}'| sed 's|"||g' | sort | uniq`
do sed '/^\[1\] "Gene: Probeset: miRNA-guided fold-change in / {
N
s|\(.*\)\n\(.*\)|\1 \2|
}' Variability_vs_miR-guided_every_quantified_mRNA.txt | sed '/\[1\] "'$gene'"/,/^\[1\] ""$/ !d' | grep -v '^[ 0-9]' | grep -v '^\[1] "Median' | grep -v '^\[1\] ""' | sed '/^\[1\] "Gene:/ {
N
s|^\[1\] *"Gene: Probeset: miRNA-guided fold-change in mRNA abundance (\(.*\)):"  *\[1\] "\(.*\)"  *"\(.*\)"  *"\([0-9\.-]*\)" *\n\[1\] "Minimal p-value:" "\([0-9\.e-]*\)" *$|\2 \3 \1 \4 \5|
}'
done
