douqiong8412
douqiong8412
2014-04-05 05:40
浏览 74
已采纳

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

I have 2 files (A.vcf and ref1.vcf) A.vcf like this:

#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

I want to change the heading of the file that corresponds to the reference A.vcf ref1.vcf. thus, this initially:

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

I want to be like this:

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

but no change happens. whether there was a mistake with my code?

<?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
";
            }
            fclose($handleSNP);
        }
        //update 'pos', but not working
        for($i = 0 ; $i < count($new_dataSNP); $i++)
        {
            echo '<pre>';
            print("chrom = ".$new_dataSNP[$i][0]. "
");
            print("position = ".$new_dataSNP[$i][1]. "
");
            $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
";
                }
                fclose($handle);
            } 


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

        }
?>

This code is simple, but I am confused to find faults. is there anything that can help me?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongzhuxun5136
    dongzhuxun5136 2014-04-05 06:41
    已采纳

    It's simple: fgets does not cut off the new-line character at the end of each line. So, $line[4] contains the new-line character, while $new_dataSNP[$i][4] does not.

    Replace

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

    with

    $line = explode("\t", trim($line));
    
    点赞 评论

相关推荐