I would like to compare two files and create a new one with the result of both. Basically the user uploads the .csv file and then the program reads the file and compares it with a .txt file. Then it creates a new file with the results of the files. For example: User .csv file:
RSID,CHROMOSOME,POSITION,RESULT
rs9651229,1,567667,CC
rs9701872,1,568208,TT
rs11497407,1,568527,GG
rs116587930,1,727841,GG
rs3131972,1,752721,GG
rs114525117,1,759036,GG
rs12127425,1,794332,GG
rs79373928,1,801536,TT
rs116452738,1,834830,GG
rs72631887,1,835092,TT
rs4970383,1,838555,CC
rs28678693,1,838665,TT
rs4970382,1,840753,CC
Template .txt file:
Comments
rsid chromosome position genotype
rs548049170 1 69869
rs13328684 1 74792
rs9283150 1 565508
i713426 1 726912
rs116587930 1 727841
rs3131972 1 752721
rs12184325 1 754105
rs12567639 1 756268
rs114525117 1 759036
rs12124819 1 776546
rs12127425 1 794332
rs79373928 1 801536
rs72888853 1 815421
rs7538305 1 824398
rs28444699 1 830181
Result .txt file:
rs548049170 1 69869 --
rs13328684 1 74792 --
rs9283150 1 565508 --
i713426 1 726912 --
rs116587930 1 727841 GG
rs3131972 1 752721 AG
rs12184325 1 754105 --
rs12567639 1 756268 --
rs114525117 1 759036 GG
rs12124819 1 776546 --
rs12127425 1 794332 GG
rs79373928 1 801536 TT
rs72888853 1 815421 --
rs7538305 1 824398 --
rs28444699 1 830181 --
In short, where there is no result, put --, otherwise, put the value. I created this code:
index.php:
<!DOCTYPE html>
<html lang="pt-br">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Convertendo dados brutos para outras plataformas</title>
</head>
<body>
<div align="center">
<form method = "POST" action = './upload.php' enctype='multipart/form-data'>
<input type = "file" id = "arquivo" name = "arquivo"></input>
</br>
<button type="submit">Salvar</button>
</form>
</div>
</body>
</html>
upload.php
<?php
//Create the array
$dna= array();
// check there are no errors
if($_FILES['arquivo']['error'] == 0){ //Receive the file from the user
$name = $_FILES['arquivo']['name'];
$ext = strtolower(end(explode('.', $name)));
$name2 = strtolower((explode('.', $name)[0]));
$type = $_FILES['arquivo']['type'];
$tmpName = $_FILES['arquivo']['tmp_name'];
// check the file is a csv
if($ext === 'csv'){
if(($handle = fopen($tmpName, 'r')) !== FALSE) {
// necessary if a large csv file
$data = fgetcsv($handle, 1000, ',');
$row = 0;
while(($data = fgetcsv($handle, 100, ',')) !== FALSE) {
array_push($dna, ["RSID" => $data[0], "CHROMOSOME" => $data[1], "POSITION" => $data[2], "RESULT" => $data[3]]);
$row++;
}
fclose($handle); //Close the csv file
}
$txt_file = fopen('Template_23andme_v5.txt','r'); //Template that I would like to compare
$modelo = array(); //Create a array
$a = 1;
while ($a <= 20) { //Taking out the comments
$line = fgets($txt_file);
$a++;
}
while ($line = fgets($txt_file)) {
$line2 = explode("\t", $line); //Separating the spaces
array_push($modelo, ["RSID" => $line2[0], "CHROMOSOME" => $line2[1], "POSITION" => $line2[2], "RESULT" => "--"]); //Putting the array
}
fclose($txt_file); //Close the txt file
//Creating a new file
header("Content-disposition: attachment; filename=$name2.txt");
header("Content-type: text/plain");
$output = fopen( 'php://output', 'w' );
$pos = 0;
foreach ($modelo as $linha) {
$key = array_search($linha["RSID"], array_column($dna, "RSID"));
if(!empty($key)){ //If find the RSID, put the result in the array
$modelo[$pos]['RESULT'] = $dna[$key]['RESULT'];
}
echo $modelo[$pos]["RSID"]."\t".$modelo[$pos]["CHROMOSOME"]."\t".$modelo[$pos]["POSITION"]."\t".$modelo[$pos]["RESULT"]."\n"; //Putting the array with RESULT
$pos++;
}
fclose($output);
}
}
If the user puts a small file, the program works perfectly, however, if the user puts a 16 mb file, for example, the program gives an error. Can someone help me?
Files that I uses it. link