modifier.get_description.php 6.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
/**
 * Smarty plugin
 * 
 * @package Smarty
 * @subpackage PluginsModifier
 */

/**
 * NG6 get_description modifier plugin
 * 
 * Type:     modifier<br>
 * Name:     get_description<br>
 * Purpose:  return a mid description from a string 
 * 
 * @link 
 * @author Jerome Mariette <jerome dot mariete at toulouse dot inra dot fr> 
 * @param string $
 * @param hash table $  
 * @return string 
 */
22 23 24 25 26 27 28 29 30 31 32 33 34
 
/**
 * Return a description for the sample based on a 
 * newbler formated file name
 * 
 * @param	string	the file name to process
 * @param	desc	the sample description table 
 * @return	the best description, if none found the $string itself
 */
function get_newbler_desc($string, $desc) {
	
	$best_desc = array();
	$best_description = $string;
35
    foreach($desc as $mid => $description) {
36 37
		// Mids name can be in between 2 dots
    	if (preg_match("/\.".$mid."\./i", $string)) {
38
    		$best_desc[$mid] = $description;
39 40
    		$best_description = $description;
    	// Or can be rigth at the end
41
    	} elseif (preg_match("/\.".$mid."$/i", $string)) {
42 43
    		$best_desc[$mid] = $description;
    		$best_description = $description;
44 45 46 47
    	} elseif (preg_match("/\.".$description."\./i", $string)) {
    		$best_desc[$mid] = $description;
    		$best_description = $description;
    	// Or can be rigth at the end
48
    	} elseif (preg_match("/\.".$description."$/i", $string)) {
49 50
    		$best_desc[$mid] = $description;
    		$best_description = $description;
Jerome Mariette's avatar
Jerome Mariette committed
51 52
    	}
    }
53 54 55 56 57
    // If no mids found in between .[desc].
    if (count($best_desc) == 0) {
    	$best_description = $string;
    // If multple mids matches take the longuest from the $final_descriptions
    } elseif (count($best_desc) > 1) {
58
    	$max_len = 0;
59
    	$best_description = $string;
60 61 62
    	foreach($best_desc as $mid => $description) {
    		if (strlen($mid) > $max_len) {
    			$max_len = strlen($mid);
63
    			$best_description = $description;
64 65
    		}
    	}
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    }
	return $best_description;
} 

/**
 * Return a description for the sample based on a 
 * casava_1_7 formated file name
 * 
 * @param	string	the file name to process
 * @param	desc	the sample description table 
 * @return	the best description, if none found the $string itself
 */
function get_casava_1_7_desc($string, $desc) {
	$best_description = $string;
    foreach($desc as $mid => $description) {
		// Index must be at begining
    	if (preg_match("/^".$description."_/i", $string)) {
    		// Find the pairend info if there is one
    		if (preg_match("/_1_qseq/i", $string)) {
85
    			$best_description = $description." (R1)";
86
    		} elseif (preg_match("/_2_qseq/i", $string)) {
87
    			$best_description = $description." (R2)";
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    		}
    	}
    }
    return $best_description;
}

/**
 * Return a description for the sample based on a 
 * casava_1_8 formated file name
 * 
 * @param	string	the file name to process
 * @param	desc	the sample description table 
 * @return	the best description, if none found the $string itself
 */
function get_casava_1_8_desc($string, $desc) {
	$best_description = $string;
    foreach($desc as $mid => $description) {
		// Index must be at begining
    	if (preg_match("/^".$description."_/i", $string)) {
    		// Find the pairend info if there is one
108 109 110 111
    		if (preg_match("/_R1/i", $string)) {
    			$best_description = $description." (R1)";
    		} elseif (preg_match("/_R2/i", $string)) {
    			$best_description = $description." (R2)";
112 113
    		} elseif (preg_match("/_I1/i", $string)) {
    			$best_description = $description." (I1)";
114 115 116 117 118 119
    		}
    	}
    }
    return $best_description;
}

120 121 122 123 124 125 126 127 128 129 130 131
/**
 * Return a description for the sample based on a 
 * generic illumina formated file name
 * 
 * @param	string	the file name to process
 * @param	desc	the sample description table 
 * @return	the best description, if none found the $string itself
 */
function get_generic_illumina_desc($string, $desc) {
	$best_description = $string;
    foreach($desc as $mid => $description) {
		// Index must be at begining
132
    	if (preg_match("/".$mid."_/i", $string)) {
133 134 135
    		// Find the pairend info if there is one
    		if (preg_match("/_1_sequence/i", $string)) {
    			$best_description = $description." (R1)";
136
    		} elseif (preg_match("/_2_sequence/i", $string)) {
137 138 139 140 141 142 143
    			$best_description = $description." (R2)";
    		}
    	}
    }
    return $best_description;
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
/**
 * Return a description for the sample based on a 
 * file name
 * 
 * @param	string	the file name to process
 * @param	desc	the sample description table 
 * @return	the best description, if none found the $string itself
 */
function get_desc($string, $desc) {
	$best_desc = array();
	$best_description = $string;
    foreach($desc as $mid => $description) {
		// Mids name can be in between 2 dots
    	if (preg_match("/".$mid."/i", $string)) {
    		$best_desc[$mid] = $description;
    		$best_description = $description;
    	}
    }
    // If no mids found
    if (count($best_desc) == 0) {
    	$best_description = $string;
Jerome Mariette's avatar
Jerome Mariette committed
165
    // If multple mids matches take the longuest from the $final_descriptions
166
    } elseif (count($best_desc) > 1) {
Jerome Mariette's avatar
Jerome Mariette committed
167
    	$max_len = 0;
168 169
    	$best_description = $string;
    	foreach($best_desc as $mid => $description) {
Jerome Mariette's avatar
Jerome Mariette committed
170 171
    		if (strlen($mid) > $max_len) {
    			$max_len = strlen($mid);
172
    			$best_description = $description;
Jerome Mariette's avatar
Jerome Mariette committed
173 174
    		}
    	}
175
    }
176 177 178 179
	return $best_description;
}
 
function smarty_modifier_get_description($string, $desc) { 
Jerome Mariette's avatar
Jerome Mariette committed
180
    
181 182 183 184 185 186 187 188 189 190 191 192 193
    $best_description = $string;
	// First try to get the newbler description
	$best_description = get_newbler_desc($string, $desc);
	
	// if no description found as newbler
	if ($best_description == $string) {
		$best_description = get_casava_1_7_desc($string, $desc);
	}
	
	// if no description found as casava_1_7
	if ($best_description == $string) {
		$best_description = get_casava_1_8_desc($string, $desc);
	}
194 195 196 197 198

	// if no description found as casava_1_8
	if ($best_description == $string) {
		$best_description = get_generic_illumina_desc($string, $desc);
	}
199 200 201 202 203 204 205
	
	// if no description found based on specific formatting, check in the whole file name
	if ($best_description == $string) {
		$best_description = get_desc($string, $desc);
	}
	
	return $best_description;
206
} 
207

Jerome Mariette's avatar
Jerome Mariette committed
208
?>