php从另一个文件读取和更新文件上的值

时间:2014-04-05 05:40:33

标签: php file fopen fgets

我有2个文件(A.vcf和ref1.vcf) A.vcf是这样的:

#CHROM  POS ID  REF ALT QUAL    FILTER  INFO    FORMAT
1   5   .   A   C   222 .   INDEL;IS=6,0.111111;DP=54;VDB=1.384012e-01;AF1=0.5;AC1=1;DP4=2,3,1,4;MQ=32;FQ=10.8;PV4=1,0.38,0.00012,0.00052   GT:PL:GQ    0/1:45,0,147:47
2   7   .   G   T   222 .   DP=106;VDB=1.997151e-13;RPB=-2.402409e+00;AF1=1;AC1=2;DP4=1,1,44,58;MQ=20;FQ=-275;PV4=1,1,0.0029,1  GT:PL:GQ    1/1:255,248,0:99
3   15  .   A   G   222 .   DP=106;VDB=2.982598e-04;RPB=-2.402409e+00;AF1=1;AC1=2;DP4=1,1,44,58;MQ=20;FQ=-266;PV4=1,1,0.003,1   GT:PL:GQ    1/1:255,239,0:99
4   11  .   T   A   222 .   DP=85;VDB=3.949915e-01;AF1=1;AC1=2;DP4=0,0,29,44;MQ=22;FQ=-247  GT:PL:GQ    1/1:255,220,0:99

ref1.vcf:

#CHROM  POS ID  REF ALT
1   5   ref12345    A   C
2   15  ref45673    A   G
3   25  ref67893    C   T
4   35  ref66663    C   A

我想更改与引用A.vcf ref1.vcf对应的文件的标题。 因此,这最初是:

id = .
ref = A
alt = C
qual = 222

我希望是这样的:

id = ref12345
ref = A
alt = C
qual = 222

但没有变化。 我的代码是否有错误?

<?php
    $dataSNP = "A.vcf";
    $handleSNP = fopen($dataSNP, "r");
        if ($handleSNP)
        {
            while (($lineSNP = fgets($handleSNP, 4096)) !== false)
            {
                $lineSNP = explode("\t", $lineSNP);
                //removing first with '#'
                if (!empty($lineSNP[0][0]) && $lineSNP[0][0] != '#')
                {
                    $new_dataSNP[] = $lineSNP;
                }
            }
            if (!feof($handleSNP))
            {
                echo "Error: unexpected fgets() fail\n";
            }
            fclose($handleSNP);
        }
        //update 'pos', but not working
        for($i = 0 ; $i < count($new_dataSNP); $i++)
        {
            echo '<pre>';
            print("chrom = ".$new_dataSNP[$i][0]. "\n");
            print("position = ".$new_dataSNP[$i][1]. "\n");
            $file = "ref1.vcf";

            $handle = fopen($file, "r");
            if ($handle)
            {
                while (($line = fgets($handle, 4096)) !== false)
                {
                    $line = explode("\t", $line);

                    if(($line[1] == $new_dataSNP[$i][1]) && ($line[3] == $new_dataSNP[$i][3]) && ($line[4] == $new_dataSNP[$i][4]))
                    {
                        $new_dataSNP[$i][2] = $line[2];
                        break;
                    }
                }
                if (!feof($handle))
                {
                    echo "Error: unexpected fgets() fail\n";
                }
                fclose($handle);
            } 


                print("id = ".$new_dataSNP[$i][2]. "\n");
                print("ref = ".$new_dataSNP[$i][3]. "\n");
                print("alt = ".$new_dataSNP[$i][4]. "\n");

        }
?>

这段代码很简单,但我很难找到错误。有什么可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

很简单:fgets不会在每行末尾切断换行符\n。因此,$line[4]包含换行符,而$new_dataSNP[$i][4]则不包含。

替换

$line = explode("\t", $line);

$line = explode("\t", trim($line));