我有多个文件。我的脚本在文件中搜索序列名和序列。如果找到了,格式将从gb改为fasta,只保留序列名和序列并将其写回文件中。但有时文件不包含序列名。在这种情况下,我不会在文件中写入任何内容,文件是空的。应该删除这些文件,因为在我的脚本结束时,将从所有这些文件创建一个multifasta。
# Find all gb files
$files = glob("*.gb");
foreach ($files as $filename){
$newname = basename($filename, ".gb"). ".fasta";
rename($filename, $newname);
$condition = false;
$lines = file($newname);
foreach($lines as $line) {
if (strstr($line, "ACCESSION") ) {
# Find the line containing the sequence name
$head = str_replace("ACCESSION ","",$line);
$final = "> " . $head;
# check if $head contains text
if ($head == ""){
$condition = true;
}
}
$sequence = trim($line);
# Find the sequence and check the condition
if (preg_match('/^\d/', $sequence) && $condition == false){
$sequence = preg_replace('/[0-9]+/', '', $sequence);
$sequence = preg_replace('/\s/',"",$sequence);
# Store in string
$out .= $sequence;
}
}
# Read lines into file
$f = fopen($newname, "w");
fwrite($f, $t);
fclose($f);
}
# Create multifasta
exec('for f in *fasta; do cat "$f"; echo; done > db', $return);
当文件为空时,我如何才能最好地删除它,这样它就不会被插入到multifasta中。我相信这很简单,但我不知道怎么做。