Php 可变动态跨度重叠

Php 可变动态跨度重叠,php,html,Php,Html,javascript: >chr1:2198584545754_genome_1000+ 500 1000 Model1 0 >chr2:2198581212154_genome_1000+ 510 992 Model2 180 >chr3:2115151215754_genome_1000+ 520 990 Model3 330 >chr4:2198584545754_genome_1000+ 530 980

javascript:

>chr1:2198584545754_genome_1000+    500 1000    Model1  0
>chr2:2198581212154_genome_1000+    510 992     Model2  180
>chr3:2115151215754_genome_1000+    520 990     Model3  330  
>chr4:2198584545754_genome_1000+    530 980     Model3  330 
>chr5:1218455145754_genome_1000+    540 970     Model2  180
>chr6:1231354645454_genome_1000+    550 960     Model1  0
>chr7:1231213211134_genome_1000+    600 950     Model3  330
>chr7:1231213211134_genome_1000+    650 940     Model3  330

试试这个尺码。它通过使用FIFO堆栈
$currentHits
来处理命中边界。我还添加了一些帮助函数来处理颜色生成-如果您更改
getcolor()
函数以返回数组而不是CSS字符串,其中一个可以被删除,从而提高效率

我无法测试这一点,因为我没有
getcolor()
rawtransform()
函数的源代码-如果您将这些添加到问题中,我确信可以进行进一步的改进,并且我可以正确地测试代码。另外,最好看看CSS
Model1
Model2
等类的外观

编辑:现在包括
getcolor()
/
rawtransform()
函数和(至少部分)


请明智且一致地缩进代码,这样在不缩进的情况下就更难调试了。还请显示示例输入数据和所需输出。此外,您应该使用该函数从文件中读取数据,并且您正在
foreach
中创建大量不必要的变量-您可以执行
$resArr[]=array('name'=>$columns['0'],'score'=>$columns['1'],'hit'=>$columns['2'],'model'=>$columns['3'],'color'=>$columns['4']);
非常感谢您的建议,非常感谢您的帮助。另外,如果可以的话,您能解释一下为什么我应该使用fgetcsv吗?因此我知道可以进一步使用。在这种情况下非常感谢,因为它会更节省内存。
文件()
call将整个文件读取到内存中,然后对其进行迭代并创建一个新数组,该数组包含基本相同的数据,因此内存中基本上有两个文件副本。通过打开一个文件指针并一次读取一行,在任何时候内存中最多有一个文件副本+1行。它还结合了two操作-读取文件并创建解析数组-转换为一个,速度会快一点。非常感谢,我现在会看一看,我编辑了我的q以显示我的函数。非常感谢,我无法用我的代码实现这一点:/…我对hsl函数以及它们如何实现颜色感到困惑?不用担心:-D我想关键的想法是除此之外,还可以使用a(有时也称为a)跟踪点击之间的边界。颜色平均功能背后的逻辑很快就组合在一起了,可能有点缺陷,可能需要更多的工作,但将颜色的组成部分表示为数组肯定会使动态计算颜色之间的边界更容易。@user1338194对。JS in与ou配合得很好tput of.为了让它以令人满意的方式工作,我不得不卸载一些处理(就像大多数颜色生成代码一样)到客户端,只是盲目地将一些数据传递给Javascript,但我实际上对结果非常满意。唯一的缺点是它需要JS才能工作。看看它是否满足您的需要……不用担心:-)只要确保您完全理解它是如何工作的,确保您可以根据您的需要进行调整。欢迎光临回来问一些你不懂的问题。其中有一个漏洞,那就是有一个
eval()
在这里,这总是让人感觉非常错误。你可以通过使用真正的JSON和JSON解析器来解决这个问题——但我没有这样做的原因是JSON使用双引号,这需要在HTML属性中传递转义,这大大增加了输出的大小。
>chr1:2198584545754_genome_1000+    500 1000    Model1  0
>chr2:2198581212154_genome_1000+    510 992     Model2  180
>chr3:2115151215754_genome_1000+    520 990     Model3  330  
>chr4:2198584545754_genome_1000+    530 980     Model3  330 
>chr5:1218455145754_genome_1000+    540 970     Model2  180
>chr6:1231354645454_genome_1000+    550 960     Model1  0
>chr7:1231213211134_genome_1000+    600 950     Model3  330
>chr7:1231213211134_genome_1000+    650 940     Model3  330
function toggle() {
var div= document.getElementById('coltext');
var modName=this.getAttribute('ModelName');
var spans=div.getElementsByTagName('span');
var spans_l=spans.length;
while (spans_l--){
    span=spans[spans_l];
    if(span.getAttribute('class')==modName && this.checked==true){
        var color= span.getAttribute('color');

        span.style.background=color;
    }
    if(span.getAttribute('class')==modName && this.checked==false){
        span.style.background="white";
    }
}

}
<?php

  function rawtransform ($raw) {
    return (int) ($raw / 50) - 9;
  }
  function getcolor ($score, $h) {
    switch ($score) {
      // working.  test each color. 
      case 1: /*500-550(Raw Score)*/
        $l = 90;
        break;
      case 2: case 3: //550-650
        $l = 85;
        break;
      case 4: //650-700
        $l = 80;
        break;
      case 5: //700-750
        $l = 70;
        break;
      case 6: //750-800
        $l = 60;
        break;
      case 7: case 8: //800-900;
        $l = 50;
        break;
      case 9: case 10: default: //900-1000 / out of range
        $l = 40;
        break;
    }
    return array(
      'h' => $h,
      's' => 100,
      'l' => $l
    );
  }

  function hsl_average_color () {
    // Takes an unlimited number of arguments, calculates the average HSL value and returns a CSS string
    $args = func_get_args();
    $h = $s = $l = array();
    foreach ($args as $arg) {
      $h[] = $arg['h'];
      $s[] = $arg['s'];
      $l[] = $arg['l'];
    }
    return sprintf('hsl(%d, %d%%, %d%%)', (int) round(array_sum($h) / count($h)), (int) round(array_sum($s) / count($s)), round(array_sum($l) / count($l)));
  }

  $fileName = 'res_example.txt';

  // Open the file
  if (!$fp = fopen($fileName, 'r')) {
    // Handle file read errors here
    die("Unable to open file $fileName");
  }

  // Loop the file data and build an associative array
  $resArr = array();
  while (($line = fgetcsv($fp, 0, "\t")) !== FALSE) {

    // You didn't declare $allModels as an empty array before the loop
    // Should you have?
    $allModels[$line[3]] = 1;

    // Note that I have dropped the hit key and instead keyed the outer
    // array by this value. I have added an end key to track the end of
    // a hit
    $resArr[$line[2]] = array(
      'name' => $line[0],
      'score' => $line[1],
      'end' => $line[2] + 4,
      'model' => $line[3],
      'color' => getcolor(rawtransform($line[1]), $line[4])
    );

  }

  // Close the file
  fclose($fp);

  // Generate a random sequence
  $seqArr = array('A', 'T', 'C', 'G');
  $randseq = array();
  for ($i = 0; $i < 1000; $i++) {
    $randseq[] = $seqArr[array_rand($seqArr)];
  }

  // $res appears to do nothing in you code
  // $res = "";

  // Open the <div>
  echo '<div id="coltext" style="font-family:monospace;background-color:#000000;color:#FFFFFF;">'."\n";

  // Iterate over $allModels and echo checkboxes
  foreach ($allModels as $modName => $value) {

    // ModelName is a non-standard HTML attribute, are you sure you meant to do this?
    echo '<input ModelName="'.$modName.'" type="checkbox" checked="checked" onclick="toggle.apply(this);" />'.$modName."\n";

  }

  echo "<hr />\n";

  // This line does nothing useful here
  // $score = rawtransform($raw);

  // An array to track the current hits
  $currentHits = array();

  foreach ($randseq as $index => $nuc) {

    // Increment $index
    $index++;

    // Track whether we are in a hit/reached a boundary
    $boundary = FALSE;
    $inHit = (bool) count($currentHits);

    // Check whether we are at the end of the lowest hit in the stack
    if ($inHit && $index == $currentHits[0]['end']) {
      $boundary = TRUE;
      array_shift($currentHits);    
    }

    // Check whether we are at the start of a new hit
    if (isset($resArr[$index])) {
      $boundary = TRUE;
      $currentHits[] = $resArr[$index];    
    }

    // If we reached a boundary
    if ($boundary) {

      // Close a hit    
      if ($inHit) {
        echo "</span>";
      }

      // Open a hit
      if (count($currentHits)) {

        // Get the current color value
        $colors = array();
        foreach ($currentHits as $hit) $colors[] = $hit['color'];
        $color = call_user_func_array('hsl_average_color', $colors);

        // Start a new span
        echo '<span class="'.$currentHits[0]['model'].'" title="position:'.$index.',score:'.$currentHits[0]['score'].'" style="color: '.$color.';">';

      }

    }

    // Print the character
    echo $nuc;

    // Split into 50 character chunks        
    if (!($index % 50)){
      echo"<br />\n";
    }

  }

  // Close the last span if one is still open
  if (count($currentHits)) {
    echo "</span>";
  }

  // Close the <div>        
  echo "</div>\n";