1
1. ZFP112
Official Symbol: ZFP112 and Name: zinc finger protein 112 homolog (mouse)[Homo sapiens]
Other Aliases: ZNF112, ZNF228
Other Designations: zfp-112; zinc finger protein 112; zinc finger protein 228
Chromosome: 19; Location: 19q13.2
Annotation: Chromosome 19NC_000019.9 (44830706..44860856, complement)
ID: 7771

2. SEP15
15 kDa selenoprotein[Homo sapiens]
Chromosome: 1; Location: 1p31
Annotation: Chromosome 1NC_000001.10 (87328128..87380107, complement)
MIM: 606254
ID: 9403

3. MLL4
myeloid/lymphoid or mixed-lineage leukemia 4[Homo sapiens]
Other Aliases: HRX2, KMT2B, MLL2, TRX2, WBP7
Other Designations: KMT2D; WBP-7; WW domain binding protein 7; WW domain-binding protein 7; histone-lysine N-methyltransferase MLL4; lysine N-methyltransferase 2B; lysine N-methyltransferase 2D; mixed lineage leukemia gene homolog 2; myeloid/lymphoid or mixed-lineage leukemia protein 4; trithorax homolog 2; trithorax homologue 2
Chromosome: 19; Location: 19q13.1
Annotation: Chromosome 19NC_000019.9 (36208921..36229779)
MIM: 606834
ID: 9757

37. LOC100509547
hypothetical protein LOC100509547[Homo sapiens]
This record was discontinued.
ID: 100509547

43. LOC100509587
hypothetical protein LOC100509587[Homo sapiens]
Chromosome: 6
This record was replaced with GeneID: 100506601
ID: 100509587

我想获取基因名称(ZFP112、SEP15、MLL4)、位置字段(如果存在)、ID 字段,并跳过其他内容。所有像 scan() 这样的字符串实用程序似乎都面向更常规的数据。记录之间的空白行实际上是记录分隔符。我可以将它写入磁盘并使用 readLines() 将其读回,但我更喜欢从内存中执行它,因为我通过 HTTP 下载了它。

4

1 回答 1

4

例如,从 中读取数据"myfile.dat"(或者L如果您之前已将其作为单独的行读取,则从下面开始)。现在提取那些以数字开头的行,后跟一个点,后跟一个空格,或者包含单词Location:或以 . 开头的行ID:。然后删除这些行中的所有内容,包括最后一个空格。创建一个组向量g,用于标识每个组件v2所属的组。(我们使用了这样一个事实,即每个组的开始字段以非数字开头,而其他字段以数字开头。)然后分成v2这些组。通过适当地插入一个 NA 来扩展 short 组件,s假设如果它的 shortLocation:丢失了。(我们假设第一个字段和ID字段不能丢失。)最后转置它,使字段在列中,案例在行中。

L <- readLines("myfile.dat")
v <- grep("^\\d+\\. |Location: |^ID: ", L, value = TRUE)
v2 <- sub(".* ", "", v)

g <- cumsum(regexpr("^\\D", v2) > 0)
s <- split(v2, g)
m <- sapply(s, function(x) if (length(x) == 2) c(x[[1]], NA, x[[2]]) else x)
t(m)

使用帖子中的示例数据,我们从最后一行得到:

  [,1]           [,2]      [,3]       
1 "ZFP112"       "19q13.2" "7771"     
2 "SEP15"        "1p31"    "9403"     
3 "MLL4"         "19q13.1" "9757"     
4 "LOC100509547" NA        "100509547"
5 "LOC100509587" NA        "100509587"
于 2013-04-15T23:59:01.727 回答