_id stringlengths 2 7 | title stringlengths 1 88 | partition stringclasses 3
values | text stringlengths 31 13.1k | language stringclasses 1
value | meta_information dict |
|---|---|---|---|---|---|
q259300 | sub_build_clustbits | validation | def sub_build_clustbits(data, usort, nseeds):
"""
A subfunction of build_clustbits to allow progress tracking. This func
splits the unaligned clusters into bits for aligning on separate cores.
"""
## load FULL concat fasta file into a dict. This could cause RAM issues.
## this file has iupac codes in it, not ambigs resolved, and is gzipped.
LOGGER.info("loading full _catcons file into memory")
allcons = {}
conshandle = os.path.join(data.dirs.across, data.name+"_catcons.tmp")
with gzip.open(conshandle, 'rb') as iocons:
cons = itertools.izip(*[iter(iocons)]*2)
for namestr, seq in cons:
nnn, sss = [i.strip() for i in namestr, seq]
allcons[nnn[1:]] = sss
## set optim to approximately 4 chunks per core. Smaller allows for a bit
## cleaner looking progress bar. 40 cores will make 160 files.
optim = ((nseeds // (data.cpus*4)) + (nseeds % (data.cpus*4)))
LOGGER.info("building clustbits, optim=%s, nseeds=%s, cpus=%s",
optim, nseeds, data.cpus)
## iterate through usort grabbing seeds and matches
with open(usort, 'rb') as insort:
## iterator, seed null, and seqlist null
isort = iter(insort)
loci = 0
lastseed = 0
fseqs = []
seqlist = []
seqsize = 0
while 1:
## grab the next line
try:
hit, seed, ori = isort.next().strip().split()
except StopIteration:
break
try:
## if same seed, append match
if seed != lastseed:
## store the last fseq, count it, and clear it
if fseqs:
seqlist.append("\n".join(fseqs))
seqsize += 1
fseqs = []
## occasionally write to file
if seqsize >= optim:
if seqlist:
loci += seqsize
with open(os.path.join(data.tmpdir,
data.name+".chunk_{}".format(loci)), 'w') as clustsout:
LOGGER.debug("writing chunk - seqsize {} loci {} {}".format(seqsize, loci, clustsout.name))
clustsout.write("\n//\n//\n".join(seqlist)+"\n//\n//\n")
## reset list and counter
seqlist = []
seqsize = 0
## store the new seed on top of fseq
| python | {
"resource": ""
} |
q259301 | cleanup_tempfiles | validation | def cleanup_tempfiles(data):
"""
Function to remove older files. This is called either in substep 1 or after
the final substep so that tempfiles are retained for restarting interrupted
jobs until we're sure they're no longer needed.
"""
## remove align-related tmp files
tmps1 = glob.glob(os.path.join(data.tmpdir, "*.fa"))
tmps2 = glob.glob(os.path.join(data.tmpdir, "*.npy"))
for tmp in tmps1 + tmps2:
if os.path.exists(tmp):
os.remove(tmp)
## remove cluster related files
removal = [
| python | {
"resource": ""
} |
q259302 | assembly_cleanup | validation | def assembly_cleanup(data):
""" cleanup for assembly object """
## build s2 results data frame
data.stats_dfs.s2 = data._build_stat("s2")
data.stats_files.s2 = os.path.join(data.dirs.edits, 's2_rawedit_stats.txt')
## write stats for all samples
| python | {
"resource": ""
} |
q259303 | parse_single_results | validation | def parse_single_results(data, sample, res1):
""" parse results from cutadapt into sample data"""
## set default values
#sample.stats_dfs.s2["reads_raw"] = 0
sample.stats_dfs.s2["trim_adapter_bp_read1"] = 0
sample.stats_dfs.s2["trim_quality_bp_read1"] = 0
sample.stats_dfs.s2["reads_filtered_by_Ns"] = 0
sample.stats_dfs.s2["reads_filtered_by_minlen"] = 0
sample.stats_dfs.s2["reads_passed_filter"] = 0
## parse new values from cutadapt results output
lines = res1.strip().split("\n")
for line in lines:
if "Total reads processed:" in line:
value = int(line.split()[3].replace(",", ""))
sample.stats_dfs.s2["reads_raw"] = value
if "Reads with adapters:" in line:
value = int(line.split()[3].replace(",", ""))
sample.stats_dfs.s2["trim_adapter_bp_read1"] = value
if "Quality-trimmed" in line:
value = int(line.split()[1].replace(",", ""))
sample.stats_dfs.s2["trim_quality_bp_read1"] = value
if "Reads that were too short" in line:
value = int(line.split()[5].replace(",", ""))
sample.stats_dfs.s2["reads_filtered_by_minlen"] = value
if "Reads with too many N" in line:
value = int(line.split()[5].replace(",", ""))
sample.stats_dfs.s2["reads_filtered_by_Ns"] | python | {
"resource": ""
} |
q259304 | run2 | validation | def run2(data, samples, force, ipyclient):
"""
Filter for samples that are already finished with this step, allow others
to run, pass them to parallel client function to filter with cutadapt.
"""
## create output directories
data.dirs.edits = os.path.join(os.path.realpath(
data.paramsdict["project_dir"]),
data.name+"_edits")
if not os.path.exists(data.dirs.edits):
os.makedirs(data.dirs.edits)
## get samples
subsamples = choose_samples(samples, force)
## only allow extra adapters in filters==3,
## and add poly repeats if not in list of adapters
if int(data.paramsdict["filter_adapters"]) == 3:
if not data._hackersonly["p3_adapters_extra"]:
for poly in ["A"*8, "T"*8, "C"*8, "G"*8]:
data._hackersonly["p3_adapters_extra"].append(poly)
| python | {
"resource": ""
} |
q259305 | concat_reads | validation | def concat_reads(data, subsamples, ipyclient):
""" concatenate if multiple input files for a single samples """
## concatenate reads if they come from merged assemblies.
if any([len(i.files.fastqs) > 1 for i in subsamples]):
## run on single engine for now
start = time.time()
printstr = " concatenating inputs | {} | s2 |"
finished = 0
catjobs = {}
for sample in subsamples:
if len(sample.files.fastqs) > 1:
catjobs[sample.name] = ipyclient[0].apply(\
concat_multiple_inputs, *(data, sample))
else:
sample.files.concat = sample.files.fastqs
## wait for all to finish
while 1:
finished = sum([i.ready() for i in catjobs.values()])
elapsed = datetime.timedelta(seconds=int(time.time()-start))
progressbar(len(catjobs), finished, printstr.format(elapsed), spacer=data._spacer)
time.sleep(0.1)
if finished == len(catjobs):
print("")
break
| python | {
"resource": ""
} |
q259306 | run_cutadapt | validation | def run_cutadapt(data, subsamples, lbview):
"""
sends fastq files to cutadapt
"""
## choose cutadapt function based on datatype
start = time.time()
printstr = " processing reads | {} | s2 |"
finished = 0
rawedits = {}
## sort subsamples so that the biggest files get submitted first
subsamples.sort(key=lambda x: x.stats.reads_raw, reverse=True)
LOGGER.info([i.stats.reads_raw for i in subsamples])
## send samples to cutadapt filtering
if "pair" in data.paramsdict["datatype"]:
for sample in subsamples:
rawedits[sample.name] = lbview.apply(cutadaptit_pairs, *(data, sample))
else:
for sample in subsamples:
rawedits[sample.name] = | python | {
"resource": ""
} |
q259307 | concat_multiple_inputs | validation | def concat_multiple_inputs(data, sample):
"""
If multiple fastq files were appended into the list of fastqs for samples
then we merge them here before proceeding.
"""
## if more than one tuple in fastq list
if len(sample.files.fastqs) > 1:
## create a cat command to append them all (doesn't matter if they
## are gzipped, cat still works). Grab index 0 of tuples for R1s.
cmd1 = ["cat"] + [i[0] for i in sample.files.fastqs]
isgzip = ".gz"
if not sample.files.fastqs[0][0].endswith(".gz"):
isgzip = ""
## write to new concat handle
conc1 = os.path.join(data.dirs.edits, sample.name+"_R1_concat.fq{}".format(isgzip))
with open(conc1, 'w') as cout1:
proc1 = sps.Popen(cmd1, stderr=sps.STDOUT, stdout=cout1, close_fds=True)
res1 = proc1.communicate()[0]
if proc1.returncode:
raise IPyradWarningExit("error in: {}, {}".format(cmd1, res1))
## Only set conc2 if R2 actually exists
conc2 = 0
if "pair" in data.paramsdict["datatype"]:
cmd2 = ["cat"] + [i[1] for i in sample.files.fastqs]
| python | {
"resource": ""
} |
q259308 | make | validation | def make( data, samples ):
""" Convert vcf from step6 to .loci format to facilitate downstream format conversion """
invcffile = os.path.join( data.dirs.consens, data.name+".vcf" )
outlocifile = | python | {
"resource": ""
} |
q259309 | importvcf | validation | def importvcf( vcffile, locifile ):
""" Function for importing a vcf file into loci format. Arguments
are the input vcffile and the loci file to write out. """
try:
## Get names of all individuals in the vcf
with open( invcffile, 'r' ) as invcf:
for line in invcf:
if line.split()[0] == "#CHROM":
## This is maybe a little clever. The names in the vcf are everything after
## the "FORMAT" column, so find that index, then slice everything after it.
| python | {
"resource": ""
} |
q259310 | get_targets | validation | def get_targets(ipyclient):
"""
A function to find 2 engines per hostname on the ipyclient.
We'll assume that the CPUs are hyperthreaded, which is why
we grab two. If they are not then no foul. Two multi-threaded
jobs will be run on each of the 2 engines per host.
"""
## fill hosts with async[gethostname]
hosts = []
for eid in ipyclient.ids:
engine = ipyclient[eid]
if not engine.outstanding:
hosts.append(engine.apply(socket.gethostname))
## capture results of asyncs
hosts = [i.get() for i in hosts]
hostset = set(hosts) | python | {
"resource": ""
} |
q259311 | compute_tree_stats | validation | def compute_tree_stats(self, ipyclient):
"""
compute stats for stats file and NHX tree features
"""
## get name indices
names = self.samples
## get majority rule consensus tree of weighted Q bootstrap trees
if self.params.nboots:
## Tree object
fulltre = ete3.Tree(self.trees.tree, format=0)
fulltre.unroot()
## only grab as many boots as the last option said was max
with open(self.trees.boots, 'r') as inboots:
bb = [ete3.Tree(i.strip(), format=0) for i in inboots.readlines()]
wboots = [fulltre] + bb[-self.params.nboots:]
## infer consensus tree and write to file
wctre, wcounts = consensus_tree(wboots, names=names)
self.trees.cons = os.path.join(self.dirs, self.name + ".cons")
with open(self.trees.cons, 'w') as ocons:
ocons.write(wctre.write(format=0))
else:
wctre = ete3.Tree(self.trees.tree, format=0)
wctre.unroot()
## build stats file and write trees
self.trees.nhx = os.path.join(self.dirs, self.name + ".nhx")
with open(self.files.stats, 'w') as ostats:
## print Tetrad info
| python | {
"resource": ""
} |
q259312 | random_product | validation | def random_product(iter1, iter2):
""" random sampler for equal_splits func"""
pool1 = | python | {
"resource": ""
} |
q259313 | n_choose_k | validation | def n_choose_k(n, k):
""" get the number of quartets as n-choose-k. This is used
in equal splits to decide whether a split should be exhaustively sampled
or randomly sampled. Edges near tips can be exhaustive while highly nested
| python | {
"resource": ""
} |
q259314 | count_snps | validation | def count_snps(mat):
"""
get dstats from the count array and return as a float tuple
"""
## get [aabb, baba, abba, aaab]
snps = np.zeros(4, dtype=np.uint32)
## get concordant (aabb) pis sites
snps[0] = np.uint32(\
mat[0, 5] + mat[0, 10] + mat[0, 15] + \
mat[5, 0] + mat[5, 10] + mat[5, 15] + \
mat[10, 0] + mat[10, 5] + mat[10, 15] + \
mat[15, 0] + mat[15, 5] + mat[15, 10])
## get discordant (baba) sites
for i in range(16):
if i % 5:
snps[1] += mat[i, | python | {
"resource": ""
} |
q259315 | chunk_to_matrices | validation | def chunk_to_matrices(narr, mapcol, nmask):
"""
numba compiled code to get matrix fast.
arr is a 4 x N seq matrix converted to np.int8
I convert the numbers for ATGC into their respective index for the MAT
matrix, and leave all others as high numbers, i.e., -==45, N==78.
"""
## get seq alignment and create an empty array for filling
mats = np.zeros((3, 16, 16), dtype=np.uint32)
## replace ints with small ints that index their place in the
## 16x16. This no longer checks for big ints to | python | {
"resource": ""
} |
q259316 | calculate | validation | def calculate(seqnon, mapcol, nmask, tests):
""" groups together several numba compiled funcs """
## create empty matrices
#LOGGER.info("tests[0] %s", tests[0])
#LOGGER.info('seqnon[[tests[0]]] %s', seqnon[[tests[0]]])
mats = chunk_to_matrices(seqnon, mapcol, nmask)
## empty svdscores for each arrangement of seqchunk
svds = np.zeros((3, 16), dtype=np.float64)
qscores = np.zeros(3, dtype=np.float64)
| python | {
"resource": ""
} |
q259317 | nworker | validation | def nworker(data, smpchunk, tests):
""" The workhorse function. Not numba. """
## tell engines to limit threads
#numba.config.NUMBA_DEFAULT_NUM_THREADS = 1
## open the seqarray view, the modified array is in bootsarr
with h5py.File(data.database.input, 'r') as io5:
seqview = io5["bootsarr"][:]
maparr = io5["bootsmap"][:]
## create an N-mask array of all seq cols (this isn't really too slow)
nall_mask = seqview[:] == 78
## tried numba compiling everythign below here, but was not faster
## than making nmask w/ axis arg in numpy
## get the input arrays ready
rquartets = np.zeros((smpchunk.shape[0], 4), dtype=np.uint16)
rweights = None
#rweights = | python | {
"resource": ""
} |
q259318 | shuffle_cols | validation | def shuffle_cols(seqarr, newarr, cols):
""" used in bootstrap resampling without a | python | {
"resource": ""
} |
q259319 | resolve_ambigs | validation | def resolve_ambigs(tmpseq):
""" returns a seq array with 'RSKYWM' randomly replaced with resolved bases"""
## iterate over the bases 'RSKWYM': [82, 83, 75, 87, 89, 77]
for ambig in np.uint8([82, 83, 75, 87, 89, 77]):
## get all site in this ambig
idx, idy = np.where(tmpseq == ambig)
## get the two resolutions of the | python | {
"resource": ""
} |
q259320 | get_spans | validation | def get_spans(maparr, spans):
""" get span distance for each locus in original seqarray """
## start at 0, finds change at 1-index of map file
bidx = 1
spans = np.zeros((maparr[-1, 0], 2), np.uint64)
## read through marr and record when locus id changes
for idx in xrange(1, maparr.shape[0]):
cur = maparr[idx, 0]
| python | {
"resource": ""
} |
q259321 | get_shape | validation | def get_shape(spans, loci):
""" get shape of new bootstrap resampled locus array """
width = 0
for idx in xrange(loci.shape[0]):
| python | {
"resource": ""
} |
q259322 | fill_boot | validation | def fill_boot(seqarr, newboot, newmap, spans, loci):
""" fills the new bootstrap resampled array """
## column index
cidx = 0
## resample each locus
for i in xrange(loci.shape[0]):
## grab a random locus's columns
x1 = spans[loci[i]][0]
x2 = spans[loci[i]][1]
cols = seqarr[:, x1:x2]
## randomize columns within colsq
cord = np.random.choice(cols.shape[1], cols.shape[1], replace=False)
rcols = cols[:, cord]
## fill bootarr with n columns from seqarr
## the required length was already measured
| python | {
"resource": ""
} |
q259323 | _byteify | validation | def _byteify(data, ignore_dicts=False):
"""
converts unicode to utf-8 when reading in json files
"""
if isinstance(data, unicode):
return data.encode("utf-8")
if isinstance(data, list):
return [_byteify(item, ignore_dicts=True) for item in data]
if isinstance(data, dict) and not ignore_dicts:
return | python | {
"resource": ""
} |
q259324 | Tetrad._parse_names | validation | def _parse_names(self):
""" parse sample names from the sequence file"""
self.samples = []
with iter(open(self.files.data, 'r')) as infile:
infile.next().strip().split()
while 1:
| python | {
"resource": ""
} |
q259325 | Tetrad._run_qmc | validation | def _run_qmc(self, boot):
""" runs quartet max-cut on a quartets file """
## convert to txt file for wQMC
self._tmp = os.path.join(self.dirs, ".tmpwtre")
cmd = [ip.bins.qmc, "qrtt="+self.files.qdump, "otre="+self._tmp]
## run them
proc = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
res = proc.communicate()
if proc.returncode:
#LOGGER.error("Error in QMC: \n({}).".format(res))
LOGGER.error(res)
raise IPyradWarningExit(res[1])
| python | {
"resource": ""
} |
q259326 | Tetrad._dump_qmc | validation | def _dump_qmc(self):
"""
Makes a reduced array that excludes quartets with no information and
prints the quartets and weights to a file formatted for wQMC
"""
## open the h5 database
io5 = h5py.File(self.database.output, 'r')
## create an output file for writing
self.files.qdump = os.path.join(self.dirs, self.name+".quartets.txt")
LOGGER.info("qdump file %s", self.files.qdump)
outfile = open(self.files.qdump, 'w')
## todo: should pull quarts order in randomly? or doesn't matter?
for idx in xrange(0, self.params.nquartets, self._chunksize):
| python | {
"resource": ""
} |
q259327 | Tetrad._renamer | validation | def _renamer(self, tre):
""" renames newick from numbers to sample names"""
## get the tre with numbered tree tip labels
names = tre.get_leaves()
## replace numbered names with snames
for name in names:
| python | {
"resource": ""
} |
q259328 | Tetrad._finalize_stats | validation | def _finalize_stats(self, ipyclient):
""" write final tree files """
## print stats file location:
#print(STATSOUT.format(opr(self.files.stats)))
## print finished tree information ---------------------
print(FINALTREES.format(opr(self.trees.tree)))
## print bootstrap information --------------------------
if self.params.nboots:
## get consensus, map values to tree edges, record stats file
self._compute_tree_stats(ipyclient)
## print bootstrap info
print(BOOTTREES.format(opr(self.trees.cons), opr(self.trees.boots)))
## print the ASCII tree only if its small
if len(self.samples) < 20:
if self.params.nboots:
wctre = ete3.Tree(self.trees.cons, format=0)
wctre.ladderize()
print(wctre.get_ascii(show_internal=True,
attributes=["dist", "name"]))
| python | {
"resource": ""
} |
q259329 | Tetrad._save | validation | def _save(self):
""" save a JSON file representation of Tetrad Class for checkpoint"""
## save each attribute as dict
fulldict = copy.deepcopy(self.__dict__)
for i, j in fulldict.items():
if isinstance(j, Params):
fulldict[i] = j.__dict__
fulldumps = json.dumps(fulldict,
sort_keys=False,
indent=4,
| python | {
"resource": ""
} |
q259330 | Tetrad._insert_to_array | validation | def _insert_to_array(self, start, results):
""" inputs results from workers into hdf4 array """
qrts, wgts, qsts = results
#qrts, wgts = results
#print(qrts)
with h5py.File(self.database.output, 'r+') as out:
chunk = self._chunksize
| python | {
"resource": ""
} |
q259331 | select_samples | validation | def select_samples(dbsamples, samples, pidx=None):
"""
Get the row index of samples that are included. If samples are in the
'excluded' they were already filtered out of 'samples' during _get_samples.
"""
## get index from dbsamples
samples = [i.name for i in samples]
if pidx:
| python | {
"resource": ""
} |
q259332 | padnames | validation | def padnames(names):
""" pads names for loci output """
## get longest name
longname_len = max(len(i) for i in names)
## Padding distance between name and seq.
padding = 5
## add pad to names
pnames = [name + " " * (longname_len - len(name)+ padding) \
| python | {
"resource": ""
} |
q259333 | locichunk | validation | def locichunk(args):
"""
Function from make_loci to apply to chunks. smask is sample mask.
"""
## parse args
data, optim, pnames, snppad, smask, start, samplecov, locuscov, upper = args
## this slice
hslice = [start, start+optim]
## get filter db info
co5 = h5py.File(data.database, 'r')
afilt = co5["filters"][hslice[0]:hslice[1], ]
aedge = co5["edges"][hslice[0]:hslice[1], ]
asnps = co5["snps"][hslice[0]:hslice[1], ]
## get seqs db
io5 = h5py.File(data.clust_database, 'r')
if upper:
aseqs = np.char.upper(io5["seqs"][hslice[0]:hslice[1], ])
else:
aseqs = io5["seqs"][hslice[0]:hslice[1], ]
| python | {
"resource": ""
} |
q259334 | enter_pairs | validation | def enter_pairs(iloc, pnames, snppad, edg, aseqs, asnps, smask, samplecov, locuscov, start):
""" enters funcs for pairs """
## snps was created using only the selected samples.
LOGGER.info("edges in enter_pairs %s", edg)
seq1 = aseqs[iloc, :, edg[0]:edg[1]+1]
snp1 = asnps[iloc, edg[0]:edg[1]+1, ]
## the 2nd read edges are +5 for the spacer
seq2 = aseqs[iloc, :, edg[2]:edg[3]+1]
snp2 = asnps[iloc, edg[2]:edg[3]+1, ]
## remove rows with all Ns, seq has only selected samples
nalln = np.all(seq1 == "N", axis=1)
## make mask of removed rows and excluded samples. Use the inverse
## of this to save the coverage for samples
nsidx = nalln + smask
LOGGER.info("nsidx %s, nalln %s, smask %s", nsidx, nalln, smask)
samplecov = samplecov + np.invert(nsidx).astype(np.int32)
LOGGER.info("samplecov %s", samplecov)
idx = np.sum(np.invert(nsidx).astype(np.int32))
LOGGER.info("idx %s", idx)
locuscov[idx] += 1
## select the remaining names in order
seq1 = seq1[~nsidx, ]
seq2 = seq2[~nsidx, ]
names = pnames[~nsidx]
## save string for printing, excluding names not in samples
outstr = "\n".join(\
[name + s1.tostring()+"nnnn"+s2.tostring() for name, s1, s2 in \
zip(names, seq1, seq2)])
#LOGGER.info("s1 %s", s1.tostring())
#LOGGER.info("s2 %s", s2.tostring())
## get snp string and add to store
| python | {
"resource": ""
} |
q259335 | enter_singles | validation | def enter_singles(iloc, pnames, snppad, edg, aseqs, asnps, smask, samplecov, locuscov, start):
""" enter funcs for SE or merged data """
## grab all seqs between edges
seq = aseqs[iloc, :, edg[0]:edg[1]+1]
## snps was created using only the selected samples, and is edge masked.
## The mask is for counting snps quickly, but trimming is still needed here
## to make the snps line up with the seqs in the snp string.
snp = asnps[iloc, edg[0]:edg[1]+1, ]
## remove rows with all Ns, seq has only selected samples
nalln = np.all(seq == "N", axis=1)
## make mask of removed rows and excluded samples. Use the inverse
## of this to save the coverage for samples
nsidx = nalln + smask
samplecov = samplecov + np.invert(nsidx).astype(np.int32)
idx = np.sum(np.invert(nsidx).astype(np.int32))
locuscov[idx] += 1
## select the remaining names | python | {
"resource": ""
} |
q259336 | init_arrays | validation | def init_arrays(data):
"""
Create database file for storing final filtered snps data as hdf5 array.
Copies splits and duplicates info from clust_database to database.
"""
## get stats from step6 h5 and create new h5
co5 = h5py.File(data.clust_database, 'r')
io5 = h5py.File(data.database, 'w')
## get maxlen and chunk len
maxlen = data._hackersonly["max_fragment_length"] + 20
chunks = co5["seqs"].attrs["chunksize"][0]
nloci = co5["seqs"].shape[0]
## make array for snp string, 2 cols, - and *
snps = io5.create_dataset("snps", (nloci, maxlen, 2),
dtype=np.bool,
chunks=(chunks, maxlen, 2),
compression='gzip')
snps.attrs["chunksize"] = chunks
snps.attrs["names"] = ["-", "*"]
## array for filters that will be applied in step7
filters = io5.create_dataset("filters", (nloci, 6), dtype=np.bool)
filters.attrs["filters"] = ["duplicates", "max_indels",
"max_snps", "max_shared_hets",
| python | {
"resource": ""
} |
q259337 | snpcount_numba | validation | def snpcount_numba(superints, snpsarr):
"""
Used to count the number of unique bases in a site for snpstring.
"""
## iterate over all loci
for iloc in xrange(superints.shape[0]):
for site in xrange(superints.shape[2]):
## make new array
catg = np.zeros(4, dtype=np.int16)
## a list for only catgs
ncol = superints[iloc, :, site]
for idx in range(ncol.shape[0]):
if ncol[idx] == 67: #C
catg[0] += 1
elif ncol[idx] == 65: #A
catg[1] += 1
elif ncol[idx] == 84: #T
catg[2] += 1
elif ncol[idx] == 71: #G
catg[3] += 1
elif ncol[idx] == 82: #R
catg[1] += 1 #A
catg[3] += 1 #G
elif ncol[idx] == 75: #K
catg[2] += 1 #T
catg[3] += 1 #G
elif ncol[idx] == 83: #S
catg[0] += 1 #C
catg[3] += 1 #G
elif ncol[idx] == 89: #Y
catg[0] += 1 | python | {
"resource": ""
} |
q259338 | maxind_numba | validation | def maxind_numba(block):
""" filter for indels """
## remove terminal edges
inds = 0
for row in xrange(block.shape[0]):
where = np.where(block[row] != 45)[0]
| python | {
"resource": ""
} |
q259339 | write_snps_map | validation | def write_snps_map(data):
""" write a map file with linkage information for SNPs file"""
## grab map data from tmparr
start = time.time()
tmparrs = os.path.join(data.dirs.outfiles, "tmp-{}.h5".format(data.name))
with h5py.File(tmparrs, 'r') as io5:
maparr = io5["maparr"][:]
## get last data
end = np.where(np.all(maparr[:] == 0, axis=1))[0]
if np.any(end):
end = end.min()
else:
end = maparr.shape[0]
## write to map file (this is too slow...)
outchunk = []
with open(data.outfiles.snpsmap, 'w') as out:
for idx in xrange(end):
## build to list
line = maparr[idx, :]
#print(line)
outchunk.append(\
| python | {
"resource": ""
} |
q259340 | write_usnps | validation | def write_usnps(data, sidx, pnames):
""" write the bisnp string """
## grab bis data from tmparr
tmparrs = os.path.join(data.dirs.outfiles, "tmp-{}.h5".format(data.name))
with h5py.File(tmparrs, 'r') as io5:
bisarr = io5["bisarr"]
## trim to size b/c it was made longer than actual
end = np.where(np.all(bisarr[:] == "", axis=0))[0]
if np.any(end):
end = end.min()
else:
end = bisarr.shape[1] | python | {
"resource": ""
} |
q259341 | write_str | validation | def write_str(data, sidx, pnames):
""" Write STRUCTURE format for all SNPs and unlinked SNPs """
## grab snp and bis data from tmparr
start = time.time()
tmparrs = os.path.join(data.dirs.outfiles, "tmp-{}.h5".format(data.name))
with h5py.File(tmparrs, 'r') as io5:
snparr = io5["snparr"]
bisarr = io5["bisarr"]
## trim to size b/c it was made longer than actual
bend = np.where(np.all(bisarr[:] == "", axis=0))[0]
if np.any(bend):
bend = bend.min()
else:
bend = bisarr.shape[1]
send = np.where(np.all(snparr[:] == "", axis=0))[0]
if np.any(send):
send = send.min()
else:
send = snparr.shape[1]
## write to str and ustr
out1 = open(data.outfiles.str, 'w')
out2 = open(data.outfiles.ustr, 'w')
numdict = {'A': '0', 'T': '1', 'G': '2', 'C': '3', 'N': '-9', '-': '-9'}
if data.paramsdict["max_alleles_consens"] > 1:
for idx, name in enumerate(pnames):
out1.write("{}\t\t\t\t\t{}\n"\
.format(name,
"\t".join([numdict[DUCT[i][0]] for i in snparr[idx, :send]])))
out1.write("{}\t\t\t\t\t{}\n"\
.format(name,
| python | {
"resource": ""
} |
q259342 | concat_vcf | validation | def concat_vcf(data, names, full):
"""
Sorts, concatenates, and gzips VCF chunks. Also cleans up chunks.
"""
## open handle and write headers
if not full:
writer = open(data.outfiles.vcf, 'w')
else:
writer = gzip.open(data.outfiles.VCF, 'w')
vcfheader(data, names, writer)
writer.close()
## get vcf chunks
vcfchunks = glob.glob(data.outfiles.vcf+".*")
vcfchunks.sort(key=lambda x: int(x.rsplit(".")[-1]))
## concatenate
if not full:
writer = open(data.outfiles.vcf, 'a')
else:
writer = gzip.open(data.outfiles.VCF, 'a')
## what order do users want? The order in the original ref file?
## Sorted by the size of chroms? that is the order in faidx.
## | python | {
"resource": ""
} |
q259343 | reftrick | validation | def reftrick(iseq, consdict):
""" Returns the most common base at each site in order. """
altrefs = np.zeros((iseq.shape[1], 4), dtype=np.uint8)
altrefs[:, 1] = 46
for col in xrange(iseq.shape[1]):
## expand colums with ambigs and remove N-
fcounts = np.zeros(111, dtype=np.int64)
counts = np.bincount(iseq[:, col])#, minlength=90)
fcounts[:counts.shape[0]] = counts
## set N and - to zero, wish numba supported minlen arg
fcounts[78] = 0
fcounts[45] = 0
## add ambig counts to true bases
for aidx in xrange(consdict.shape[0]):
nbases = fcounts[consdict[aidx, 0]]
for _ in xrange(nbases):
fcounts[consdict[aidx, 1]] += 1
fcounts[consdict[aidx, 2]] += 1
fcounts[consdict[aidx, 0]] = 0
## now get counts from the modified counts arr
who = np.argmax(fcounts)
altrefs[col, 0] = who
fcounts[who] = 0
| python | {
"resource": ""
} |
q259344 | _collapse_outgroup | validation | def _collapse_outgroup(tree, taxdicts):
""" collapse outgroup in ete Tree for easier viewing """
## check that all tests have the same outgroup
outg = taxdicts[0]["p4"]
if not all([i["p4"] == outg for i in taxdicts]):
raise Exception("no good")
## prune tree, keep only one sample from outgroup
tre = ete.Tree(tree.write(format=1)) #tree.copy(method="deepcopy")
alltax = [i for i in tre.get_leaf_names() if i not in outg]
alltax += [outg[0]]
tre.prune(alltax)
tre.search_nodes(name=outg[0])[0].name | python | {
"resource": ""
} |
q259345 | Tree.draw | validation | def draw(
self,
show_tip_labels=True,
show_node_support=False,
use_edge_lengths=False,
orient="right",
print_args=False,
*args,
**kwargs):
"""
plot the tree using toyplot.graph.
Parameters:
-----------
show_tip_labels: bool
Show tip names from tree.
use_edge_lengths: bool
Use edge lengths from newick tree.
show_node_support: bool
Show support values at nodes using a set of default
options.
| python | {
"resource": ""
} |
q259346 | get_quick_depths | validation | def get_quick_depths(data, sample):
""" iterate over clustS files to get data """
## use existing sample cluster path if it exists, since this
## func can be used in step 4 and that can occur after merging
## assemblies after step3, and if we then referenced by data.dirs.clusts
## the path would be broken.
##
## If branching at step 3 to test different clust thresholds, the
## branched samples will retain the samples.files.clusters of the
## parent (which have the clust_threshold value of the parent), so
## it will look like nothing has changed. If we call this func
## from step 3 then it indicates we are in a branch and should
## reset the sample.files.clusters handle to point to the correct
## data.dirs.clusts directory. See issue #229.
## Easier to just always trust that samples.files.clusters is right,
## no matter what step?
#if sample.files.clusters and not sample.stats.state == 3:
# pass
#else:
# ## set cluster file handles
sample.files.clusters = os.path.join(
data.dirs.clusts, sample.name+".clustS.gz")
## get new clustered loci
fclust = data.samples[sample.name].files.clusters
clusters = gzip.open(fclust, 'r')
pairdealer = itertools.izip(*[iter(clusters)]*2)
## storage
depths = []
maxlen = []
## start with cluster 0
| python | {
"resource": ""
} |
q259347 | align_and_parse | validation | def align_and_parse(handle, max_internal_indels=5, is_gbs=False):
""" much faster implementation for aligning chunks """
## data are already chunked, read in the whole thing. bail if no data.
try:
with open(handle, 'rb') as infile:
clusts = infile.read().split("//\n//\n")
## remove any empty spots
clusts = [i for i in clusts if i]
## Skip entirely empty chunks
if not clusts:
raise IPyradError
except (IOError, IPyradError):
LOGGER.debug("skipping empty chunk - {}".format(handle))
return 0
## count discarded clusters for printing to stats later
highindels = 0
## iterate over clusters sending each to muscle, splits and aligns pairs
try:
aligned = persistent_popen_align3(clusts, 200, is_gbs)
except Exception as inst:
LOGGER.debug("Error in handle - {} - {}".format(handle, inst))
#raise IPyradWarningExit("error hrere {}".format(inst))
aligned = []
## store good alignments to be written to file
refined = []
## filter and trim alignments
for clust in aligned:
## check for too many internal indels
filtered = aligned_indel_filter(clust, max_internal_indels)
## reverse complement matches. | python | {
"resource": ""
} |
q259348 | aligned_indel_filter | validation | def aligned_indel_filter(clust, max_internal_indels):
""" checks for too many internal indels in muscle aligned clusters """
## make into list
lclust = clust.split()
## paired or not
try:
seq1 = [i.split("nnnn")[0] for i in lclust[1::2]]
seq2 = [i.split("nnnn")[1] for i in lclust[1::2]]
intindels1 = [i.rstrip("-").lstrip("-").count("-") for i in seq1]
intindels2 = [i.rstrip("-").lstrip("-").count("-") for i in seq2]
| python | {
"resource": ""
} |
q259349 | setup_dirs | validation | def setup_dirs(data):
""" sets up directories for step3 data """
## make output folder for clusters
pdir = os.path.realpath(data.paramsdict["project_dir"])
data.dirs.clusts = os.path.join(pdir, "{}_clust_{}"\
.format(data.name, data.paramsdict["clust_threshold"]))
if not os.path.exists(data.dirs.clusts):
os.mkdir(data.dirs.clusts)
## make a tmpdir for align files
data.tmpdir = os.path.abspath(os.path.expanduser(
os.path.join(pdir, data.name+'-tmpalign')))
if not os.path.exists(data.tmpdir):
| python | {
"resource": ""
} |
q259350 | build_dag | validation | def build_dag(data, samples):
"""
build a directed acyclic graph describing jobs to be run in order.
"""
## Create DAGs for the assembly method being used, store jobs in nodes
snames = [i.name for i in samples]
dag = nx.DiGraph()
## get list of pre-align jobs from globals based on assembly method
joborder = JOBORDER[data.paramsdict["assembly_method"]]
## WHICH JOBS TO RUN: iterate over the sample names
for sname in snames:
## append pre-align job for each sample to nodes list
for func in joborder:
dag.add_node("{}-{}-{}".format(func, 0, sname))
## append align func jobs, each will have max 10
for chunk in xrange(10):
dag.add_node("{}-{}-{}".format("muscle_align", chunk, sname))
## append final reconcat jobs
dag.add_node("{}-{}-{}".format("reconcat", 0, sname))
## ORDER OF JOBS: add edges/dependency between jobs: (first-this, then-that)
for sname in snames:
for sname2 in snames:
## enforce that clust/map cannot start until derep is done for ALL
## samples. This is b/c...
dag.add_edge("{}-{}-{}".format(joborder[0], 0, sname2),
"{}-{}-{}".format(joborder[1], 0, sname))
## add remaining pre-align jobs
for idx in xrange(2, len(joborder)):
| python | {
"resource": ""
} |
q259351 | _plot_dag | validation | def _plot_dag(dag, results, snames):
"""
makes plot to help visualize the DAG setup. For developers only.
"""
try:
import matplotlib.pyplot as plt
from matplotlib.dates import date2num
from matplotlib.cm import gist_rainbow
## first figure is dag layout
plt.figure("dag_layout", figsize=(10, 10))
nx.draw(dag,
pos=nx.spring_layout(dag),
node_color='pink',
with_labels=True)
plt.savefig("./dag_layout.png", bbox_inches='tight', dpi=200)
## second figure is times for steps
pos = {}
colors = {}
for node in dag:
#jobkey = "{}-{}".format(node, sample)
mtd = results[node].metadata
start = date2num(mtd.started)
#runtime = date2num(md.completed)# - start
## sample id to separate samples on x-axis
_, _, sname = node.split("-", 2)
sid = snames.index(sname)
## 1e6 to separate on y-axis
pos[node] = (start+sid, start*1e6)
| python | {
"resource": ""
} |
q259352 | trackjobs | validation | def trackjobs(func, results, spacer):
"""
Blocks and prints progress for just the func being requested from a list
of submitted engine jobs. Returns whether any of the jobs failed.
func = str
results = dict of asyncs
"""
## TODO: try to insert a better way to break on KBD here.
LOGGER.info("inside trackjobs of %s", func)
## get just the jobs from results that are relevant to this func
asyncs = [(i, results[i]) for i in results if i.split("-", 2)[0] == func]
## progress bar
start = time.time()
while 1:
## how many of this func have finished so far
ready = [i[1].ready() for i in asyncs]
elapsed = datetime.timedelta(seconds=int(time.time()-start))
| python | {
"resource": ""
} |
q259353 | concat_multiple_edits | validation | def concat_multiple_edits(data, sample):
"""
if multiple fastq files were appended into the list of fastqs for samples
then we merge them here before proceeding.
"""
## if more than one tuple in fastq list
if len(sample.files.edits) > 1:
## create a cat command to append them all (doesn't matter if they
## are gzipped, cat still works). Grab index 0 of tuples for R1s.
cmd1 = ["cat"] + [i[0] for i in sample.files.edits]
## write to new concat handle
conc1 = os.path.join(data.dirs.edits, sample.name+"_R1_concatedit.fq.gz")
with open(conc1, 'w') as cout1:
| python | {
"resource": ""
} |
q259354 | cluster | validation | def cluster(data, sample, nthreads, force):
"""
Calls vsearch for clustering. cov varies by data type, values were chosen
based on experience, but could be edited by users
"""
## get the dereplicated reads
if "reference" in data.paramsdict["assembly_method"]:
derephandle = os.path.join(data.dirs.edits, sample.name+"-refmap_derep.fastq")
## In the event all reads for all samples map successfully then clustering
## the unmapped reads makes no sense, so just bail out.
if not os.stat(derephandle).st_size:
## In this case you do have to create empty, dummy vsearch output
## files so building_clusters will not fail.
uhandle = os.path.join(data.dirs.clusts, sample.name+".utemp")
usort = os.path.join(data.dirs.clusts, sample.name+".utemp.sort")
hhandle = os.path.join(data.dirs.clusts, sample.name+".htemp")
for f in [uhandle, usort, hhandle]:
open(f, 'a').close()
return
else:
derephandle = os.path.join(data.dirs.edits, sample.name+"_derep.fastq")
## create handles for the outfiles
uhandle = os.path.join(data.dirs.clusts, sample.name+".utemp")
temphandle = os.path.join(data.dirs.clusts, sample.name+".htemp")
## If derep file doesn't exist then bail out
if not os.path.isfile(derephandle):
LOGGER.warn("Bad derephandle - {}".format(derephandle))
raise IPyradError("Input file for clustering doesn't exist - {}"\
.format(derephandle))
## testing one sample fail
#if sample.name == "1C_0":
# x
## datatype specific optimization
## minsl: the percentage of the seed that must be matched
## smaller values for RAD/ddRAD where we might want to combine, say 50bp
## reads and 100bp reads in the same analysis.
## query_cov: the percentage of the query sequence that must match seed
## smaller values are needed for gbs where only the tips might overlap
## larger values for pairgbs where they should overlap near completely
## small minsl and high query cov allows trimmed reads to match to untrim
## seed for rad/ddrad/pairddrad.
strand = "plus"
cov = 0.75
minsl = 0.5
if data.paramsdict["datatype"] in ["gbs", "2brad"]:
strand = "both"
cov = 0.5
minsl = 0.5
elif data.paramsdict["datatype"] == 'pairgbs':
strand = "both"
cov = 0.75
minsl = 0.75
## If this value is not null (which is the default) then override query cov
if data._hackersonly["query_cov"]:
cov = str(data._hackersonly["query_cov"])
assert float(cov) <= 1, "query_cov must be <= 1.0"
| python | {
"resource": ""
} |
q259355 | muscle_chunker | validation | def muscle_chunker(data, sample):
"""
Splits the muscle alignment into chunks. Each chunk is run on a separate
computing core. Because the largest clusters are at the beginning of the
clusters file, assigning equal clusters to each file would put all of the
large cluster, that take longer to align, near the top. So instead we
randomly distribute the clusters among the files. If assembly method is
reference then this step is just a placeholder and nothing happens.
"""
## log our location for debugging
LOGGER.info("inside muscle_chunker")
## only chunk up denovo data, refdata has its own chunking method which
## makes equal size chunks, instead of uneven chunks like in denovo
if data.paramsdict["assembly_method"] != "reference":
## get the number of clusters
clustfile = os.path.join(data.dirs.clusts, sample.name+".clust.gz")
with iter(gzip.open(clustfile, 'rb')) as clustio:
nloci = sum(1 for i in clustio if "//" in i) // 2
| python | {
"resource": ""
} |
q259356 | derep_concat_split | validation | def derep_concat_split(data, sample, nthreads, force):
"""
Running on remote Engine. Refmaps, then merges, then dereplicates,
then denovo clusters reads.
"""
## report location for debugging
LOGGER.info("INSIDE derep %s", sample.name)
## MERGED ASSEMBIES ONLY:
## concatenate edits files within Samples. Returns a new sample.files.edits
## with the concat file. No change if not merged Assembly.
mergefile = os.path.join(data.dirs.edits, sample.name+"_merged_.fastq")
if not force:
if not os.path.exists(mergefile):
sample.files.edits = concat_multiple_edits(data, sample)
else:
LOGGER.info("skipped concat_multiple_edits: {} exists"\
.format(mergefile))
else:
sample.files.edits = concat_multiple_edits(data, sample)
## PAIRED DATA ONLY:
## Denovo: merge or concat fastq pairs [sample.files.pairs]
## Reference: only concat fastq pairs []
## Denovo + Reference: ...
if 'pair' in data.paramsdict['datatype']:
## the output file handle for merged reads
## modify behavior of merging vs concating if reference
if "reference" in data.paramsdict["assembly_method"]:
nmerged = merge_pairs(data, sample.files.edits, mergefile, 0, 0)
else:
nmerged = merge_pairs(data, sample.files.edits, mergefile, 1, 1)
## store results
sample.files.edits = [(mergefile, )]
sample.stats.reads_merged | python | {
"resource": ""
} |
q259357 | branch_assembly | validation | def branch_assembly(args, parsedict):
"""
Load the passed in assembly and create a branch. Copy it
to a new assembly, and also write out the appropriate params.txt
"""
## Get the current assembly
data = getassembly(args, parsedict)
## get arguments to branch command
bargs = args.branch
## get new name, trim off .txt if it was accidentally added
newname = bargs[0]
if newname.endswith(".txt"):
newname = newname[:-4]
## look for subsamples
if len(bargs) > 1:
## Branching and subsampling at step 6 is a bad idea, it messes up
## indexing into the hdf5 cluster file. Warn against this.
if any([x.stats.state == 6 for x in data.samples.values()]):
pass
## TODODODODODO
#print("wat")
## are we removing or keeping listed samples?
subsamples = bargs[1:]
## drop the matching samples
if bargs[1] == "-":
## check drop names
fails = [i for i in subsamples[1:] if i not in data.samples.keys()]
if any(fails):
raise IPyradWarningExit("\
\n Failed: unrecognized names requested, check spelling:\n {}"\
| python | {
"resource": ""
} |
q259358 | getassembly | validation | def getassembly(args, parsedict):
"""
loads assembly or creates a new one and set its params from
parsedict. Does not launch ipcluster.
"""
## Creating an assembly with a full path in the name will "work"
## but it is potentially dangerous, so here we have assembly_name
## and assembly_file, name is used for creating new in cwd, file is
## used for loading existing.
##
## Be nice if the user includes the extension.
#project_dir = ip.core.assembly._expander(parsedict['1'])
#assembly_name = parsedict['0']
project_dir = ip.core.assembly._expander(parsedict['project_dir'])
assembly_name = parsedict['assembly_name']
assembly_file = os.path.join(project_dir, assembly_name)
## Assembly creation will handle error checking on
## the format of the assembly_name
## make sure the working directory exists.
if not os.path.exists(project_dir):
os.mkdir(project_dir)
try:
## If 1 and force then go ahead and create a new assembly
if ('1' in args.steps) and args.force:
data = ip.Assembly(assembly_name, cli=True)
else:
data = ip.load_json(assembly_file, cli=True)
data._cli = True
except IPyradWarningExit as _:
## if no assembly is found then go ahead and make one
if '1' not in args.steps:
| python | {
"resource": ""
} |
q259359 | get_binom | validation | def get_binom(base1, base2, estE, estH):
"""
return probability of base call
"""
prior_homo = (1. - estH) / 2.
prior_hete = estH
## calculate probs
bsum = base1 + base2
hetprob = scipy.misc.comb(bsum, base1)/(2. **(bsum))
homoa = scipy.stats.binom.pmf(base2, bsum, estE)
homob = scipy.stats.binom.pmf(base1, bsum, estE)
## calculate probs
hetprob *= prior_hete
homoa *= | python | {
"resource": ""
} |
q259360 | basecaller | validation | def basecaller(arrayed, mindepth_majrule, mindepth_statistical, estH, estE):
"""
call all sites in a locus array.
"""
## an array to fill with consensus site calls
cons = np.zeros(arrayed.shape[1], dtype=np.uint8)
cons.fill(78)
arr = arrayed.view(np.uint8)
## iterate over columns
for col in xrange(arr.shape[1]):
## the site of focus
carr = arr[:, col]
## make mask of N and - sites
mask = carr == 45
mask += carr == 78
marr = carr[~mask]
## skip if only empties (e.g., N-)
if not marr.shape[0]:
cons[col] = 78
## skip if not variable
elif np.all(marr == marr[0]):
cons[col] = marr[0]
## estimate variable site call
else:
## get allele freqs (first-most, second, third = p, q, r)
counts = np.bincount(marr)
pbase = np.argmax(counts)
nump = counts[pbase]
counts[pbase] = 0
qbase = np.argmax(counts)
numq = counts[qbase]
counts[qbase] = 0
rbase = np.argmax(counts)
numr = counts[rbase]
## based on biallelic depth
bidepth = nump + numq
if bidepth < mindepth_majrule:
cons[col] = 78
else:
## if depth is too high, reduce to sampled int
if bidepth > 500:
base1 = int(500 * (nump / float(bidepth)))
base2 = int(500 * (numq / | python | {
"resource": ""
} |
q259361 | nfilter1 | validation | def nfilter1(data, reps):
""" applies read depths filter """
if sum(reps) >= data.paramsdict["mindepth_majrule"] and \
| python | {
"resource": ""
} |
q259362 | storealleles | validation | def storealleles(consens, hidx, alleles):
""" store phased allele data for diploids """
## find the first hetero site and choose the priority base
## example, if W: then priority base in A and not T. PRIORITY=(order: CATG)
bigbase = PRIORITY[consens[hidx[0]]]
## find which allele has priority based on bigbase
bigallele = [i for i in alleles if i[0] == bigbase][0]
## uplow | python | {
"resource": ""
} |
q259363 | chunk_clusters | validation | def chunk_clusters(data, sample):
""" split job into bits and pass to the client """
## counter for split job submission
num = 0
## set optim size for chunks in N clusters. The first few chunks take longer
## because they contain larger clusters, so we create 4X as many chunks as
## processors so that they are split more evenly.
optim = int((sample.stats.clusters_total // data.cpus) + \
(sample.stats.clusters_total % data.cpus))
## break up the file into smaller tmp files for each engine
## chunking by cluster is a bit trickier than chunking by N lines | python | {
"resource": ""
} |
q259364 | run | validation | def run(data, samples, force, ipyclient):
""" checks if the sample should be run and passes the args """
## prepare dirs
data.dirs.consens = os.path.join(data.dirs.project, data.name+"_consens")
if not os.path.exists(data.dirs.consens):
os.mkdir(data.dirs.consens)
## zap any tmp files that might be leftover
tmpcons = glob.glob(os.path.join(data.dirs.consens, "*_tmpcons.*"))
tmpcats = glob.glob(os.path.join(data.dirs.consens, "*_tmpcats.*"))
for tmpfile in tmpcons+tmpcats:
os.remove(tmpfile)
## filter through samples for those ready
samples = get_subsamples(data, samples, force)
## set up parallel client: how many cores?
lbview = ipyclient.load_balanced_view()
data.cpus = data._ipcluster["cores"]
| python | {
"resource": ""
} |
q259365 | calculate_depths | validation | def calculate_depths(data, samples, lbview):
"""
check whether mindepth has changed, and thus whether clusters_hidepth
needs to be recalculated, and get new maxlen for new highdepth clusts.
if mindepth not changed then nothing changes.
"""
## send jobs to be processed on engines
start = time.time()
printstr = " calculating depths | {} | s5 |"
recaljobs = {}
maxlens = []
for sample in samples:
recaljobs[sample.name] = lbview.apply(recal_hidepth, *(data, sample))
## block until finished
while 1:
ready = [i.ready() for i in recaljobs.values()]
elapsed = datetime.timedelta(seconds=int(time.time()-start))
progressbar(len(ready), sum(ready), printstr.format(elapsed), spacer=data._spacer)
time.sleep(0.1)
if len(ready) == sum(ready):
print("")
| python | {
"resource": ""
} |
q259366 | make_chunks | validation | def make_chunks(data, samples, lbview):
"""
calls chunk_clusters and tracks progress.
"""
## first progress bar
start = time.time()
printstr = " chunking clusters | {} | s5 |"
elapsed = datetime.timedelta(seconds=int(time.time()-start))
progressbar(10, 0, printstr.format(elapsed), spacer=data._spacer)
## send off samples to be chunked
lasyncs = {}
for sample in samples:
lasyncs[sample.name] = lbview.apply(chunk_clusters, *(data, sample)) | python | {
"resource": ""
} |
q259367 | make | validation | def make(data, samples):
""" reads in .loci and builds alleles from case characters """
#read in loci file
outfile = open(os.path.join(data.dirs.outfiles, data.name+".alleles"), 'w')
lines = open(os.path.join(data.dirs.outfiles, data.name+".loci"), 'r')
## Get the longest sample name for pretty printing
longname = max(len(x) for x in data.samples.keys())
## Padding between name and sequence in output file. This should be the
## same as write_outfiles.write_tmp_loci.name_padding
name_padding = 5
writing = []
loc = 0
for line in lines:
if ">" in line:
name, seq = line.split(" ")[0], line.split(" ")[-1]
allele1, allele2 = splitalleles(seq.strip())
## Format the output string. the "-2" below accounts for the additional
## 2 characters added to the sample name that don't get added to the
## snpsites line, so you gotta bump this line back 2 to make it
## line up right.
| python | {
"resource": ""
} |
q259368 | cluster_info | validation | def cluster_info(ipyclient, spacer=""):
""" reports host and engine info for an ipyclient """
## get engine data, skips busy engines.
hosts = []
for eid in ipyclient.ids:
engine = ipyclient[eid]
if not engine.outstanding:
hosts.append(engine.apply(_socket.gethostname))
## report it
hosts = [i.get() for | python | {
"resource": ""
} |
q259369 | _set_debug_dict | validation | def _set_debug_dict(__loglevel__):
""" set the debug dict """
_lconfig.dictConfig({
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'standard': {
'format': "%(asctime)s \t"\
+"pid=%(process)d \t"\
+"[%(filename)s]\t"\
+"%(levelname)s \t"\
+"%(message)s"
},
},
'handlers': {
__name__: {
| python | {
"resource": ""
} |
q259370 | _debug_off | validation | def _debug_off():
""" turns off debugging by removing hidden tmp file """
if _os.path.exists(__debugflag__):
| python | {
"resource": ""
} |
q259371 | _cmd_exists | validation | def _cmd_exists(cmd):
""" check if dependency program is there """
return _subprocess.call("type " + cmd,
shell=True,
| python | {
"resource": ""
} |
q259372 | _getbins | validation | def _getbins():
""" gets the right version of vsearch, muscle, and smalt
depending on linux vs osx """
# Return error if system is 32-bit arch.
# This is straight from the python docs:
# https://docs.python.org/2/library/platform.html#cross-platform
if not _sys.maxsize > 2**32:
_sys.exit("ipyrad requires 64bit architecture")
## get platform mac or linux
_platform = _sys.platform
## get current location
if 'VIRTUAL_ENV' in _os.environ:
ipyrad_path = _os.environ['VIRTUAL_ENV']
else:
path = _os.path.abspath(_os.path.dirname(__file__))
ipyrad_path = _os.path.dirname(path)
## find bin directory
ipyrad_path = _os.path.dirname(path)
bin_path = _os.path.join(ipyrad_path, "bin")
## get the correct binaries
if 'linux' in _platform:
vsearch = _os.path.join(
_os.path.abspath(bin_path),
"vsearch-linux-x86_64")
muscle = _os.path.join(
_os.path.abspath(bin_path),
"muscle-linux-x86_64")
smalt = _os.path.join(
_os.path.abspath(bin_path),
"smalt-linux-x86_64")
bwa = _os.path.join(
_os.path.abspath(bin_path),
"bwa-linux-x86_64")
samtools = _os.path.join(
_os.path.abspath(bin_path),
"samtools-linux-x86_64")
bedtools = _os.path.join(
_os.path.abspath(bin_path),
"bedtools-linux-x86_64")
qmc = _os.path.join(
_os.path.abspath(bin_path),
"QMC-linux-x86_64")
else:
vsearch = _os.path.join(
_os.path.abspath(bin_path),
"vsearch-osx-x86_64")
muscle = _os.path.join(
| python | {
"resource": ""
} |
q259373 | nworker | validation | def nworker(data, chunk):
"""
Worker to distribute work to jit funcs. Wraps everything on an
engine to run single-threaded to maximize efficiency for
multi-processing.
"""
## set the thread limit on the remote engine
oldlimit = set_mkl_thread_limit(1)
## open seqarray view, the modified arr is in bootstarr
with h5py.File(data.database.input, 'r') as io5:
seqview = io5["bootsarr"][:]
maparr = io5["bootsmap"][:, 0]
smps = io5["quartets"][chunk:chunk+data._chunksize]
## create an N-mask array of all seq cols
nall_mask = seqview[:] == 78
## init arrays to fill with results
rquartets = np.zeros((smps.shape[0], 4), dtype=np.uint16)
rinvariants = np.zeros((smps.shape[0], 16, | python | {
"resource": ""
} |
q259374 | store_all | validation | def store_all(self):
"""
Populate array with all possible quartets. This allows us to
sample from the total, and also to continue from a checkpoint
"""
with h5py.File(self.database.input, 'a') as io5:
fillsets = io5["quartets"]
## generator for all quartet sets
qiter = itertools.combinations(xrange(len(self.samples)), 4)
| python | {
"resource": ""
} |
q259375 | store_random | validation | def store_random(self):
"""
Populate array with random quartets sampled from a generator.
Holding all sets in memory might take a lot, but holding a very
large list of random numbers for which ones to sample will fit
into memory for most reasonable sized sets. So we'll load a
list of random numbers in the range of the length of total
sets that can be generated, then only keep sets from the set
generator if they are in the int list. I did several tests to
check that random pairs are as likely as 0 & 1 to come up together
in a random quartet set.
"""
with h5py.File(self.database.input, 'a') as io5:
fillsets = io5["quartets"]
## set generators
qiter = itertools.combinations(xrange(len(self.samples)), 4)
rand = np.arange(0, n_choose_k(len(self.samples), 4))
np.random.shuffle(rand)
rslice = rand[:self.params.nquartets]
rss = np.sort(rslice)
riter = iter(rss)
del rand, rslice
## print progress update 1 to the engine stdout
print(self._chunksize)
## set to store
rando = riter.next()
tmpr = np.zeros((self.params.nquartets, 4), dtype=np.uint16)
| python | {
"resource": ""
} |
q259376 | random_combination | validation | def random_combination(nsets, n, k):
"""
Returns nsets unique random quartet sets sampled from
n-choose-k without replacement combinations.
| python | {
"resource": ""
} |
q259377 | random_product | validation | def random_product(iter1, iter2):
"""
Random sampler for equal_splits functions
"""
iter4 = np.concatenate([
| python | {
"resource": ""
} |
q259378 | resolve_ambigs | validation | def resolve_ambigs(tmpseq):
"""
Randomly resolve ambiguous bases. This is applied to each boot
replicate so that over reps the random resolutions don't matter.
Sites are randomly resolved, so best for unlinked SNPs since
otherwise linked SNPs are losing their linkage information...
though it's not like we're using it anyways.
"""
## the order of rows in GETCONS
for aidx in xrange(6):
#np.uint([82, 75, 83, 89, 87, 77]):
ambig, res1, res2 = GETCONS[aidx]
## get true wherever tmpseq is ambig
| python | {
"resource": ""
} |
q259379 | set_mkl_thread_limit | validation | def set_mkl_thread_limit(cores):
"""
set mkl thread limit and return old value so we can reset
when finished.
"""
if "linux" in sys.platform:
| python | {
"resource": ""
} |
q259380 | get_total | validation | def get_total(tots, node):
""" get total number of quartets possible for a split"""
if (node.is_leaf() or node.is_root()):
return 0
else:
## Get counts on down edges.
## How to treat polytomies here?
if len(node.children) > 2:
down_r = node.children[0]
down_l = node.children[1]
for child in node.children[2:]:
down_l += child
else:
down_r, down_l = node.children
lendr = sum(1 for i | python | {
"resource": ""
} |
q259381 | get_sampled | validation | def get_sampled(data, totn, node):
""" get total number of quartets sampled for a split"""
## convert tip names to ints
names = sorted(totn)
cdict = {name: idx for idx, name in enumerate(names)}
## skip some nodes
if (node.is_leaf() or node.is_root()):
return 0
else:
## get counts on down edges
if len(node.children) > 2:
down_r = node.children[0]
down_l = node.children[1]
for child in node.children[2:]:
down_l += child
else:
down_r, down_l = node.children
lendr = set(cdict[i] for i in down_r.get_leaf_names())
lendl = set(cdict[i] for i in down_l.get_leaf_names())
## get count on up edge sister
up_r = node.get_sisters()[0]
lenur = set(cdict[i] for i in up_r.get_leaf_names())
## everyone else | python | {
"resource": ""
} |
q259382 | Tetrad._run_qmc | validation | def _run_qmc(self, boot):
"""
Runs quartet max-cut QMC on the quartets qdump file.
"""
## build command
self._tmp = os.path.join(self.dirs, ".tmptre")
cmd = [ip.bins.qmc, "qrtt="+self.files.qdump, "otre="+self._tmp]
## run it
proc = subprocess.Popen(cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
res = proc.communicate()
if proc.returncode:
raise IPyradWarningExit(res[1])
## parse tmp file written by qmc into a tree and rename it | python | {
"resource": ""
} |
q259383 | Tetrad._insert_to_array | validation | def _insert_to_array(self, chunk, results):
"""
Enters results arrays into the HDF5 database.
"""
## two result arrs
chunksize = self._chunksize
qrts, invs = results
## enter into db
with h5py.File(self.database.output, 'r+') as io5:
io5['quartets'][chunk:chunk+chunksize] = qrts
## entered as 0-indexed !
if self.params.save_invariants:
if self.checkpoint.boots:
| python | {
"resource": ""
} |
q259384 | get_client | validation | def get_client(cluster_id, profile, engines, timeout, cores, quiet, spacer, **kwargs):
"""
Creates a client to view ipcluster engines for a given profile and
returns it with at least one engine spun up and ready to go. If no
engines are found after nwait amount of time then an error is raised.
If engines==MPI it waits a bit longer to find engines. If the number
of engines is set then it waits even longer to try to find that number
of engines.
"""
## save stds for later, we're gonna hide them to prevent external printing
save_stdout = sys.stdout
save_stderr = sys.stderr
sys.stdout = cStringIO.StringIO()
sys.stderr = cStringIO.StringIO()
## get cluster_info print string
connection_string = "{}establishing parallel connection:".format(spacer)
## wrapped search for ipcluster
try:
## are we looking for a running ipcluster instance?
if profile not in [None, "default"]:
args = {'profile': profile, "timeout": timeout}
else:
clusterargs = [cluster_id, profile, timeout]
argnames = ["cluster_id", "profile", "timeout"]
args = {key:value for key, value in zip(argnames, clusterargs)}
## get connection within timeout window of wait time and hide messages
ipyclient = ipp.Client(**args)
sys.stdout = save_stdout
sys.stderr = save_stderr
## check that all engines have connected
if (engines == "MPI") or ("ipyrad-cli-" in cluster_id):
| python | {
"resource": ""
} |
q259385 | memoize | validation | def memoize(func):
""" Memoization decorator for a function taking one or more arguments. """
class Memodict(dict):
""" just a dict"""
def __getitem__(self, *key):
return dict.__getitem__(self, key)
def __missing__(self, key):
| python | {
"resource": ""
} |
q259386 | ambigcutters | validation | def ambigcutters(seq):
"""
Returns both resolutions of a cut site that has an ambiguous base in
it, else the single cut site
"""
resos = []
if any([i in list("RKSYWM") for i in seq]):
for base in list("RKSYWM"):
if base in seq:
| python | {
"resource": ""
} |
q259387 | splitalleles | validation | def splitalleles(consensus):
""" takes diploid consensus alleles with phase data stored as a mixture
of upper and lower case characters and splits it into 2 alleles """
## store two alleles, allele1 will start with bigbase
allele1 = list(consensus)
allele2 = list(consensus)
hidx = [i for (i, j) in enumerate(consensus) if j in "RKSWYMrkswym"]
## do remaining h sites
for idx in hidx:
hsite = consensus[idx]
if hsite.isupper():
allele1[idx] = PRIORITY[hsite]
allele2[idx] | python | {
"resource": ""
} |
q259388 | comp | validation | def comp(seq):
""" returns a seq with complement. Preserves little n's for splitters."""
## makes base to its small complement then makes upper
return seq.replace("A", 't')\
.replace('T', 'a')\
| python | {
"resource": ""
} |
q259389 | fullcomp | validation | def fullcomp(seq):
""" returns complement of sequence including ambiguity characters,
and saves lower case info for multiple hetero sequences"""
## this is surely not the most efficient...
seq = seq.replace("A", 'u')\
.replace('T', 'v')\
.replace('C', 'p')\
.replace('G', 'z')\
.replace('u', 'T')\
.replace('v', 'A')\
.replace('p', 'G')\
| python | {
"resource": ""
} |
q259390 | fastq_touchup_for_vsearch_merge | validation | def fastq_touchup_for_vsearch_merge(read, outfile, reverse=False):
""" option to change orientation of reads and sets Qscore to B """
counts = 0
with open(outfile, 'w') as out:
## read in paired end read files 4 lines at a time
if read.endswith(".gz"):
fr1 = gzip.open(read, 'rb')
else:
fr1 = open(read, 'rb')
quarts = itertools.izip(*[iter(fr1)]*4)
## a list to store until writing
writing = []
while 1:
try:
lines = quarts.next()
except StopIteration:
break
if reverse:
seq = lines[1].strip()[::-1]
else:
seq = lines[1].strip()
| python | {
"resource": ""
} |
q259391 | revcomp | validation | def revcomp(sequence):
"returns reverse complement of a string"
sequence = sequence[::-1].strip()\
| python | {
"resource": ""
} |
q259392 | clustdealer | validation | def clustdealer(pairdealer, optim):
""" return optim clusters given iterators, and whether it got all or not"""
ccnt = 0
chunk = []
while ccnt < optim:
## try refreshing taker, else quit
try:
taker = itertools.takewhile(lambda x: x[0] != "//\n", pairdealer)
oneclust = ["".join(taker.next())]
except StopIteration:
#LOGGER.debug('last chunk %s', chunk)
return 1, chunk
## load one cluster
| python | {
"resource": ""
} |
q259393 | progressbar | validation | def progressbar(njobs, finished, msg="", spacer=" "):
""" prints a progress bar """
if njobs:
progress = 100*(finished / float(njobs))
else:
progress = 100
hashes = '#'*int(progress/5.)
nohash = ' '*int(20-len(hashes))
if not ipyrad.__interactive__:
| python | {
"resource": ""
} |
q259394 | get_threaded_view | validation | def get_threaded_view(ipyclient, split=True):
""" gets optimum threaded view of ids given the host setup """
## engine ids
## e.g., [0, 1, 2, 3, 4, 5, 6, 7, 8]
eids = ipyclient.ids
## get host names
## e.g., ['a', 'a', 'b', 'b', 'a', 'c', 'c', 'c', 'c']
dview = ipyclient.direct_view()
hosts = dview.apply_sync(socket.gethostname)
## group ids into a dict by their hostnames
## e.g., {a: [0, 1, 4], b: [2, 3], c: [5, 6, 7, 8]}
hostdict = defaultdict(list)
for host, eid in zip(hosts, eids):
hostdict[host].append(eid)
## Now split threads on the same host into separate proc if there are many
hostdictkeys = hostdict.keys()
for key in hostdictkeys:
gids = hostdict[key]
maxt = len(gids)
if len(gids) >= 4:
maxt = 2
## if 4 nodes and 4 ppn, put one sample per host
if (len(gids) == 4) and (len(hosts) >= 4):
maxt = 4
if len(gids) >= 6:
maxt = 3
if len(gids) >= | python | {
"resource": ""
} |
q259395 | detect_cpus | validation | def detect_cpus():
"""
Detects the number of CPUs on a system. This is better than asking
ipyparallel since ipp has to wait for Engines to spin up.
"""
# Linux, Unix and MacOS:
if hasattr(os, "sysconf"):
if os.sysconf_names.has_key("SC_NPROCESSORS_ONLN"):
# Linux & Unix:
ncpus = os.sysconf("SC_NPROCESSORS_ONLN")
if isinstance(ncpus, int) and ncpus > 0:
return ncpus
| python | {
"resource": ""
} |
q259396 | _call_structure | validation | def _call_structure(mname, ename, sname, name, workdir, seed, ntaxa, nsites, kpop, rep):
""" make the subprocess call to structure """
## create call string
outname = os.path.join(workdir, "{}-K-{}-rep-{}".format(name, kpop, rep))
cmd = ["structure",
"-m", mname,
"-e", ename,
"-K", str(kpop),
"-D", str(seed),
"-N", str(ntaxa),
"-L", str(nsites),
"-i", sname,
"-o", outname]
| python | {
"resource": ""
} |
q259397 | _get_clumpp_table | validation | def _get_clumpp_table(self, kpop, max_var_multiple, quiet):
""" private function to clumpp results"""
## concat results for k=x
reps, excluded = _concat_reps(self, kpop, max_var_multiple, quiet)
if reps:
ninds = reps[0].inds
nreps = len(reps)
else:
ninds = nreps = 0
if not reps:
return "no result files found"
clumphandle = os.path.join(self.workdir, "tmp.clumppparams.txt")
self.clumppparams.kpop = kpop
self.clumppparams.c = ninds
self.clumppparams.r = nreps
with open(clumphandle, 'w') as tmp_c:
tmp_c.write(self.clumppparams._asfile())
## create CLUMPP args string
outfile = os.path.join(self.workdir,
"{}-K-{}.outfile".format(self.name, kpop))
indfile = os.path.join(self.workdir,
"{}-K-{}.indfile".format(self.name, kpop))
miscfile = os.path.join(self.workdir,
"{}-K-{}.miscfile".format(self.name, kpop))
cmd = ["CLUMPP", clumphandle,
"-i", indfile,
"-o", outfile,
"-j", miscfile,
"-r", str(nreps),
"-c", str(ninds),
"-k", str(kpop)]
## call clumpp
proc = subprocess.Popen(cmd,
| python | {
"resource": ""
} |
q259398 | _get_evanno_table | validation | def _get_evanno_table(self, kpops, max_var_multiple, quiet):
"""
Calculates Evanno method K value scores for a series
of permuted clumpp results.
"""
## iterate across k-vals
kpops = sorted(kpops)
replnliks = []
for kpop in kpops:
## concat results for k=x
reps, excluded = _concat_reps(self, kpop, max_var_multiple, quiet)
## report if some results were excluded
if excluded:
if not quiet:
sys.stderr.write(
"[K{}] {} reps excluded (not converged) see 'max_var_multiple'.\n"\
.format(kpop, excluded))
if reps:
ninds = reps[0].inds
nreps = len(reps)
else:
ninds = nreps = 0
if not reps:
print "no result files found"
## all we really need is the lnlik
replnliks.append([i.est_lnlik for i in reps])
## compare lnlik and var of results
if len(replnliks) > 1:
lnmean = [np.mean(i) for i in replnliks]
lnstds = [np.std(i, ddof=1) for i in replnliks]
else:
lnmean = replnliks
lnstds = np.nan
tab = pd.DataFrame(
index=kpops,
data={
"Nreps": [len(i) for i in replnliks],
"lnPK": [0] * len(kpops),
| python | {
"resource": ""
} |
q259399 | Structure.result_files | validation | def result_files(self):
""" returns a list of files that have finished structure """
| python | {
"resource": ""
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.