Instruction:
Write a Python 3 function for
generating a sequence of combinations needed to compose given text.

Response:
def compose(self, text, minimal_clears=False, no_clears=False):
        '''
        Returns the sequence of combinations necessary to compose given text.

        If the text expression is not possible with the given layout an ComposeException is thrown.

        Iterate over the string, converting each character into a key sequence.
        Between each character, an empty combo is inserted to handle duplicate strings (and USB HID codes between characters)

        @param text: Input UTF-8 string
        @param minimal_clears: Set to True to minimize the number of code clears. False (default) includes a clear after every character.
        @param no_clears: Set to True to not add any code clears (useful for input sequences). False (default) to include code clears.

        @returns: Sequence of combinations needed to generate the given text string
        '''
        sequence = []
        clear = self.json_data['to_hid_keyboard']['0x00'] # No Event

        for char in text:
            # Make sure the composition element is available
            if char not in self.json_data['composition']:
                raise ComposeException("'{}' is not defined as a composition in the layout '{}'".format(char, self.name))

            # Lookup the sequence to handle this character
            lookup = self.json_data['composition'][char]

            # If using minimal clears, check to see if we need to re-use any codes
            # Only need to check the most recent addition with the first combo
            if sequence and set(tuple(lookup[0])) & set(tuple(sequence[-1])) and not no_clears:
                sequence.extend([[clear]])

            # Add to overall sequence
            sequence.extend(lookup)

            # Add empty combo for sequence splitting
            if not minimal_clears and not no_clears:
                # Blindly add a clear combo between characters
                sequence.extend([[clear]])

        # When using minimal clears, we still need to add a final clear
        if minimal_clears and not no_clears:
            sequence.extend([[clear]])

        return sequence


Instruction:
Can you tell what is the following Python 3 function doing
def main(self):
        logging.info('Starting {at} analysis pipeline'.format(at=self.analysistype))
        # Create the objects to be used in the analyses
        objects = Objectprep(self)
        objects.objectprep()
        self.runmetadata = objects.samples
        self.threads = int(self.cpus / len(self.runmetadata.samples)) if self.cpus / len(self.runmetadata.samples) > 1 \
            else 1
        if self.genesippr:
            # Run the genesippr analyses
            self.analysistype = 'genesippr'
            self.targetpath = os.path.join(self.reffilepath, self.analysistype)
            Sippr(inputobject=self,
                  cutoff=0.90,
                  averagedepth=5)
            # Create the reports
            self.reports = Reports(self)
            Reports.reporter(self.reports)
        if self.sixteens:
            # Run the 16S analyses
            SixteensFull(args=self,
                         pipelinecommit=self.commit,
                         startingtime=self.starttime,
                         scriptpath=self.homepath,
                         analysistype='sixteens_full',
                         cutoff=0.985)
        if self.closestreference:
            self.pipeline = True
            mash.Mash(inputobject=self,
                      analysistype='mash')
        if self.rmlst:
            rmlst = MLSTSippr(args=self,
                              pipelinecommit=self.commit,
                              startingtime=self.starttime,
                              scriptpath=self.homepath,
                              analysistype='rMLST',
                              cutoff=1.0,
                              pipeline=True)
            rmlst.runner()
        if self.resistance:
            # ResFinding
            res = Resistance(args=self,
                             pipelinecommit=self.commit,
                             startingtime=self.starttime,
                             scriptpath=self.homepath,
                             analysistype='resfinder',
                             cutoff=0.7,
                             pipeline=False,
                             revbait=True)
            res.main()
        if self.virulence:
            self.genus_specific()
            Virulence(args=self,
                      pipelinecommit=self.commit,
                      startingtime=self.starttime,
                      scriptpath=self.homepath,
                      analysistype='virulence',
                      cutoff=0.95,
                      pipeline=False,
                      revbait=True)
        if self.gdcs:
            self.genus_specific()
            # Run the GDCS analysis
            self.analysistype = 'GDCS'
            self.targetpath = os.path.join(self.reffilepath, self.analysistype)
            Sippr(inputobject=self,
                  cutoff=0.95,
                  k=self.gdcs_kmer_size)
            # Create the reports
            self.reports = Reports(self)
            Reports.gdcsreporter(self.reports)
        if self.mlst:
            self.genus_specific()
            mlst = MLSTSippr(args=self,
                             pipelinecommit=self.commit,
                             startingtime=self.starttime,
                             scriptpath=self.homepath,
                             analysistype='MLST',
                             cutoff=1.0,
                             pipeline=True)
            mlst.runner()
        # Serotyping
        if self.serotype:
            self.genus_specific()
            SeroSippr(args=self,
                      pipelinecommit=self.commit,
                      startingtime=self.starttime,
                      scriptpath=self.homepath,
                      analysistype='serosippr',
                      cutoff=0.90,
                      pipeline=True)
        # Point mutation detection
        if self.pointfinder:
            self.genus_specific()
            PointSippr(args=self,
                       pipelinecommit=self.commit,
                       startingtime=self.starttime,
                       scriptpath=self.homepath,
                       analysistype='pointfinder',
                       cutoff=0.85,
                       pipeline=True,
                       revbait=True)
        if self.user_genes:
            custom = CustomGenes(args=self,
                                 kmer_size=self.kmer_size,
                                 allow_soft_clips=self.allow_soft_clips)
            custom.main()
        # Print the metadata
        MetadataPrinter(self)

Response: Run the analyses in the correct order.


Instruction:
Can you create a Python 3 function that
determines if the closest refseq genus is known - i. e. if the MASH analyses are performed and if required is True then perform the analyses if required is False then perform the analyses if required is True.

Response:
def genus_specific(self):
        """
        For genus-specific targets, MLST and serotyping, determine if the closest refseq genus is known - i.e. if 16S
        analyses have been performed. Perform the analyses if required
        """
        # Initialise a variable to store whether the necessary analyses have already been performed
        closestrefseqgenus = False
        for sample in self.runmetadata.samples:
            if sample.general.bestassemblyfile != 'NA':
                try:
                    closestrefseqgenus = sample.general.closestrefseqgenus
                except AttributeError:
                    pass
        # Perform the 16S analyses as required
        if not closestrefseqgenus:
            logging.info('Must perform MASH analyses to determine genera of samples')
            self.pipeline = True
            # Run the analyses
            mash.Mash(self, 'mash')


Instruction:
How would you code a function in Python 3 to
tell iTunes to pause

Response:
def pause():
	"""Tell iTunes to pause"""

	if not settings.platformCompatible():
		return False

	(output, error) = subprocess.Popen(["osascript", "-e", PAUSE], stdout=subprocess.PIPE).communicate()


Instruction:
Can you generate the documentation for the following Python 3 function
def resume():

	if not settings.platformCompatible():
		return False

	(output, error) = subprocess.Popen(["osascript", "-e", RESUME], stdout=subprocess.PIPE).communicate()

Response: Tell iTunes to resume


Instruction:
Write a Python 3 script to
tell iTunes to skip a song

Response:
def skip():
	"""Tell iTunes to skip a song"""

	if not settings.platformCompatible():
		return False

	(output, error) = subprocess.Popen(["osascript", "-e", SKIP], stdout=subprocess.PIPE).communicate()


Instruction:
How would you implement a function in Python 3 that
tells iTunes to play a given song artist or album.

Response:
def play(song, artist=None, album=None):
	"""Tells iTunes to play a given song/artist/album - MACOSX ONLY"""

	if not settings.platformCompatible():
		return False

	if song and not artist and not album:
		(output, error) = subprocess.Popen(["osascript", "-e", DEFAULT_ITUNES_PLAY % (song, song, song)], stdout=subprocess.PIPE).communicate()
		if output:
			speech.speak("Playing " + output)
		else:
			speech.speak("Unable to find " + song + " in your library.")

	elif song and artist and not album:
		(output, error) = subprocess.Popen(["osascript", "-e", ITUNES_SONG_AND_ARTIST % (song, artist, song, artist)], stdout=subprocess.PIPE).communicate()
		if output:
			speech.speak("Playing " + output)
		else:
			speech.speak("Unable to find " + song + " in your library.")

	elif album and artist and not song:
		(output, error) = subprocess.Popen(["osascript", "-e", ITUNES_ALBUM_AND_ARTIST % (artist, album)], stdout=subprocess.PIPE).communicate()
		if output:
			speech.speak("Playing " + output)
		else:
			speech.speak("Unable to find " + song + " in your library.")

	elif album and not artist and not song:
		(output, error) = subprocess.Popen(["osascript", "-e", ITUNES_ALBUM % (album)], stdout=subprocess.PIPE).communicate()
		if output:
			speech.speak("Playing " + output)
		else:
			speech.speak("Unable to find " + song + " in your library.")

	elif artist and not album and not song:
		(output, error) = subprocess.Popen(["osascript", "-e", ITUNES_ARTIST % (artist)], stdout=subprocess.PIPE).communicate()
		if output:
			speech.speak("Playing " + output)
		else:
			speech.speak("Unable to find " + song + " in your library.")


Instruction:
Can you generate a brief explanation for the following Python 3 code
def service_provider(*services):

    def real_decorator(clazz):
        instance = clazz()
        for service in services:
            global_lookup.add(service, instance)
        return clazz

    return real_decorator

Response: This is a class decorator that declares a class to provide a set of services.


Instruction:
Create a Python 3 function for
syllabifying the given word whether simplex or complex.

Response:
def syllabify(word):
    '''Syllabify the given word, whether simplex or complex.'''
    compound = bool(re.search(r'(-| |=)', word))
    syllabify = _syllabify_compound if compound else _syllabify_simplex
    syllabifications = list(syllabify(word))

    for word, rules in rank(syllabifications):
        # post-process
        word = str(replace_umlauts(word, put_back=True))
        rules = rules[1:]

        yield word, rules


Instruction:
How would you explain what the following Python 3 function does
def apply_T6(word):
    '''If a VVV-sequence contains a long vowel, there is a syllable boundary
    between it and the third vowel, e.g. [kor.ke.aa], [yh.ti.öön], [ruu.an],
    [mää.yt.te].'''
    WORD = word
    offset = 0

    for vvv in vvv_sequences(WORD):
        seq = vvv.group(2)
        j = 2 if is_long(seq[:2]) else 1 if is_long(seq[1:]) else 0

        if j:
            i = vvv.start(2) + j + offset
            WORD = WORD[:i] + '.' + WORD[i:]
            offset += 1

    RULE = ' T6' if word != WORD else ''

    return WORD, RULE

Response: This function applies T6 to a word.


Instruction:
Can you generate the documentation for the following Python 3 function
def apply_T7(word):
    '''If a VVV-sequence does not contain a potential /i/-final diphthong,
    there is a syllable boundary between the second and third vowels, e.g.
    [kau.an], [leu.an], [kiu.as].'''
    WORD = word
    offset = 0

    for vvv in vvv_sequences(WORD):
        i = vvv.start(2) + 2 + offset
        WORD = WORD[:i] + '.' + WORD[i:]
        offset += 1

    RULE = ' T7' if word != WORD else ''

    return WORD, RULE

Response: A function to apply T7 to a VVV - sequence.


Instruction:
Can you write a function in Python 3 where it
splits the syllables that do not take primary stress.

Response:
def apply_T8(word):
    '''Split /ie/, /uo/, or /yö/ sequences in syllables that do not take
    primary stress.'''
    WORD = word
    offset = 0

    for vv in tail_diphthongs(WORD):
        i = vv.start(1) + 1 + offset
        WORD = WORD[:i] + '.' + WORD[i:]
        offset += 1

    RULE = ' T8' if word != WORD else ''

    return WORD, RULE


Instruction:
Can you generate the documentation for the following Python 3 function
def wsp(word):
    '''Return the number of unstressed heavy syllables.'''
    HEAVY = r'[ieaAoO]{1}[\.]*(u|y)[^ieaAoO]+(\.|$)'

    # # if the word is not monosyllabic, lop off the final syllable, which is
    # # extrametrical
    # if '.' in word:
    #     word = word[:word.rindex('.')]

    # gather the indices of syllable boundaries
    delimiters = [i for i, char in enumerate(word) if char == '.']

    if len(delimiters) % 2 != 0:
        delimiters.append(len(word))

    unstressed = []

    # gather the indices of unstressed positions
    for i, d in enumerate(delimiters):
        if i % 2 == 0:
            unstressed.extend(range(d + 1, delimiters[i + 1]))

    # find the number of unstressed heavy syllables
    heavies = re.finditer(HEAVY, word)
    violations = sum(1 for m in heavies if m.start(0) in unstressed)

    return violations

Response: Return the number of unstressed heavy syllables.


Instruction:
Can you tell what is the following Python 3 function doing
def pk_prom(word):
    '''Return the number of stressed light syllables.'''
    LIGHT = r'[ieaAoO]{1}[\.]*(u|y)(\.|$)'

    # # if the word is not monosyllabic, lop off the final syllable, which is
    # # extrametrical
    # if '.' in word:
    #     word = word[:word.rindex('.')]

    # gather the indices of syllable boundaries
    delimiters = [0, ] + [i for i, char in enumerate(word) if char == '.']

    if len(delimiters) % 2 != 0:
        delimiters.append(len(word))

    stressed = []

    # gather the indices of stressed positions
    for i, d in enumerate(delimiters):
        if i % 2 == 0:
            stressed.extend(range(d + 1, delimiters[i + 1]))

    # find the number of stressed light syllables
    heavies = re.finditer(LIGHT, word)
    violations = sum(1 for m in heavies if m.start(1) in stressed)

    return violations

Response: Return the number of stressed light syllables.


Instruction:
Explain what the following Python 3 code does
def ext(self, extension):
        new_pathq = copy(self)
        new_pathq._pattern.ext = extension
        return new_pathq

Response: Match files with an extension - e. g. js txt


Instruction:
Given the following Python 3 function, write the documentation
def apply_T8(word):
    '''Split /ie/ sequences in syllables that do not take primary stress.'''
    WORD = word
    offset = 0

    for ie in ie_sequences(WORD):
        i = ie.start(1) + 1 + offset
        WORD = WORD[:i] + '.' + WORD[i:]
        offset += 1

    RULE = ' T8' if word != WORD else ''

    return WORD, RULE

Response: Split / ie sequences in syllables that do not take primary stress.


Instruction:
Make a summary of the following Python 3 code
def apply_T9(word):
    '''Split /iu/ sequences that do not appear in the first, second, or final
    syllables.'''
    WORD = word
    index = 0
    offset = 0

    for iu in iu_sequences(WORD):
        if iu.start(1) != index:
            i = iu.start(1) + 1 + offset
            WORD = WORD[:i] + '.' + WORD[i:]
            index = iu.start(1)
            offset += 1

    RULE = ' T9' if word != WORD else ''

    return WORD, RULE

Response:
Split / iu sequences that do not appear in the first second or final
    syllables.


Instruction:
Write a Python 3 function that can
return a class from a string including module and class.

Response:
def import_class(import_str):
    """Returns a class from a string including module and class."""
    mod_str, _sep, class_str = import_str.rpartition('.')
    try:
        __import__(mod_str)
        return getattr(sys.modules[mod_str], class_str)
    except (ValueError, AttributeError):
        raise ImportError('Class %s cannot be found (%s)' %
                          (class_str,
                           traceback.format_exception(*sys.exc_info())))


Instruction:
Create a Python 3 function to
try to import an object from the default namespace and returns an instance of it

Response:
def import_object_ns(name_space, import_str, *args, **kwargs):
    """Tries to import object from default namespace.

    Imports a class and return an instance of it, first by trying
    to find the class in a default namespace, then failing back to
    a full path if not found in the default namespace.
    """
    import_value = "%s.%s" % (name_space, import_str)
    try:
        return import_class(import_value)(*args, **kwargs)
    except ImportError:
        return import_class(import_str)(*args, **kwargs)


Instruction:
Here you have a function in Python 3, explain what it does
def install_virtualbox(distribution, force_setup=False):

    if 'ubuntu' in distribution:
        with hide('running', 'stdout'):
            sudo('DEBIAN_FRONTEND=noninteractive apt-get update')
            sudo("sudo DEBIAN_FRONTEND=noninteractive apt-get -y -o "
                    "Dpkg::Options::='--force-confdef' "
                    "-o Dpkg::Options::='--force-confold' upgrade --force-yes")
            install_ubuntu_development_tools()
            apt_install(packages=['dkms',
                                  'linux-headers-generic',
                                  'build-essential'])
            sudo('wget -q '
                 'https://www.virtualbox.org/download/oracle_vbox.asc -O- |'
                 'sudo apt-key add -')

            os = lsb_release()
            apt_string = ' '.join(
                ['deb',
                 'http://download.virtualbox.org/virtualbox/debian',
                 '%s contrib' % os['DISTRIB_CODENAME']])

            apt_add_repository_from_apt_string(apt_string, 'vbox.list')

            apt_install(packages=['virtualbox-5.0'])

            loaded_modules = sudo('lsmod')

            if 'vboxdrv' not in loaded_modules or force_setup:

                if 'Vivid Vervet' in run('cat /etc/os-release'):
                    sudo('systemctl start vboxdrv')
                else:
                    sudo('/etc/init.d/vboxdrv start')

            sudo('wget -c '
                 'http://download.virtualbox.org/virtualbox/5.0.4/'
                 'Oracle_VM_VirtualBox_Extension_Pack-5.0.4-102546.vbox-extpack') # noqa

            sudo('VBoxManage extpack install --replace '
                 'Oracle_VM_VirtualBox_Extension_Pack-5.0.4-102546.vbox-extpack')

Response: installs the virtualbox if the distribution is ubuntu


Instruction:
Can you generate a brief explanation for the following Python 3 code
def runner(self):
        printtime('Starting mashsippr analysis pipeline', self.starttime)
        if not self.pipeline:
            # Create the objects to be used in the analyses
            objects = Objectprep(self)
            objects.objectprep()
            self.runmetadata = objects.samples
        # Run the analyses
        Mash(self, self.analysistype)

Response: Run the necessary methods in the correct order for the mashsippr analysis pipeline


Instruction:
Explain what the following Python 3 code does
def cree_widgets(self):
        for t in self.FIELDS:
            if type(t) is str:
                attr, kwargs = t, {}
            else:
                attr, kwargs = t[0], t[1].copy()
            self.champs.append(attr)
            is_editable = kwargs.pop("is_editable", self.is_editable)
            args = [self.acces[attr], is_editable]
            with_base = kwargs.pop("with_base", False)
            if with_base:
                args.append(self.acces.base)

            if 'with_label' in kwargs:
                label = kwargs.pop('with_label')
            else:
                label = ASSOCIATION[attr][0]
            if kwargs:
                w = ASSOCIATION[attr][3](*args, **kwargs)
            else:
                w = ASSOCIATION[attr][3](*args)

            self.widgets[attr] = (w, label)

Response: Create widgets and store them in self. widgets


Instruction:
Here you have a function in Python 3, explain what it does
def preservesurrogates(s):
    if not isinstance(s, six.text_type):
        raise TypeError(u"String to split must be of type 'unicode'!")
    surrogates_regex_str = u"[{0}-{1}][{2}-{3}]".format(HIGH_SURROGATE_START,
                                                        HIGH_SURROGATE_END,
                                                        LOW_SURROGATE_START,
                                                        LOW_SURROGATE_END)
    surrogates_regex = re.compile(u"(?:{0})|.".format(surrogates_regex_str))
    return surrogates_regex.findall(s)

Response: Function for splitting a string into a list of characters preserving surrogates.


Instruction:
Can you tell what is the following Python 3 function doing
def _unichr(i):
    if not isinstance(i, int):
        raise TypeError
    try:
        return six.unichr(i)
    except ValueError:
        # Workaround the error "ValueError: unichr() arg not in range(0x10000) (narrow Python build)"
        return struct.pack("i", i).decode("utf-32")

Response: Helper function for taking a Unicode scalar value and returning a Unicode character.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _uax44lm2transform(s):
    result = s

    # For the regex, we are using lookaround assertions to verify that there is a word character immediately before (the
    # lookbehind assertion (?<=\w)) and immediately after (the lookahead assertion (?=\w)) the hyphen, per the "medial
    # hyphen" definition that it is a hyphen occurring immediately between two letters.
    medialhyphen = re.compile(r"(?<=\w)-(?=\w)")
    whitespaceunderscore = re.compile(r"[\s_]", re.UNICODE)

    # Ok to hard code, this name should never change: https://www.unicode.org/policies/stability_policy.html#Name
    if result != "HANGUL JUNGSEONG O-E":
        result = medialhyphen.sub("", result)
    result = whitespaceunderscore.sub("", result)
    return result.lower()

Response: This function transforms a string into a UAX44 - LM2 string.


Instruction:
Can you generate the documentation for the following Python 3 function
def _to_unicode_scalar_value(s):
    if len(s) == 1:
        return ord(s)
    elif len(s) == 2:
        return (ord(s[0]) - 0xD800) * 0x0400 + (ord(s[1]) - 0xDC00) + 0x10000
    else:
        raise ValueError

Response: Helper function to convert a Unicode scalar value from a string to a Unicode scalar value.


Instruction:
Given the following Python 3 function, write the documentation
def _get_nr_prefix(i):
    for lookup_range, prefix_string in _nr_prefix_strings.items():
        if i in lookup_range:
            return prefix_string
    raise ValueError("No prefix string associated with {0}!".format(i))

Response: Helper function for looking up the derived name prefix associated with a Unicode scalar value.


Instruction:
Here you have a function in Python 3, explain what it does
def casefold(s, fullcasefold=True, useturkicmapping=False):
    if not isinstance(s, six.text_type):
        raise TypeError(u"String to casefold must be of type 'unicode'!")
    lookup_order = "CF"
    if not fullcasefold:
        lookup_order = "CS"
    if useturkicmapping:
        lookup_order = "T" + lookup_order
    return u"".join([casefold_map.lookup(c, lookup_order=lookup_order) for c in preservesurrogates(s)])

Response: This function will perform a case folding on the input string s.


Instruction:
Can you generate the documentation for the following Python 3 function
def _build_unicode_character_database(self):
        filename = "UnicodeData.txt"
        current_dir = os.path.abspath(os.path.dirname(__file__))
        tag = re.compile(r"<\w+?>")
        with codecs.open(os.path.join(current_dir, filename), mode="r", encoding="utf-8") as fp:
            for line in fp:
                if not line.strip():
                    continue
                data = line.strip().split(";")
                # Replace the start/end range markers with their proper derived names.
                if data[1].endswith((u"First>", u"Last>")) and _is_derived(int(data[0], 16)):
                    data[1] = _get_nr_prefix(int(data[0], 16))
                    if data[1].startswith("HANGUL SYLLABLE"):  # For Hangul syllables, use naming rule NR1
                        data[1] += _get_hangul_syllable_name(int(data[0], 16))
                    else:  # Others should use naming rule NR2
                        data[1] += data[0]
                data[3] = int(data[3])  # Convert the Canonical Combining Class value into an int.
                if data[5]:  # Convert the contents of the decomposition into characters, preserving tag info.
                    data[5] = u" ".join([_hexstr_to_unichr(s) if not tag.match(s) else s for s in data[5].split()])
                for i in [6, 7, 8]:  # Convert the decimal, digit and numeric fields to either ints or fractions.
                    if data[i]:
                        if "/" in data[i]:
                            data[i] = Fraction(data[i])
                        else:
                            data[i] = int(data[i])
                for i in [12, 13, 14]:  # Convert the uppercase, lowercase and titlecase fields to characters.
                    if data[i]:
                        data[i] = _hexstr_to_unichr(data[i])
                lookup_name = _uax44lm2transform(data[1])
                uc_data = UnicodeCharacter(u"U+" + data[0], *data[1:])
                self._unicode_character_database[int(data[0], 16)] = uc_data
                self._name_database[lookup_name] = uc_data
        # Fill out the "compressed" ranges of UnicodeData.txt i.e. fill out the remaining characters per the Name
        # Derivation Rules.  See the Unicode Standard, ch. 4, section 4.8, Unicode Name Property
        for lookup_range, prefix_string in _nr_prefix_strings.items():
            exemplar = self._unicode_character_database.__getitem__(lookup_range[0])
            for item in lookup_range:
                hex_code = _padded_hex(item)
                new_name = prefix_string
                if prefix_string.startswith("HANGUL SYLLABLE"):  # For Hangul, use naming rule NR1
                    new_name += _get_hangul_syllable_name(item)
                else:  # Everything else uses naming rule NR2
                    new_name += hex_code
                uc_data = exemplar._replace(code=u"U+" + hex_code, name=new_name)
                self._unicode_character_database[item] = uc_data
                self._name_database[_uax44lm2transform(new_name)] = uc_data

Response: Function for parsing the Unicode Character Database and generating a lookup table.


Instruction:
Write a Python 3 function for
functioning for retrieving the UnicodeCharacter associated with a name.

Response:
def lookup_by_name(self, name):
        """
        Function for retrieving the UnicodeCharacter associated with a name.  The name lookup uses the loose matching
        rule UAX44-LM2 for loose matching.  See the following for more info:

        https://www.unicode.org/reports/tr44/#UAX44-LM2

        For example:

        ucd = UnicodeData()
        ucd.lookup_by_name("LATIN SMALL LETTER SHARP S") -> UnicodeCharacter(name='LATIN SMALL LETTER SHARP S',...)
        ucd.lookup_by_name("latin_small_letter_sharp_s") -> UnicodeCharacter(name='LATIN SMALL LETTER SHARP S',...)

        :param name: Name of the character to look up.
        :return: UnicodeCharacter instance with data associated with the character.
        """
        try:
            return self._name_database[_uax44lm2transform(name)]
        except KeyError:
            raise KeyError(u"Unknown character name: '{0}'!".format(name))


Instruction:
How would you explain what the following Python 3 function does
def lookup_by_partial_name(self, partial_name):
        for k, v in self._name_database.items():
            if _uax44lm2transform(partial_name) in k:
                yield v

Response: This method returns a generator that yields UnicodeCharacter instances for the given partial name.


Instruction:
Make a summary of the following Python 3 code
def _load_unicode_block_info(self):
        filename = "Blocks.txt"
        current_dir = os.path.abspath(os.path.dirname(__file__))
        with codecs.open(os.path.join(current_dir, filename), mode="r", encoding="utf-8") as fp:
            for line in fp:
                if not line.strip() or line.startswith("#"):
                    continue  # Skip empty lines or lines that are comments (comments start with '#')
                # Format: Start Code..End Code; Block Name
                block_range, block_name = line.strip().split(";")
                start_range, end_range = block_range.strip().split("..")
                self._unicode_blocks[six.moves.range(int(start_range, 16), int(end_range, 16) + 1)] = block_name.strip()

Response: Function for parsing Unicode character set info from the Unicode CharacterSet Database and generating a lookup table.


Instruction:
How would you explain what the following Python 3 function does
def _build_casefold_map(self):
        self._casefold_map = defaultdict(dict)
        filename = "CaseFolding.txt"
        current_dir = os.path.abspath(os.path.dirname(__file__))
        with codecs.open(os.path.join(current_dir, filename), mode="r", encoding="utf-8") as fp:
            for line in fp:
                if not line.strip() or line.startswith("#"):
                    continue  # Skip empty lines or lines that are comments (comments start with '#')
                code, status, mapping, name = line.split(";")
                src = _hexstr_to_unichr(code)
                target = u"".join([_hexstr_to_unichr(c) for c in mapping.strip().split()])
                self._casefold_map[status.strip()][src] = target

Response:
Function for parsing the Unicode Character
        database and generating a lookup table.


Instruction:
Can you write a function in Python 3 where it
functions to lookup a character in the casefold map.

Response:
def lookup(self, c, lookup_order="CF"):
        """
        Function to lookup a character in the casefold map.

        The casefold map has four sub-tables, the 'C' or common table, the 'F' or
        full table, the 'S' or simple table and the 'T' or the Turkic special
        case table.  These tables correspond to the statuses defined in the
        CaseFolding.txt file.  We can specify the order of the tables to use for
        performing the lookup by the lookup_order parameter.

        Per the usage specified in the CaseFolding.txt file, we can use the 'C'
        and 'S' tables for doing a simple case fold.  To perform a full case
        fold, we can use the 'C' and 'F' tables.  The default behavior for this
        function is a full case fold (lookup_order="CF").

        :param c: character to lookup
        :param lookup_order:
        """
        if not isinstance(c, six.text_type):
            raise TypeError(u"Character to lookup must be of type 'unicode'!")
        for d in lookup_order:
            try:
                return self._casefold_map[d][c]
            except KeyError:
                pass
        return c


Instruction:
Explain what the following Python 3 code does
def load_from_json(data):
        if isinstance(data, str):
            data = json.loads(data)
        applications = [
            ApplicationResponse.load_from_json(a) for a in data['applications']
        ] if data['applications'] is not None else []
        return RegistryResponse(
            data['query_uri'], data['success'],
            data['has_references'], data['count'], applications
        )

Response: Load a RegistryResponse from a dictionary or a string.


Instruction:
Make a summary of the following Python 3 code
def load_from_json(data):
        if isinstance(data, str):
            data = json.loads(data)
        items = [Item.load_from_json(a) for a in data['items']] if data['items'] is not None else []
        return ApplicationResponse(
            data['title'], data['uri'], data['service_url'],
            data['success'], data['has_references'], data['count'], items
        )

Response: Load a : class : ApplicationResponse from a dictionary or string.


Instruction:
Can you write a function in Python 3 where it
loads a : class : Item from a dictionary ot string

Response:
def load_from_json(data):
        """
        Load a :class:`Item` from a dictionary ot string (that will be parsed
        as json)
        """
        if isinstance(data, str):
            data = json.loads(data)
        return Item(data['title'], data['uri'])


Instruction:
Given the following Python 3 function, write the documentation
def apply_T11(word):
    '''If a VVV sequence contains a /u, y/-final diphthong and the third vowel
    is /i/, there is a syllable boundary between the diphthong and /i/.'''
    WORD = word
    offset = 0

    for vvv in t11_vvv_sequences(WORD):
        # i = vvv.start(1) + (1 if vvv.group(1).startswith('i') else 2) + offset
        i = vvv.start(1) + (1 if vvv.group(1)[-1] in 'uy' else 2) + offset
        WORD = WORD[:i] + '.' + WORD[i:]
        offset += 1

    RULE = ' T11' if word != WORD else ''

    return WORD, RULE

Response: Apply T11 to a word.


Instruction:
Can you tell what is the following Python 3 function doing
def _syllabify_simplex(word):
    '''Syllabify the given word.'''
    word, rules = apply_T1(word)

    if re.search(r'[^ieAyOauo]*([ieAyOauo]{2})[^ieAyOauo]*', word):
        word, T2 = apply_T2(word)
        word, T8 = apply_T8(word)
        word, T9 = apply_T9(word)
        rules += T2 + T8 + T9

        # T4 produces variation
        syllabifications = list(apply_T4(word))

    else:
        syllabifications = [(word, ''), ]

    for word, rule in syllabifications:
        RULES = rules + rule

        if re.search(r'[ieAyOauo]{3}', word):
            word, T6 = apply_T6(word)
            word, T5 = apply_T5(word)
            word, T10 = apply_T10(word)
            word, T7 = apply_T7(word)
            word, T2 = apply_T2(word)
            RULES += T5 + T6 + T10 + T7 + T2

        RULES = RULES or ' T0'  # T0 means no rules have applied

        yield word, RULES

Response: Syllabify the given word.


Instruction:
Can you tell what is the following Python 3 function doing
def T1(word):
    '''Insert a syllable boundary in front of every CV sequence.'''
    # split consonants and vowels: 'balloon' -> ['b', 'a', 'll', 'oo', 'n']
    WORD = [i for i in re.split(r'([ieaouäöy]+)', word, flags=FLAGS) if i]

    # keep track of which sub-rules are applying
    sub_rules = set()

    # a count divisible by 2 indicates an even syllable
    count = 1

    for i, v in enumerate(WORD):

        # T1B
        # If there is a consonant cluster word-initially, the entire cluster
        # forms the onset of the first syllable:
        # CCV > #CCV
        if i == 0 and phon.is_consonant(v[0]):
            sub_rules.add('b')

        elif phon.is_consonant(v[0]):
            count += 1

            # True if the current syllable is unstressed, else False
            unstressed = count % 2 == 0

            # T1C
            # If there is a consonant cluster word-finally, the entire cluster
            # forms the coda of the final syllable:
            # VCC# > VCC#
            if i + 1 == len(WORD):
                sub_rules.add('c')

            # T1D
            # If there is a bare "Finnish" consonant cluster word-medially and
            # the previous syllable receives stress, the first consonant of the
            # cluster forms the coda of the previous syllable (to create a
            # heavy syllable); otherwise, the whole cluster forms the onset of
            # the current syllable (this is the /kr/ rule):
            # 'VCCV > 'VC.CV,  VCCV > V.CCV
            elif phon.is_cluster(v):
                sub_rules.add('d')
                WORD[i] = v[0] + '.' + v[1:] if unstressed else '.' + v

            elif phon.is_cluster(v[1:]):

                # T1E (optional)
                # If there is a word-medial "Finnish" consonant cluster that is
                # preceded by a sonorant consonant, if the previous syllable
                # receives stress, the sonorant consonant and the first
                # consonant of the cluster form the coda of the previous
                # syllable, and the remainder of the cluster forms the onset of
                # the current syllable:
                # 'VlCC > VlC.C
                if phon.is_sonorant(v[0]) and unstressed:
                    sub_rules.add('e')
                    WORD[i] = v[:2] + '.' + v[2:]

                # T1F
                # If there is a word-medial "Finnish" cluster that follows a
                # consonant, that first consonant forms the coda of the
                # previous syllable, and the cluster forms the onset of the
                # current syllable:
                # VCkr > VC.kr
                else:
                    sub_rules.add('f')
                    WORD[i] = v[0] + '.' + v[1:]

            # T1A
            # There is a syllable boundary in front of every CV sequence:
            # VCV > V.CV, CCV > C.CV
            else:
                WORD[i] = v[:-1] + '.' + v[-1]
                sub_rules.add('a')

    WORD = ''.join(WORD)
    rules = '' if word == WORD else ' T1'  # + ''.join(sub_rules)  # TODO: sort

    return WORD, rules

Response: T1 - > T1.


Instruction:
Write a Python 3 function that can
split any VV sequence that is not a genuine diphthong or long vowel. Eg., [ ta. e ko. et. taa ]. This rule can apply within VVV + sequences.

Response:
def T2(word, rules):
    '''Split any VV sequence that is not a genuine diphthong or long vowel.
    E.g., [ta.e], [ko.et.taa]. This rule can apply within VVV+ sequences.'''
    WORD = word
    offset = 0

    for vv in vv_sequences(WORD):
        seq = vv.group(1)

        if not phon.is_diphthong(seq) and not phon.is_long(seq):
            i = vv.start(1) + 1 + offset
            WORD = WORD[:i] + '.' + WORD[i:]
            offset += 1

    rules += ' T2' if word != WORD else ''

    return WORD, rules


Instruction:
Make a summary of the following Python 3 code
def T8(word, rules):
    '''Join /ie/, /uo/, or /yö/ sequences in syllables that take primary
    stress.'''
    WORD = word

    try:
        vv = tail_diphthongs(WORD)
        i = vv.start(1) + 1
        WORD = WORD[:i] + word[i + 1:]

    except AttributeError:
        pass

    rules += ' T8' if word != WORD else ''

    return WORD, rules

Response:
Join IE UO or Yö or Yö sequences in syllables that take primary
 stress.


Instruction:
Can you create a Python 3 function that
returns the number of stressed light syllables.

Response:
def pk_prom(word):
    '''Return the number of stressed light syllables.'''
    violations = 0
    stressed = []

    for w in extract_words(word):
        stressed += w.split('.')[2:-1:2]  # odd syllables, excl. word-initial

    # (CVV = light)
    for syll in stressed:
        if phon.is_vowel(syll[-1]):
            violations += 1

    # # (CVV = heavy)
    # for syll in stressed:
    #     if re.search(
    #             ur'^[^ieaouäöy]*[ieaouäöy]{1}$',  syll, flags=re.I | re.U):
    #         violations += 1

    return violations


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ansi_format_iter( self, x_start=0, y_start=0, width=None, height=None, frame=0, columns=1, downsample=1 ):

        image = self.get_image()
        frames = []
        frame_count = 1 if not hasattr( image, 'n_frames' ) else image.n_frames
        if isinstance( frame, int ):
            assert frame in range( 0, frame_count )
            frames = [frame]
        else:
            frames = [f for f in frame if f in range( 0, frame_count )]

        if not width:
            width = image.size[0]-x_start
        if not height:
            height = image.size[1]-y_start

        if image.mode == 'P':
            palette = from_palette_bytes( image.getpalette() )

            def data_fetch( x, y, fr ):
                if fr not in range( 0, frame_count ):
                    return Transparent()
                if not ((0 <= x < image.size[0]) and (0 <= y < image.size[1])):
                    return Transparent()
                image.seek( fr )
                return palette[image.getpixel( (x, y) )]

            for x in ansi.format_image_iter( data_fetch, x_start, y_start, width, height, frames, columns, downsample ):
                yield x
        return

Response: Return an iterator over the ANSI escape sequences that can be used to render the image.


Instruction:
Can you generate the documentation for the following Python 3 function
def ansi_format_iter( self, x_start=0, y_start=0, width=None, height=None, frame=0, columns=1, downsample=1, frame_index=None, frame_flip_v=0, frame_flip_h=0 ):

        assert x_start in range( 0, self.width )
        assert y_start in range( 0, self.height )
        if frame_index is not None:
            fn_index = lambda fr: mrc.property_get( frame_index, fr )
        else:
            fn_index = lambda fr: fr if fr in range( 0, self.frame_count ) else None
        fn_flip_v = lambda fr: mrc.property_get( frame_flip_v, fr )
        fn_flip_h = lambda fr: mrc.property_get( frame_flip_h, fr )

        frames = []
        try:
            frame_iter = iter( frame )
            frames = [f for f in frame_iter]
        except TypeError:
            frames = [frame]

        if not width:
            width = self.width-x_start
        if not height:
            height = self.height-y_start

        stride = width*height

        def data_fetch( x, y, fr_obj ):
            fr = fn_index( fr_obj )
            if fr is None:
                return Transparent()
            if not ((0 <= x < self.width) and (0 <= y < self.height)):
                return Transparent()
            if fn_flip_h( fr_obj ):
                x = self.width - x - 1
            if fn_flip_v( fr_obj ):
                y = self.height - y - 1
            index = self.width*y + x
            p = self.source[stride*fr+index]
            if self.mask:
                p = p if self.mask[stride*fr+index] else None
            return self.palette[p] if p is not None else Transparent()

        for x in ansi.format_image_iter( data_fetch, x_start, y_start, width, height, frames, columns, downsample ):
            yield x
        return

Response: Return an iterator over the ANSI escape sequences that can be used to render the image.


Instruction:
Given the following Python 3 function, write the documentation
def main():

    parser = OptionParser(description=
             "Purge a single url from fastly.")
    parser.add_option("-k", "--key", dest="apikey",
                      default="", help="fastly api key")
    parser.add_option("-H", "--host", dest="host",
                      help="host to purge from")
    parser.add_option("-p", "--path", dest="path",
                      help="path to purge")

    (options, args) = parser.parse_args()
    for val in options.__dict__.values():
        if val is None:
            print "Missing required options"
            parser.print_help()
            sys.exit(1)

    client = fastly.connect(options.apikey)
    purge = client.purge_url(options.host, options.path)
    print purge

Response: Purges a single fastly url from fastly.


Instruction:
Write a Python 3 script for
registering callbacks needed by the interface object.

Response:
def set_callbacks(self, **dic_functions):
        """Register callbacks needed by the interface object"""
        for action in self.interface.CALLBACKS:
            try:
                f = dic_functions[action]
            except KeyError:
                pass
            else:
                setattr(self.interface.callbacks, action, f)
        manquantes = [
            a for a in self.interface.CALLBACKS if not a in dic_functions]
        if not manquantes:
            logging.debug(
                f"{self.__class__.__name__} : Tous les callbacks demandés sont fournis.")
        else:
            logging.warning(
                f"{self.__class__.__name__} didn't set asked callbacks {manquantes}")


Instruction:
Make a summary of the following Python 3 code
def populate(self, obj=None, section=None, parse_types=True):
        section = self.default_section if section is None else section
        obj = Settings() if obj is None else obj
        is_dict = isinstance(obj, dict)
        for k, v in self.get_options(section).items():
            if parse_types:
                if v == 'None':
                    v = None
                elif self.FLOAT_REGEXP.match(v):
                    v = float(v)
                elif self.INT_REGEXP.match(v):
                    v = int(v)
                elif self.BOOL_REGEXP.match(v):
                    v = v == 'True'
                else:
                    m = self.EVAL_REGEXP.match(v)
                    if m:
                        evalstr = m.group(1)
                        v = eval(evalstr)
            logger.debug('setting {} => {} on {}'.format(k, v, obj))
            if is_dict:
                obj[k] = v
            else:
                setattr(obj, k, v)
        return obj

Response: Populates obj with the values from the all values in section.


Instruction:
How would you implement a function in Python 3 that
gets the module that is calling this function.

Response:
def _get_calling_module(self):
        """Get the last module in the call stack that is not this module or ``None`` if
        the call originated from this module.

        """
        for frame in inspect.stack():
            mod = inspect.getmodule(frame[0])
            logger.debug(f'calling module: {mod}')
            if mod is not None:
                mod_name = mod.__name__
                if mod_name != __name__:
                    return mod


Instruction:
Create a Python 3 function for
returning a resource based on a file name.

Response:
def resource_filename(self, resource_name, module_name=None):
        """Return a resource based on a file name.  This uses the ``pkg_resources``
        package first to find the resources.  If it doesn't find it, it returns
        a path on the file system.

        :param: resource_name the file name of the resource to obtain (or name
            if obtained from an installed module)
        :param module_name: the name of the module to obtain the data, which
            defaults to ``__name__``
        :return: a path on the file system or resource of the installed module

        """
        if module_name is None:
            mod = self._get_calling_module()
            logger.debug(f'calling module: {mod}')
            if mod is not None:
                mod_name = mod.__name__
        if module_name is None:
            module_name = __name__
        if pkg_resources.resource_exists(mod_name, resource_name):
            res = pkg_resources.resource_filename(mod_name, resource_name)
        else:
            res = resource_name
        return Path(res)


Instruction:
Make a summary of the following Python 3 code
def parser(self):
        "Load the configuration file."
        if not hasattr(self, '_conf'):
            cfile = self.config_file
            logger.debug('loading config %s' % cfile)
            if os.path.isfile(cfile):
                conf = self._create_config_parser()
                conf.read(os.path.expanduser(cfile))
            else:
                if self.robust:
                    logger.debug('no default config file %s--skipping' % cfile)
                else:
                    raise IOError('no such file: %s' % cfile)
                conf = None
            self._conf = conf
        return self._conf

Response: Load the configuration file.


Instruction:
Given the following Python 3 function, write the documentation
def get_options(self, section='default', opt_keys=None, vars=None):
        vars = vars if vars else self.default_vars
        conf = self.parser
        opts = {}
        if opt_keys is None:
            if conf is None:
                opt_keys = {}
            else:
                if not self.robust or conf.has_section(section):
                    opt_keys = conf.options(section)
                else:
                    opt_keys = {}
        else:
            logger.debug('conf: %s' % conf)
            copts = conf.options(section) if conf else {}
            opt_keys = set(opt_keys).intersection(set(copts))
        for option in opt_keys:
            logger.debug(f'option: {option}, vars: {vars}')
            opts[option] = conf.get(section, option, vars=vars)
        return opts

Response: Get all options for a section.


Instruction:
Write a Python 3 function for
returning an option from section with name.

Response:
def get_option(self, name, section=None, vars=None, expect=None):
        """Return an option from ``section`` with ``name``.

        :param section: section in the ini file to fetch the value; defaults to
        constructor's ``default_section``

        """
        vars = vars if vars else self.default_vars
        if section is None:
            section = self.default_section
        opts = self.get_options(section, opt_keys=[name], vars=vars)
        if opts:
            return opts[name]
        else:
            if self._narrow_expect(expect):
                raise ValueError('no option \'{}\' found in section {}'.
                                 format(name, section))


Instruction:
Make a summary of the following Python 3 code
def get_option_list(self, name, section=None, vars=None,
                        expect=None, separator=','):
        """Just like ``get_option`` but parse as a list using ``split``.

        """
        val = self.get_option(name, section, vars, expect)
        return val.split(separator) if val else []

Response: Return a list of values from the option named name in section.


Instruction:
How would you implement a function in Python 3 that
gets option as a boolean.

Response:
def get_option_boolean(self, name, section=None, vars=None, expect=None):
        """Just like ``get_option`` but parse as a boolean (any case `true`).

        """
        val = self.get_option(name, section, vars, expect)
        val = val.lower() if val else 'false'
        return val == 'true'


Instruction:
Given the following Python 3 function, write the documentation
def get_option_path(self, name, section=None, vars=None, expect=None):
        val = self.get_option(name, section, vars, expect)
        return Path(val)

Response:
Like get_option but return a Path object of
        the string.


Instruction:
Given the following Python 3 function, write the documentation
def property_get( prop, instance, **kwargs ):
    if isinstance( prop, Ref ):
        return prop.get( instance, **kwargs )
    return prop

Response: Wrapper for property reads which auto - dereferences Refs if required.


Instruction:
Can you tell what is the following Python 3 function doing
def property_set( prop, instance, value, **kwargs ):

    if isinstance( prop, Ref ):
        return prop.set( instance, value, **kwargs )
    raise AttributeError( "can't change value of constant {} (context: {})".format( prop, instance ) )

Response: Wrapper for property. set


Instruction:
Can you create a Python 3 function that
wraps for attributes of a View class which auto-dereferences Refs. Equivalent to setting a property on the class with the getter wrapped with property_get(), and the setter wrapped with property_set(). prop A string containing the name of the class attribute to wrap.

Response:
def view_property( prop ):
    """Wrapper for attributes of a View class which auto-dereferences Refs.

    Equivalent to setting a property on the class with the getter wrapped
    with property_get(), and the setter wrapped with property_set().

    prop
        A string containing the name of the class attribute to wrap.
    """
    def getter( self ):
        return property_get( getattr( self, prop ), self.parent )

    def setter( self, value ):
        return property_set( getattr( self, prop ), self.parent, value )

    return property( getter, setter )


Instruction:
Make a summary of the following Python 3 code
def get( self, instance, **kwargs ):
        target = instance
        for attr in self._path:
            target = getattr( target, attr )
        return target

Response: Return an attribute from an object using the Ref path.


Instruction:
How would you code a function in Python 3 to
set an attribute on an object using the Ref path.

Response:
def set( self, instance, value, **kwargs ):
        """Set an attribute on an object using the Ref path.

        instance
            The object instance to traverse.

        value
            The value to set.

        Throws AttributeError if allow_write is False.
        """
        if not self._allow_write:
            raise AttributeError( "can't set Ref directly, allow_write is disabled" )
        target = instance
        for attr in self._path[:-1]:
            target = getattr( target, attr )
        setattr( target, self._path[-1], value )
        return


Instruction:
How would you code a function in Python 3 to
traverse the tree until the tree is not modified.

Response:
def traverse_until_fixpoint(predicate, tree):
    """Traverses the tree again and again until it is not modified."""
    old_tree = None
    tree = simplify(tree)
    while tree and old_tree != tree:
        old_tree = tree
        tree = tree.traverse(predicate)
        if not tree:
            return None
        tree = simplify(tree)
    return tree


Instruction:
Write a Python 3 function for
running the necessary methods in the correct order for the current runmetadata object

Response:
def runner(self):
        """
        Run the necessary methods in the correct order
        """
        logging.info('Starting {} analysis pipeline'.format(self.analysistype))
        # Initialise the GenObject
        for sample in self.runmetadata.samples:
            setattr(sample, self.analysistype, GenObject())
            try:
                sample[self.analysistype].pointfindergenus = self.pointfinder_org_dict[sample.general.referencegenus]
            except KeyError:
                sample[self.analysistype].pointfindergenus = 'ND'
        # Run the raw read mapping
        PointSipping(inputobject=self,
                     cutoff=self.cutoff)
        # Create FASTA files from the raw read matcves
        self.fasta()
        # Run PointFinder on the FASTA files
        self.run_pointfinder()
        # Create summary reports of the PointFinder outputs
        self.parse_pointfinder()


Instruction:
How would you implement a function in Python 3 that
creates the FASTA files of the PointFinder results that are fed into PointFinder

Response:
def fasta(self):
        """
        Create FASTA files of the PointFinder results to be fed into PointFinder
        """
        logging.info('Extracting FASTA sequences matching PointFinder database')
        for sample in self.runmetadata.samples:
            # Ensure that there are sequence data to extract from the GenObject
            if GenObject.isattr(sample[self.analysistype], 'sequences'):
                # Set the name of the FASTA file
                sample[self.analysistype].pointfinderfasta = \
                    os.path.join(sample[self.analysistype].outputdir,
                                 '{seqid}_pointfinder.fasta'.format(seqid=sample.name))
                # Create a list to store all the SeqRecords created
                sequences = list()
                with open(sample[self.analysistype].pointfinderfasta, 'w') as fasta:
                    for gene, sequence in sample[self.analysistype].sequences.items():
                        # Create a SeqRecord using a Seq() of the sequence - both SeqRecord and Seq are from BioPython
                        seq = SeqRecord(seq=Seq(sequence),
                                        id=gene,
                                        name=str(),
                                        description=str())
                        sequences.append(seq)
                    # Write all the SeqRecords to file
                    SeqIO.write(sequences, fasta, 'fasta')


Instruction:
How would you code a function in Python 3 to
run PointFinder on the FASTA files extracted from the raw reads and store the results in the output directory.

Response:
def run_pointfinder(self):
        """
        Run PointFinder on the FASTA sequences extracted from the raw reads
        """
        logging.info('Running PointFinder on FASTA files')
        for i in range(len(self.runmetadata.samples)):
            # Start threads
            threads = Thread(target=self.pointfinder_threads, args=())
            # Set the daemon to True - something to do with thread management
            threads.setDaemon(True)
            # Start the threading
            threads.start()
        # PointFinder requires the path to the blastn executable
        blast_path = shutil.which('blastn')
        for sample in self.runmetadata.samples:
            # Ensure that the attribute storing the name of the FASTA file has been created
            if GenObject.isattr(sample[self.analysistype], 'pointfinderfasta'):
                sample[self.analysistype].pointfinder_outputs = os.path.join(sample[self.analysistype].outputdir,
                                                                             'pointfinder_outputs')
                # Don't run the analyses if the outputs have already been created
                if not os.path.isfile(os.path.join(sample[self.analysistype].pointfinder_outputs,
                                                   '{samplename}_blastn_results.tsv'.format(samplename=sample.name))):
                    make_path(sample[self.analysistype].pointfinder_outputs)
                    # Create and run the PointFinder system call
                    pointfinder_cmd = \
                        'python -m pointfinder.PointFinder -i {input} -s {species} -p {db_path} -m blastn ' \
                        '-o {output_dir} -m_p {blast_path}'\
                        .format(input=sample[self.analysistype].pointfinderfasta,
                                species=sample[self.analysistype].pointfindergenus,
                                db_path=self.targetpath,
                                output_dir=sample[self.analysistype].pointfinder_outputs,
                                blast_path=blast_path)
                    self.queue.put(pointfinder_cmd)
        self.queue.join()


Instruction:
How would you code a function in Python 3 to
populate the summary dictionary for the specified genus.

Response:
def populate_summary_dict(self, genus=str(), key=str()):
        """
        :param genus: Non-supported genus to be added to the dictionary
        :param key: section of dictionary to be populated. Supported keys are: prediction, table, and results
        Populate self.summary_dict as required. If the genus is not provided, populate the dictionary for Salmonella
        Escherichia and Campylobacter. If the genus is provided, this genus is non-standard, and an 'empty' profile
        must be created for it
        """
        # If the genus is not provided, generate the generic dictionary
        if not genus:
            # Populate the summary dict
            self.summary_dict = {
                'Salmonella':
                    {
                        'prediction':
                            {
                                'header': 'Strain,Colitsin,Colistin,Spectinomycin,Quinolones,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'Salmonella_prediction_summary.csv')
                            },
                        'table':
                            {
                                'header': 'Strain,parE,parC,gyrA,pmrB,pmrA,gyrB,16S_rrsD,23S,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'Salmonella_table_summary.csv')
                            },
                        'results':
                            {
                                'header': 'Strain,Genus,Mutation,NucleotideChange,AminoAcidChange,Resistance,PMID,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'PointFinder_results_summary.csv')
                            }
                    },
                'Escherichia':
                    {
                        'prediction':
                            {
                                'header': 'Strain,Colistin,GentamicinC,gentamicinC,Streptomycin,Macrolide,Sulfonamide,'
                                          'Tobramycin,Neomycin,Fluoroquinolones,Aminocoumarin,Tetracycline,KanamycinA,'
                                          'Spectinomycin,B-lactamResistance,Paromomycin,Kasugamicin,Quinolones,G418,'
                                          'QuinolonesAndfluoroquinolones,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'Escherichia_prediction_summary.csv')
                            },
                        'table':
                            {
                                'header': 'Strain,parE,parC,folP,gyrA,pmrB,pmrA,16S_rrsB,16S_rrsH,gyrB,ampC,'
                                          '16S_rrsC,23S,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'Escherichia_table_summary.csv')
                            },
                        'results':
                            {
                                'header': 'Strain,Genus,Mutation,NucleotideChange,AminoAcidChange,Resistance,PMID,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'PointFinder_results_summary.csv')
                            }
                    },
                'Campylobacter':
                    {

                        'prediction':
                            {
                                'header': 'Strain,LowLevelIncreaseMIC,AssociatedWithT86Mutations,Macrolide,Quinolone,'
                                          'Streptinomycin,Erythromycin,IntermediateResistance,HighLevelResistance_'
                                          'nalidixic_and_ciprofloxacin,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'Campylobacter_prediction_summary.csv')
                            },
                        'table':
                            {
                                'header': 'Strain,L22,rpsL,cmeR,gyrA,23S,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'Campylobacter_table_summary.csv')
                            },
                        'results':
                            {
                                'header': 'Strain,Genus,Mutation,NucleotideChange,AminoAcidChange,Resistance,PMID,\n',
                                'output': str(),
                                'summary': os.path.join(self.reportpath, 'PointFinder_results_summary.csv')
                            }
                    }
            }
        else:
            # Create the nesting structure as required
            if genus not in self.summary_dict:
                self.summary_dict[genus] = dict()
            if key not in self.summary_dict[genus]:
                self.summary_dict[genus][key] = dict()
            # The output section is the same regardless of the key
            self.summary_dict[genus][key]['output'] = str()
            # The results report is more generic, and contains all strains, so the header and summary are set to
            # the default values required to generate this report
            if key == 'results':
                self.summary_dict[genus][key]['header'] = \
                    'Strain,Genus,Mutation,NucleotideChange,AminoAcidChange,Resistance,PMID,\n'
                self.summary_dict[genus][key]['summary'] = \
                    os.path.join(self.reportpath, 'PointFinder_results_summary.csv')
            # Create an empty header, and a report with the genus name
            else:
                self.summary_dict[genus][key]['header'] = 'Strain,\n'
                self.summary_dict[genus][key]['summary'] = os.path.join(self.reportpath, '{genus}_{key}_summary.csv'
                                                                        .format(genus=genus,
                                                                                key=key))
                # Remove the report if it exists, as the script will append data to this existing report
                if os.path.isfile(self.summary_dict[genus][key]['summary']):
                    os.remove(self.summary_dict[genus][key]['summary'])


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_pointfinder(self):
        # Create the nested dictionary that stores the necessary values for creating summary reports
        self.populate_summary_dict()
        # Clear out any previous reports
        for organism in self.summary_dict:
            for report in self.summary_dict[organism]:
                try:
                    os.remove(self.summary_dict[organism][report]['summary'])
                except FileNotFoundError:
                    pass
        for sample in self.runmetadata.samples:
            # Find the PointFinder outputs. If the outputs don't exist, create the appropriate entries in the
            # summary dictionary as required
            try:
                self.summary_dict[sample.general.referencegenus]['prediction']['output'] = \
                    glob(os.path.join(sample[self.analysistype].pointfinder_outputs, '{seq}*prediction.txt'
                                      .format(seq=sample.name)))[0]
            except IndexError:
                try:
                    self.summary_dict[sample.general.referencegenus]['prediction']['output'] = str()
                except KeyError:
                    self.populate_summary_dict(genus=sample.general.referencegenus,
                                               key='prediction')
            try:
                self.summary_dict[sample.general.referencegenus]['table']['output'] = \
                    glob(os.path.join(sample[self.analysistype].pointfinder_outputs, '{seq}*table.txt'
                                      .format(seq=sample.name)))[0]
            except IndexError:
                try:
                    self.summary_dict[sample.general.referencegenus]['table']['output'] = str()
                except KeyError:
                    self.populate_summary_dict(genus=sample.general.referencegenus,
                                               key='table')
            try:
                self.summary_dict[sample.general.referencegenus]['results']['output'] = \
                    glob(os.path.join(sample[self.analysistype].pointfinder_outputs, '{seq}*results.tsv'
                                      .format(seq=sample.name)))[0]
            except IndexError:
                try:
                    self.summary_dict[sample.general.referencegenus]['results']['output'] = str()
                except KeyError:
                    self.populate_summary_dict(genus=sample.general.referencegenus,
                                               key='results')
            # Process the predictions
            self.write_report(summary_dict=self.summary_dict,
                              seqid=sample.name,
                              genus=sample.general.referencegenus,
                              key='prediction')
            # Process the results summary
            self.write_report(summary_dict=self.summary_dict,
                              seqid=sample.name,
                              genus=sample.general.referencegenus,
                              key='results')

            # Process the table summary
            self.write_table_report(summary_dict=self.summary_dict,
                                    seqid=sample.name,
                                    genus=sample.general.referencegenus)

Response: Parse the PointFinder output files and create the nested dictionary that stores the necessary values for creating the summary reports for the PointFinder outputs


Instruction:
Given the following Python 3 function, write the documentation
def write_report(summary_dict, seqid, genus, key):
        # Set the header string if the summary report doesn't already exist
        if not os.path.isfile(summary_dict[genus][key]['summary']):
            header_string = summary_dict[genus][key]['header']
        else:
            header_string = str()
        summary_string = str()
        try:
            # Read in the predictions
            with open(summary_dict[genus][key]['output'], 'r') as outputs:
                # Skip the header
                next(outputs)
                for line in outputs:
                    # Skip empty lines
                    if line != '\n':
                        # When processing the results outputs, add the seqid to the summary string
                        if key == 'results':
                            summary_string += '{seq},{genus},'.format(seq=seqid,
                                                                      genus=genus)
                        # Clean up the string before adding it to the summary string - replace commas
                        # with semi-colons, and replace tabs with commas
                        summary_string += line.replace(',', ';').replace('\t', ',')
            # Ensure that there were results to report
            if summary_string:
                if not summary_string.endswith('\n'):
                    summary_string += '\n'
            else:
                if key == 'results':
                    summary_string += '{seq},{genus}\n'.format(seq=seqid,
                                                               genus=genus)
                else:
                    summary_string += '{seq}\n'.format(seq=seqid)
            # Write the summaries to the summary file
            with open(summary_dict[genus][key]['summary'], 'a+') as summary:
                # Write the header if necessary
                if header_string:
                    summary.write(header_string)
                summary.write(summary_string)
        # Add the strain information If no FASTA file could be created by reference mapping
        except FileNotFoundError:
            # Extract the length of the header from the dictionary. Subtract two (don't need the strain, or the
            # empty column created by a trailing comma
            header_len = len(summary_dict[genus][key]['header'].split(',')) - 2
            # When processing the results outputs, add the seqid to the summary string
            if key == 'results':
                summary_string += '{seq},{genus}\n'.format(seq=seqid,
                                                           genus=genus)
            # For the prediction summary, populate the summary string with the appropriate number of comma-separated
            # '0' entries
            elif key == 'prediction':
                summary_string += '{seq}{empty}\n'.format(seq=seqid,
                                                          empty=',0' * header_len)
            # Write the summaries to the summary file
            with open(summary_dict[genus][key]['summary'], 'a+') as summary:
                # Write the header if necessary
                if header_string:
                    summary.write(header_string)
                summary.write(summary_string)

Response: Parse the PointFinder outputs and write the summary report for the current analysis type and the current result type.


Instruction:
Can you write a function in Python 3 where it
parses the PointFinder table output and write a summary report

Response:
def write_table_report(summary_dict, seqid, genus):
        """
        Parse the PointFinder table output, and write a summary report
        :param summary_dict: nested dictionary containing data such as header strings, and paths to reports
        :param seqid: name of the strain,
        :param genus: MASH-calculated genus of current isolate
        """
        # Set the header string if the summary report doesn't already exist
        if not os.path.isfile(summary_dict[genus]['table']['summary']):
            header_string = summary_dict[genus]['table']['header']
        else:
            header_string = str()
        summary_string = '{seq},'.format(seq=seqid)
        try:
            # Read in the predictions
            with open(summary_dict[genus]['table']['output'], 'r') as outputs:
                for header_value in summary_dict[genus]['table']['header'].split(',')[:-1]:
                    for line in outputs:
                        if line.startswith('{hv}\n'.format(hv=header_value)):
                            # Iterate through the lines following the match
                            for subline in outputs:
                                if subline != '\n':
                                    if subline.startswith('Mutation'):
                                        for detailline in outputs:
                                            if detailline != '\n':
                                                summary_string += '{},'.format(detailline.split('\t')[0])
                                            else:
                                                break
                                    else:
                                        summary_string += '{},'.format(
                                            subline.replace(',', ';').replace('\t', ',').rstrip())
                                        break
                                else:
                                    break
                                break
                    # Reset the file iterator to the first line in preparation for the next header
                    outputs.seek(0)
            # Ensure that there were results to report
            if summary_string:
                if not summary_string.endswith('\n'):
                    summary_string += '\n'
                # Write the summaries to the summary file
                with open(summary_dict[genus]['table']['summary'], 'a+') as summary:
                    # Write the header if necessary
                    if header_string:
                        summary.write(header_string)
                    summary.write(summary_string)
        except FileNotFoundError:
            # Write the summaries to the summary file
            with open(summary_dict[genus]['table']['summary'], 'a+') as summary:
                # Extract the length of the header from the dictionary. Subtract two (don't need the strain, or the
                # empty column created by a trailing comma
                header_len = len(summary_dict[genus]['table']['header'].split(',')) - 2
                # Populate the summary strain with the appropriate number of comma-separated 'Gene not found' entries
                summary_string += '{empty}\n'.format(empty='Gene not found,' * header_len)
                # Write the header if necessary
                if header_string:
                    summary.write(header_string)
                summary.write(summary_string)


Instruction:
Can you tell what is the following Python 3 function doing
def targets(self):
        logging.info('Performing analysis with {} targets folder'.format(self.analysistype))
        for sample in self.runmetadata:
            sample[self.analysistype].runanalysis = True
            sample[self.analysistype].targetpath = (os.path.join(self.targetpath,
                                                                 sample[self.analysistype].pointfindergenus))
            # There is a relatively strict databasing scheme necessary for the custom targets. Eventually,
            # there will be a helper script to combine individual files into a properly formatted combined file
            try:
                sample[self.analysistype].baitfile = glob(os.path.join(sample[self.analysistype].targetpath,
                                                                       '*.fasta'))[0]
            # If the fasta file is missing, raise a custom error
            except IndexError:
                # Combine any .tfa files in the directory into a combined targets .fasta file
                fsafiles = glob(os.path.join(sample[self.analysistype].targetpath, '*.fsa'))
                if fsafiles:
                    combinetargets(fsafiles, sample[self.analysistype].targetpath)
                try:
                    sample[self.analysistype].baitfile = glob(os.path.join(sample[self.analysistype].targetpath,
                                                                           '*.fasta'))[0]
                except IndexError as e:
                    # noinspection PyPropertyAccess
                    e.args = [
                        'Cannot find the combined fasta file in {}. Please note that the file must have a '
                        '.fasta extension'.format(sample[self.analysistype].targetpath)]
                    if os.path.isdir(sample[self.analysistype].targetpath):
                        raise
                    else:
                        sample[self.analysistype].runanalysis = False
        for sample in self.runmetadata:
            # Set the necessary attributes
            sample[self.analysistype].outputdir = os.path.join(sample.run.outputdirectory, self.analysistype)
            make_path(sample[self.analysistype].outputdir)
            sample[self.analysistype].logout = os.path.join(sample[self.analysistype].outputdir, 'logout.txt')
            sample[self.analysistype].logerr = os.path.join(sample[self.analysistype].outputdir, 'logerr.txt')
            sample[self.analysistype].baitedfastq = \
                os.path.join(sample[self.analysistype].outputdir,
                             '{at}_targetMatches.fastq.gz'.format(at=self.analysistype))

Response: Create the multi - FASTA files of all targets if necessary and populate objects


Instruction:
Given the following Python 3 function, write the documentation
def strains(self):
        with open(os.path.join(self.path, 'strains.csv')) as strains:
            next(strains)
            for line in strains:
                oln, seqid = line.split(',')
                self.straindict[oln] = seqid.rstrip()
                self.strainset.add(oln)
                logging.debug(oln)
                if self.debug:
                    break

Response: Create a dictionary of SEQID - > OLNID


Instruction:
Can you generate a brief explanation for the following Python 3 code
def write_json(metadata):
        # Open the metadata file to write
        with open(metadata.jsonfile, 'w') as metadatafile:
            # Write the json dump of the object dump to the metadata file
            json.dump(metadata.dump(), metadatafile, sort_keys=True, indent=4, separators=(',', ': '))

Response: Write the metadata object to the metadata. jsonfile


Instruction:
Given the following Python 3 function, write the documentation
def read_json(json_metadata):
        # Load the metadata object from the file
        with open(json_metadata) as metadatareport:
            jsondata = json.load(metadatareport)
        # Create the metadata objects
        metadata = MetadataObject()
        # Initialise the metadata categories as GenObjects created using the appropriate key
        for attr in jsondata:
            if not isinstance(jsondata[attr], dict):
                setattr(metadata, attr, jsondata[attr])
            else:
                setattr(metadata, attr, GenObject(jsondata[attr]))
        return metadata

Response: Read the metadata object from a JSON - formatted metadata object file.


Instruction:
Implement a Python 3 function for
determining the total number of bases in each assembly file in each assembly file and update the assembly_length attribute with the total number of bases in each assembly file in each assembly file.

Response:
def assembly_length(self):
        """
        Use SeqIO.parse to extract the total number of bases in each assembly file
        """
        for sample in self.metadata:
            # Only determine the assembly length if is has not been previously calculated
            if not GenObject.isattr(sample, 'assembly_length'):
                # Create the assembly_length attribute, and set it to 0
                sample.assembly_length = 0
                for record in SeqIO.parse(sample.bestassemblyfile, 'fasta'):
                    # Update the assembly_length attribute with the length of the current contig
                    sample.assembly_length += len(record.seq)
                # Write the updated object to file
                self.write_json(sample)


Instruction:
Can you tell what is the following Python 3 function doing
def simulate_reads(self):
        logging.info('Read simulation')
        for sample in self.metadata:
            # Create the simulated_reads GenObject
            sample.simulated_reads = GenObject()
            # Iterate through all the desired depths of coverage
            for depth in self.read_depths:
                # Create the depth GenObject
                setattr(sample.simulated_reads, depth, GenObject())
                # Set the depth and output directory attributes for the depth GenObject
                sample.simulated_reads[depth].depth = depth
                sample.simulated_reads[depth].depth_dir = os.path.join(sample.outputdir, 'simulated', depth)
                # Create the output directory
                make_path(sample.simulated_reads[depth].depth_dir)
                # Iterate through all the desired forward and reverse read pair lengths
                for read_pair in self.read_lengths:
                    # Create the read_pair GenObject within the depth GenObject
                    setattr(sample.simulated_reads[depth], read_pair, GenObject())
                    # Set and create the output directory
                    sample.simulated_reads[depth][read_pair].outputdir = \
                        os.path.join(sample.simulated_reads[depth].depth_dir, read_pair)
                    make_path(sample.simulated_reads[depth][read_pair].outputdir)
                    # Create both forward_reads and reverse_reads sub-GenObjects
                    sample.simulated_reads[depth][read_pair].forward_reads = GenObject()
                    sample.simulated_reads[depth][read_pair].reverse_reads = GenObject()
                    # Extract the forward and reverse reads lengths from the read_pair variable
                    sample.simulated_reads[depth][read_pair].forward_reads.length, \
                        sample.simulated_reads[depth][read_pair].reverse_reads.length = read_pair.split('_')
                    # Set the name of the forward reads - include the depth and read length information
                    sample.simulated_reads[depth][read_pair].forward_reads.fastq = \
                        os.path.join(sample.simulated_reads[depth][read_pair].outputdir,
                                     '{name}_{depth}_{read_pair}_R1.fastq.gz'
                                     .format(name=sample.name,
                                             depth=depth,
                                             read_pair=read_pair))
                    # Reverse reads
                    sample.simulated_reads[depth][read_pair].reverse_reads.fastq = \
                        os.path.join(sample.simulated_reads[depth][read_pair].outputdir,
                                     '{name}_{depth}_{read_pair}_R2.fastq.gz'
                                     .format(name=sample.name,
                                             depth=depth,
                                             read_pair=read_pair))
                    # Create the trimmed output directory attribute
                    sample.simulated_reads[depth][read_pair].simulated_trimmed_outputdir \
                        = os.path.join(sample.simulated_reads[depth][read_pair].outputdir,
                                       'simulated_trimmed')
                    # Set the name of the forward trimmed reads - include the depth and read length information
                    # This is set now, as the untrimmed files will be removed, and a check is necessary
                    sample.simulated_reads[depth][read_pair].forward_reads.trimmed_simulated_fastq = \
                        os.path.join(sample.simulated_reads[depth][read_pair].simulated_trimmed_outputdir,
                                     '{name}_simulated_{depth}_{read_pair}_R1.fastq.gz'
                                     .format(name=sample.name,
                                             depth=depth,
                                             read_pair=read_pair))
                    # Reverse reads
                    sample.simulated_reads[depth][read_pair].reverse_reads.trimmed_simulated_fastq = \
                        os.path.join(sample.simulated_reads[depth][read_pair].simulated_trimmed_outputdir,
                                     '{name}_simulated_{depth}_{read_pair}_R2.fastq.gz'
                                     .format(name=sample.name,
                                             depth=depth,
                                             read_pair=read_pair))
                    # Calculate the number of reads required for the forward and reverse reads to yield the
                    # desired coverage depth e.g. 5Mbp genome at 20X coverage: 100Mbp in reads. 50bp forward reads
                    # 150bp reverse reads: forward proportion is 50 / (150 + 50) = 0.25 (and reverse is 0.75).
                    # Forward total reads is 25Mbp (75Mbp reverse). Number of reads required = 25Mbp / 50 bp
                    # 500000 reads total (same for reverse, as the reads are longer)
                    sample.simulated_reads[depth][read_pair].num_reads = \
                        int(sample.assembly_length *
                            int(depth) *
                            (int(sample.simulated_reads[depth][read_pair].forward_reads.length) /
                             (int(sample.simulated_reads[depth][read_pair].forward_reads.length) +
                              int(sample.simulated_reads[depth][read_pair].reverse_reads.length)
                              )
                             ) /
                            int(sample.simulated_reads[depth][read_pair].forward_reads.length)
                            )
                    logging.info(
                        'Simulating {num_reads} paired reads for sample {name} with the following parameters:\n'
                        'depth {dp}, forward reads {fl}bp, and reverse reads {rl}bp'
                        .format(num_reads=sample.simulated_reads[depth][read_pair].num_reads,
                                dp=depth,
                                name=sample.name,
                                fl=sample.simulated_reads[depth][read_pair].forward_reads.length,
                                rl=sample.simulated_reads[depth][read_pair].reverse_reads.length))
                    # If the reverse reads are set to 0, supply different parameters to randomreads
                    if sample.simulated_reads[depth][read_pair].reverse_reads.length != '0':
                        # Ensure that both the simulated reads, and the trimmed simulated reads files don't
                        # exist before simulating the reads
                        if not os.path.isfile(sample.simulated_reads[depth][read_pair].forward_reads.fastq) and \
                                not os.path.isfile(
                                    sample.simulated_reads[depth][read_pair].forward_reads.trimmed_simulated_fastq):
                            # Use the randomreads method in the OLCTools bbtools wrapper to simulate the reads
                            out, \
                                err, \
                                sample.simulated_reads[depth][read_pair].forward_reads.simulate_call = bbtools\
                                .randomreads(reference=sample.bestassemblyfile,
                                             length=sample.simulated_reads[depth][read_pair].reverse_reads.length,
                                             reads=sample.simulated_reads[depth][read_pair].num_reads,
                                             out_fastq=sample.simulated_reads[depth][read_pair].forward_reads.fastq,
                                             paired=True,
                                             returncmd=True,
                                             **{'ziplevel': '9',
                                                'illuminanames': 't',
                                                'Xmx': self.mem}
                                             )
                        else:
                            try:
                                forward_size = os.path.getsize(sample.simulated_reads[depth][read_pair]
                                                               .forward_reads.fastq)
                            except FileNotFoundError:
                                forward_size = 0
                            try:
                                reverse_size = os.path.getsize(sample.simulated_reads[depth][read_pair]
                                                               .reverse_reads.fastq)
                            except FileNotFoundError:
                                reverse_size = 0
                            if forward_size <= 100 or reverse_size <= 100:
                                try:
                                    os.remove(sample.simulated_reads[depth][read_pair].forward_reads.fastq)
                                except FileNotFoundError:
                                    pass
                                try:
                                    os.remove(sample.simulated_reads[depth][read_pair].reverse_reads.fastq)
                                except FileNotFoundError:
                                    pass
                                # Use the randomreads method in the OLCTools bbtools wrapper to simulate the reads
                                out, \
                                err, \
                                sample.simulated_reads[depth][read_pair].forward_reads.simulate_call = bbtools \
                                    .randomreads(reference=sample.bestassemblyfile,
                                                 length=sample.simulated_reads[depth][read_pair].reverse_reads.length,
                                                 reads=sample.simulated_reads[depth][read_pair].num_reads,
                                                 out_fastq=sample.simulated_reads[depth][read_pair].forward_reads.fastq,
                                                 paired=True,
                                                 returncmd=True,
                                                 **{'ziplevel': '9',
                                                    'illuminanames': 't'}
                                                 )
                    else:
                        if not os.path.isfile(sample.simulated_reads[depth][read_pair].forward_reads.fastq):
                            # Use the randomreads method in the OLCTools bbtools wrapper to simulate the reads
                            out, \
                                err, \
                                sample.simulated_reads[depth][read_pair].forward_reads.simulate_call = bbtools\
                                .randomreads(reference=sample.bestassemblyfile,
                                             length=sample.simulated_reads[depth][read_pair].forward_reads.length,
                                             reads=sample.simulated_reads[depth][read_pair].num_reads,
                                             out_fastq=sample.simulated_reads[depth][read_pair].forward_reads.fastq,
                                             paired=False,
                                             returncmd=True,
                                             **{'ziplevel': '9',
                                                'illuminanames': 't'}
                                             )
                # Update the JSON file
                self.write_json(sample)

Response: Create the simulated reads file for each sample in the metadata and generate the corresponding forward and reverse lengths for each depth of sequencing.


Instruction:
Can you tell what is the following Python 3 function doing
def read_length_adjust(self, analysistype):
        logging.info('Trimming {at} reads'.format(at=analysistype))
        for sample in self.metadata:
            # Iterate through all the desired depths of coverage
            for depth in self.read_depths:
                for read_pair in self.read_lengths:
                    # Create variables using the analysis type. These will be used in setting GenObject attributes
                    read_type = '{at}_reads'.format(at=analysistype)
                    fastq_type = 'trimmed_{at}_fastq'.format(at=analysistype)
                    logging.info(
                        'Trimming forward {at} reads for sample {name} at depth {depth} to length {length}'
                        .format(at=analysistype,
                                name=sample.name,
                                depth=depth,
                                length=sample[read_type][depth][read_pair].forward_reads.length))
                    # Create the output path if necessary
                    make_path(os.path.dirname(sample[read_type][depth][read_pair].forward_reads[fastq_type]))
                    if sample[read_type][depth][read_pair].reverse_reads.length != '0':
                        # Use the reformat method in the OLCTools bbtools wrapper to trim the reads
                        out, \
                            err, \
                            sample[read_type][depth][read_pair].forward_reads.sample_call = bbtools \
                            .reformat_reads(forward_in=sample[read_type][depth][read_pair].forward_reads.fastq,
                                            reverse_in=None,
                                            forward_out=sample[read_type][depth][read_pair].forward_reads[fastq_type],
                                            returncmd=True,
                                            **{'ziplevel': '9',
                                               'forcetrimright':
                                                   sample[read_type][depth][read_pair].forward_reads.length,
                                               'tossbrokenreads': 't',
                                               'tossjunk': 't',
                                               'Xmx': self.mem
                                               }
                                            )
                        # # Remove the untrimmed reads
                        # try:
                        #     os.remove(sample[read_type][depth][read_pair].forward_reads.fastq)
                        # except FileNotFoundError:
                        #     pass

                    else:
                        # If the files do not need to be trimmed, create a symlink to the original file
                        relative_symlink(sample[read_type][depth][read_pair].forward_reads.fastq,
                                         os.path.dirname(sample[read_type][depth][read_pair].
                                                         forward_reads[fastq_type]),
                                         os.path.basename(sample[read_type][depth][read_pair].
                                                          forward_reads[fastq_type])
                                         )
                    # Same as above, but for the reverse reads
                    logging.info(
                        'Trimming reverse {at} reads for sample {name} at depth {depth} to length {length}'
                        .format(at=analysistype,
                                name=sample.name,
                                depth=depth,
                                length=sample[read_type][depth][read_pair].reverse_reads.length))
                    if sample[read_type][depth][read_pair].reverse_reads.length != '0':
                        # Use the reformat method in the OLCTools bbtools wrapper to trim the reads
                        out, \
                            err, \
                            sample[read_type][depth][read_pair].reverse_reads.sample_call = bbtools \
                            .reformat_reads(forward_in=sample[read_type][depth][read_pair].reverse_reads.fastq,
                                            reverse_in=None,
                                            forward_out=sample[read_type][depth][read_pair].reverse_reads[fastq_type],
                                            returncmd=True,
                                            **{'ziplevel': '9',
                                               'forcetrimright':
                                                   sample[read_type][depth][read_pair].reverse_reads.length,
                                               'tossbrokenreads': 't',
                                               'tossjunk': 't',
                                               'Xmx': self.mem
                                               })
                        # # Remove the untrimmed reads
                        # try:
                        #     os.remove(sample[read_type][depth][read_pair].reverse_reads.fastq)
                        # except FileNotFoundError:
                        #     pass
            # Update the JSON file
            self.write_json(sample)

Response: This method will trim the reads to the correct length using reformat. sh