示例#1
0
 /**
  * Save file attributes from File
  * @param File $file
  * @param boolean $update
  */
 private function setAutoFileAttributes($file, $update = false)
 {
     // We download the file first in /tmp/ folder
     if (!file_exists(ReadFile::TEMP_FOLDER . $file->name)) {
         ReadFile::downloadRemoteFile($file->location, $file->name);
     }
     // from update page, we delete all auto attributes and make them again
     if ($update) {
         $criteria = new CDbCriteria();
         $criteria->addCondition('file_id = ' . $file->id);
         $fileAttributes = FileAttributes::model()->findAll($criteria);
         foreach ($fileAttributes as $fileAttribute) {
             if (strpos(Attribute::AUTO_ATTRIBUTE, $fileAttribute->attribute->structured_comment_name)) {
                 $fileAttribute->delete();
             }
         }
     }
     $fileAttribute = new FileAttributes();
     $fileAttribute->file_id = $file->id;
     /*$name = strpos($file->name, '.') ? explode('.', $file->name) : $file->name;
       $name = is_array($name) ? $name[0] : $name;*/
     $extension = str_replace('.', '', $file->extension);
     switch ($extension) {
         case 'fasta':
         case 'fa':
         case 'fastq':
         case 'seq':
         case 'bam':
         case 'sam':
         case 'cram':
             // Launche the python script to get the result
             list($aminoAcids, $nucleotides) = ReadFile::readPythonFile($file->name);
             // Number of Amino Acids
             $numberAminoAcids = clone $fileAttribute;
             $attribute = Attribute::model()->findByAttributes(array('structured_comment_name' => 'num_amino_acids'));
             $numberAminoAcids->attribute_id = $attribute->id;
             $numberAminoAcids->value = $aminoAcids;
             $numberAminoAcids->save();
             // Number of nucleotides
             $numberNucleotides = clone $fileAttribute;
             $attribute = Attribute::model()->findByAttributes(array('structured_comment_name' => 'num_nucleotides'));
             $numberNucleotides->value = $nucleotides;
             $numberNucleotides->attribute_id = $attribute->id;
             $numberNucleotides->save();
             break;
         case 'txt':
         case 'text':
         case 'doc':
         case 'docx':
         case 'rtf':
         case 'odt':
         case 'wpd':
         case 'lwd':
         case 'readme':
         case 'README':
         case 'pdf':
         case '':
             $result = null;
             if (in_array($extension, array('txt', 'text', 'readme', 'README', '', 'wpd', 'lwd'))) {
                 $result = ReadFile::readTextFile($file->name);
             }
             if (in_array($extension, array('docx', 'odt'))) {
                 $result = ReadFile::readDocxOdtFile($file->name);
             }
             if ($extension == 'doc') {
                 $result = ReadFile::readDocFile($file->name);
             }
             if ($extension == 'rtf') {
                 $result = ReadFile::readRtfFile($file->name);
             }
             if ($extension == 'pdf') {
                 $result = ReadFile::readPdfFile($file->name);
             }
             $numberOfLines = $numberOfWords = 0;
             $numberOfWords = str_word_count($result);
             $numberOfLines = substr_count($result, "\n");
             // Number of words
             $numberWords = clone $fileAttribute;
             $attribute = Attribute::model()->findByAttributes(array('structured_comment_name' => 'num_words'));
             $numberWords->attribute_id = $attribute->id;
             $numberWords->value = $numberOfWords;
             $numberWords->save();
             // Number of lines
             $numberLines = clone $fileAttribute;
             $attribute = Attribute::model()->findByAttributes(array('structured_comment_name' => 'num_lines'));
             $numberLines->attribute_id = $attribute->id;
             $numberLines->value = $numberOfLines;
             $numberLines->save();
             break;
         case 'csv':
         case 'tsv':
         case 'xls':
         case 'xlsx':
         case 'tab':
         case 'sdc':
         case 'ods':
         case 'gff':
         case 'gff3':
         case 'snp':
         case 'vcf':
         case 'ipr':
             $rows = $columns = 0;
             if ($extension == 'xls') {
                 list($rows, $columns) = ReadFile::readXlsFile($file->name);
             }
             if ($extension == 'ods') {
                 list($rows, $columns) = ReadFile::readOdsFile($file->name);
             }
             if ($extension == 'xlsx') {
                 list($rows, $columns) = ReadFile::readXlsxFile($file->name);
             }
             if (in_array($extension, array('csv', 'tsv', 'vcf', 'gff'))) {
                 list($rows, $columns) = ReadFile::readTableFile($file->name, $extension);
             }
             // Number of rows
             $numberRows = clone $fileAttribute;
             $attribute = Attribute::model()->findByAttributes(array('structured_comment_name' => 'num_rows'));
             $numberRows->attribute_id = $attribute->id;
             $numberRows->value = $rows;
             $numberRows->save();
             // Number of columns
             $numberColumns = clone $fileAttribute;
             $attribute = Attribute::model()->findByAttributes(array('structured_comment_name' => 'num_columns'));
             $numberColumns->value = $columns;
             $numberColumns->attribute_id = $attribute->id;
             $numberColumns->save();
             break;
     }
 }
示例#2
0
 /**
  * Before save file
  */
 public function setSizeValue()
 {
     // Save the size from file if not set by user
     if ($this->attributes['location'] && $this->attributes['name']) {
         $size = trim($this->attributes['size']);
         if (empty($size)) {
             if (!file_exists(ReadFile::TEMP_FOLDER . $this->attributes['name'])) {
                 ReadFile::downloadRemoteFile($this->attributes['location'], $this->attributes['name']);
             }
             if (empty($this->size)) {
                 $this->size = filesize(ReadFile::TEMP_FOLDER . $this->name);
             }
         }
     }
 }