php read and updating value on file from another file -
i have 2 files (a.vcf , ref1.vcf) a.vcf this:
#chrom pos id ref alt qual filter info format 1 5 . 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 . 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 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 c 2 15 ref45673 g 3 25 ref67893 c t 4 35 ref66663 c
i want change heading of file corresponds reference a.vcf ref1.vcf. thus, initially:
id = . ref = alt = c qual = 222
i want this:
id = ref12345 ref = alt = c qual = 222
but no change happens. whether there mistake code?
<?php $datasnp = "a.vcf"; $handlesnp = fopen($datasnp, "r"); if ($handlesnp) { while (($linesnp = fgets($handlesnp, 4096)) !== false) { $linesnp = explode("\t", $linesnp); //removing first '#' if (!empty($linesnp[0][0]) && $linesnp[0][0] != '#') { $new_datasnp[] = $linesnp; } } if (!feof($handlesnp)) { echo "error: unexpected fgets() fail\n"; } fclose($handlesnp); } //update 'pos', 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"); } ?>
this code simple, confused find faults. there can me?
it's simple: fgets
not cut off new-line character \n
@ end of each line. so, $line[4]
contains new-line character, while $new_datasnp[$i][4]
not.
replace
$line = explode("\t", $line);
with
$line = explode("\t", trim($line));
Comments
Post a Comment