0

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

  • "the program gives an error." -- what error? Is it related to `fclone` (which looks like it should be `fclose`)? – rickdenhaan Dec 18 '21 at 20:18
  • The error is: "This page isn’t workingentrefolhas.loc is currently unable to handle this request. HTTP ERROR 500" – Rodrigo Franco Dec 18 '21 at 20:51
  • 1
    That error comes from your webserver and means you have PHP configured to write error details to your server’s log file. Check that log file for the actual error or [have PHP display errors](https://stackoverflow.com/a/21429652/1941241) while you’re debugging this issue. – rickdenhaan Dec 18 '21 at 21:06
  • Well, the log files is: Notice: Only variables should be passed by reference in /shared/httpd/entrefolhas/converter/upload.php on line 11 Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /shared/httpd/entrefolhas/converter/upload.php on line 38 – Rodrigo Franco Dec 18 '21 at 21:09
  • That notice refers to `end(explode(…))` but is not relevant for your problem. The main problem is the fatal error. You currently have 512 MB of memory available for PHP and that is not enough to handle the larger files. If you cannot increase the available memory, you’ll have to refactor this code to be more memory efficient. Perhaps some things can be moved to a database instead of in-memory arrays. – rickdenhaan Dec 18 '21 at 22:22

0 Answers0