_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q259200 | Functions.run | validation | async def run(self, *args, data):
""" run the function you want """
cmd = self._get(data.text)
try:
if cmd is not None:
command = self[cmd](*args, data=data)
return await peony.utils.execute(command)
| python | {
"resource": ""
} |
q259201 | HivePlot.simplified_edges | validation | def simplified_edges(self):
"""
A generator for getting all of the edges without consuming extra
| python | {
"resource": ""
} |
q259202 | HivePlot.has_edge_within_group | validation | def has_edge_within_group(self, group):
"""
Checks whether there are within-group edges or not.
"""
assert group in self.nodes.keys(),\
"{0} not one of the group of nodes".format(group)
nodelist = self.nodes[group]
| python | {
"resource": ""
} |
q259203 | HivePlot.plot_axis | validation | def plot_axis(self, rs, theta):
"""
Renders the axis.
"""
xs, | python | {
"resource": ""
} |
q259204 | HivePlot.plot_nodes | validation | def plot_nodes(self, nodelist, theta, group):
"""
Plots nodes to screen.
"""
for i, node in enumerate(nodelist):
r = self.internal_radius + i * self.scale
x, y = get_cartesian(r, theta)
circle = plt.Circle(xy=(x, | python | {
"resource": ""
} |
q259205 | HivePlot.group_theta | validation | def group_theta(self, group):
"""
Computes the theta along which a group's nodes are aligned.
"""
for | python | {
"resource": ""
} |
q259206 | HivePlot.find_node_group_membership | validation | def find_node_group_membership(self, node):
"""
Identifies the group for which a node belongs to.
"""
| python | {
"resource": ""
} |
q259207 | HivePlot.get_idx | validation | def get_idx(self, node):
"""
Finds the index of the node in the sorted list.
"""
| python | {
"resource": ""
} |
q259208 | HivePlot.node_radius | validation | def node_radius(self, node):
"""
Computes the radial position of the node.
"""
| python | {
"resource": ""
} |
q259209 | HivePlot.node_theta | validation | def node_theta(self, node):
"""
Convenience function to find the node's theta angle.
"""
| python | {
"resource": ""
} |
q259210 | HivePlot.add_edges | validation | def add_edges(self):
"""
Draws all of the edges in the graph.
"""
for group, edgelist in self.edges.items():
| python | {
"resource": ""
} |
q259211 | HivePlot.draw | validation | def draw(self):
"""
The master function that is called that draws everything.
"""
self.ax.set_xlim(-self.plot_radius(), self.plot_radius())
| python | {
"resource": ""
} |
q259212 | HivePlot.adjust_angles | validation | def adjust_angles(self, start_node, start_angle, end_node, end_angle):
"""
This function adjusts the start and end angles to correct for
duplicated axes.
"""
start_group = self.find_node_group_membership(start_node)
end_group = self.find_node_group_membership(end_node)
if start_group == 0 and end_group == len(self.nodes.keys())-1:
if self.has_edge_within_group(start_group):
start_angle = correct_negative_angle(start_angle -
self.minor_angle)
if self.has_edge_within_group(end_group):
end_angle = correct_negative_angle(end_angle +
self.minor_angle)
elif start_group == len(self.nodes.keys())-1 and end_group == 0:
if self.has_edge_within_group(start_group):
| python | {
"resource": ""
} |
q259213 | Type.mods_genre | validation | def mods_genre(self):
"""
Guesses an appropriate MODS XML genre type.
"""
type2genre = {
'conference': 'conference publication',
'book chapter': 'bibliography', | python | {
"resource": ""
} |
q259214 | get_publications | validation | def get_publications(context, template='publications/publications.html'):
"""
Get all publications.
"""
types = Type.objects.filter(hidden=False)
publications = Publication.objects.select_related()
publications = | python | {
"resource": ""
} |
q259215 | get_publication | validation | def get_publication(context, id):
"""
Get a single publication.
"""
pbl = Publication.objects.filter(pk=int(id))
if len(pbl) < 1:
return ''
pbl[0].links = pbl[0].customlink_set.all()
| python | {
"resource": ""
} |
q259216 | get_publication_list | validation | def get_publication_list(context, list, template='publications/publications.html'):
"""
Get a publication list.
"""
list = List.objects.filter(list__iexact=list)
if not list:
return ''
list = list[0]
publications = list.publication_set.all()
publications = publications.order_by('-year', '-month', '-id')
if not publications:
return | python | {
"resource": ""
} |
q259217 | tex_parse | validation | def tex_parse(string):
"""
Renders some basic TeX math to HTML.
"""
string = string.replace('{', '').replace('}', '')
def tex_replace(match):
return \
sub(r'\^(\w)', r'<sup>\1</sup>',
| python | {
"resource": ""
} |
q259218 | parse | validation | def parse(string):
"""
Takes a string in BibTex format and returns a list of BibTex entries, where
each entry is a dictionary containing the entries' key-value pairs.
@type string: string
@param string: bibliography in BibTex format
@rtype: list
@return: a list of dictionaries representing a bibliography
"""
# bibliography
bib = []
# make sure we are dealing with unicode strings
if not isinstance(string, six.text_type):
string = string.decode('utf-8')
# replace special characters
for key, value in special_chars:
string = string.replace(key, value)
string = re.sub(r'\\[cuHvs]{?([a-zA-Z])}?', r'\1', string)
# split into BibTex entries
| python | {
"resource": ""
} |
q259219 | OrderedModel.swap | validation | def swap(self, qs):
"""
Swap the positions of this object with a reference object.
"""
try:
replacement = qs[0]
except IndexError:
# already first/last
return
if not self._valid_ordering_reference(replacement):
| python | {
"resource": ""
} |
q259220 | OrderedModel.up | validation | def up(self):
"""
Move this object up one position.
"""
| python | {
"resource": ""
} |
q259221 | OrderedModel.down | validation | def down(self):
"""
Move this object down one position.
| python | {
"resource": ""
} |
q259222 | OrderedModel.to | validation | def to(self, order):
"""
Move object to a certain position, updating all affected objects to move accordingly up or down.
"""
if order is None or self.order == order:
# object is already at desired position
return
qs = self.get_ordering_queryset()
if self.order > | python | {
"resource": ""
} |
q259223 | OrderedModel.above | validation | def above(self, ref):
"""
Move this object above the referenced object.
"""
if not self._valid_ordering_reference(ref):
raise ValueError(
"%r can only be moved above instances of %r which %s equals %r." % (
self, self.__class__, self.order_with_respect_to,
self._get_order_with_respect_to()
)
)
if | python | {
"resource": ""
} |
q259224 | OrderedModel.below | validation | def below(self, ref):
"""
Move this object below the referenced object.
"""
if not self._valid_ordering_reference(ref):
raise ValueError(
"%r can only be moved below instances of %r which %s equals %r." % (
self, self.__class__, self.order_with_respect_to,
self._get_order_with_respect_to()
)
)
if | python | {
"resource": ""
} |
q259225 | OrderedModel.top | validation | def top(self):
"""
Move this object to the top of the ordered stack.
"""
o | python | {
"resource": ""
} |
q259226 | OrderedModel.bottom | validation | def bottom(self):
"""
Move this object to the bottom of the ordered stack.
"""
| python | {
"resource": ""
} |
q259227 | populate | validation | def populate(publications):
"""
Load custom links and files from database and attach to publications.
"""
customlinks = CustomLink.objects.filter(publication__in=publications)
customfiles = CustomFile.objects.filter(publication__in=publications)
publications_ = {}
for publication in publications:
publication.links = []
publication.files | python | {
"resource": ""
} |
q259228 | worker | validation | def worker(self):
"""
Calculates the quartet weights for the test at a random
subsampled chunk of loci.
"""
## subsample loci
fullseqs = self.sample_loci()
## find all iterations of samples for this quartet
liters = itertools.product(*self.imap.values())
## run tree inference for each iteration of sampledict
hashval = uuid.uuid4().hex
weights = []
for ridx, lidx in enumerate(liters):
## get subalignment for this iteration and make to nex
a,b,c,d = lidx
sub = {}
for i in lidx:
if self.rmap[i] == "p1":
sub["A"] = fullseqs[i]
elif self.rmap[i] == "p2":
sub["B"] = fullseqs[i]
elif self.rmap[i] == "p3":
sub["C"] = fullseqs[i]
else:
sub["D"] = fullseqs[i]
## write as nexus file
nex = []
for tax in list("ABCD"):
nex.append(">{} {}".format(tax, sub[tax]))
## check for | python | {
"resource": ""
} |
q259229 | get_order | validation | def get_order(tre):
"""
return tree order
"""
anode = tre.tree&">A"
sister = anode.get_sisters()[0]
| python | {
"resource": ""
} |
q259230 | count_var | validation | def count_var(nex):
"""
count number of sites with cov=4, and number of variable sites.
"""
arr = np.array([list(i.split()[-1]) for i in nex])
miss = np.any(arr=="N", axis=0)
nomiss = | python | {
"resource": ""
} |
q259231 | Twiist.sample_loci | validation | def sample_loci(self):
""" finds loci with sufficient sampling for this test"""
## store idx of passing loci
idxs = np.random.choice(self.idxs, self.ntests)
## open handle, make a proper generator to reduce mem
with open(self.data) as indata:
liter = (indata.read().strip().split("|\n"))
## store data as dict
seqdata = {i:"" for i in self.samples}
## put chunks into a list
for idx, loc in enumerate(liter):
if idx in idxs:
## parse chunk
lines = loc.split("\n")[:-1]
names = [i.split()[0] for i in lines]
seqs = [i.split()[1] for i in lines]
dd = {i:j for i,j in zip(names, seqs)}
| python | {
"resource": ""
} |
q259232 | Twiist.run_tree_inference | validation | def run_tree_inference(self, nexus, idx):
"""
Write nexus to tmpfile, runs phyml tree inference, and parses
and returns the resulting tree.
"""
## create a tmpdir for this test
tmpdir = tempfile.tempdir
tmpfile = os.path.join(tempfile.NamedTemporaryFile(
delete=False,
prefix=str(idx),
dir=tmpdir,
))
## write nexus to tmpfile
tmpfile.write(nexus)
tmpfile.flush()
| python | {
"resource": ""
} |
q259233 | Twiist.plot | validation | def plot(self):
"""
return a toyplot barplot of the results table.
"""
if self.results_table == None:
return "no results found"
else:
bb = self.results_table.sort_values(
by=["ABCD", "ACBD"],
ascending=[False, True],
)
## make a barplot
| python | {
"resource": ""
} |
q259234 | PCA.plot_pairwise_dist | validation | def plot_pairwise_dist(self, labels=None, ax=None, cmap=None, cdict=None, metric="euclidean"):
"""
Plot pairwise distances between all samples
labels: bool or list
by default labels aren't included. If labels == True, then labels are read in
from the vcf file. Alternatively, labels can be passed in as a list, should
be same length as the number of samples.
"""
allele_counts = self.genotypes.to_n_alt()
| python | {
"resource": ""
} |
q259235 | PCA.copy | validation | def copy(self):
""" returns a copy of the pca analysis object """
| python | {
"resource": ""
} |
q259236 | loci2migrate | validation | def loci2migrate(name, locifile, popdict, mindict=1):
"""
A function to build an input file for the program migrate from an ipyrad
.loci file, and a dictionary grouping Samples into populations.
Parameters:
-----------
name: (str)
The name prefix for the migrate formatted output file.
locifile: (str)
The path to the .loci file produced by ipyrad.
popdict: (dict)
A Python dictionary grouping Samples into Populations.
Examples:
---------
You can create the population dictionary by hand, and pass in the path
to your .loci file as a string.
>> popdict = {'A': ['a', 'b', 'c'], 'B': ['d', 'e', 'f']}
>> loci2migrate("outfile.migrate", "./mydata.loci", popdict)
Or, if you load your ipyrad.Assembly object from it's JSON file, you can
access the loci file path and population information from there directly.
>> data = ip.load_json("mydata.json")
>> loci2migrate("outfile.migrate", data.outfiles.loci, data.populations)
"""
## I/O
outfile = open(name+".migrate", 'w')
infile = open(locifile, 'r')
## minhits dictionary can be an int (all same) or a dictionary (set each)
if isinstance(mindict, int):
mindict = {pop: mindict for pop in popdict}
else:
| python | {
"resource": ""
} |
q259237 | update | validation | def update(assembly, idict, count):
""" updates dictionary with the next .5M reads from the super long string
phylip file. Makes for faster reading. """
data = iter(open(os.path.join(assembly.dirs.outfiles,
assembly.name+".phy"), 'r'))
ntax, nchar = data.next().strip().split()
## read in max N bp at a time | python | {
"resource": ""
} |
q259238 | make | validation | def make(assembly, samples):
""" Make phylip and nexus formats. This is hackish since I'm recycling the
code whole-hog from pyrad V3. Probably could be good to go back through
and clean up | python | {
"resource": ""
} |
q259239 | sample_cleanup | validation | def sample_cleanup(data, sample):
"""
Clean up a bunch of loose files.
"""
umap1file = os.path.join(data.dirs.edits, sample.name+"-tmp-umap1.fastq")
umap2file = os.path.join(data.dirs.edits, sample.name+"-tmp-umap2.fastq")
unmapped = os.path.join(data.dirs.refmapping, sample.name+"-unmapped.bam")
samplesam = os.path.join(data.dirs.refmapping, sample.name+".sam")
split1 = os.path.join(data.dirs.edits, sample.name+"-split1.fastq")
| python | {
"resource": ""
} |
q259240 | index_reference_sequence | validation | def index_reference_sequence(data, force=False):
"""
Index the reference sequence, unless it already exists. Also make a mapping
of scaffolds to index numbers for later user in steps 5-6.
"""
## get ref file from params
refseq_file = data.paramsdict['reference_sequence']
index_files = []
## Check for existence of index files. Default to bwa unless you specify smalt
if "smalt" in data._hackersonly["aligner"]:
# These are smalt index files. Only referenced here to ensure they exist
index_files.extend([".sma", ".smi"])
else:
index_files.extend([".amb", ".ann", ".bwt", ".pac", ".sa"])
## samtools specific index
index_files.extend([".fai"])
## If reference sequence already exists then bail out of this func
if not force:
if all([os.path.isfile(refseq_file+i) for i in index_files]):
return
#if data._headers:
# print(INDEX_MSG.format(data._hackersonly["aligner"]))
if "smalt" in data._hackersonly["aligner"]:
## Create smalt index for mapping
## smalt index [-k <wordlen>] [-s <stepsiz>] <index_name> <reference_file>
cmd1 = [ipyrad.bins.smalt, "index",
"-k", str(data._hackersonly["smalt_index_wordlen"]),
refseq_file,
refseq_file]
else:
| python | {
"resource": ""
} |
q259241 | fetch_cluster_se | validation | def fetch_cluster_se(data, samfile, chrom, rstart, rend):
"""
Builds a single end cluster from the refmapped data.
"""
## If SE then we enforce the minimum overlap distance to avoid the
## staircase syndrome of multiple reads overlapping just a little.
overlap_buffer = data._hackersonly["min_SE_refmap_overlap"]
## the *_buff variables here are because we have to play patty
## cake here with the rstart/rend vals because we want pysam to
## enforce the buffer for SE, but we want the reference sequence
## start and end positions to print correctly for downstream.
rstart_buff = rstart + overlap_buffer
rend_buff = rend - overlap_buffer
## Reads that map to only very short segements of the reference
## sequence will return buffer end values that are before the
## start values causing pysam to complain. Very short mappings.
if rstart_buff > rend_buff:
tmp = rstart_buff
rstart_buff = rend_buff
rend_buff = tmp
## Buffering can't make start and end equal or pysam returns nothing.
if rstart_buff == rend_buff:
rend_buff += 1
## store pairs
rdict = {}
clust = []
iterreg = []
iterreg = samfile.fetch(chrom, rstart_buff, rend_buff)
## use dict to match up read pairs
for read in iterreg:
if read.qname not in rdict:
rdict[read.qname] = read
## sort dict keys so highest derep is first ('seed')
sfunc = lambda x: int(x.split(";size=")[1].split(";")[0])
rkeys = sorted(rdict.keys(), key=sfunc, reverse=True)
| python | {
"resource": ""
} |
q259242 | ref_build_and_muscle_chunk | validation | def ref_build_and_muscle_chunk(data, sample):
"""
1. Run bedtools to get all overlapping regions
2. Parse out reads from regions using pysam and dump into chunk files.
We measure it out to create 10 chunk files per sample.
3. If we really wanted to speed this up, though it is pretty fast already,
we could parallelize it since we can easily break the regions into
a list of chunks.
"""
## get regions using bedtools
regions = bedtools_merge(data, sample).strip().split("\n")
nregions = len(regions)
chunksize = (nregions / 10) + (nregions % 10)
LOGGER.debug("nregions {} chunksize {}".format(nregions, chunksize))
## create an output file to write clusters to
idx = 0
tmpfile = os.path.join(data.tmpdir, sample.name+"_chunk_{}.ali")
## remove old files if they exist to avoid append errors
for i in range(11):
if os.path.exists(tmpfile.format(i)):
os.remove(tmpfile.format(i))
fopen = open
## If reference+denovo we drop the reads back into clust.gz
## and let the muscle_chunker do it's thing back in cluster_within
if data.paramsdict["assembly_method"] == "denovo+reference":
tmpfile = os.path.join(data.dirs.clusts, sample.name+".clust.gz")
fopen = gzip.open
## build clusters for aligning with muscle from the sorted bam file
samfile = pysam.AlignmentFile(sample.files.mapped_reads, 'rb')
#"./tortas_refmapping/PZ70-mapped-sorted.bam", "rb")
## fill clusts list and dump periodically
clusts = []
nclusts = 0
for region in regions:
chrom, pos1, pos2 = region.split()
try:
## fetches pairs quickly but then goes slow to merge them.
if "pair" in data.paramsdict["datatype"]:
clust = fetch_cluster_pairs(data, samfile, chrom, int(pos1), int(pos2))
## fetch but no need to merge
else:
clust = fetch_cluster_se(data, samfile, chrom, int(pos1), int(pos2))
except IndexError as inst:
LOGGER.error("Bad region chrom:start-end {}:{}-{}".format(chrom, pos1, pos2))
continue
if clust:
clusts.append("\n".join(clust))
nclusts += 1
if nclusts == chunksize:
| python | {
"resource": ""
} |
q259243 | ref_muscle_chunker | validation | def ref_muscle_chunker(data, sample):
"""
Run bedtools to get all overlapping regions. Pass this list into the func
'get_overlapping_reads' which will write fastq chunks to the clust.gz file.
1) Run bedtools merge to get a list of all contiguous blocks of bases
in the reference seqeunce where one or more of our reads overlap.
The output will look like this:
1 45230754 45230783
1 74956568 74956596
...
1 116202035 116202060 | python | {
"resource": ""
} |
q259244 | check_insert_size | validation | def check_insert_size(data, sample):
"""
check mean insert size for this sample and update
hackersonly.max_inner_mate_distance if need be. This value controls how
far apart mate pairs can be to still be considered for bedtools merging
downstream.
"""
## pipe stats output to grep
cmd1 = [ipyrad.bins.samtools, "stats", sample.files.mapped_reads]
cmd2 = ["grep", "SN"]
proc1 = sps.Popen(cmd1, stderr=sps.STDOUT, stdout=sps.PIPE)
proc2 = sps.Popen(cmd2, stderr=sps.STDOUT, stdout=sps.PIPE, stdin=proc1.stdout)
## get piped result
res = proc2.communicate()[0]
## raise exception on failure and do cleanup
if proc2.returncode:
raise IPyradWarningExit("error in %s: %s", cmd2, res)
## starting vals
avg_insert = 0
stdv_insert = 0
avg_len = 0
## iterate over results
for line in res.split("\n"):
if "insert size average" in line:
avg_insert = float(line.split(":")[-1].strip())
elif "insert size standard deviation" in line:
## hack to fix sim data when stdv is 0.0. Shouldn't
## impact real data bcz stdv gets rounded up below
stdv_insert = float(line.split(":")[-1].strip()) + 0.1
elif "average length" in line:
avg_len = float(line.split(":")[-1].strip())
LOGGER.debug("avg {} stdv {} avg_len {}"\
.format(avg_insert, stdv_insert, avg_len))
## If all values return successfully set the max inner mate distance.
## This is tricky. avg_insert is the average length of R1+R2+inner mate
## distance. avg_len is the average length of a read. If there are lots
## of reads that overlap then avg_insert will be close to but bigger than
| python | {
"resource": ""
} |
q259245 | bedtools_merge | validation | def bedtools_merge(data, sample):
"""
Get all contiguous genomic regions with one or more overlapping
reads. This is the shell command we'll eventually run
bedtools bamtobed -i 1A_0.sorted.bam | bedtools merge [-d 100]
-i <input_bam> : specifies the input file to bed'ize
-d <int> : For PE set max distance between reads
"""
LOGGER.info("Entering bedtools_merge: %s", sample.name)
mappedreads = os.path.join(data.dirs.refmapping,
sample.name+"-mapped-sorted.bam")
## command to call `bedtools bamtobed`, and pipe output to stdout
## Usage: bedtools bamtobed [OPTIONS] -i <bam>
## Usage: bedtools merge [OPTIONS] -i <bam>
cmd1 = [ipyrad.bins.bedtools, "bamtobed", "-i", mappedreads]
cmd2 = [ipyrad.bins.bedtools, "merge", "-i", "-"]
## If PE the -d flag to tell bedtools how far apart to allow mate pairs.
## If SE the -d flag is negative, specifying that SE reads need to
## overlap by at least a specific number of bp. This prevents the
## stairstep syndrome when a + and - read are both extending from
## the same cutsite. Passing a negative number to `merge -d` gets this done.
if 'pair' in data.paramsdict["datatype"]:
check_insert_size(data, sample)
#cmd2.insert(2, str(data._hackersonly["max_inner_mate_distance"]))
cmd2.insert(2, str(data._hackersonly["max_inner_mate_distance"]))
cmd2.insert(2, "-d")
else:
cmd2.insert(2, str(-1 * data._hackersonly["min_SE_refmap_overlap"]))
cmd2.insert(2, "-d")
## pipe output from bamtobed into merge
LOGGER.info("stdv: | python | {
"resource": ""
} |
q259246 | refmap_stats | validation | def refmap_stats(data, sample):
"""
Get the number of mapped and unmapped reads for a sample
and update sample.stats
"""
## shorter names
mapf = os.path.join(data.dirs.refmapping, sample.name+"-mapped-sorted.bam")
umapf = os.path.join(data.dirs.refmapping, sample.name+"-unmapped.bam")
## get from unmapped
cmd1 = [ipyrad.bins.samtools, "flagstat", umapf]
proc1 = sps.Popen(cmd1, stderr=sps.STDOUT, stdout=sps.PIPE)
result1 = proc1.communicate()[0]
## get from mapped
cmd2 = [ipyrad.bins.samtools, "flagstat", mapf]
proc2 = sps.Popen(cmd2, stderr=sps.STDOUT, stdout=sps.PIPE)
result2 = proc2.communicate()[0]
## store results
## If PE, samtools reports the _actual_ number of reads mapped, both
## R1 and R2, so here if PE divide the results by 2 to stay consistent
| python | {
"resource": ""
} |
q259247 | refmap_init | validation | def refmap_init(data, sample, force):
""" create some file handles for refmapping """
## make some persistent file handles for the refmap reads files
sample.files.unmapped_reads = os.path.join(data.dirs.edits,
| python | {
"resource": ""
} |
q259248 | Treemix._subsample | validation | def _subsample(self):
""" returns a subsample of unlinked snp sites """
spans = self.maparr
samp = | python | {
"resource": ""
} |
q259249 | Treemix.draw | validation | def draw(self, axes):
"""
Returns a treemix plot on a toyplot.axes object.
"""
## create a toytree object from the treemix tree result
tre = toytree.tree(newick=self.results.tree)
tre.draw(
axes=axes,
use_edge_lengths=True,
tree_style='c',
tip_labels_align=True,
edge_align_style={"stroke-width": 1}
);
## get coords
for admix in self.results.admixture:
## parse admix event
pidx, pdist, cidx, cdist, weight = admix
a = _get_admix_point(tre, pidx, pdist)
b = _get_admix_point(tre, cidx, cdist)
## add line for admixture edge
mark = axes.plot(
a = (a[0], b[0]),
b = (a[1], b[1]),
style={"stroke-width": 10*weight,
| python | {
"resource": ""
} |
q259250 | _resolveambig | validation | def _resolveambig(subseq):
"""
Randomly resolves iupac hetero codes. This is a shortcut
for now, we could instead use the phased alleles in RAD loci.
"""
N = []
for col in subseq:
rand = | python | {
"resource": ""
} |
q259251 | _count_PIS | validation | def _count_PIS(seqsamp, N):
""" filters for loci with >= N PIS """
counts = [Counter(col) for col in seqsamp.T if not ("-" in col or "N" in col)]
pis = [i.most_common(2)[1][1] > 1 for i | python | {
"resource": ""
} |
q259252 | Bucky._write_nex | validation | def _write_nex(self, mdict, nlocus):
"""
function that takes a dictionary mapping names to sequences,
and a locus number, and writes it as a NEXUS file with a mrbayes
analysis block given a set of mcmc arguments.
"""
## create matrix as a string
max_name_len = max([len(i) for i in mdict])
namestring = "{:<" + str(max_name_len+1) + "} {}\n"
matrix = ""
for i in mdict.items():
matrix += namestring.format(i[0], i[1])
## ensure dir
minidir = os.path.realpath(os.path.join(self.workdir, self.name))
if not os.path.exists(minidir):
os.makedirs(minidir)
## write nexus block
| python | {
"resource": ""
} |
q259253 | _read_sample_names | validation | def _read_sample_names(fname):
""" Read in sample names from a plain text file. This is a convenience
function for branching so if you have tons of sample names you can
pass in a file rather than having to set all the names at the command
line.
"""
try:
with open(fname, 'r') as infile:
| python | {
"resource": ""
} |
q259254 | _bufcountlines | validation | def _bufcountlines(filename, gzipped):
"""
fast line counter. Used to quickly sum number of input reads when running
link_fastqs to append files. """
if gzipped:
fin = gzip.open(filename)
else:
fin = open(filename)
nlines = 0
buf_size = 1024 * 1024
| python | {
"resource": ""
} |
q259255 | _zbufcountlines | validation | def _zbufcountlines(filename, gzipped):
""" faster line counter """
if gzipped:
cmd1 = ["gunzip", "-c", filename]
else:
cmd1 = ["cat", filename]
cmd2 = ["wc"]
proc1 = sps.Popen(cmd1, stdout=sps.PIPE, stderr=sps.PIPE)
proc2 = sps.Popen(cmd2, stdin=proc1.stdout, stdout=sps.PIPE, stderr=sps.PIPE)
res = | python | {
"resource": ""
} |
q259256 | _tuplecheck | validation | def _tuplecheck(newvalue, dtype=str):
"""
Takes a string argument and returns value as a tuple.
Needed for paramfile conversion from CLI to set_params args
"""
if isinstance(newvalue, list):
newvalue = tuple(newvalue)
if isinstance(newvalue, str):
newvalue = newvalue.rstrip(")").strip("(")
try:
newvalue = tuple([dtype(i.strip()) for i in newvalue.split(",")])
## Type error is thrown by tuple if it's applied to a non-iterable.
except TypeError:
newvalue = tuple(dtype(newvalue))
## If dtype fails to cast any element of newvalue
| python | {
"resource": ""
} |
q259257 | Assembly.stats | validation | def stats(self):
""" Returns a data frame with Sample data and state. """
nameordered = self.samples.keys()
nameordered.sort()
## Set pandas to display all samples instead of truncating
pd.options.display.max_rows = len(self.samples)
statdat = pd.DataFrame([self.samples[i].stats for i in nameordered],
| python | {
"resource": ""
} |
q259258 | Assembly.files | validation | def files(self):
""" Returns a data frame with Sample files. Not very readable... """
nameordered = self.samples.keys()
nameordered.sort()
| python | {
"resource": ""
} |
q259259 | Assembly._build_stat | validation | def _build_stat(self, idx):
""" Returns a data frame with Sample stats for each step """
nameordered = self.samples.keys()
nameordered.sort()
newdat = pd.DataFrame([self.samples[i].stats_dfs[idx] \
| python | {
"resource": ""
} |
q259260 | Assembly.get_params | validation | def get_params(self, param=""):
""" pretty prints params if called as a function """
fullcurdir = os.path.realpath(os.path.curdir)
if not param:
for index, (key, value) in enumerate(self.paramsdict.items()):
if isinstance(value, str):
value = value.replace(fullcurdir+"/", "./")
sys.stdout.write("{}{:<4}{:<28}{:<45}\n"\
.format(self._spacer, index, key, value))
else:
try:
if | python | {
"resource": ""
} |
q259261 | Assembly.set_params | validation | def set_params(self, param, newvalue):
"""
Set a parameter to a new value. Raises error if newvalue is wrong type.
Note
----
Use [Assembly].get_params() to see the parameter values currently
linked to the Assembly object.
Parameters
----------
param : int or str
The index (e.g., 1) or string name (e.g., "project_dir")
for the parameter that will be changed.
newvalue : int, str, or tuple
The new value for the parameter selected for `param`. Use
`ipyrad.get_params_info()` to get further information about
a given parameter. If the wrong type is entered for newvalue
(e.g., a str when it should be an int), an error will be raised.
Further information about each parameter is also available
in the documentation.
Examples
--------
## param 'project_dir' takes only a str as input
[Assembly].set_params('project_dir', 'new_directory')
## param 'restriction_overhang' must be a tuple or str, if str it is
## converted to a tuple with the second entry empty.
[Assembly].set_params('restriction_overhang', ('CTGCAG', 'CCGG')
## param 'max_shared_Hs_locus' can be an int or a float:
[Assembly].set_params('max_shared_Hs_locus', 0.25)
"""
## this includes current params and some legacy params for conversion
legacy_params = ["edit_cutsites", "trim_overhang"]
current_params = self.paramsdict.keys()
| python | {
"resource": ""
} |
q259262 | Assembly.branch | validation | def branch(self, newname, subsamples=None, infile=None):
"""
Returns a copy of the Assembly object. Does not allow Assembly
object names to be replicated in namespace or path.
"""
## subsample by removal or keeping.
remove = 0
## is there a better way to ask if it already exists?
if (newname == self.name or os.path.exists(
os.path.join(self.paramsdict["project_dir"],
newname+".assembly"))):
print("{}Assembly object named {} already exists"\
.format(self._spacer, newname))
else:
## Make sure the new name doesn't have any wacky characters
self._check_name(newname)
## Bozo-check. Carve off 'params-' if it's in the new name.
if newname.startswith("params-"):
newname = newname.split("params-")[1]
## create a copy of the Assembly obj
newobj = copy.deepcopy(self)
newobj.name = newname
newobj.paramsdict["assembly_name"] = newname
if subsamples and infile:
print(BRANCH_NAMES_AND_INPUT)
if infile:
if infile[0] == "-":
remove = 1
infile = infile[1:]
if os.path.exists(infile):
subsamples = _read_sample_names(infile)
## if remove then swap the samples
if remove:
subsamples = list(set(self.samples.keys()) - set(subsamples))
## create copies of each subsampled Sample obj
if subsamples: | python | {
"resource": ""
} |
q259263 | Assembly._step1func | validation | def _step1func(self, force, ipyclient):
""" hidden wrapped function to start step 1 """
## check input data files
sfiles = self.paramsdict["sorted_fastq_path"]
rfiles = self.paramsdict["raw_fastq_path"]
## do not allow both a sorted_fastq_path and a raw_fastq
if sfiles and rfiles:
raise IPyradWarningExit(NOT_TWO_PATHS)
## but also require that at least one exists
if not (sfiles or rfiles):
raise IPyradWarningExit(NO_SEQ_PATH_FOUND)
## print headers
if self._headers:
if sfiles:
print("\n{}Step 1: Loading sorted fastq data to Samples"\
.format(self._spacer))
else:
print("\n{}Step 1: Demultiplexing fastq data to Samples"\
.format(self._spacer))
## if Samples already exist then no demultiplexing
if self.samples:
if not force:
print(SAMPLES_EXIST.format(len(self.samples), self.name))
| python | {
"resource": ""
} |
q259264 | Assembly._step2func | validation | def _step2func(self, samples, force, ipyclient):
""" hidden wrapped function to start step 2"""
## print header
if self._headers:
print("\n Step 2: Filtering reads ")
## If no samples in this assembly then it means you skipped step1,
if not self.samples.keys():
raise IPyradWarningExit(FIRST_RUN_1)
## Get sample objects from list of strings, if API.
samples = _get_samples(self, samples)
if not force:
## print warning and skip | python | {
"resource": ""
} |
q259265 | Assembly._step4func | validation | def _step4func(self, samples, force, ipyclient):
""" hidden wrapped function to start step 4 """
if self._headers:
print("\n Step 4: Joint estimation of error rate and heterozygosity")
## Get sample objects from list of strings
samples = _get_samples(self, samples)
## Check if all/none in the right state
if not self._samples_precheck(samples, 4, force):
raise IPyradError(FIRST_RUN_3)
elif not force:
## skip if all are | python | {
"resource": ""
} |
q259266 | Assembly._step5func | validation | def _step5func(self, samples, force, ipyclient):
""" hidden wrapped function to start step 5 """
## print header
if self._headers:
print("\n Step 5: Consensus base calling ")
## Get sample objects from list of strings
samples = _get_samples(self, samples)
## Check if all/none in the right state
if not self._samples_precheck(samples, 5, force):
raise IPyradError(FIRST_RUN_4)
elif not force:
## skip if all are | python | {
"resource": ""
} |
q259267 | Assembly._step6func | validation | def _step6func(self,
samples,
noreverse,
force,
randomseed,
ipyclient,
**kwargs):
"""
Hidden function to start Step 6.
"""
## Get sample objects from list of strings
samples = _get_samples(self, samples)
## remove samples that aren't ready
csamples = self._samples_precheck(samples, 6, force)
## print CLI header
if self._headers:
print("\n Step 6: Clustering at {} similarity across {} samples".\
| python | {
"resource": ""
} |
q259268 | Assembly._samples_precheck | validation | def _samples_precheck(self, samples, mystep, force):
""" Return a list of samples that are actually ready for the next step.
Each step runs this prior to calling run, makes it easier to
centralize and normalize how each step is checking sample states.
mystep is the state produced by the current step.
"""
subsample = []
## filter by state
for sample in samples:
| python | {
"resource": ""
} |
q259269 | combinefiles | validation | def combinefiles(filepath):
""" Joins first and second read file names """
## unpack seq files in filepath
fastqs = glob.glob(filepath)
firsts = [i for i in fastqs if | python | {
"resource": ""
} |
q259270 | get_barcode_func | validation | def get_barcode_func(data, longbar):
""" returns the fastest func given data & longbar"""
## build func for finding barcode
if longbar[1] == 'same':
if data.paramsdict["datatype"] == '2brad':
def getbarcode(cutters, read1, longbar):
""" find barcode for 2bRAD data """
return read1[1][:-(len(cutters[0][0]) + 1)][-longbar[0]:]
else:
def getbarcode(_, read1, longbar):
""" finds barcode for invariable length barcode data """ | python | {
"resource": ""
} |
q259271 | get_quart_iter | validation | def get_quart_iter(tups):
""" returns an iterator to grab four lines at a time """
if tups[0].endswith(".gz"):
ofunc = gzip.open
else:
ofunc = open
## create iterators
ofile1 = ofunc(tups[0], 'r')
fr1 = iter(ofile1)
quart1 = itertools.izip(fr1, fr1, fr1, fr1)
if tups[1]:
ofile2 = ofunc(tups[1], 'r')
fr2 = iter(ofile2)
quart2 = itertools.izip(fr2, fr2, fr2, fr2)
quarts = itertools.izip(quart1, quart2)
else:
ofile2 = 0
| python | {
"resource": ""
} |
q259272 | writetofastq | validation | def writetofastq(data, dsort, read):
"""
Writes sorted data 'dsort dict' to a tmp files
"""
if read == 1:
rrr = "R1"
else:
rrr = "R2"
for sname in dsort:
## skip writing if empty. Write to tmpname
handle = os.path.join(data.dirs.fastqs,
| python | {
"resource": ""
} |
q259273 | collate_files | validation | def collate_files(data, sname, tmp1s, tmp2s):
"""
Collate temp fastq files in tmp-dir into 1 gzipped sample.
"""
## out handle
out1 = os.path.join(data.dirs.fastqs, "{}_R1_.fastq.gz".format(sname))
out = io.BufferedWriter(gzip.open(out1, 'w'))
## build cmd
cmd1 = ['cat']
for tmpfile in tmp1s:
cmd1 += [tmpfile]
## compression function
proc = sps.Popen(['which', 'pigz'], stderr=sps.PIPE, stdout=sps.PIPE).communicate()
if proc[0].strip():
compress = ["pigz"]
else:
compress = ["gzip"]
## call cmd
proc1 = sps.Popen(cmd1, stderr=sps.PIPE, stdout=sps.PIPE)
proc2 = sps.Popen(compress, stdin=proc1.stdout, stderr=sps.PIPE, stdout=out)
err = | python | {
"resource": ""
} |
q259274 | estimate_optim | validation | def estimate_optim(data, testfile, ipyclient):
"""
Estimate a reasonable optim value by grabbing a chunk of sequences,
decompressing and counting them, to estimate the full file size.
"""
## count the len of one file and assume all others are similar len
insize = os.path.getsize(testfile)
tmp_file_name = os.path.join(data.paramsdict["project_dir"], "tmp-step1-count.fq")
if testfile.endswith(".gz"):
infile = gzip.open(testfile)
outfile = gzip.open(tmp_file_name, 'wb', compresslevel=5)
else:
infile = open(testfile)
outfile = open(tmp_file_name, 'w')
## We'll take the average of the size of a file based on the
## | python | {
"resource": ""
} |
q259275 | _cleanup_and_die | validation | def _cleanup_and_die(data):
""" cleanup func for step 1 """
tmpfiles = glob.glob(os.path.join(data.dirs.fastqs, | python | {
"resource": ""
} |
q259276 | splitfiles | validation | def splitfiles(data, raws, ipyclient):
""" sends raws to be chunked"""
## create a tmpdir for chunked_files and a chunk optimizer
tmpdir = os.path.join(data.paramsdict["project_dir"], "tmp-chunks-"+data.name)
if os.path.exists(tmpdir):
shutil.rmtree(tmpdir)
os.makedirs(tmpdir)
## chunk into 8M reads
totalreads = estimate_optim(data, raws[0][0], ipyclient)
optim = int(8e6)
njobs = int(totalreads/(optim/4.)) * len(raws)
## if more files than cpus: no chunking
nosplit = 0
if (len(raws) > len(ipyclient)) or (totalreads < optim):
nosplit = 1
## send slices N at a time. The dict chunkfiles stores a tuple of rawpairs
## dictionary to store asyncresults for sorting jobs
start = time.time()
| python | {
"resource": ""
} |
q259277 | putstats | validation | def putstats(pfile, handle, statdicts):
""" puts stats from pickles into a dictionary """
## load in stats
with open(pfile, 'r') as infile:
filestats, samplestats = pickle.load(infile)
## get dicts from statdicts tuple
perfile, fsamplehits, fbarhits, fmisses, fdbars = statdicts
## pull new stats
#handle = os.path.splitext(os.path.basename(handle))[0]
perfile[handle] += filestats
## update sample stats
| python | {
"resource": ""
} |
q259278 | _countmatrix | validation | def _countmatrix(lxs):
""" fill a matrix with pairwise data sharing """
## an empty matrix
share = np.zeros((lxs.shape[0], lxs.shape[0]))
## fill above
names = range(lxs.shape[0])
for row in lxs:
for samp1, samp2 in itertools.combinations(names, 2):
shared = lxs[samp1, lxs[samp2] > 0].sum()
| python | {
"resource": ""
} |
q259279 | paramname | validation | def paramname(param=""):
""" Get the param name from the dict index value.
"""
try:
name = pinfo[str(param)][0].strip().split(" ")[1]
except (KeyError, ValueError) as err:
## TODO: paramsinfo get description by param string not working.
## It would be cool to have an assembly object bcz then you could
| python | {
"resource": ""
} |
q259280 | save_json2 | validation | def save_json2(data):
""" save to json."""
## convert everything to dicts
## skip _ipcluster cuz it's made new.
datadict = OrderedDict([
("outfiles", data.__dict__["outfiles"]),
| python | {
"resource": ""
} |
q259281 | save_json | validation | def save_json(data):
""" Save assembly and samples as json """
## data as dict
#### skip _ipcluster because it's made new
#### skip _headers because it's loaded new
#### statsfiles save only keys
#### samples save only keys
datadict = OrderedDict([
("_version", data.__dict__["_version"]),
("_checkpoint", data.__dict__["_checkpoint"]),
("name", data.__dict__["name"]),
("dirs", data.__dict__["dirs"]),
("paramsdict", data.__dict__["paramsdict"]),
("samples", data.__dict__["samples"].keys()),
("populations", data.__dict__["populations"]),
("database", data.__dict__["database"]),
("clust_database", data.__dict__["clust_database"]),
("outfiles", data.__dict__["outfiles"]),
("barcodes", data.__dict__["barcodes"]),
("stats_files", data.__dict__["stats_files"]),
("_hackersonly", data.__dict__["_hackersonly"]),
])
## sample dict
sampledict = OrderedDict([])
for key, sample in data.samples.iteritems():
sampledict[key] = sample._to_fulldict()
## json format it using cumstom Encoder class
fulldumps = json.dumps({
"assembly": datadict,
"samples": sampledict
},
| python | {
"resource": ""
} |
q259282 | Encoder.encode | validation | def encode(self, obj):
""" function to encode json string"""
def hint_tuples(item):
""" embeds __tuple__ hinter in json strings """
if isinstance(item, tuple):
return {'__tuple__': True, 'items': item}
if isinstance(item, list):
return [hint_tuples(e) for e in item]
if isinstance(item, dict):
return {
| python | {
"resource": ""
} |
q259283 | depthplot | validation | def depthplot(data, samples=None, dims=(None,None), canvas=(None,None),
xmax=50, log=False, outprefix=None, use_maxdepth=False):
""" plots histogram of coverages across clusters"""
## select samples to be plotted, requires depths info
if not samples:
samples = data.samples.keys()
samples.sort()
subsamples = OrderedDict([(i, data.samples[i]) for i in samples])
## get canvas dimensions based on n-samples
if any(dims):
## user-supplied dimensions (...)
print("userdims")
else:
if len(subsamples) <= 4:
## set dimension to N samples
dims = (1, len(subsamples))
else:
dims = (len(subsamples)/4, 4)
## create canvas
if any(canvas):
print("usercanvas")
canvas = toyplot.Canvas(width=canvas[0], height=canvas[1])
else:
canvas = toyplot.Canvas(width=200*dims[1], height=150*dims[0])
## get all of the data arrays
for panel, sample in enumerate(subsamples):
## statistical called bins
statdat = subsamples[sample].depths
statdat = statdat[statdat >= data.paramsdict["mindepth_statistical"]]
if use_maxdepth:
statdat = {i:j for (i, j) in statdat if \
i < data.paramsdict["maxdepth"]}
sdat = np.histogram(statdat, range(50))
## majrule called bins
statdat = | python | {
"resource": ""
} |
q259284 | _parse_00 | validation | def _parse_00(ofile):
"""
return 00 outfile as a pandas DataFrame
"""
with open(ofile) as infile:
## read in the results summary from the end of the outfile
arr = np.array(
[" "] + infile.read().split("Summary of MCMC results\n\n\n")[1:][0]\
.strip().split())
## reshape array
rows = 12
cols = (arr.shape[0] + 1) / rows
arr = arr.reshape(rows, cols)
| python | {
"resource": ""
} |
q259285 | _parse_01 | validation | def _parse_01(ofiles, individual=False):
"""
a subfunction for summarizing results
"""
## parse results from outfiles
cols = []
dats = []
for ofile in ofiles:
## parse file
with open(ofile) as infile:
dat = infile.read()
lastbits = dat.split(".mcmc.txt\n\n")[1:]
results = lastbits[0].split("\n\n")[0].split()
## get shape from ...
shape = (((len(results) - 3) / 4), 4)
dat = np.array(results[3:]).reshape(shape)
cols.append(dat[:, 3].astype(float))
if not individual:
| python | {
"resource": ""
} |
q259286 | Bpp._load_existing_results | validation | def _load_existing_results(self, name, workdir):
"""
Load existing results files for an object with this workdir and name.
This does NOT reload the parameter settings for the object...
"""
## get mcmcs
path = os.path.realpath(os.path.join(self.workdir, self.name))
mcmcs = glob.glob(path+"_r*.mcmc.txt")
outs = glob.glob(path+"_r*.out.txt")
trees = glob.glob(path+"_r*.tre")
for mcmcfile in mcmcs:
if mcmcfile not in self.files.mcmcfiles:
self.files.mcmcfiles.append(mcmcfile)
| python | {
"resource": ""
} |
q259287 | Bpp.summarize_results | validation | def summarize_results(self, individual_results=False):
"""
Prints a summarized table of results from replicate runs, or,
if individual_result=True, then returns a list of separate
dataframes for each replicate run.
"""
## return results depending on algorithm
## algorithm 00
if (not self.params.infer_delimit) & (not self.params.infer_sptree):
if individual_results:
## return a list of parsed CSV results
return [_parse_00(i) for i in self.files.outfiles]
else:
## concatenate each CSV and then get stats w/ describe
return pd.concat(
[pd.read_csv(i, sep='\t', index_col=0) \
| python | {
"resource": ""
} |
q259288 | multi_muscle_align | validation | def multi_muscle_align(data, samples, ipyclient):
"""
Sends the cluster bits to nprocessors for muscle alignment. They return
with indel.h5 handles to be concatenated into a joint h5.
"""
LOGGER.info("starting alignments")
## get client
lbview = ipyclient.load_balanced_view()
start = time.time()
printstr = " aligning clusters | {} | s6 |"
elapsed = datetime.timedelta(seconds=int(time.time()-start))
progressbar(20, 0, printstr.format(elapsed), spacer=data._spacer)
## submit clustbits as jobs to engines. The chunkfiles are removed when they
## are finished so this job can even be restarted if it was half finished,
## though that is probably rare.
path = os.path.join(data.tmpdir, data.name + ".chunk_*")
clustbits = glob.glob(path)
jobs = {}
for idx in xrange(len(clustbits)):
args = [data, samples, clustbits[idx]]
jobs[idx] = lbview.apply(persistent_popen_align3, *args)
allwait = len(jobs)
elapsed = datetime.timedelta(seconds=int(time.time()-start))
progressbar(20, 0, printstr.format(elapsed), spacer=data._spacer)
| python | {
"resource": ""
} |
q259289 | concatclusts | validation | def concatclusts(outhandle, alignbits):
""" concatenates sorted aligned cluster tmpfiles and removes them."""
with gzip.open(outhandle, 'wb') as out:
for | python | {
"resource": ""
} |
q259290 | fill_dups_arr | validation | def fill_dups_arr(data):
"""
fills the duplicates array from the multi_muscle_align tmp files
"""
## build the duplicates array
duplefiles = glob.glob(os.path.join(data.tmpdir, "duples_*.tmp.npy"))
duplefiles.sort(key=lambda x: int(x.rsplit("_", 1)[-1][:-8]))
## enter the duplicates filter into super h5 array
io5 = h5py.File(data.clust_database, 'r+')
dfilter = io5["duplicates"]
## enter all duple arrays into full duplicates array
| python | {
"resource": ""
} |
q259291 | build_tmp_h5 | validation | def build_tmp_h5(data, samples):
""" build tmp h5 arrays that can return quick access for nloci"""
## get samples and names, sorted
snames = [i.name for i in samples]
snames.sort()
## Build an array for quickly indexing consens reads from catg files.
## save as a npy | python | {
"resource": ""
} |
q259292 | get_nloci | validation | def get_nloci(data):
""" return nloci from the tmp h5 arr"""
bseeds = os.path.join(data.dirs.across, data.name+".tmparrs.h5")
| python | {
"resource": ""
} |
q259293 | singlecat | validation | def singlecat(data, sample, bseeds, sidx, nloci):
"""
Orders catg data for each sample into the final locus order. This allows
all of the individual catgs to simply be combined later. They are also in
the same order as the indels array, so indels are inserted from the indel
array that is passed in.
"""
LOGGER.info("in single cat here")
## enter ref data?
isref = 'reference' in data.paramsdict["assembly_method"]
## grab seeds and hits info for this sample
with h5py.File(bseeds, 'r') as io5:
## get hits just for this sample and sort them by sample order index
hits = io5["uarr"][:]
hits = hits[hits[:, 1] == sidx, :]
#hits = hits[hits[:, 2].argsort()]
## get seeds just for this sample and sort them by sample order index
seeds = io5["seedsarr"][:]
seeds = seeds[seeds[:, 1] == sidx, :]
#seeds = seeds[seeds[:, 2].argsort()]
full = np.concatenate((seeds, hits))
full = full[full[:, 0].argsort()]
## still using max+20 len limit, rare longer merged reads get trimmed
## we need to allow room for indels to be added too
maxlen = data._hackersonly["max_fragment_length"] + 20
## we'll fill a new catg and alleles arr for this sample in locus order,
## which is known from seeds and hits
ocatg = np.zeros((nloci, maxlen, 4), dtype=np.uint32)
onall = np.zeros(nloci, dtype=np.uint8)
ochrom = np.zeros((nloci, 3), dtype=np.int64)
## grab the sample's data and write to ocatg and onall
if not sample.files.database:
raise IPyradWarningExit("missing catg file - {}".format(sample.name))
with h5py.File(sample.files.database, 'r') as io5:
## get it and delete it
catarr = io5["catg"][:]
| python | {
"resource": ""
} |
q259294 | write_to_fullarr | validation | def write_to_fullarr(data, sample, sidx):
""" writes arrays to h5 disk """
## enter ref data?
#isref = 'reference' in data.paramsdict["assembly_method"]
LOGGER.info("writing fullarr %s %s", sample.name, sidx)
## save big arrays to disk temporarily
with h5py.File(data.clust_database, 'r+') as io5:
## open views into the arrays we plan to fill
chunk = io5["catgs"].attrs["chunksize"][0]
catg = io5["catgs"]
nall = io5["nalleles"]
## adding an axis to newcatg makes it write about 1000X faster.
smpio = os.path.join(data.dirs.across, sample.name+'.tmp.h5')
with h5py.File(smpio) as indat:
## grab all of | python | {
"resource": ""
} |
q259295 | dask_chroms | validation | def dask_chroms(data, samples):
"""
A dask relay function to fill chroms for all samples
"""
## example concatenating with dask
h5s = [os.path.join(data.dirs.across, s.name+".tmp.h5") for s in samples]
handles = [h5py.File(i) for i in h5s]
dsets = [i['/ichrom'] for i in handles]
arrays = [da.from_array(dset, chunks=(10000, 3)) for dset in dsets]
stack = da.stack(arrays, axis=2)
## max chrom (should we check for variable hits? if so, things can get wonk)
maxchrom = da.max(stack, axis=2)[:, 0]
## max pos
maxpos = da.max(stack, axis=2)[:, 2]
## min pos
mask = stack | python | {
"resource": ""
} |
q259296 | inserted_indels | validation | def inserted_indels(indels, ocatg):
"""
inserts indels into the catg array
"""
## return copy with indels inserted
newcatg = np.zeros(ocatg.shape, dtype=np.uint32)
## iterate over loci and make extensions for indels
for iloc in xrange(ocatg.shape[0]):
## get indels indices
indidx = np.where(indels[iloc, :])[0]
if np.any(indidx):
## which new (empty) rows will be added
allrows = np.arange(ocatg.shape[1])
mask = np.ones(allrows.shape[0], dtype=np.bool_)
| python | {
"resource": ""
} |
q259297 | count_seeds | validation | def count_seeds(usort):
"""
uses bash commands to quickly count N seeds from utemp file
"""
with open(usort, 'r') as insort:
cmd1 = ["cut", "-f", "2"]
cmd2 = ["uniq"]
cmd3 = ["wc"]
proc1 = sps.Popen(cmd1, stdin=insort, stdout=sps.PIPE, close_fds=True)
proc2 = sps.Popen(cmd2, stdin=proc1.stdout, stdout=sps.PIPE, close_fds=True)
| python | {
"resource": ""
} |
q259298 | sort_seeds | validation | def sort_seeds(uhandle, usort):
""" sort seeds from cluster results"""
cmd = ["sort", "-k", "2", uhandle, "-o", | python | {
"resource": ""
} |
q259299 | build_clustbits | validation | def build_clustbits(data, ipyclient, force):
"""
Reconstitutes clusters from .utemp and htemp files and writes them
to chunked files for aligning in muscle.
"""
## If you run this step then we clear all tmp .fa and .indel.h5 files
if os.path.exists(data.tmpdir):
shutil.rmtree(data.tmpdir)
os.mkdir(data.tmpdir)
## parallel client
lbview = ipyclient.load_balanced_view()
start = time.time()
printstr = " building clusters | {} | s6 |"
elapsed = datetime.timedelta(seconds=int(time.time()-start))
progressbar(3, 0, printstr.format(elapsed), spacer=data._spacer)
uhandle = os.path.join(data.dirs.across, data.name+".utemp")
usort = os.path.join(data.dirs.across, data.name+".utemp.sort")
async1 = ""
## skip usorting if not force and already exists
if not os.path.exists(usort) or force:
## send sort job to engines. Sorted seeds allows us to work through
## the utemp file one locus at a time instead of reading all into mem.
LOGGER.info("building reads file -- loading utemp file into mem")
async1 = lbview.apply(sort_seeds, *(uhandle, usort))
while 1:
elapsed = datetime.timedelta(seconds=int(time.time()-start))
progressbar(3, 0, printstr.format(elapsed), spacer=data._spacer)
if async1.ready():
break
else:
time.sleep(0.1)
## send count seeds job to engines.
async2 = lbview.apply(count_seeds, usort)
while 1:
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.