# TODO: Add comment
# 
# Author: Korsching Feb 11, 2014 - GPL v3
###############################################################################



#### 14.2.2014

# workflow 1
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

# create database
db.create.structure(DB.name="brca1x2", DB.path="")	# only once, you cannot overwrite

# create log on import tasks
log.db()	# only once, you cannot overwrite

# fill and append database
import.in.db(import.path.xlsx="/home/korschi/eclipseR/BerndD/data1", DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2")

# search for specific position
aa <- db.get.all.specific.position(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", position=32900364)		# 114
sapply(aa,mode)

# some report
library(lattice)
library(stats)
library(plotrix)

ab <- data.frame(matrix(
		c(median(aa[,"Quality"]), mean(aa[,"Quality"]), range(aa[,"Quality"]), sd(aa[,"Quality"]),
		median(aa[,"Frequency"]), mean(aa[,"Frequency"]), range(aa[,"Frequency"]), sd(aa[,"Frequency"]),
		median(aa[,"Coverage"]), mean(aa[,"Coverage"]), range(aa[,"Coverage"]), sd(aa[,"Coverage"]),
		median(aa[,"Reads_Var"]), mean(aa[,"Reads_Var"]), range(aa[,"Reads_Var"]), sd(aa[,"Reads_Var"]),
		median(aa[,"Balance"]), mean(aa[,"Balance"]), range(aa[,"Balance"]), sd(aa[,"Balance"]) ),
		5, 5, byrow=T
	))
row.names(ab) <- c("Quality","Frequency","Coverage","Reads_Var","Balance")
names(ab) <- c("median","mean","min","max","sd")

pdf(file=paste(getwd(),"/results1/test.",format(Sys.time(), "%Y%m%d%H%M"),".pdf", sep=""),
		width=11, height=7, onefile=T, title="test", pointsize=12)
par(mfrow=c(2,3))
plot(density(x=aa[,5]), main="Quality")
plot(density(x=aa[,6]), main="Frequency")
plot(density(x=aa[,7]), main="Coverage")
plot(density(x=aa[,8]), main="Reads_Var")
plot(density(x=aa[,9]), main="Balance")
plot(1:10,1:10, type="n", axes=F, xlab="", ylab="")
addtable2plot(x=1,y=5,table=round(ab,2),display.colnames=T,display.rownames=T,vlines=T)
dev.off()


### 18.2.2014
# export sample table
db.export.sample(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2")


### 20.2.2014
# extract new mutations from group sample.Sample_mutation

# import revised and extended base table sample
db.overwrite.table.sample(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", data.path.name="/home/korschi/eclipseR/BerndD/data/export.sample.csv")

## extract new mutations from group sample.mutation = 1 or "" -- given the complete group sample.mutation = 0
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
## windows.size=0 -- exact position
result.found.1.mutations.ws0 <- db.get.all.new.mutations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", what1="0", what2="1", window.size=0)
#basic set - total number of lines :  10361
#basic set - number of unique positions :  866
#for result set searched in  3241  lines
#result set - number of lines :  121

result.found.NA.mutations.ws0 <- db.get.all.new.mutations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", what1="0", what2="-1", window.size=0)
#basic set - total number of lines :  10361
#basic set - number of unique positions :  866
#for result set searched in  1796  lines
#result set - number of lines :  24

## windows.size=2 -- relaxed view
#result.found.1.mutations.ws2 <- db.get.all.new.mutations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", what1="0", what2="1", window.size=2)
#basic set - total number of lines :  10361
#basic set - number of unique positions :  866
#for result set searched in  3241  lines
#result set - number of lines :  85

#result.found.NA.mutations.ws2 <- db.get.all.new.mutations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", what1="0", what2="-1", window.size=2)
#basic set - total number of lines :  10361
#basic set - number of unique positions :  866
#for result set searched in  1796  lines
#result set - number of lines :  19


### 25.2.2014
# update sample table mutations == "" -> == "-1"
db <- dbConnect(dbDriver("SQLite"), dbname="/home/korschi/eclipseR/BerndD/brca1x2")
aa <- dbGetQuery(conn=db, "SELECT * FROM sample WHERE mutation=''")
nrow(aa)
aa <- dbSendQuery(conn=db, paste("UPDATE sample SET mutation=-1 WHERE mutation=''",sep=""))
aa <- dbGetQuery(conn=db, "SELECT * FROM sample WHERE mutation=-1")
nrow(aa)
dbDisconnect(db)
# export (version 3) sample and variations table
db.export.sample(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2")
db.export.variations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2")


### 10.3.2014
## extract new mutations from group sample.mutation - including: var and frequency
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
save.image()
get.chr.name(provider="ensembl", db.name="hsapiens_gene_ensembl", gene.symbol="BRCA1")

# w/ var
aa <- db.get.all.new.mutations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2",
		what1="1", what2="2", chr=17, geneToWork="BRCA1", window.size=0)
ab <- db.get.all.new.mutations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2",
		what1="1", what2="2", chr=13, geneToWork="BRCA2", window.size=0)

## create statistics
aa <- db.stat.frequency(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", what1="1", chr=17, geneToWork="")
#all: 27 - 10361

# "T>A"    "G>A"    "delC"   "insC"   "delG"   "delT"   "delA"   "A>G"   
# "C>A"    "T>C"    "insG"   "insA"   "C>T"    "insT"   "A>C"    "G>C"   
# "G>T"    "C>G"    "T>G"    "A>T"    "insTT"  "insTC"  "insAA"  "insTG" 
# "insTTT" "insGG"  "insAT" 

aa <- db.stat.frequency(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", what1="1", chr=17, geneToWork="BRCA1")
# 27 - 4981
aa <- db.stat.frequency(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", what1="1", chr=13, geneToWork="BRCA2")
# 27 - 5380


### 11.3.2014
## ex- import of database : add mutation flag column in variations
db.export.sample(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2")
db.export.variations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2")
# manual changes in tables, and changes in db functions :
db.create.structure(DB.name="brca1x2", DB.path="")
db.overwrite.table.sample(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", data.path.name="/home/korschi/eclipseR/BerndD/data/export.sample.txt")
# 272
db.overwrite.table.variations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", data.path.name="/home/korschi/eclipseR/BerndD/data/export.variations.txt")
# 15398


### 12.3.2014
# analysis for frequency threshold
aa <- db.get.all.specific.position(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", position=41197784)

ab <- db.get.all.specific.position(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", position=41246584)
ac <- db.get.all.specific.position(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", position=41245671)

ad <- db.get.all.specific.position(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", position=32904420)

write.table(x=rbind(aa,ab,ac,ad),
		file="32904420.txt", append=F, quote=F, sep="\t", eol="\n", na="NA", dec=".", row.names=F, col.names=T)


### 27.3.2014
# start from the beginning -- import revised data
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

# create database
db.create.structure(DB.name="brca1x2", DB.path="")	# only once, you cannot overwrite

# create log on import tasks
rm(protocol.import.mass.db)
rm(list.import.data.objects.db)

log.db(log.prefix="")	# only once, you cannot overwrite

# fill and append database
import.in.db(import.path.xlsx="/home/korschi/eclipseR/BerndD/data1", log.prefix="d",
		DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", group.type=0)

#export for Bernd
db.export.variations(DB.path.name=paste(getwd(),"/brca1x2",sep=""), export.path=paste(getwd(),"/data/export.variations28032014.txt",sep=""))
# 13555 rows


### 02.04.2014 - re-import from Bernd
db.overwrite.table.variations(DB.path.name="brca1x2", data.path.name="data/export.variations28032014.csv")


### 4.4.2014 - import new entity : kidney stones  , 17.4.2014

# start from the beginning
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

# create database
db.create.structure(DB.name="kidneySt", DB.path="")	# only once, you cannot overwrite

# create log on import tasks - create different log files
log.db(log.prefix="kidneySt.")

# fill and append database
get.batch.import.number(DB.path.name="/kidneySt")	# 1
import.in.db(import.path.xlsx="/data_kidney", log.prefix="kidneySt.", DB.path.name="/kidneySt")
# error corrected in sheet 26 : frist column header has to be empty (copy empty cell from good sheet to bad sheet first column)
# sil111 001-002-003-007: chr X -> chr 23

# export variants table from new database --  and  correct new database table by mutations and chr from old datbase table
db.export.sample(DB.path.name="/kidneySt", export.path="/data/export.kidneySt.sample17042014.txt")
db.export.variations(DB.path.name="/kidneySt", export.path="/data/export.kidneySt.variations17042014.txt")



### 9.4.2014, 11.4.2014
# start from the beginning -- import revised data

#export variations table - add new chromosom col
db.export.variations(DB.path.name=paste(getwd(),"/brca1x2",sep=""), export.path=paste(getwd(),"/data/export.variations09042014.txt",sep=""))
# 13555 rows
#export sample table
db.export.sample(DB.path.name=paste(getwd(),"/brca1x2",sep=""), export.path=paste(getwd(),"/data/export.sample09042014.txt",sep=""))
# 248 rows

# add column in variations
# delete database

# create database
db.create.structure(DB.name="brca1x2", DB.path="")	# only once, you cannot overwrite

# log remains unaltered

# re-import sample
db.overwrite.table.sample(DB.path.name="brca1x2", data.path.name="data/export.sample09042014.txt")
# re-import variations - with new chromosom col
db.overwrite.table.variations(DB.path.name="brca1x2", data.path.name="data/export.variations09042014.txt")

# backup db

### import new tables for mutation detection (group.type=1, BRCA: log.prefix="d" default)
import.in.db(import.path.xlsx="/home/korschi/eclipseR/BerndD/data_brca12_plus/not_Sanger_tested", log.prefix="d",
		DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2", group.type=1)
# now 313-19546 rows

# backup db


### statistics
stat.14042014 <- db.stat.frequency(DB.path.name="/brca1x2", what1="1",
		geneToWork=c("BRCA1","BRCA2"),
		out.path.name="/results1/stat.14042014")
#we take group : 0
#total number of lines in group :  13466
#statistics - number of rows :  831


# detect mutations   (app. 6 min.)
date()
mutations.brca1.15042014 <- db.test.on.mutations(DB.path.name="/home/korschi/eclipseR/BerndD/brca1x2",
		ref1="1", test2="2", geneToWork="BRCA1", stat=NULL, window.size=0, out.path.name="/results1/new.mut")
date()
#basic set - total number of lines :  13555
#for result set searched in  5991  lines
#result set - number of lines :  1133

# get back blocks for test
db.get.all.specific.position(DB.path.name="/brca1x2", var.show=T)
# check in db state before db.test.on.mutations run
db.get.all.specific.position(DB.path.name="/data/brca1x2", what1="1", position=41209053, var=NULL, var.show=F, out.path.name="/results1/block.new.pos")
db.get.all.specific.position(DB.path.name="/data/brca1x2", what1="1", position=41256250, var="insT", var.show=F, out.path.name="/results1/block.new.var")
db.get.all.specific.position(DB.path.name="/data/brca1x2", what1="1", position=41256250, var=NULL, var.show=F, out.path.name="/results1/block.to.pos")

db.get.all.specific.position(DB.path.name="/data/brca1x2", what1="1", position=41209050, var=NULL, var.show=F, out.path.name="/results1/b.5")


### 16.4.2014
# start from the beginning (because of data error - reverse results ) -- import revised data
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

# create database w/ new polymorphism column
db.create.structure(DB.name="brca1x2", DB.path="")
# create database log files
log.db(log.prefix="brca1x2.")
# what is the first batch number ?
get.batch.import.number(DB.path.name="/brca1x2")	# 1
# import reference data
import.in.db(import.path.xlsx="/data_brca12_plus", log.prefix="brca1x2.", DB.path.name="/brca1x2")
#number of files:  32 
#number of sheets:  248 
#number of rows:  13555 

# export variants table from old database
db.export.variations(DB.path.name="/data/brca1x2", export.path="/data/export.variations17042014.old.txt")
# export variants table from new database --  and  correct new database table by mutations and chr from old datbase table
db.export.variations(DB.path.name="/brca1x2", export.path="/data/export.variations17042014.new.txt")
db.export.sample(DB.path.name="/brca1x2", export.path="/data/export.sample17042014.new.txt")

# re-import variants table in new database
db.overwrite.table.variations(DB.path.name="/brca1x2", data.path.name="/data/export.variations17042014.new.txt")
# export variants table from new database again and send Bernd
db.export.variations(DB.path.name="/brca1x2", export.path="/data/export.variations17042014.new2.txt")

# import second batch
# what is the batch number ?
get.batch.import.number(DB.path.name="/brca1x2")	# 1
import.in.db(import.path.xlsx="/data_brca12_plus/not_Sanger_tested", log.prefix="brca1x2.", DB.path.name="/brca1x2")


### 23.4.2014
# import in data.frame annd reduce to unique and export again in xlsx
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

log.one(log.prefix="analyse.")
import.in.one.df(import.path.xlsx="/data_brca12_plus/not_Sanger_tested/extramenge", log.prefix="analyse.", db.df="test.data01")
#
nrow(test.data01)	# 5991
export.in.one.df(export.path.name.xlsx="/test.data01.a", db.df="test.data01")
#
#aa <- matrix(cbind(rep(2,20),
#				c(124,124,124,125,125,125,126,126,126,127,127,127,128,128,128,128,128,128,128,128),
#				c("A->T","A->T","G->C","A->T","G->C","insA","A->T","A->T","A->T","ins","A->T","del","A->T","A->T","A->T","A->T","ins","A->T","del","A->T"),
#				stringsAsFactors=F),
#		20,3)
#export.in.one.df(export.path.name.xlsx="/aa", db.df="aa")
#ab <- aa[!duplicated(aa[,c(1,2,3)]), ]
#export.in.one.df(export.path.name.xlsx="/ab", db.df="ab")

test.data01.b <- test.data01[!duplicated(test.data01[,c("chr","pos","var")]), ]
#
export.in.one.df(export.path.name.xlsx="/test.data01.b", db.df="test.data01.b")

test.data01.c <- how.many.dupli(x=test.data01.b[,c("chr","pos","var")], ref=test.data01[,c("chr","pos","var")])
test.data01.c <- cbind(test.data01.b,test.data01.c[,4,drop=F])
#
export.in.one.df(export.path.name.xlsx="/test.data01.c", db.df="test.data01.c")

# correct several Excel sheets by adding a chr column & correcting Position->Pos
correct.xlsx.01(import.path.xlsx="/data_brca12_plus/tmp1", export.path.xlsx="/data_brca12_plus/tmp2", chr=17)
#number of files:  16 
#number of sheets:  124 
#number of rows:  5632 
correct.xlsx.01(import.path.xlsx="/data_brca12_plus/tmp3", export.path.xlsx="/data_brca12_plus/tmp4", chr=13)
#number of files:  16 
#number of sheets:  124 
#number of rows:  7923 

#sum : 5632+7923=13555


### find mutation - test run with SIL132
# copy backup db in main folder and import test data

### test negative
import.in.db(import.path.xlsx="/data_brca12_plus/test_mut_neg", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140502")
#present group number :  2
#number of files:  2 
#number of sheets:  16 
#number of rows:  1147 

# find mutations
test.mutations02 <- db.test.on.mutations(DB.path.name="/brca1x2_20140502", ref1="1", test2="3", geneToWork=c("BRCA1","BRCA2"),
		stat=NULL, window.size=0, out.path.name="/test.mutations02")
#for result set searched in  1147  lines
#result set - number of lines :  2
#basic set - total number of lines :  13555
#file output name:  /home/korschi/eclipseR/BerndD/test.mutations02.BRCA1_BRCA2.0.2.201405061900.txt 

# analyse
db.get.all.specific.position(DB.path.name="/brca1x2_20140502", what1="", position=41197018, var=NULL, var.show=F, out.path.name="")


### test positive
import.in.db(import.path.xlsx="/data_brca12_plus/test_mut_posA", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140502")
#present group number :  2
#number of files:  2 
#number of sheets:  16 
#number of rows:  1147 

# find mutations
test.mutations03 <- db.test.on.mutations(DB.path.name="/brca1x2_20140502", ref1="1", test2="3", geneToWork=c("BRCA1","BRCA2"),
		stat=NULL, window.size=0, out.path.name="/test.mutations03")
#for result set searched in  1147  lines
#result set - number of lines :  6
#basic set - total number of lines :  13555
#file output name:  /home/korschi/eclipseR/BerndD/test.mutations03.BRCA1_BRCA2.0.2.201405061856.txt 

# analyse
db.get.all.specific.position(DB.path.name="/brca1x2_20140502", what1="", position=32912189, var=NULL, var.show=F, out.path.name="")




### 02.05.2014  -- import data in a stable database version
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
# import data not Sanger tested but mutation free
import.in.db(import.path.xlsx="/data_brca12_plus/not_Sanger_tested/nurGute132_139", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140417")
# dataset no. 1
# rename & backup database : brca1x2_20140502

# import data mutations_132_139 in a test database
import.in.db(import.path.xlsx="/data_brca12_plus/mutations132_139", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140502")
# dataset no. 2
# rename & backup database : brca1x2_20140502_mut_test

# import data not_Sanger_tested140_155 (only PGM) in a test database
import.in.db(import.path.xlsx="/data_brca12_plus/not_Sanger_tested140_155", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140502_mut_test")
# dataset no. 3
# rename & backup database : brca1x2_20140502_mut_andNotSanger_test


# analysis for no. 2
test.mutations_01vs2 <- db.test.on.mutations(DB.path.name="/brca1x2_20140502_mut_andNotSanger_test", ref1=c("1","2"), test2="3",
		geneToWork=c("BRCA1","BRCA2"), stat=NULL, window.size=0, out.path.name="/test.mutations_01vs2")
#for result set searched in  797  lines
#result set - number of lines :  16
#basic set - total number of lines :  16385
#file output name:  /home/korschi/eclipseR/BerndD/test.mutations_01vs2.BRCA1_BRCA2.0_1.2.201405061904.txt 

# analysis for no. 3
test.mutations_01vs3 <- db.test.on.mutations(DB.path.name="/brca1x2_20140502_mut_andNotSanger_test", ref1=c("1","2"), test2="4",
		geneToWork=c("BRCA1","BRCA2"), stat=NULL, window.size=0, out.path.name="/test.mutations_01vs3")
#for result set searched in  3330  lines
#result set - number of lines :  66
#basic set - total number of lines :  16385
#file output name:  /home/korschi/eclipseR/BerndD/test.mutations_01vs3.BRCA1_BRCA2.0_1.3.201405061908.txt 


### 09.05.2014 - developing database
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

# insert db column
db.insert.column(DB.path.name="/brca1x2_20140502_mut_andNotSanger_test", table.name="variations", col.name="pcranno", data.type="VARCHAR(40)")

# import bed files
import.bed.info(import.path.bed="/data_brca12_plus/bed_PCR/BRCA1.bed", name.df="pcr.names")
import.bed.info(import.path.bed="/data_brca12_plus/bed_PCR/BRCA2.bed", name.df="pcr.names")

import.bed.info(import.path.bed="/data_br_8panel/bed_PCR/ATM.bed", name.df="pcr.names")
import.bed.info(import.path.bed="/data_br_8panel/bed_PCR/CHEK2.bed", name.df="pcr.names")
import.bed.info(import.path.bed="/data_br_8panel/bed_PCR/NBN.bed", name.df="pcr.names")
import.bed.info(import.path.bed="/data_br_8panel/bed_PCR/PALB2.bed", name.df="pcr.names")
import.bed.info(import.path.bed="/data_br_8panel/bed_PCR/TP53.bed", name.df="pcr.names")
# fehlen noch 3


# add pcr annoation to db
db.insert.PCR.anno(DB.path.name="/brca1x2_20140502_mut_andNotSanger_test", df.anno="pcr.names")
#db.set.nr:  20512
#processed rows:  20512   pcr name exact:  14924   pcr 1:n :  5174   no pcr:  414 

# export
db.export.variations(DB.path.name="/brca1x2_20140502_mut_andNotSanger_test", export.path="/data/export.BRCA12.variations13052014.txt")


### 13.05.2014 create 8panel database
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

log.db(log.prefix="br8panel.")
db.create.structure(DB.name="br8panel", DB.path="")
#
# do not use at the moment !
import.in.db(import.path.xlsx="/data_br_8panel/PGM146_153", log.prefix="br8panel.", DB.path.name="/br8panel")
#present group number :  1
#number of files:  1 
#number of sheets:  30 
#number of rows:  3516 			app. 95-125 rows per sheet

import.in.db(import.path.xlsx="/data_br_8panel/Proton149", log.prefix="br8panel.", DB.path.name="/br8panel")
#present group number :  2
#number of files:  1 
#number of sheets:  32 
#number of rows:  18187 		app. 500-730 rows per sheet

import.in.db(import.path.xlsx="/data_br_8panel/Proton160", log.prefix="br8panel.", DB.path.name="/br8panel")
#present group number :  3
#number of files:  1 
#number of sheets:  46 
#number of rows:  32192 		app. 300-824 rows per sheet

# sum 53895

# add pcr annoation to db
db.insert.PCR.anno(DB.path.name="/br8panel_20140707", df.anno="pcr.names")
#db.set.nr:  53895
#processed rows:  53895   pcr name exact:  36623   pcr 1:n :  1895   no pcr:  15377 

# export
db.export.sample.variations(DB.path.name="/br8panel_20140707", export.path="/data/export.br8panel.variations20140707.txt")


### 20.5.2014 - import new data - test new pcr.name annotation during import
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

# before copy /brca1x2_20140502_mut_andNotSanger_test (with pcr names -> data)
# and rename to /brca1x2_20140520 and also copy  -> data
import.in.db(import.path.xlsx="/data_brca12_plus/sil157_143_158", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140520")
#present group number :  4
#number of files:  1 
#number of sheets:  34 
#number of rows:  2988 

# check new pcr anno fn during import -  and if successful copy to -> data : ok

# test new mutation search fn (16K-lines-db-3k-lines-search: appr. 12min)
test.mutations_01vs4 <- db.test.on.mutations(DB.path.name="/brca1x2_20140520", ref1=c("1","2"), test2="5",
		geneToWork=c("BRCA1","BRCA2"), flagIt=F, stat=NULL, window.size=0, out.path.name="/test.mutations_01vs4")
#for result set searched in  2988  lines
#suspected mutation set - number of lines :  2609
#negative observation set - number of lines :  375
#basic set - total number of lines :  16385
#file output suspected mutations:  /home/korschi/eclipseR/BerndD/test.mutations_01vs4.mut.BRCA1_BRCA2.0_1.4.201405201727.txt 
#file output negative observations:  /home/korschi/eclipseR/BerndD/test.mutations_01vs4.neg.BRCA1_BRCA2.0_1.4.201405201727.txt 


### 22.5.2014 - export batch 1,2,3 for manual mutation analysis
# but export all - easier
db.export.sample.variations(DB.path.name="/brca1x2_20140520", export.path="/data/export.brca1x2.variations22052014.txt")



### 16.06.2014 -- again  statistics and thersholds
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

stat.all <- db.stat.frequency(DB.path.name="/brca1x2_20140520",
		batch=NULL, what1="",
		geneToWork=c("BRCA1","BRCA2"), out.path.name="/stat.all.txt")
stat.1.2.3 <- db.stat.frequency(DB.path.name="/brca1x2_20140520",
		batch=c(1,2,3), what1="",
		geneToWork=c("BRCA1","BRCA2"), out.path.name="/stat.1.2.3.txt")
names(stat.all)

hist.plot.simple(x=aa[,6], bin.num=F, bin.size=F, bin.fix=NULL, bin.fix.open=F, norm=F, n.factor=1, curve=F,
		xlab="", ylab="", xlim=NULL, y.max=NULL, x.at=NULL, y.at=NULL, y.num.format=F, ylog=F, bar.width=1, offset=0, digits=1,
		col=c("red","green","blue"), cex=1, smooth.f=0,
		subfolder.filename="/test1", h.title="", s.title=T, add=F)

# find mutations test run -- with statistics and >max criteria
names(stat.1.2.3)
stat.1.2.3[1,]

# 15 s pro patient(100 rows) -> 40 pat.: 10 min
db.test.on.mutations(DB.path.name="/brca1x2_20140520", ref1=c("1","2","3"), test2="4",
		geneToWork=c("BRCA1","BRCA2"), flagIt=F, stat=stat.1.2.3, out.path.name="/test.mutations_123vs4")
#test set - processed lines :  3330
#suspected mutation set - number of lines :  135
#negative observation set - number of lines :  3195
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs4.mut.BRCA1_BRCA2.1_2_3.4.201406181505.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs4.neg.BRCA1_BRCA2.1_2_3.4.201406181505.txt

db.test.on.mutations(DB.path.name="/brca1x2_20140620", ref1=c("1","2","3"), test2="4",
		geneToWork=c("BRCA1","BRCA2"), flagIt=F, stat=stat.1.2.3, out.path.name="/test.mutations_123vs4")
#test set - processed lines :  3330
#suspected mutation set - number of lines :  163
#negative observation set - number of lines :  3167
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs4.mut.BRCA1_BRCA2.1_2_3.4.201406201407.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs4.neg.BRCA1_BRCA2.1_2_3.4.201406201407.txt 



### 20.06.2014 -- new data
import.in.db(import.path.xlsx="/data_brca12_plus/sil156_161", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140620")
#present group number :  6
#number of files:  5 
#number of sheets:  61 
#number of rows:  4700 

#
db.test.on.mutations(DB.path.name="/brca1x2_20140620", ref1=c("1","2","3"), test2="5",
		geneToWork=c("BRCA1","BRCA2"), flagIt=F, stat=stat.1.2.3, out.path.name="/test.mutations_123vs5")
#test set - processed lines :  2988
#suspected mutation set - number of lines :  239
#negative observation set - number of lines :  2745
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs5.mut.BRCA1_BRCA2.1_2_3.5.201406201425.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs5.neg.BRCA1_BRCA2.1_2_3.5.201406201425.txt 

#
db.test.on.mutations(DB.path.name="/brca1x2_20140620", ref1=c("1","2","3"), test2="6",
		geneToWork=c("BRCA1","BRCA2"), flagIt=F, stat=stat.1.2.3, out.path.name="/test.mutations_123vs6")
#test set - processed lines :  4700
#suspected mutation set - number of lines :  449
#negative observation set - number of lines :  4248
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs6.mut.BRCA1_BRCA2.1_2_3.6.201406201451.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs6.neg.BRCA1_BRCA2.1_2_3.6.201406201451.txt 


### 07.07.2014 -- br8panel_20140707 - statistics
# get gene names
aa <- get.unique.genes(DB.path.name="/br8panel_20140707", choose="first")
#[1] "NBN"          "ATM"          "CDH1"         "PALB2"        "RAD51L3-RFFL"
#[6] "TP53"         "CHEK2"        "RAD51C"       "TEX14"

ab <- get.unique.genes(DB.path.name="/br8panel_20140707", choose="all")		# 4 min / 53000 lines
#[1] ""              "NBN"           "ATM"           "NPAT"         
#[5] "Y_RNA"         "C11orf65"      "CDH1"          "RNA5SP429"    
#[9] "RP11-354M1.2"  "FTLP14"        "PALB2"         "CTD-2196E14.3"
#[13] "RAD51L3-RFFL"  "RAD51D"        "FNDC8"         "TP53"         
#[17] "CHEK2"         "AP001925.1"    "RAD51C"        "TEX14"        
#[21] "Vault"         "DCTN5"

# stat
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")

aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")

db.stat.frequency(DB.path.name="/br8panel_20140707",
		batch=NULL, what1="",
		geneToWork=aa, out.path.name="/stat.br8", workspace.suffix="br8.08072014")
#total number of lines in table :  53895
#number of lines in selection :  53895
#statistics - number of rows :  2081
db.stat.frequency(DB.path.name="/br8panel_20140707",
		batch=c(2,3), what1="",
		geneToWork=aa, out.path.name="/stat.br8", workspace.suffix="br8.08072014")
# 1 min
#total number of lines in table :  53895
#number of lines in selection :  50379
#statistics - number of rows :  1988
# NBN.A>C.90946723

hist.plot.simple(x=stat.br8.08072014[,6], bin.num=F, bin.size=F, bin.fix=NULL, bin.fix.open=F, norm=F, n.factor=1, curve=F,
		xlab="observed counts", ylab="number of events per intervall", xlim=NULL, y.max=NULL, x.at=NULL, y.at=NULL, y.num.format=F, ylog=F, bar.width=1, offset=0, digits=1,
		col=c("red","green","blue"), cex=1, smooth.f=0,
		subfolder.filename=NULL, h.title="", s.title=T, add=F)


### 11.07.2014 - import BRCA1/2 data
import.in.db(import.path.xlsx="/data_brca12_plus/sil162_163", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140620")
#present group number :  7
#number of files:  2 
#number of sheets:  22 
#number of rows:  1431 					43-94 rows per patient

db.test.on.mutations(DB.path.name="/brca1x2_20140620", ref1=c("1","2","3"), test2="7",
		geneToWork=c("BRCA1","BRCA2"), flagIt=F, stat=stat.1.2.3, stat.freq=NULL, out.path.name="/test.mutations_123vs7", method="brca12")
#test set - processed lines :  1431
#suspected mutation set - number of lines :  113
#negative observation set - number of lines :  1318
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs7.mut.BRCA1_BRCA2.1_2_3.7.201407111142.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs7.neg.BRCA1_BRCA2.1_2_3.7.201407111142.txt 


### 17.7.2014 - import br8
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

import.in.db(import.path.xlsx="/data_br_8panel/Proton166", log.prefix="br8panel.", DB.path.name="/br8panel_20140707")
#present group number :  4
#number of files:  1 
#number of sheets:  47 
#number of rows:  21120 

# create a separate list containing all frequency distributions
aa <- date()
db.test.dist.basic.dist.frequency(DB.path.name="/br8panel_20140707", batch=4, ref.stat=stat.br8.08072014, ref.freq=freq.br8.08072014,
		out.path.name="/test.freq.ref.23.4")
aa;date()	# 3 min
#batch:  4  ref.stat:  stat.br8.08072014  ref.freq:  freq.br8.08072014
#number of lines in batch :  21120
#ref statistics & frequency list : #  19194  (number of observations)
#only ref statistics : #  1926
#only frequency list : #  0
#no reference at all : #  0


### 28.7.2014 - pcr anno br8 , export db , stat br8 batch 2,3,4 , pos/neg list with 10% and no skip >2
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import bed files
import.pcr.info(import.path.bed="/data_br_8panel/bed_PCR/10_Gene.bed", name.df="pcr.names.br8", flag=F)

# add pcr annoation to db
db.insert.PCR.anno(DB.path.name="/br8panel_20140707", df.anno="pcr.names.br8")
#db.set.nr:  75015
#processed rows:  75015   pcr name exact:  59618   pcr 1:n :  5193   no pcr:  10204 

# export Proton & PGM
db.export.sample.variations(DB.path.name="/br8panel_20140707", export.path="/data/export.br8panel_20140707.variations28072014.txt")

# stat br8 batch 2,3,4
aa <- get.unique.genes(DB.path.name="/br8panel_20140707", choose="first")
#[1] "NBN"          "ATM"          "CDH1"         "PALB2"        "RAD51L3-RFFL"
#[6] "TP53"         "CHEK2"        "RAD51C"       "TEX14"       
ab <- get.unique.genes(DB.path.name="/br8panel_20140707", choose="all")
#[1] ""              "NBN"           "ATM"           "NPAT"         
#[5] "Y_RNA"         "C11orf65"      "CDH1"          "RNA5SP429"    
#[9] "RP11-354M1.2"  "FTLP14"        "PALB2"         "CTD-2196E14.3"
#[13] "RAD51L3-RFFL"  "RAD51D"        "FNDC8"         "TP53"         
#[17] "CHEK2"         "AP001925.1"    "RAD51C"        "TEX14"        
#[21] "Vault"         "DCTN5"        
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
db.stat.frequency(DB.path.name="/br8panel_20140707", batch=c(2,3,4), what1="",
		geneToWork=aa, out.path.name="/stat/stat.br8.2.3.4.28072014", workspace.suffix="br8.2.3.4.28072014")
#out path matrix :  /home/korschi/eclipseR/BerndD/stat/stat.br8.2.3.4.28072014..201407282353.txt
#we take the whole table 
#SELECT variations.*
#		FROM sample
#INNER JOIN variations
#ON sample.sample_id = variations.sample_id
#WHERE sample.batch IN (2,3,4)
#total number of lines in table :  75015
#number of lines in selection :  71499
#statistics - number of rows :  2321

# pos/neg list with 10% and no skip >2
# import vcf data
aa <- date()
ab <- import.rs.info(import.path.abs="/home/korschi/Downloads/00all.vcf", in.df=stat.br8.2.3.4.28072014[,c(1,2),drop=F])
aa;date()	# 8 min/2321 rows in 61220532 rows
dim(ab)
ncol(ab)
ab <- t(ab)		# 843 rows

# create stand-alone All.vcf database   (text file ~10GB -> database file ~22GB)
aa <- date()
create.rs.db(import.path.abs="/home/korschi/Downloads/00all.vcf")
aa;date()	# 10 min - db:00all table:t_00all


# pos-neg
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br8panel_20140707", ref1=c("2","3","4"), test2=c("2","3","4"),
		geneToWork=aa, flagIt=F, stat=stat.br8.2.3.4.28072014, stat.freq=freq.br8.2.3.4.28072014,
		out.path.name="/test.mutations_234vs234", method="br8simple")
ac;date()	# 


### 27.08.2014 - import BRCA1/2 data
import.in.db(import.path.xlsx="/data_brca12_plus/sil168_172", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140620")
#present group number :  8
#number of files:  2 
#number of sheets:  24 
#number of rows:  2000 

db.test.on.mutations(DB.path.name="/brca1x2_20140620", ref1=c("1","2","3"), test2="8",
		geneToWork=c("BRCA1","BRCA2"), flagIt=F, stat=stat.1.2.3, stat.freq=NULL, out.path.name="/test.mutations_123vs8", method="brca12")
# ca. 100 +/-10 pro entry

#test set - processed lines :  2000
#suspected mutation set - number of lines :  516
#negative observation set - number of lines :  1484
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs8.mut.geneToWork2.201408271911.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs8.neg.geneToWork2.201408271911.txt 


### 28.08.2014 - import BRCA1/2 data
import.in.db(import.path.xlsx="/data_brca12_plus/sil182", log.prefix="brca1x2.", DB.path.name="/brca1x2_20140620")
#present group number :  9
#number of files:  1 
#number of sheets:  6 
#number of rows:  816 

db.test.on.mutations(DB.path.name="/brca1x2_20140620", ref1=c("1","2","3"), test2="9",
		geneToWork=c("BRCA1","BRCA2"), flagIt=F, stat=stat.1.2.3, stat.freq=NULL, out.path.name="/test.mutations_123vs9", method="brca12")
# ca. 133 pro entry

#test set - processed lines :  816
#suspected mutation set - number of lines :  165
#negative observation set - number of lines :  651
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs9.mut.geneToWork2.201408281847.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/test.mutations_123vs9.neg.geneToWork2.201408281847.txt 


### 3.9.2014 - import br8
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

import.in.db(import.path.xlsx="/data_br_8panel/sil182", log.prefix="br8panel.", DB.path.name="/br8panel_20140707")
#present group number :  5
#number of files:  1 
#number of sheets:  6 
#number of rows:  713 

aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
db.stat.frequency(DB.path.name="/br8panel_20140707", batch=c(2,3,4,5), what1="",
		geneToWork=aa, out.path.name="/stat/stat.br8.2bis5.03092014", workspace.suffix="br8.2bis5.03092014")
#out path matrix :  /home/korschi/eclipseR/BerndD/stat/stat.br8.2bis5.03092014..201409031309.txt
#we take the whole table 
#SELECT variations.*
#		FROM sample
#INNER JOIN variations
#ON sample.sample_id = variations.sample_id
#WHERE sample.batch IN (2,3,4,5)
#total number of lines in table :  75728
#number of lines in selection :  72212
#statistics - number of rows :  2355

# pos-neg
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br8panel_20140707", ref1=c("2","3","4","5"), test2=c("2","3","4","5"),
		geneToWork=aa, flagIt=F, stat=stat.br8.2bis5.03092014, stat.freq=freq.br8.2bis5.03092014,
		out.path.name="/test.mutations_2345vs2345", method="br8simple")
ac;date()	# 16 h !


### 15.09.2014 - export patients table (unique) and re-import true mutations
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

db.export.sample(DB.path.name="/brca1x2_20140620", export.path="/results1/export.sample_all.txt", unip=F, batch="")
db.export.sample(DB.path.name="/brca1x2_20140620", export.path="/results1/export.sample_all_unique.txt", unip=T, batch="")


### 18.09.2014 -- change of strategy : merging of breast 2panel and 8panel to a master database and adding now 4panel data
# concept and code in join.db.tables.R

# take old database - empty tables and fill with merged data --> sqliteman program
# so not execute this line:
#db.create.structure(DB.name="br_new", DB.path="")	# only once, you cannot overwrite

# but create log data.frames
log.db(log.prefix="br4x8x2panel.")	# only once, you cannot overwrite

# fill database (done by sqlite tools) and import new set 
import.in.db(import.path.xlsx="/data_br_4panel/SIL178.1", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20140918")
#present group number :  16   --  ok, last was 15 after merge
#number of files:  1 
#number of sheets:  30 
#number of rows:  1559 


### 7.10.2014 export log files
write.table(x=br4x8x2panel.protocol.db, file="br4x8x2panel.protocol.db", append=F, quote=T, sep="\t", dec=".", row.names=T, col.names=T)
write.table(x=br8panel.protocol.db, file="br8panel.protocol.db", append=F, quote=T, sep="\t", dec=".", row.names=T, col.names=T)
write.table(x=brca1x2.protocol.db, file="brca1x2.protocol.db", append=F, quote=T, sep="\t", dec=".", row.names=T, col.names=T)
write.table(x=br4x8x2panel.list.db, file="br4x8x2panel.list.db", append=F, quote=T, sep="\t", dec=".", row.names=T, col.names=T)
write.table(x=br8panel.list.db, file="br8panel.list.db", append=F, quote=T, sep="\t", dec=".", row.names=T, col.names=T)
write.table(x=brca1x2.list.db, file="brca1x2.list.db", append=F, quote=T, sep="\t", dec=".", row.names=T, col.names=T)
# export sample table - to document batch number  and  to add column with number of genes in the performed analysis
db.export.sample(DB.path.name="/br4x8x2panel_20140918", export.path="/0info/br4x8x2panel_batchpanel_20141007.csv", orderby="batch", unip=F, batch="")


### 7.10.2014 brca12 batch 1,2 und 1,2,3
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

aa <- c("BRCA1","BRCA2")
db.stat.frequency(DB.path.name="/br4x8x2panel_20140918", batch=c(1,2), what1="", geneToWork=aa,
		out.path.name="/stat/stat.br4x8x2panel.1.2.20141008", workspace.suffix="br4x8x2panel.1.2.20141008")
db.stat.frequency(DB.path.name="/br4x8x2panel_20140918", batch=c(1,2,3), what1="", geneToWork=aa,
		out.path.name="/stat/stat.br4x8x2panel.1.2.3.20141008", workspace.suffix="br4x8x2panel.1.2.3.20141008")

db.test.dist.basic.dist.frequency(DB.path.name="/br4x8x2panel_20140918", batch=4,
		ref.stat=stat.br4x8x2panel.1.2.20141008, ref.freq=freq.br4x8x2panel.1.2.20141008, ref.bala=bala.br4x8x2panel.1.2.20141008,
		out.path.name="/stat/test.freq.ref.12.4")
#batch:  4  ref.stat:  stat.br4x8x2panel.1.2.20141008  ref.freq:  freq.br4x8x2panel.1.2.20141008  ref.bala:  bala.br4x8x2panel.1.2.20141008
#number of lines in batch :  3330
#ref statistics & frequency(balance) list : #  3271  (number of observations)
#no ref statistics : #  59

db.test.dist.basic.dist.frequency(DB.path.name="/br4x8x2panel_20140918", batch=4,
		ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008, ref.bala=bala.br4x8x2panel.1.2.3.20141008,
		out.path.name="/stat/test.freq.ref.123.4")
#batch:  4  ref.stat:  stat.br4x8x2panel.1.2.3.20141008  ref.freq:  freq.br4x8x2panel.1.2.3.20141008  ref.bala:  bala.br4x8x2panel.1.2.3.20141008
#number of lines in batch :  3330
#ref statistics & frequency(balance) list :  3273  (number of observations)
#no ref statistics :  57


### 8.10.2014 4panel
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_4panel/SIL185", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20140918")
#present group number :  17
#number of files:  1 
#number of sheets:  30 
#number of rows:  1331 

#### export sample table - to document batch number  and  to add column with number of genes in the performed analysis ####
db.export.sample(DB.path.name="/br4x8x2panel_20140918", export.path="/0info/br4x8x2panel_batchpanel_20141009.csv", orderby="batch", unip=F, batch="")

# statistics
aa <- c("BRCA1","BRCA2","RAD51C","CHEK2")
db.stat.frequency(DB.path.name="/br4x8x2panel_20140918", batch=c(16,17), what1="", geneToWork=aa,
		out.path.name="/stat/stat.br4x8x2panel.16.17.20141009", workspace.suffix="br4x8x2panel.16.17.20141009")
#total number of lines in table :  111065
#number of lines in selection :  2890
#statistics - number of rows :  206

# test run
db.test.dist.basic.dist.frequency(DB.path.name="/br4x8x2panel_20140918", batch=c(16,17),
		ref.stat=stat.br4x8x2panel.16.17.20141009, ref.freq=freq.br4x8x2panel.16.17.20141009, ref.bala=bala.br4x8x2panel.16.17.20141009,
		out.path.name="/stat/test.freq.ref.16.17x16.17")
#batch:  16 17  ref.stat:  stat.br4x8x2panel.16.17.20141009  ref.freq:  freq.br4x8x2panel.16.17.20141009  ref.bala:  bala.br4x8x2panel.16.17.20141009
#number of lines in batch :  2890
#ref statistics & frequency(balance) list :  2890  (number of observations)
#no ref statistics :  0

# get all unique gene names
get.unique.genes(DB.path.name="/br4x8x2panel_20140918", out.path.name="/0info/unique.gene.names.all.20141009.txt")


## statistics  12,13,14
aa <- c("RAD51C","CHEK2")
db.stat.frequency(DB.path.name="/br4x8x2panel_20140918", batch=c(12,13,14), what1="", geneToWork=aa,
		out.path.name="/stat/stat.br4x8x2panel.radcheck.12.13.14.20141010", workspace.suffix="br4x8x2panel.radcheck.12.13.14.20141010")
#total number of lines in table :  111065
#number of lines in selection :  71499
#statistics - number of rows :  397


# test run 12,13,14
db.test.dist.basic.dist.frequency(DB.path.name="/br4x8x2panel_20140918", batch=c(12,13,14),
		ref.stat=stat.br4x8x2panel.radcheck.12.13.14.20141010, ref.freq=freq.br4x8x2panel.radcheck.12.13.14.20141010, ref.bala=bala.br4x8x2panel.radcheck.12.13.14.20141010,
		out.path.name="/stat/test.freq.radcheck.ref.12.13.14x12.13.14")
#batch:  12 13 14  ref.stat:  stat.br4x8x2panel.radcheck.12.13.14.20141010  ref.freq:  freq.br4x8x2panel.radcheck.12.13.14.20141010  ref.bala:  bala.br4x8x2panel.radcheck.12.13.14.20141010 
#number of lines in batch :  71499 
#ref statistics & frequency(balance) list :  12207  (number of observations) 
#no ref statistics :  59104 
#due to -a new event is observed, -not all genes are analysed, -wrong stat table used

# pos-neg 1  - fail
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20140918", ref1=c("1","2","3"), test2=c("16","17"),
		geneToWork=aa, flagIt=F, stat=stat.br4x8x2panel.1.2.3.20141008, stat.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x16.17", method="brca12")
ac; date()
#  44 min
#test set - processed lines :  2890
#suspected mutation set - number of lines :  724
#negative observation set - number of lines :  1681
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x16.17.mut.genes2.201410131646.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x16.17.neg.genes2.201410131646.txt 

# pos-neg 2  - run
aa <- c("RAD51C","CHEK2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20140918", ref1=c("12","13","14"), test2=c("16","17"),
		geneToWork=aa, flagIt=F, stat=stat.br4x8x2panel.radcheck.12.13.14.20141010, stat.freq=freq.br4x8x2panel.radcheck.12.13.14.20141010,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.radcheck.12.13.14x16.17", method="br8simple")
ac; date()
#  5 min
#test set - processed lines :  2890
#suspected mutation set - number of lines :  224
#negative observation set - number of lines :  2666
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.radcheck.12.13.14x16.17.mut.genes2.201410131747.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.radcheck.12.13.14x16.17.neg.genes2.201410131747.txt 



### 14.10.2014 - statistics - test - of color coding in balance   12,13,14
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

aa <- c("RAD51C","CHEK2")
db.stat.frequency(DB.path.name="/br4x8x2panel_20140918", batch=c(12,13,14), what1="", geneToWork=aa,
		out.path.name="/stat/stat.br.test.test.RAD51C.CHEK2.12.13.14", workspace.suffix="br4x8x2panel.testtest")
#total number of lines in table :  111065
#number of lines in selection :  71499
#statistics - number of rows :  397


## set flag for SNP (polymorphism) in mutation column
set.flags.variations(DB.path.name="/br4x8x2panel_20140918", table.field="mutation", flag.value="3",
		work.list="/SNPs_der_Gesamtliste_149_160_stat.br8.201407081046.txt")
#number of rows processed:  105
#successful:  105
#missed:      0
#total found in database:  2053 



### 30.10.2014 2panel
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_brca12_plus/sil186_193", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20140918")
#present group number :  18
#number of files:  7 
#number of sheets:  35 
#number of rows:  2180 

# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20140918", ref1=c("1","2","3"), test2=c("18"),
		geneToWork=aa, flagIt=F, stat=stat.br4x8x2panel.1.2.3.20141008, stat.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x18", method="brca12")
ac; date()
#  41 min
#test set - processed lines :  2180
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  254
#negative observation set - number of lines :  1926
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x18.mut.genes2.201410301031.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x18.neg.genes2.201410301031.txt 



### 6.11.2014 10panel -> 2panel ref 1,2,3  &  8panel ref 12,13,14
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL189", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20140918")
#present group number :  19
#number of files:  1 
#number of sheets:  30 
#number of rows:  4812 

# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20140918", ref1=c("1","2","3"), test2=c("19"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x19", method="brca12")
ac; date()
#  32 min
#test set - processed lines :  4812
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  515
#negative observation set - number of lines :  1232
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x19.mut.genes2.201411061233.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x19.neg.genes2.201411061233.txt 

## statistics  12,13,14
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
db.stat.frequency(DB.path.name="/br4x8x2panel_20140918", batch=c(12,13,14), what1="", geneToWork=aa,
		out.path.name="/stat/stat.br4x8x2panel.8gene.12.13.14.20141106", workspace.suffix="br4x8x2panel.8gene.12.13.14.20141106")
#total number of lines in table :  113245 
#number of lines in selection :  71499 
#statistics - number of rows :  2321 

# pos-neg - 8 run - 12,13,14 ref
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20140918", ref1=c("12","13","14"), test2=c("19"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x19", method="br8simple")
ac; date()
#  55 min
#test set - processed lines :  4812
#suspected mutation set - number of lines :  1447
#negative observation set - number of lines :  3365
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x19.mut.genes8.201411061342.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x19.neg.genes8.201411061342.txt 


### 01.12.2014 - 10 genes: 2 algo1 + 8 algo2 : pos neg list
### 2panel ref 1,2,3  &  8panel ref 12,13,14
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL192.2", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20140918")
#present group number :  20
#number of files:  1 
#number of sheets:  33 
#number of rows:  6404
delete.batch.in.db(del.batch=c(20,23), log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20140918")
delete.batch.in.db(del.batch=20, log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")

import.in.db(import.path.xlsx="/data_br_10panel/SIL192", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")
#present group number :  20
#number of files:  1 
#number of sheets:  33 
#number of rows:  3802 


# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("20"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x20", method="brca12")
ac; date()
# 25 min
#test set - processed lines :  3858
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  392
#negative observation set - number of lines :  1018
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x20.mut.genes2.201412011820.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x20.neg.genes2.201412011820.txt 

# pos-neg - 8 run - 12,13,14 ref
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("20"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x20", method="br8simple")
ac; date()
#  44 min
#test set - processed lines :  3858
#suspected mutation set - number of lines :  1300
#negative observation set - number of lines :  2558
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x20.mut.genes8.201412011904.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x20.neg.genes8.201412011904.txt 


### 9.12.2014
#### export sample table - to document batch number  and  to add column with number of genes in the performed analysis ####
db.export.sample(DB.path.name="/br4x8x2panel_20141201", export.path="/0info/br4x8x2panel_batchpanel_20141209.csv", orderby="batch", unip=F, batch="")
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=20, first=T, out.path.name=NULL)

# analysis run - distributions - all proton runs
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=c(12,13,14,16,17,19), first=T, out.path.name=NULL)
aa <- c("BRCA1","BRCA2","NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ab <- date()
db.stat.frequency(DB.path.name="/br4x8x2panel_20141201", batch=c(12,13,14,16,17,19), sampleID=c(444:457), what1="", geneToWork=aa,
		out.path.name="/stat/stat.br4x8x2panel.10gene.8a.12.13.14.16.17.19.20141209", workspace.suffix="br4x8x2panel.10gene.8a.12.13.14.16.17.19.20141209")
ab; date()	# 2 min
# nr  80599
#out path matrix :  /home/korschi/eclipseR/BerndD/stat/stat.br4x8x2panel.10gene.8a.12.13.14.16.17.19.20141209..201412091702.txt 
#ba. we take the whole  
#WHERE sample.batch IN (12,13,14,16,17,19) 
#OR sample.sample_id IN (444,445,446,447,448,449,450,451,452,453,454,455,456,457) 
#total number of lines in table :  121859 
#number of lines in selection :  80599 
#statistics - number of rows :  2731 


### 12.12.2014
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL194", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")
#present group number :  21
#number of files:  1 
#number of sheets:  26 
#number of rows:  5092 

# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("21"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x21", method="brca12")
ac; date()
# 6 min
#test set - processed lines :  5092
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  576
#negative observation set - number of lines :  1247
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x21.mut.genes2.201412151122.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x21.neg.genes2.201412151122.txt 


# pos-neg - 8 run - 12,13,14 ref
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("21"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x21", method="br8simple")
ac; date()
# 9 min
#test set - processed lines :  5092
#suspected mutation set - number of lines :  2077
#negative observation set - number of lines :  3015
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x21.mut.genes8.201412151135.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x21.neg.genes8.201412151135.txt 


### 16.12.2014
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL198", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")
#present group number :  22
#number of files:  1 
#number of sheets:  23 
#number of rows:  3430 


# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("22"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x22", method="brca12")
ac; date()
# 5 min
#test set - processed lines :  3430
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  542
#negative observation set - number of lines :  889
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x22.mut.genes2.201412161300.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x22.neg.genes2.201412161300.txt 


# pos-neg - 8 run - 12,13,14 ref
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("22"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x22", method="br8simple")
ac; date()
# 5.5 min
#test set - processed lines :  3430
#suspected mutation set - number of lines :  1215
#negative observation set - number of lines :  2215
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x22.mut.genes8.201412161314.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x22.neg.genes8.201412161314.txt 

#### export sample table - to document batch number  and  to add column with number of genes in the performed analysis ####
db.export.sample(DB.path.name="/br4x8x2panel_20141201", export.path="/0info/br4x8x2panel_batchpanel_20141216.csv", orderby="batch", unip=F, batch="")
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=21, first=T, out.path.name=NULL)
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=22, first=T, out.path.name=NULL)


### 9.2.2015
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL204", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")
#present group number :  23
#number of files:  1 
#number of sheets:  17 
#number of rows:  34380 

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL206", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")
#present group number :  24
#number of files:  1 
#number of sheets:  26 
#number of rows:  11165 

# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("23"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x23", method="brca12")
ac; date()
# 42 min  -- nr about 1904-2528 (!)
#test set - processed lines :  34380
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  7621
#negative observation set - number of lines :  3188
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x23.mut.genes2.201502091111.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x23.neg.genes2.201502091111.txt 

ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("24"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x24", method="brca12")
ac; date()
# 12 min  -- nr 399-482
#test set - processed lines :  11165
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  2292
#negative observation set - number of lines :  1872
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x24.mut.genes2.201502091125.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x24.neg.genes2.201502091125.txt 

# pos-neg - 8 run - 12,13,14 ref
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("23"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x23", method="br8simple")
ac; date()
# 1h 6 min  -- nr 1904-2528
#test set - processed lines :  34380
#suspected mutation set - number of lines :  19143
#negative observation set - number of lines :  15237
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x23.mut.genes8.201502091233.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x23.neg.genes8.201502091233.txt 

ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("24"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x24", method="br8simple")
ac; date()
# 19 min  -- nr 399-482
#test set - processed lines :  11165
#suspected mutation set - number of lines :  4834
#negative observation set - number of lines :  6331
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x24.mut.genes8.201502091256.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x24.neg.genes8.201502091256.txt 

#### export sample table - to document batch number  and  to add column with number of genes in the performed analysis ####
db.export.sample(DB.path.name="/br4x8x2panel_20141201", export.path="/0info/br4x8x2panel_batchpanel_20150209.csv", orderby="batch", unip=F, batch="")
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=23, first=T, out.path.name=NULL)
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=24, first=T, out.path.name=NULL)


### 13.2.2015  (vgl. 9.12.2014)
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# analysis all PROTON runs so far      batch and sampleID (of batch 8)
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=c(12,13,14,16,17,19,22,23,24), first=T, out.path.name=NULL)
# not included: "RAD51L3-RFFL" "TEX14"
aa <- c("BRCA1","BRCA2","NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ab <- date()
db.stat.frequency(DB.path.name="/br4x8x2panel_20141201", batch=c(12,13,14,16,17,19,22,23,24), sampleID=c(444:457), what1="", geneToWork=aa,
		out.path.name="/stat/stat.br4x8x2panel.10gene.8a.12.13.14.16.17.19.22.23.24.20150213proton", workspace.suffix="br4x8x2panel.10gene.8a.12.13.14.16.17.19.22.23.24.20150213proton")
ab; date()	# 2 min
#out path matrix :  /home/korschi/eclipseR/BerndD/stat/stat.br4x8x2panel.10gene.8a.12.13.14.16.17.19.22.23.24.20150213proton..201502131115.txt 
#ba. we take the whole  
#SELECT variations.*
#		FROM sample
#INNER JOIN variations
#ON sample.sample_id = variations.sample_id
#WHERE sample.batch IN (12,13,14,16,17,19,22,23,24) 
#OR sample.sample_id IN (444,445,446,447,448,449,450,451,452,453,454,455,456,457) 
#total number of lines in table :  175926 
#number of lines in selection :  129574 
#statistics - number of rows :  5311 


### 23.2.2015
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL211", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")
#present group number :  25
#number of files:  1 
#number of sheets:  30 
#number of rows:  12985 

# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("25"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x25", method="brca12")
ac; date()
# 14 min  -- nr 405-479
#test set - processed lines :  12985
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  2402
#negative observation set - number of lines :  2345
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x25.mut.genes2.201502271552.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x25.neg.genes2.201502271552.txt 

# pos-neg - 8 run - 12,13,14 ref
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("25"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x25", method="br8simple")
ac; date()
# 22 min  -- nr 405-479
#test set - processed lines :  12985
#suspected mutation set - number of lines :  5720
#negative observation set - number of lines :  7265
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x25.mut.genes8.201502271616.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x25.neg.genes8.201502271616.txt 

#### export sample table - to document batch number  and  to add column with number of genes in the performed analysis ####
db.export.sample(DB.path.name="/br4x8x2panel_20141201", export.path="/0info/br4x8x2panel_batchpanel_20150224.csv", orderby="batch", unip=F, batch="")
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=25, first=T, out.path.name=NULL)



### 12.3.2015
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL218", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")
#present group number :  26
#number of files:  1 
#number of sheets:  30 
#number of rows:  37375

# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("26"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x26", method="brca12")
ac; date()
# 36 min  -- nr 1167-1364
#test set - processed lines :  37375
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  7455
#negative observation set - number of lines :  4668
#reference set - number of lines :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x26.mut.genes2.201503131217.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x26.neg.genes2.201503131217.txt 

# pos-neg - 8 run - 12,13,14 ref
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("26"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x26", method="br8simple")
ac; date()
# 1 h 13 min  -- nr 1167-1364
#test set - processed lines :  37375
#suspected mutation set - number of lines :  16488
#negative observation set - number of lines :  20887
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x26.mut.genes8.201503131532.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x26.neg.genes8.201503131532.txt 

#### export sample table - to document batch number  and  to add column with number of genes in the performed analysis ####
db.export.sample(DB.path.name="/br4x8x2panel_20141201", export.path="/0info/br4x8x2panel_batchpanel_20150224.csv", orderby="batch", unip=F, batch="")
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=26, first=T, out.path.name=NULL)



### 27.3.2015 -- test run, new br8adv1 function with all proton runs agains proton reference
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# pos-neg - 8 run - 12,13,14 ref,      rule: > 3 (2 old and one new) (br12: 1 old 1 new)
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("16","17","19","22","23","24","25","26"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14xAllProton", method="br8adv1")
ac; date()
# 4 h 30 min -- nr app.30 - app.2500
#test set - processed lines :  107037
#mutations flagged in the db (as  2 ) :  0
#suspected mutation set - number of lines :  31489
#negative observation set - number of lines :  36558
#reference set - number of lines :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14xAllProton.mut.genes8.201503271720.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14xAllProton.neg.genes8.201503271720.txt 



#### test 31.3.-1.4.2015
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()
#new br8adv1 function
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("25"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/del.test.test.br12.1.2.3x25", method="br8adv1")
ac; date()

# del from file space : del.test.test.br12.1.2.3x25

#test set - fetched lines                   :  12985	# 100 %
#test set - gene not in work list - lines   :  8238		# 63 %
#mutations flagged in the db (as  2 )      :  0
#suspected mutation set - number of lines   :  2742
#negative observation set - number of lines :  2005
#reference set - number of lines           :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/del.test.test.br12.1.2.3x25.mut.genes2.201504011329.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/del.test.test.br12.1.2.3x25.neg.genes2.201504011329.txt 

# 14 min


### 1.4.2015 - Wiederholung des 27.3.2015 -- test run, new br8adv1 function with all proton runs agains proton reference
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# pos-neg - 8 run - 12,13,14 ref,      rule: > 3 (2 old and one new) (br12: 1 old 1 new)
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("16","17","19","22","23","24","25","26"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14xAllProton", method="br8adv1")
ac; date()
# 5 h -- nr app.30 - app.2500
#test set - fetched lines                  :  107037
#test set - gene not in work list - lines   :  38035
#mutations flagged in the db (as  2 )      :  0
#suspected mutation set - number of lines   :  34087
#negative observation set - number of lines :  34915
#reference set - number of lines           :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14xAllProton.mut.genes8.201504011953.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14xAllProton.neg.genes8.201504011953.txt 


### 13.4.2015
source(file="/home/korschi/eclipseR/0functions/var_db/database.R")
source(file="/home/korschi/eclipseR/0functions/var_db/import.R")
save.image()

# import new set 
import.in.db(import.path.xlsx="/data_br_10panel/SIL226", log.prefix="br4x8x2panel.", DB.path.name="/br4x8x2panel_20141201")
#present group number :  27
#number of files:  1 
#number of sheets:  30 
#number of rows:  9384 

# pos-neg - 2 run - 1,2,3 ref
aa <- c("BRCA1","BRCA2")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("1","2","3"), test2=c("27"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.1.2.3.20141008, ref.freq=freq.br4x8x2panel.1.2.3.20141008,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br12.1.2.3x27", method="brca12")
ac; date()
# 10 min  -- nr 287-401
#test set - fetched lines                  :  9384
#test set - gene not in work list - lines   :  6064
#mutations flagged in the db (as  2 )      :  0
#suspected mutation set - number of lines   :  1645
#negative observation set - number of lines :  1675
#reference set - number of lines           :  17182
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x27.mut.genes2.201504131518.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br12.1.2.3x27.neg.genes2.201504131518.txt 

# NEW  pos-neg - 8 run - 12,13,14 ref
aa <- c("NBN","ATM","CDH1","PALB2","RAD51D","TP53","CHEK2","RAD51C")
ac <- date()
db.test.on.mutations(DB.path.name="/br4x8x2panel_20141201", ref1=c("12","13","14"), test2=c("27"),
		geneToWork=aa, flagIt=F, ref.stat=stat.br4x8x2panel.8gene.12.13.14.20141106, ref.freq=freq.br4x8x2panel.8gene.12.13.14.20141106,
		mcode=1, mcode.in=2, out.path.name="/results1/test.mutations.br8.12.13.14x27", method="br8indel")
ac; date()
# 8 min  -- nr 273-401  (30% schneller)
#test set - fetched lines                  :  9384
#test set - gene not in work list - lines   :  3320
#test set - no indel - lines                :  617
#test set - no stat (1) - lines             :  1226
#test set - no stat (2) - lines             :  0
#suspected mutation set - number of lines  :  3019
#negative observation set - number of lines :  3045
#reference set - number of lines           :  71499
#output suspected mutations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x27.mut.genes8.201504131637.txt
#output negative observations:  /home/korschi/eclipseR/BerndD/results1/test.mutations.br8.12.13.14x27.neg.genes8.201504131637.txt 


#### export sample table - to document batch number  and  to add column with number of genes in the performed analysis ####
db.export.sample(DB.path.name="/br4x8x2panel_20141201", export.path="/0info/br4x8x2panel_batchpanel_20150224.csv", orderby="batch", unip=F, batch="")
get.unique.genes(DB.path.name="/br4x8x2panel_20141201", batch=26, first=T, out.path.name=NULL)



