Skip to content

Commit

Permalink
Merge pull request #93 from McTavishLab/re-org
Browse files Browse the repository at this point in the history
Re org
  • Loading branch information
snacktavish committed May 30, 2020
2 parents fe68fb6 + 3b09753 commit dde3594
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 62 deletions.
5 changes: 2 additions & 3 deletions docs/examples/minimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from physcraper.opentree_helpers import scraper_from_opentree

configfi = "tests/data/test.config"
workdir ="physcraper_example_minimal"
workdir ="example_minimal"
aln_fi = "tests/data/tiny_test_example/test.fas"
blast_dir = "tests/data/precooked/fixed/tte_blast_files"
# mattype="fasta"
Expand Down Expand Up @@ -61,8 +61,7 @@
sys.stdout.write("Running align_query_seqs()...\n")
scraper.align_new_seqs()
scraper.est_full_tree()
scraper.data.write_labelled(label="^ot:ottTaxonName", filename="updated_taxon_name", norepeats=True)
scraper.data.write_labelled(label="^ncbi:taxon", filename="updated_ncbi_id", norepeats=False)
scraper.data.write_labelled(label="^ncbi:taxon", filename="updated_ncbi_id", norepeats=False, direc = scraper.outputsdir)


# sys.stdout.write("estimating tree...")
Expand Down
44 changes: 30 additions & 14 deletions physcraper/aligntreetax.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def __init__(self, tree, otu_dict, alignment, ingroup_mrca, workdir, configfile=
elif isinstance(configfile, str):
self.config = ConfigObj(configfile)
if not os.path.exists("{}/run.config".format(self.workdir)):
shutil.copyfile(self.config.configfi, "{}/run.config".format(self.workdir))
shutil.copyfile(self.config.configfi, "{}/copy.config".format(self.workdir))
else:
assert(isinstance(configfile, ConfigObj)),type(configfile)
self.config = configfile
Expand Down Expand Up @@ -557,10 +557,14 @@ def write_papara_files(self, treefilename="random_resolve.tre", alnfilename="aln
self.aln.write(path="{}/{}".format(self.workdir, alnfilename), schema="phylip")


def write_random_resolve_tre(self, treefilename='random_resolve.tre'):
def write_random_resolve_tre(self, treefilename='random_resolve.tre', direc='workdir'):
self.tre.resolve_polytomies()
self.tre.deroot()
treepath= "{}/{}".format(self.workdir, treefilename)
if direc == 'workdir':
direc = self.workdir
else:
assert(os.path.exists(direc))
treepath= "{}/{}".format(direc, treefilename)
tmptre = self.tre.as_string(schema="newick",
unquoted_underscores=True,
suppress_rooting=True)
Expand All @@ -571,30 +575,39 @@ def write_random_resolve_tre(self, treefilename='random_resolve.tre'):
fi.close()
return treepath

def write_aln(self, filename=None, alnschema="fasta"):
def write_aln(self, filename=None, alnschema="fasta", direc='workdir'):
if direc == 'workdir':
direc = self.workdir
else:
assert(os.path.exists(direc))
if filename == None:
filename = "physcraper_{}.fas".format(self.tag)
alnpath = "{}/{}".format(self.workdir, filename)
alnpath = "{}/{}".format(direc, filename)
self.aln.write(path=alnpath,
schema=alnschema)
return os.path.abspath(alnpath)

def write_files(self, treefilename=None, treeschema="newick", alnfilename=None, alnschema="fasta"):
def write_files(self, treefilename=None, treeschema="newick", alnfilename=None, alnschema="fasta", direc='workdir'):
"""Outputs both the streaming files, labeled with OTU ids.
Can be mapped to original labels using otu_dict.json or otu_seq_info.csv"""
#debug("write_files")
if direc == 'workdir':
direc = self.workdir
else:
assert(os.path.exists(direc))
if treefilename == None:
treepath = "physcraper_{}.tre".format(self.tag)
treepath = "{}/physcraper_{}.tre".format(direc, self.tag)
else:
treepath = "{}/{}".format(self.workdir, treefilename)
treepath = "{}/{}".format(direc, treefilename)
if alnfilename == None:
alnpath = "physcraper_{}.fas".format(self.tag)
alnpath = "{}/physcraper_{}.fas".format(direc, self.tag)
else:
alnpath = "{}/{}".format(self.workdir, alnfilename)
alnpath = "{}/{}".format(direc, alnfilename)
self.tre.write(path=treepath,
schema=treeschema, unquoted_underscores=True)
self.aln.write(path=alnpath,
schema=alnschema)

return(treepath, alnpath)


Expand Down Expand Up @@ -665,17 +678,20 @@ def write_labelled(self, label, filename = "labelled", direc='workdir', norepeat
tmp_aln.write(path=alnpath,
schema="fasta")

def write_otus(self, filename = "otu_info", schema="table"):
def write_otus(self, filename = "otu_info", schema="table", direc='workdir'):
"""Writes out OTU dict as json or table.
:param filename: filename
:param schema: either table or json format
:return: writes out otu_dict to file
"""

if direc == 'workdir':
direc = self.workdir
else:
assert(os.path.exists(direc))
assert schema in ["table", "json"]
if schema == "json":
with open("{}/{}_{}.json".format(self.workdir, filename, self.tag), "w") as outfile:
with open("{}/{}_{}.json".format(direc, filename, self.tag), "w") as outfile:
json.dump(self.otu_dict, outfile)
if schema == "table":
#all_keys = set()
Expand All @@ -685,7 +701,7 @@ def write_otus(self, filename = "otu_info", schema="table"):
#keys.sort()
keys = ['^ot:ottTaxonName','^ot:ottId','^ncbi:taxon','^ncbi:accession','^ncbi:gi','^physcraper:last_blasted','^physcraper:status','^ot:originalLabel','^ncbi:title']
header = ["otu_id"] + keys
with open("{}/{}_{}.csv".format(self.workdir, filename, self.tag), "w") as outfile:
with open("{}/{}_{}.csv".format(direc, filename, self.tag), "w") as outfile:
outfile.write("\t".join(header)+"\n")
for otu in self.otu_dict:
vals = [str(self.otu_dict[otu].get(key, "-")) for key in keys]
Expand Down
4 changes: 2 additions & 2 deletions physcraper/configobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def config_str(self):
maxlen=self.maxlen,
taxonomy = self.taxonomy_dir)
return(config_text)
def write_file(self, workdir, filename = "run.config"):
def write_file(self, direc, filename = "run.config"):
config_text = self.config_str()
fi = open("{}/{}".format(workdir, filename),"w")
fi = open("{}/{}".format(direc, filename),"w")
fi.write(config_text)
fi.close()
def read_config(self, configfi, interactive):
Expand Down
2 changes: 1 addition & 1 deletion physcraper/ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(self, configfile = None, workdir=None):
self.ncbi_parser = ncbi_data_parser.Parser(names_file=self.config.ncbi_names,
nodes_file=self.config.ncbi_nodes)
self.acc_tax_seq_dict = {}
self.full_seq_path = "{}/full_seqs".format(self.config.taxonomy_dir)
self.full_seq_path = "{}/downloaded_ncbi_sequences".format(self.config.taxonomy_dir)


def get_ncbiid_from_acc(self, acc):
Expand Down

0 comments on commit dde3594

Please sign in to comment.