Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8023803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:40:23+00:00 2026-06-04T22:40:23+00:00

$lines = file(res_example.txt); $resArr = array(); foreach ($lines as $line_num => $line) { $columns

  • 0
$lines = file("res_example.txt");

$resArr = array();
foreach ($lines as $line_num => $line) {
  $columns = explode("\t", $line);
  $raws = $columns['1'];
  $hits = $columns['2'];
  $names = $columns['0'];
  $models = $columns['3'];
  $colors = $columns['4'];
  $allModels[$models] = 1;
  $resArr[] = array(
    name => $names,
    score => $raws,
    hit => $hits,
    model => $models,
    color => $colors
  );
}


$seqArr = array('A', 'T', 'C', 'G');
$randseq = array();

for ($i = 0; $i < 1000; $i++) {
  $randseq[] = $seqArr[array_rand($seqArr)];
}

$res = "";

echo "<div id=\"coltext\" style=\"font-family:monospace;\">";

foreach ($allModels as $modName => $value) {

  echo "<input ModelName=$modName type=\"checkbox\"
  checked==\"TRUE\" onclick=\"toggle.apply(this)\" />$modName";

}

echo "<hr />";
$score = rawtransform($raw);
foreach ($randseq as $index => $nuc) {
  $index = $index + 1;
  foreach ($resArr as $hitArr) {
    $hit = $hitArr['hit'];
    $raw = $hitArr['score'];
    $model = $hitArr['model'];
    $colval = $hitArr['color'];
    $score = rawtransform($raw);

    $color = getcolor($score, $colval);

    if (($hit+3) == $index) { 
      echo "</span>";
    }
    if ($hit == $index) { 
      echo "<span class=$model Title=\"position:$index,score:$raw\"
      style=\"background:$color;\" color=\"$color\">";
      //problem when theres overlap !? 
    }

  }

  echo $nuc;

  if (($index%50)==0){
    echo"<br />";
  }
}

echo "</div>";

function rawtransform($raw) {
  return (int)($raw/50)-9;
}

function getcolor($score,$ArrayModelval)
{
switch ($score){
// working.  test each color. 
case 1: /*500-550(Raw Score)*/
        $col=$ArrayModelval;
            return"hsl( $col,100%,90%)";
    break;
case 2: //550-600
        $col=$ArrayModelval;
            return "hsl( $col,100%,85%)";
    break;
case 3: //600-650
        $col=$ArrayModelval;
            return "hsl( $col,100%,85%)";
    break;
case 4: //650-700
        $col=$ArrayModelval;
            return"hsl( $col,100%,80%)";
    break;
case 5: //700-750
        $col=$ArrayModelval;
            return"hsl( $col,100%,70%)";
    break;
case 6: //750-800
        $col=$ArrayModelval;
            return "hsl( $col,100%,60%)";
    break;
case 7: //800-850
        $col=$ArrayModelval;
            return "hsl( $col,100%,50%)";
    break;
case 8: //850-900;
        $col=$ArrayModelval;
            return "hsl( $col,100%,50%)";
    break;
case 9: //900-950
        $col=$ArrayModelval;
            return "hsl( $col,100%,40%)";
    break;
case 10: //950-1000
        $col=$ArrayModelval;
    return "hsl($col,100%,40%)";
    break;

   }
}

For the most part does what I want: I want to color parts of the random seqeunce where there is a $hit – defined on external file. My only problem is when there is any overlap, i.e. if two hits are within 3 bases of each other the span is elongated and colored as if its one span.

The external file has a position to start a span which have variable colors depending on a score given in the external file. Basically if I have 3 results, 2 of which have almost the same hit (+-1) and the other a different hit, I would only see two sections colored, can anyone see what my problem is? Sorry I know I probably worded this horribly but its hard to explain. Thanks.

>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

javascript:

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";
    }
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T22:40:25+00:00Added an answer on June 4, 2026 at 10:40 pm

    Try this on for size. It works by using a FIFO stack $currentHits to handle the hit boundaries. I have also added a few helper functions to deal with color generation – if you alter your getcolor() function to return an array instead of a CSS string one of them could be dropped, making it more efficient.

    I have been unable to test this because I don’t have the source code for your getcolor() or rawtransform() functions – if you add these to the question I am certain further improvements can be made and I can test the code properly. Also, it would be good to see what your CSS Model1, Model2 etc classes look like.

    Edit: now includes getcolor()/rawtransform() functions and (at least partially) tested

    <?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";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

//assign variables string measurementIn; //read in file in array string[] lines = File.ReadAllLines(../../convert.txt); //ask
abc.dat file contains lines 1 2 3 code is tradeline= for line in $(cat
Possible Duplicate: Prepend lines to file in Java I have a CSV-file which supports
I want to log what lines per file was run in php. Xdebug can
I am using fgets() to read lines from file. I am able to read
I've got log file with lines like: 07:44:24||||234.234.234.234|123.123.123.123|www.website.pl/some,site.html|a:0:{} How do I obtain only www.website.pl/some,site.html
I am receiving a large file 500k+ lines but all the content is in
So I have several lines in a file that looks like this useradmin:$NT$asdlfkjwaeroisglkwerlkj23424tlj::: useradmin:c2q3r4lsdfk23rlsdfkj3rjsdflk2k23:::
I am to find lines in a file whose fifth character is a digit.
I've read I have to add these lines into my .au3 file: #Region #AutoIt3Wrapper_Icon=C:\myicon.ico

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.