text
stringlengths
0
828
corefile = os.path.join(self.reffilepath, self.analysistype, 'Escherichia', 'core_combined.fasta')
for record in SeqIO.parse(corefile, 'fasta'):
gene_name = record.id.split('-')[0]
if gene_name not in self.coregenomes:
self.coregenomes.append(gene_name)"
87,"def blastparser(self, report, sample, fieldnames):
""""""
Parse the number of core genes present in the strain from the BLAST outputs
:param report: the name and path of the BLAST outputs
:param sample: the sample object
:param fieldnames: type LIST: List of fields used to in BLAST analyses
""""""
try:
# Open the sequence profile file as a dictionary
blastdict = DictReader(open(report), fieldnames=self.fieldnames, dialect='excel-tab')
# Go through each BLAST result
for row in blastdict:
# Ignore the headers
if row['query_id'].startswith(fieldnames[0]):
pass
else:
# Calculate the percent identity and extract the bitscore from the row
# Percent identity is the (length of the alignment - number of mismatches) / total subject length
percentidentity = float('{:0.2f}'.format((float(row['positives']) - float(row['gaps'])) /
float(row['subject_length']) * 100))
# Split off any | and - from the sample name
target = row['subject_id'].split('|')[0].split('-')[0]
# If the hit passes the cutoff threshold, add it to the set of core genes present
if percentidentity >= self.cutoff:
sample[self.analysistype].coreset.add(target)
except FileNotFoundError:
pass"
88,"def reporter(self):
""""""
Create a .csv file with the strain name, and the number of core genes present/the total number of core genes
""""""
with open(os.path.join(self.reportpath, 'Escherichia_core.csv'), 'w') as report:
data = 'Strain,Genes Present/Total\n'
for sample in self.runmetadata.samples:
# Convert the set to a list for JSON serialization
sample[self.analysistype].coreset = list(sample[self.analysistype].coreset)
sample[self.analysistype].coreresults = '{cs}/{cg}'.format(cs=len(sample[self.analysistype].coreset),
cg=len(self.coregenomes))
# Add strain name, the number of core genes present, and the number of total core genes to the string
data += '{sn},{cr}\n'.format(sn=sample.name,
cr=sample[self.analysistype].coreresults)
report.write(data)
for sample in self.metadata:
# Remove the messy blast results and set/list of core genes from the object
try:
delattr(sample[self.analysistype], ""blastresults"")
except AttributeError:
pass
try:
delattr(sample[self.analysistype], 'coreset')
except AttributeError:
pass"
89,"def get_simple_output(self, stderr=STDOUT):
""""""Executes a simple external command and get its output
The command contains no pipes. Error messages are
redirected to the standard output by default
:param stderr: where to put stderr
:return: output of command
""""""
args = shlex.split(self.cmd)
proc = Popen(args, stdout=PIPE, stderr=stderr)
return proc.communicate()[0].decode(""utf8"")"
90,"def get_complex_output(self, stderr=STDOUT):
""""""Executes a piped command and get the lines of the output in a list
:param stderr: where to put stderr
:return: output of command
""""""
proc = Popen(self.cmd, shell=True, stdout=PIPE, stderr=stderr)
return proc.stdout.readlines()"
91,"def get_output_from_pipe(self, input_file):
""""""Executes an external command and get its output. The command
receives its input_file from the stdin through a pipe
:param input_file: input file
:return: output of command
""""""
args = shlex.split(self.cmd)
p = Popen(args, stdout=PIPE, stdin=PIPE) # | grep es
p.stdin.write(bytearray(input_file.encode(""utf8""))) # echo test |
return p.communicate()[0].decode(""utf8"")"
92,"def get_return_code(self, stderr=STDOUT):
""""""Executes a simple external command and return its exit status
:param stderr: where to put stderr
:return: return code of command
""""""
args = shlex.split(self.cmd)
return call(args, stdout=PIPE, stderr=stderr)"
93,"def get_exit_code(self):
""""""Executes the external command and get its exitcode, stdout and stderr
:return: exit code of command