2015-08-28 44 views
1

我有兩個大文件,我試圖將來自file_1第一列的信息與file_2的頭文件匹配。有一點小細節,file_2的頭部在開頭有一些信息,每列都有所不同,但最後它有模式匹配。基本上,我必須找到file_1的'pattern'出現在文件2的列名的末尾,並輸出data.frame和這些信息。
請參考下面的文件什麼樣子:在另一個文件(R,Unix)中將'pattern'從一個文件匹配到頭文件的名稱

**file_1** dim (757*3) the first column of the file_1 contains patterns 
10001-101A3 a t 
10008-101B6 b g 
10235-104A1 c h 
-   - - 
-   - - 
etc... 

**file_2** dim (4120*1079) 
blabla.10001.101A3 blbl.2348.101B6 trsdr.1111.111D2 gfder.10008.101B6 .... 
12       1223   544    -    - 
132       23   3564    -    - 
14       223   33    -    - 
162       13   344    -    - 


**Desired output file-3:** I assume that the output size will be 4120*757 
blabla.10001.101A3 gfder.10008.101B6 .... 
12     -    - 
132     -    - 
14     -    - 
162     -    - 

我試圖讓與R(下面是我的腳本)的輸出,但我也想了解我怎麼能在Unix的做到這一點(我猜-awk和-grep可以幫助解決這個問題)。

這裏是我的[R腳本:

table1=read.table("file2.tsv.gz", quote=NULL, sep='\t', header=T, fill=T) 
table2=read.table("file1.txt", quote=NULL, sep='\t', header=T, fill=T) 
    # dim(table1 4120 * 1079) -> need to reduce amount of columns to 757 
    # dim(table2 757 * 3) 

###### the header in table1 has following view 10001.101A3, thus we need to substitute '-' to '.' in pattern 
### What to do: 
### 1) Use gsub() function to substitute '-' by '.' 
### 2) Use gsub() function to remove space in the end of string ' ' by '' 
### 3) Find modified pattern in the end of column's name 
### 4) Apply to the entire table 

pattern=table2[,1]   # '10001-101A3 ' '10008-101B6 ' 
for (x in pattern) { 
    ptn=gsub('-','.',x) 
    ptn1=gsub(' ','',ptn)   # pattern to be matched' 
            # '10001.101A3' '10008.101B6' 

    find_match=table1[,(grepl('^.+ptn1$', header))] 
    final_tb=table1[,find_match] 
} 

我認爲這個問題是在grepl()PTN1的數據表示功能,因爲當我插入10001.101A3代替PTN1我得到答案的一個運行,但顯然我需要循環它。

我也嘗試過(ptn1),但仍然無法使它工作。我希望你的意見,以及任何想法如何可以不在Unix(我是非常基本的Unix用戶,因此目前無法實現此任務)。小數據

df=data.frame(aa24.12a,dda43.23s,fds24.12a,sdf24.112f) 

Z = C('24 -12a」,'43

########################線索 - 23S「)#模式

aa24.12a fds24.12a aa24.12a.1 fds24.12a.1 
1  2  34   2   34 
2  3   2   3   2 
3  4   1   4   1 
4  56   3   56   3 
5  3   5   3   5 


header=colnames(df) 
for (x in z){ 
    ptn=gsub('-','.',x) 
    ptn1=gsub(' ','',ptn)# correct pattern 

    find_match=grep('^.+24.12a$', header)# find match of pattern in header 
    tbl=df[,find_match] 
} 
> tbl 
    aa24.12a fds24.12a 
1  2  34 
2  3   2 
3  4   1 
4  56   3 
5  3   5 

謝謝

回答

-1

謝謝你N8TRO,爲你解決方案並及時回覆。

我自己的解決方案,我問的問題:

# Modify pattern z=('24-12a','43-23s') 
ptn=gsub('-','.',z) 
ptn1=gsub(' ','',ptn) 
# so no it looks like '24.12a' '34.23s' 

i=1   
# create empty vector 
df2=c()   
# Iterate: 
# first loop through column names of data frame 
# second loop goes through vector's value 
# grepl -> searches for matches 
# condition, ==TRUE 
# if so: append to the empty vector, values in the vector will be column numbers 

for (x in colnames(df)){ 
    for (y in ptn1){ 
     e=grepl(y,x) 
      if (e==TRUE){ 
       df2=append(df2,i) 
     } 
    } 
    i=i+1 
} 

desired_output = DF [,DF2]

1

我敢肯定有一些更簡潔,但使用快速劈一行代碼:

read.table(text= 
"**file_1** dim (757*3) the first column of the file_1 contains patterns 
10001-101A3 a t 
10008-101B6 b g 
10235-104A1 c h", 
comment.char="*") -> dat1 

read.table(text=" 
**file_2** dim (4120*1079) 
blabla.10001.101A3 blbl.2348.101B6 trsdr.1111.111D2 gfder.10008.101B6 .... 
12       1223   544    -    - 
132       23   3564    -    - 
14       223   33    -    - 
162       13   344    -    -", 
comment.char="*", header=TRUE) -> dat2 


dat2[,unlist(sapply(dat1[,1], function(x) grep(sub(x, pattern="-", replacement="."), colnames(dat2))))] 

# blabla.10001.101A3 gfder.10008.101B6 
#1     12     - 
#2    132     - 
#3     14     - 
#4    162     - 
相關問題