python 查找指定字符串 ,找出相应的数据行
- def get_target_Lines(fr, s):
- f_handle = open(fr, "r")
- targets = []
- #flag = False
- for num, line in enumerate(f_handle):
- if line.startswith(s):
- tmp = num
- try:
- if num > tmp and line.rstrip() != "":
- targets.append(line.rstrip())
- elif num > tmp and line.rstrip() == "":
- # flag = True
- break
- except NameError, e:
- pass
- #if flag:
- # break
- return targets
tgets = get_target_Lines("outfile_f" , "Inferred ancestry")
用 R 自定义函数
用 apply, sprintf , order 等函数
- > mean
- function (x, ...)
- UseMethod("mean")
- <bytecode: 0x00000000111edea8>
- <environment: namespace:base>
- > mean.default
- function (x, trim = 0, na.rm = FALSE, ...)
- {
- if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
- warning("argument is not numeric or logical: returning NA")
- return(NA_real_)
- }
- if (na.rm)
- x <- x[!is.na(x)]
- if (!is.numeric(trim) || length(trim) != 1L)
- stop("'trim' must be numeric of length one")
- n <- length(x)
- if (trim > 0 && n) {
- if (is.complex(x))
- stop("trimmed means are not defined for complex data")
- if (anyNA(x))
- return(NA_real_)
- if (trim >= 0.5)
- return(stats::median(x, na.rm = FALSE))
- lo <- floor(n * trim) + 1
- hi <- n + 1 - lo
- x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi]
- }
- .Internal(mean(x))
- }
- <bytecode: 0x00000000111ee3c0>
- <environment: namespace:base>
- /////////////////////////////////////////////////////////////////////
- > mt <- function(v){
- + n <- length(v)
- + t1 <- v[1:5]
- + tp <- sum(v[6:n])
- + t2 <- v[6:n]
- + if (tp > 35){
- + max_index = which(t2==max(t2))[1]
- + t2[max_index] = max(t2)- tp + 35
- + }
- + return(c(t1,t2))
- + }
- > mt(1:10)
- [1] 1 2 3 4 5 6 7 8 9 5
- > a <- 1:10
- > b <- c(2,5,6,7, 466, 466,466, 34,5,2)
- > c <- c(2,5,6,7, 466, 89,14, 56,7,2)
- > data1 <- data.frame(a,b,c)
- > data1
- a b c
- 1 1 2 2
- 2 2 5 5
- 3 3 6 6
- 4 4 7 7
- 5 5 466 466
- 6 6 466 89
- 7 7 466 14
- 8 8 34 56
- 9 9 5 7
- 10 10 2 2
- > apply(data1, 2, mt)
- a b c
- [1,] 1 2 2
- [2,] 2 5 5
- [3,] 3 6 6
- [4,] 4 7 7
- [5,] 5 466 466
- [6,] 6 -472 -44
- [7,] 7 466 14
- [8,] 8 34 56
- [9,] 9 5 7
- [10,] 5 2 2
- > data1 <- data.frame(rbind(a,b,c) )
- > data1
- X1 X2 X3 X4 X5 X6 X7 X8 X9 X10
- a 1 2 3 4 5 6 7 8 9 10
- b 2 5 6 7 466 466 466 34 5 2
- c 2 5 6 7 466 89 14 56 7 2
- > apply(data1, 1, mt)
- a b c
- X1 1 2 2
- X2 2 5 5
- X3 3 6 6
- X4 4 7 7
- X5 5 466 466
- X6 6 -472 -44
- X7 7 466 14
- X8 8 34 56
- X9 9 5 7
- X10 5 2 2
- ///////////////////////////////////////////////////////////////////////
- > data <- read.table("test_plot.txt", skip=1, stringsAsFactors =F)
- > c("A","B",3)
- [1] "A" "B" "3"
- > mt <- function(v){
- n <- length(v)
- t1 <- v[1:5]
- t2 <- as.numeric(v[6:n])
- tp <- sum(t2)
- if (tp > 1){
- max_index = which(t2==max(t2))[1]
- t2[max_index] = max(t2)- tp + 1
- } else if (tp < 1){
- min_index = which(t2==min(t2))[1]
- t2[min_index] = min(t2) + 1 - tp
- }
- return(c(t1,t2))
- }
- >
- > apply(data[1:3,], 1, mt)
- 1 2 3
- V1 "1" "2" "3"
- V2 "100" "101" "102"
- V3 "(0)" "(0)" "(0)"
- V4 "1" "1" "1"
- V5 ":" ":" ":"
- "0.663" "0.767" "0.651"
- "0.185" "0.042" "0.083"
- "0.039" "0.033" "0.062"
- "0.113" "0.158" "0.204"
- > t(apply(data, 1, mt))
- V1 V2 V3 V4 V5
- [1,] " 1" "100" "(0)" "1" ":" "0.663" "0.185"
- [2,] " 2" "101" "(0)" "1" ":" "0.767" "0.042"
- [3,] " 3" "102" "(0)" "1" ":" "0.651" "0.083"
- [4,] " 4" "103" "(0)" "1" ":" "0.358" "0.078"
- [5,] " 5" "104" "(0)" "1" ":" "0.062" "0.096"
- [6,] " 6" "105" "(0)" "1" ":" "0.058" "0.098"
- > t <- sprintf( "%.7f", 1.44*1.21)
- > t
- [1] "1.7424000"
- > t <- sprintf( "%.3f", 1.44*1.21)
- > t
- [1] "1.742"
- > mt <- function(v){
- + n <- length(v)
- + t1 <- v[1:5]
- + t2 <- as.numeric(v[6:n])
- + tp <- sum(t2)
- +
- + if (tp > 1){
- + max_index <- which(t2==max(t2))[1]
- + t2[max_index] <- sprintf("%.3f", max(t2)-tp+1)
- + } else if (tp < 1){
- + min_index <- which(t2==min(t2))[1]
- + t2[min_index] <- sprintf("%.3f", min(t2) + 1 - tp)
- + }
- + return(c(t1,t2))
- + }
- > head(data)
- V1 V2 V3 V4 V5 V6 V7 V8 V9
- 1 1 100 (0) 1 : 0.663 0.185 0.038 0.113
- 2 2 101 (0) 1 : 0.767 0.042 0.033 0.158
- 3 3 102 (0) 1 : 0.651 0.083 0.062 0.204
- 4 4 103 (0) 1 : 0.358 0.078 0.185 0.379
- 5 5 104 (0) 1 : 0.062 0.096 0.487 0.355
- 6 6 105 (0) 1 : 0.058 0.098 0.516 0.329
- > head(data[,6:9])
- V6 V7 V8 V9
- 1 0.663 0.185 0.038 0.113
- 2 0.767 0.042 0.033 0.158
- 3 0.651 0.083 0.062 0.204
- 4 0.358 0.078 0.185 0.379
- 5 0.062 0.096 0.487 0.355
- 6 0.058 0.098 0.516 0.329
- > head(t(apply(data, 1, mt)))
- V1 V2 V3 V4 V5
- [1,] " 1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
- [2,] " 2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
- [3,] " 3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
- [4,] " 4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
- [5,] " 5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
- [6,] " 6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
- > apply(data[,6:9], 1 , sum)
- [1] 0.999 1.000 1.000 1.000 1.000 1.001 1.000 1.000 1.000 1.000 1.000 1.000
- [13] 1.001 1.000 1.000 1.001 0.999 0.999 1.000 1.000 1.000 1.000 1.000 1.000
- [25] 1.001 1.000 1.001 1.000 0.999 1.000 1.000 1.000 1.000 1.000 1.001 1.000
- [37] 1.000 1.000 0.999 1.000 0.999 1.001 1.000 0.999 1.000 1.000 1.000 1.000
- [49] 1.000 0.999 1.000 1.000 1.001 1.000 1.001 1.000 1.000 1.001 1.000 1.000
- [61] 1.000 1.000 1.000 1.001 0.999 1.000 0.999 1.001 0.999 0.999 1.000 1.000
- [73] 0.999 1.000 1.000 1.000 0.999 1.000 1.000 1.001 1.000 1.000 1.000 1.001
- [85] 1.000 1.001 1.000 1.000 1.000 1.001 1.000 1.000 1.000 1.000 1.000 1.001
- [97] 1.001 1.000 1.000 1.000 1.000 1.000 1.001 1.001 1.000 1.001 1.000 1.000
- [109] 1.000 1.000 0.999 0.999 1.001 1.000 1.000 1.000 0.999 1.001 1.000 1.000
- [121] 1.000 1.000 1.000 1.000 1.000 0.999 1.000 1.000 1.000 1.000 1.000 1.000
- [133] 1.000 1.001 1.001 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000 1.000
- [145] 1.001 0.999 1.000 0.999 1.000 1.000 0.999 1.000 0.999 1.000 0.999 1.000
- [157] 0.999 0.999 1.000 1.001 1.000 1.001 1.000 1.000 1.000 0.999 1.000 1.001
- [169] 1.000 1.000 0.999 1.000 1.000 1.000 0.999
- > x <- t(apply(data, 1, mt))
- > head(x)
- V1 V2 V3 V4 V5
- [1,] " 1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
- [2,] " 2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
- [3,] " 3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
- [4,] " 4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
- [5,] " 5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
- [6,] " 6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
- > head(x[,6:9])
- [1,] "0.663" "0.185" "0.039" "0.113"
- [2,] "0.767" "0.042" "0.033" "0.158"
- [3,] "0.651" "0.083" "0.062" "0.204"
- [4,] "0.358" "0.078" "0.185" "0.379"
- [5,] "0.062" "0.096" "0.487" "0.355"
- [6,] "0.058" "0.098" "0.515" "0.329"
- > m <- apply(data.frame(x[,6:9]), 1, as.numeric)
- > apply(m,2,sum)
- [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- [37] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- [73] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- [109] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- [145] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- > n <- apply(data.frame(x[,6:9]), 2, as.numeric)
- > apply(n,1,sum)
- [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- [37] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- [73] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- [109] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- [145] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
- >
- /////////////////////////////////////////////////////////////////////
- > x <- t(apply(data, 1, mt))
- > head(x)
- V1 V2 V3 V4 V5
- [1,] " 1" "100" "(0)" "1" ":" "0.663" "0.185" "0.039" "0.113"
- [2,] " 2" "101" "(0)" "1" ":" "0.767" "0.042" "0.033" "0.158"
- [3,] " 3" "102" "(0)" "1" ":" "0.651" "0.083" "0.062" "0.204"
- [4,] " 4" "103" "(0)" "1" ":" "0.358" "0.078" "0.185" "0.379"
- [5,] " 5" "104" "(0)" "1" ":" "0.062" "0.096" "0.487" "0.355"
- [6,] " 6" "105" "(0)" "1" ":" "0.058" "0.098" "0.515" "0.329"
- > x1 <- as.data.frame(x)
- > head(x1)
- V1 V2 V3 V4 V5 V6 V7 V8 V9
- 1 1 100 (0) 1 : 0.663 0.185 0.039 0.113
- 2 2 101 (0) 1 : 0.767 0.042 0.033 0.158
- 3 3 102 (0) 1 : 0.651 0.083 0.062 0.204
- 4 4 103 (0) 1 : 0.358 0.078 0.185 0.379
- 5 5 104 (0) 1 : 0.062 0.096 0.487 0.355
- 6 6 105 (0) 1 : 0.058 0.098 0.515 0.329
- > data_p <- x1[order(x1[,6], decreasing=T),]
- > pdf("graph.pdf", height=5, width=15)
- > barplot(as.matrix(t(data_p[,6:9])), names.arg=data_p$V2, col=rainbow(4), border= rainbow(4), las=2, cex.names=0.35, cex.axis =0.65)
- > dev.off()
- null device
- 1
- >
绘图结果:
来源: