Selectes motifs from a meme file based on the number of sites.

    :param memeFile: MEME file to be read
    :param outFile: MEME file to be written
    :param minSites: minimum number of sites each motif needs to have to be valid

    :returns: nothing
495027
def filterMotifs(memeFile,outFile, minSites):
    

    with open(memeFile, "r") as mF:
        oldMEME=mF.readlines()
        newMEME=oldMEME[:7]
        i=7
        while i < len(oldMEME):
            if oldMEME[i].split(" ")[0] == "MOTIF":
                print(oldMEME[i].split("\n")[0], int(oldMEME[i+2].split("nsites= ")[1].split(" ")[0]))
                sys.stdout.flush()
                if int(oldMEME[i+2].split("nsites= ")[1].split(" ")[0]) > minSites:
                    newMEME.append(oldMEME[i])
                    f=i+1
                    while oldMEME[f].split(" ")[0] != "MOTIF":
                        newMEME.append(oldMEME[f])
                        f=f+1
                    i=i+1
                else:
                    i=i+1
            else:
                i=i+1
    with open(outFile, "w+") as out:
        out.write("".join(newMEME) )

    return newMEME
1366754
def site_symbols(self):
        
        syms = [site.specie.symbol for site in self.structures[0]]
        return [a[0] for a in itertools.groupby(syms)]
869704
def read_follower_file(fname, min_followers=0, max_followers=1e10, blacklist=set()):
    
    result = {}
    with open(fname, 'rt') as f:
        for line in f:
            parts = line.split()
            if len(parts) > 3:
                if parts[1].lower() not in blacklist:
                    followers = set(int(x) for x in parts[2:])
                    if len(followers) > min_followers and len(followers) <= max_followers:
                        result[parts[1].lower()] = followers
                else:
                    print('skipping exemplar', parts[1].lower())
    return result
1182860
def rst_to_docs_rst(infile, outfile):
    """"""

    # Read infile into a list of lines
    with open(infile, 'r') as fin:
        rst = fin.readlines()

    # Inspect outfile path components to determine whether outfile
    # is in the root of the examples directory or in a subdirectory
    # thererof
    ps = pathsplit(outfile)[-3:]
    if ps[-2] == 'examples':
        ps = ps[-2:]
        idx = 'index'
    else:
        idx = ''

    # Output string starts with a cross-reference anchor constructed from
    # the file name and path
    out = '.. _' + '_'.join(ps) + ':\n\n'

    # Iterate over lines from infile
    it = iter(rst)
    for line in it:
        if line[0:12] == '.. toc-start':  # Line has start of toc marker
            # Initialise current toc array and iterate over lines until
            # end of toc marker encountered
            toc = []
            for line in it:
                if line == '\n':  # Drop newline lines
                    continue
                elif line[0:10] == '.. toc-end':  # End of toc marker
                    # Add toctree section to output string
                    out += '.. toctree::\n   :maxdepth: 1\n\n'
                    for c in toc:
                        out += '   %s <%s>\n' % c
                    break
                else:  #  Still within toc section
                    # Extract link text and target url and append to
                    # toc array
                    m = re.search(r'`(.*?)\s*<(.*?)(?:.py)?>`', line)
                    if m:
                        if idx == '':
                            toc.append((m.group(1), m.group(2)))
                        else:
                            toc.append((m.group(1),
                                        os.path.join(m.group(2), idx)))
        else:  # Not within toc section
            out += line

    with open(outfile, 'w') as fout:
        fout.write(out)
1114713
def plot_images(self, outfile):
        
        sum_struct = self.__images[0].sites
        for image in self.__images:
            for site_i in self.__relax_sites:
                sum_struct.append(PeriodicSite(image.sites[site_i].specie,
                                               image.sites[site_i].frac_coords,
                                               self.__images[0].lattice,
                                               to_unit_cell=True,
                                               coords_are_cartesian=False))
        sum_struct = Structure.from_sites(sum_struct, validate_proximity=False)
        p = Poscar(sum_struct)
        p.write_file(outfile)
721387
def extract_annotations_from(file, pattern)
      lineno = 0
      result = File.readlines(file, encoding: Encoding::BINARY).inject([]) do |list, line|
        lineno += 1
        next list unless line =~ pattern
        list << Annotation.new(lineno, $1, $2)
      end
      result.empty? ? {} : { file => result }
    end
795549
def scrape_file(self, file, encoding=None, base_url=None):
        '''
        '''
        elements = self.iter_elements(file, encoding=encoding)

        link_contexts = set()

        link_infos = self._element_walker.iter_links(elements)

        for link_info in link_infos:
            element_base_url = base_url

            if link_info.base_link:
                clean_base_url = clean_link_soup(link_info.base_link)

                if element_base_url and base_url:
                    element_base_url = urljoin_safe(
                        base_url, clean_base_url
                    ) or base_url

            if element_base_url:
                url = urljoin_safe(
                    element_base_url,
                    clean_link_soup(link_info.link),
                    allow_fragments=False
                )
            else:
                url = clean_link_soup(link_info.link)

            if url:
                link_contexts.add(LinkContext(
                    url,
                    inline=link_info.inline,
                    linked=link_info.linked,
                    link_type=link_info.link_type,
                    extra=link_info
                ))

        scrape_result = ScrapeResult(link_contexts, encoding)
        scrape_result['base_url'] = base_url
        return scrape_result
1269807
def to_meme(self):
        
        motif_id = self.id.replace(" ", "_")
        m = "MOTIF %s\n" % motif_id
        m += "BL   MOTIF %s width=0 seqs=0\n"% motif_id
        m += "letter-probability matrix: alength= 4 w= %s nsites= %s E= 0\n" % (len(self), np.sum(self.pfm[0]))
        m +="\n".join(["\t".join(["%s" % x for x in row]) for row in self.pwm])
        return m
308868
def fetch_followers(account_file, outfile, limit, do_loop):
    
    print('Fetching followers for accounts in %s' % account_file)
    niters = 1
    while True:
        outf = gzip.open(outfile, 'wt')
        for screen_name in iter_lines(account_file):
            timestamp = datetime.datetime.now().isoformat()
            print('collecting followers for', screen_name)
            followers = twutil.collect.followers_for_screen_name(screen_name, limit)
            if len(followers) > 0:
                outf.write('%s %s %s\n' % (timestamp, screen_name, ' '.join(followers)))
                outf.flush()
            else:
                print('unknown user', screen_name)
        outf.close()
        if not do_loop:
            return
        else:
            if niters == 1:
                outfile = '%s.%d' % (outfile, niters)
            else:
                outfile = outfile[:outfile.rindex('.')] + '.%d' % niters
            niters += 1
131357
def run_count(bam_file, dexseq_gff, stranded, out_file, data):
    
    assert file_exists(bam_file), "%s does not exist." % bam_file
    sort_order = bam._get_sort_order(bam_file, {})
    assert sort_order, "Cannot determine sort order of %s." % bam_file
    strand_flag = _strand_flag(stranded)
    assert strand_flag, "%s is not a valid strandedness value." % stranded
    if not dexseq_gff:
        logger.info("No DEXSeq GFF file was found, skipping exon-level counting.")
        return None
    elif not file_exists(dexseq_gff):
        logger.info("%s was not found, so exon-level counting is being "
                    "skipped." % dexseq_gff)
        return None

    dexseq_count = _dexseq_count_path()
    if not dexseq_count:
        logger.info("DEXseq is not installed, skipping exon-level counting.")
        return None

    if dd.get_aligner(data) == "bwa":
        logger.info("Can't use DEXSeq with bwa alignments, skipping exon-level counting.")
        return None

    sort_flag = "name" if sort_order == "queryname" else "pos"
    is_paired = bam.is_paired(bam_file)
    paired_flag = "yes" if is_paired else "no"
    bcbio_python = sys.executable

    if file_exists(out_file):
        return out_file
    cmd = ("{bcbio_python} {dexseq_count} -f bam -r {sort_flag} -p {paired_flag} "
           "-s {strand_flag} {dexseq_gff} {bam_file} {tx_out_file}")
    message = "Counting exon-level counts with %s and %s." % (bam_file, dexseq_gff)
    with file_transaction(data, out_file) as tx_out_file:
        do.run(cmd.format(**locals()), message)
    return out_file
758439
def _get_files(sample):
    
    analysis = sample.get("analysis")
    if analysis.lower() in ["variant", "snp calling", "variant2", "standard"]:
        return _get_files_variantcall(sample)
    elif analysis.lower() in ["rna-seq", "fastrna-seq"]:
        return _get_files_rnaseq(sample)
    elif analysis.lower() in ["smallrna-seq"]:
        return _get_files_srnaseq(sample)
    elif analysis.lower() in ["chip-seq"]:
        return _get_files_chipseq(sample)
    elif analysis.lower() in ["scrna-seq"]:
        return _get_files_scrnaseq(sample)
    else:
        return []
1207379
def show_patterned_file(dir_path, pattern=list(), filename_only=True):
        
        pattern = [i.lower() for i in pattern]
        if filename_only:
            def filter(winfile):
                for p in pattern:
                    if p in winfile.fname.lower():
                        return True
                return False
        else:
            def filter(winfile):
                for p in pattern:
                    if p in winfile.abspath.lower():
                        return True
                return False
        
        fc = FileCollection.from_path_by_criterion(
            dir_path, filter, keepboth=False)
        if filename_only:
            fc.sort_by("fname")
        else:
            fc.sort_by("abspath")
            
        table = {p: "<%s>" % p for p in pattern}
        lines = list()
        lines.append("Results:")
        for winfile in fc.iterfiles():
            lines.append("  %s" % winfile)
            
        if filename_only:
            lines.append("Above are all files that file name contains %s" % pattern)
        else:
            lines.append("Above are all files that abspath contains %s" % pattern)
            
        text = "\n".join(lines)
        print(text)
        with open("__show_patterned_file__.log", "wb") as f:
            f.write(text.encode("utf-8"))
615486
def write
      each_site_file do |item|
        item.write(dest) if regenerator.regenerate?(item)
      end
      regenerator.write_metadata
      Jekyll::Hooks.trigger :site, :post_write, self
    end
131247
def count_markers_samples(prefix, file_type):
    
    # The files that will need counting
    sample_file = None
    marker_file = None

    if file_type == "bfile":
        # Binary files (.bed, .bim and .fam)
        sample_file = prefix + ".fam"
        marker_file = prefix + ".bim"

    elif file_type == "file":
        # Pedfile (.ped and .map)
        sample_file = prefix + ".ped"
        marker_file = prefix + ".map"

    elif file_type == "tfile":
        # Transposed pedfile (.tped and .tfam)
        sample_file = prefix + ".tfam"
        marker_file = prefix + ".tped"

    # Counting (this may take some time)
    nb_samples = 0
    with open(sample_file, "r") as f:
        for line in f:
            nb_samples += 1

    nb_markers = 0
    with open(marker_file, "r") as f:
        for line in f:
            nb_markers += 1

    return nb_markers, nb_samples
1013463
def minimum_sys(cls, inherit_path):
    
    site_libs = set(cls.site_libs())
    for site_lib in site_libs:
      TRACER.log('Found site-library: %s' % site_lib)
    for extras_path in cls._extras_paths():
      TRACER.log('Found site extra: %s' % extras_path)
      site_libs.add(extras_path)
    site_libs = set(os.path.normpath(path) for path in site_libs)

    sys_path, sys_path_importer_cache = cls.minimum_sys_path(site_libs, inherit_path)
    sys_modules = cls.minimum_sys_modules(site_libs)

    return sys_path, sys_path_importer_cache, sys_modules
833882
def filter_single_reads_by_length(in_file, quality_format, min_length=20,
                                  out_file=None):
    
    logger.info("Removing reads in %s thare are less than %d bases."
                % (in_file, min_length))
    in_iterator = SeqIO.parse(in_file, quality_format)
    out_iterator = (record for record in in_iterator if
                    len(record.seq) > min_length)
    with file_transaction(out_file) as tmp_out_file:
        with open(tmp_out_file, "w") as out_handle:
            SeqIO.write(out_iterator, out_handle, quality_format)
    return out_file
589581
def match_file(pattern, filename):
    '''
    
    '''

    # Validate user data.

    if pattern is None:
        return None

    if os.stat(filename).st_size == 0:
        return None

    rexobj = REX(pattern, filename)

    rexpatstr = reformat_pattern(pattern)
    #print "rexpatstr: ", rexpatstr

    rexpat = re.compile(rexpatstr)
    rexobj.rex_patternstr = rexpatstr
    rexobj.rex_pattern = rexpat

    sfile = open(filename, 'r')
    data = sfile.read()
    sfile.close()

    line_count = 1
    for line in data.splitlines():
        mobj = rexpat.match(line)
        if mobj:
            populate_resobj(rexobj, mobj, line_count)

        line_count += 1

    return rexobj