使用R函数

时间:2016-07-22 15:38:09

标签: r function dataframe bioinformatics

我有一个名为gwas.data

的data.frame
        SNP A1 A2         EFF     FRQ
2353 rs10001803  A  G -0.06620391 0.06860
2307 rs10002573  T  C -0.03969763 0.78100
504  rs10003143  A  C  0.03829721 0.53170
1802  rs1001022  T  C  0.08159842 0.96174
461  rs10011564  T  C  0.04930432 0.27840
2331 rs10013187  A  C -0.03600030 0.54490

我有一个名为correct.orientation的第二帧:

      SNP   CLST A1 A2  FRQ IMP       POS CHR BVAL
54445  rs10001803 Brahui  G  A 1.00   1 157121506   4  898
49713  rs10002573 Brahui  C  T 0.26   0  31120097   4  983
52885  rs10003143 Brahui  A  C 0.42   0 114272159   4  918
193805  rs1001022 Brahui  T  C 0.98   0  24733488  22  970
48257  rs10011564 Brahui  T  C 0.10   1  18734768   4  863
52313  rs10013187 Brahui  C  A 0.34   1 103040573   4  908

我试图让A1和A2的列在两个文件之间匹配。如果从gwas.data翻转了correct.orientation的列,那么我想将它们翻转到正确的方向。如果我翻转它们,我还想更改EFF列的符号并为FRQ列取(1-FRQ)。以下是我目前正在尝试使用的代码:

gwas.data <- MatchAlleles ( gwas.data , assoc.loci.freqs)    
MatchAlleles <- function ( gwas.data , assoc.loci.freqs ) {

            if ( nrow ( gwas.data ) != nrow ( correct.orientation ) ) {
                    stop ( "GWAS dataset and Orientation Matching dataset contain differing numbers of SNPs" )
            }

            flip <- gwas.data$A1 == correct.orientation$A2 & gwas.data$A2 == correct.orientation$A1
            dont.flip <- gwas.data$A1 == correct.orientation$A1 & gwas.data$A2 == correct.orientation$A2
            for ( i in 1 : nrow ( gwas.data ) ) {
                    if ( flip [ i ] ) {
                            gwas.data$A1 [ i ] <- correct.orientation$A1 [ i ]
                            gwas.data$A2 [ i ] <- correct.orientation$A2 [ i ]
                            gwas.data$EFF [ i ] <- - gwas.data$EFF [ i ]
                            gwas.data$FRQ [ i ] <- 1 - gwas.data$FRQ [ i ]
                    } else if ( dont.flip [ i ] ) {
                            #do nothing
                    } else {
                            stop ( "Strand Issue")
                    }
            }
    return ( gwas.data )
    }

assoc.loci.freqs字词无关紧要,并且包含在原始代码中,但在函数中更高,并且不会影响它。当我尝试使用此代码时,我收到错误:Error in Ops.factor(gwas.data$A1, correct.orientation$A2) : level sets of factors are different可能导致此问题的原因是什么?

1 个答案:

答案 0 :(得分:0)

是否某个核苷酸位于gwas.data$A1但不是correct.orientation$A2?使用上面给出的示例,A1的因子级别为AT,而A2的因子级别为AT ,和C

@ dayne的评论中的建议应该绕过这个问题。