setSource($source); } } //###############Get/Set functions################## /** * Return the next sequence record as an array, * @return Array */ function fetchNext() { $record=Array(); //for returning results if($this->next_label) { list($id,$sequence)=$this->findNextLabel(); //treat the Defline used at NCBI //>gi|42794768|ref|NM_002576.3| Homo sapiens p21/Cdc42/Rac1-activated kinase 1 (STE20 homolog, yeast) (PAK1), mRNA if(ereg("^gi", $id)) { genbankDefline($id, $record); } elseif(ereg("^ref", $id)) { refseqDefline($id, $record); } else { $record['id']=$id; } $record['sequence']=$sequence; return $record; } else { //no more records in file return false; } } /** *

Determines whether the source is a file or a string

* Mainly for checking whether or not you've accidentally * passed an invalid filename */ function isFromFile() { if(is_resource($this->source)) { return true; } else { return false; } } /** *

Declare or change the source data

* Can be an actual string containing data, a file handle, or * a filename. */ function setSource(& $source) { if(is_resource($source)) { $this->source=$source; $this->sourcetype='resource'; $this->readToLabel(); //find first label } elseif(is_array($source)) { //assume an already-split array of lines $this->sourcetype='text'; $this->source_lines=$source; } elseif(@file_exists($source)) { //if passed a filename, opens it echo("$source"); $this->sourcetype='file'; $this->source=fopen($source,"r"); $this->readToLabel(); } else { // assume source is a string containing data $this->sourcetype='text'; $this->source_lines=preg_split("/[\r\n]/",$source); } } //################"Internal" functions############# function findNextLabel() { // a "wrapper" - calls the appropriate function for string or file if($this->isFromFile()) { return $this->findNextInFile(); } else { return $this->findNextInString(); } } /** *

Read to next label or end of file

* Return an array with label and * sequence, or false if no more records */ function findNextInFile() { $sequencedata=""; //hold collected sequence lines $label=$this->next_label; while(!(preg_match("/^>/",$line=fgets($this->source,1024)) || feof($this->source))) { //keep adding remaining lines up until end of file or next label $sequencedata.=trim($line); } if(!(feof($this->source))) { $this->next_label=trim(substr($line,1)); //remove the leading > character } else { if ($this->sourcetype=='file') { fclose($this->source); } $this->next_label=false; } return Array($label,$sequencedata); } /** *

Pulls the next sequence from the string array

* Returns an array with label,sequence */ function findNextInString() { $label=$this->next_label; while ((!preg_match("/^>".$label."/",$line=array_shift($this->source_lines))) && (count($this->source_lines) > 0) ) { //keep adding remaining lines up until end of file or next FASTA label $sequencedata.=trim($line); } if(count($this->source_lines) > 0) { $this->next_label=trim(substr($line,1)); //remove the leading > character } else { $this->next_label=false; } return Array($label,$sequencedata); } /** * "wrapper" - calls appropriate function type */ function readToLabel($label=".*") { if($this->isFromFile()) { return $this->readFileToLabel($label); } else { return $this->readStringToLabel($label); } } /** * scans to next label by default, or to the label name specified */ function readFileToLabel($label=".*") { while(!(preg_match("/^>".$label."/",$line=fgets($this->source,1024)) || feof($this->source))) { //do nothing, really...just scan until that point } if(feof($this->source)) { $this->next_label=false; if($this->sourcetype=='file') { fclose($this->source); } } else { $this->next_label=trim(substr($line,1)); } } /** * Advances through the source array to the specified label */ function readStringToLabel($label=".*") { while(!(preg_match("/^>".$label."/",$line=array_shift($this->source_lines))) && (count($this->source_lines) > 0)) { //do nothing, really...just scan until that point } if(count($this->source_lines) < 1) { $this->next_label=false; } else { $this->next_label=trim(substr($line,1)); } } } //################functions to treat get informations from the defline############# function genbankDefline($id, &$record) { $id = substr($id, 3); //take off gi| list($id, $db, $accession_version, $definition) = explode("|", $id); list($accession, $version) = explode(".", $accession_version); $record['ncbi_gi_id'] =$id; $record['id'] =$accession; $record['accession'] =$accession; $record['version'] =$accession_version; $record['definition'] =$definition; if (ereg("mRNA", $definition)) $record['moltype'] = "mRNA"; if (ereg("Homo sapiens", $definition)) { $record['source'] = "Homo sapiens (human)"; $record['organism'] = "Homo sapiens"; } } function refseqDefline($id, &$record) { $id = substr($id, 4); //take off ref| list($accession_version, $definition) = explode("|", $id); list($accession, $version) = explode(".", $accession_version); $record['accession'] =$accession; $record['version'] =$accession_version; $record['definition'] =$definition; if (ereg("mRNA", $definition)) $record['moltype'] = "mRNA"; if (ereg("Homo sapiens", $definition)) { $record['source'] = "Homo sapiens (human)"; $record['organism'] = "Homo sapiens"; } } ?>