def make_otu_table(self): """Ask the counts from the OTU class and do some modifications. OTUs are columns and sample names are rows.""" # Remove unwanted # result = self.otu.cluster_counts_table.copy() for otu_name in result: species = self.assignments.get(otu_name, None) if len(species) > 2 and species[2] in self.unwanted: result = result.drop(otu_name, 1) # Convert to CSV # result.to_csv(str(self.otu_csv), sep='\t', encoding='utf-8') prepend_to_file(self.otu_csv, 'X')
def process_log_file(name, directory, start_time=None): # Find one log file # if not directory.endswith('/') : directory = directory + '/' logs = list(find_files_by_regex('mothur.[0-9]+.logfile')) if len(logs) > 1: raise Exception("Found more than one mothur log file in '%s'" % os.getcwd()) path = logs[0] # Add time # prepend_to_file(path, "Start time: " + start_time + "\n") append_to_file(path, "\nEnd time: " + time.asctime()) # Move it # destination = directory + name shutil.move(path, directory + name) # Check for error # raw = open(destination, 'r').read() if re.findall('\[ERROR\]', raw, re.M): shutil.move(destination, directory + name.upper()) raise Exception("Mothur log file reports an error in step '%s'" % name) if os.path.exists(directory + name.upper()): os.remove(directory + name.upper())
def prepend(self, what): """Append some text or an other file to the current file""" if isinstance(what, FilePath): what = what.contents prepend_to_file(self.path, what)
def make_otu_table_norm(self): """Convert to CSV. OTUs are columns and sample names are rows.""" self.otu_table_norm.to_csv(self.otu_csv_norm.path, sep='\t', float_format='%.5g', encoding='utf-8') prepend_to_file(self.otu_csv_norm, 'X')