<commit_before>#!/usr/loca/bin/python3
# mapsParser1.py
# Parsing script for GT Analyst multi-plate spectrophotometer
#
# Author: <NAME>
# Created on 13 Apr 2015
# Updated on 28 Jul 2015

from __future__ import absolute_import, division, print_function
import argparse
import os
import sys
import re
import datetime as dt
from operator import itemgetter


###############################################################################
# Utility methods
###############################################################################
def errOut(msg, parser=None):
    """Send a system exit exception with the given message"""
    if parser is not None:
        parser.print_help()
    script = os.path.basename(__file__)
    sys.exit("ERROR in {}\n    {}".format(script, msg))


def getDataFiles(mypath):
    """Check directory exists and return list of non-hidden files"""
    # Add forward slash if directory does not have one
    if not mypath.endswith("/"):
        mypath = mypath + "/"
    # Check if directory exists
    if not os.path.isdir(mypath):
        errOut("Data directory not found: {}".format(mypath))
    files = ["{}{}".format(mypath, f) for f in os.listdir(mypath)
             if os.path.isfile(os.path.join(mypath, f)) and
             not f.startswith(".")]
    return files


def readPlate(mypath):
    """Parse plate file into dictionary"""
    plate = {}
    with open(mypath, "r") as fh:
        for l in fh:
            l = l.rstrip()
            # Order of elements are:
            #   1) well
            #   2) mainsource
            #   3) compound
            #   4) concentration
            well, ms, cpd, conc = l.split("\t")
            well = parseWell(well)
            plate[well] = (ms, cpd)
    return plate


def parseWell(well):
    """Convert well string to 2-tuple"""
    well = re.match(r"(\w)(\d+)", well).group(1, 2)
    well = (well[0], int(well[1]))
    return well


def readData(data, f, t0):
    # Get only filename
    fname = os.path.basename(f)
    # Remove file extension
    fname = os.path.splitext(fname)[0]

    # Extract sample name and replicate from file
    m = re.match(r"^([A-z0-9-.]+)_([A-z0-9]+)", fname)
    if m is None:
        errOut("Could not extract name and replicate from filename: "
               "{}".format(f))
    name, rep = m.group(1, 2)<commit_msg>mapsParser1.py - (Fix) Sample and replicate name regex made more specific.<commit_after>#!/usr/loca/bin/python3
# mapsParser1.py
# Parsing script for GT Analyst multi-plate spectrophotometer
#
# Author: <NAME>
# Created on 13 Apr 2015
# Updated on 28 Jul 2015

from __future__ import absolute_import, division, print_function
import argparse
import os
import sys
import re
import datetime as dt
from operator import itemgetter


###############################################################################
# Utility methods
###############################################################################
def errOut(msg, parser=None):
    """Send a system exit exception with the given message"""
    if parser is not None:
        parser.print_help()
    script = os.path.basename(__file__)
    sys.exit("ERROR in {}\n    {}".format(script, msg))


def getDataFiles(mypath):
    """Check directory exists and return list of non-hidden files"""
    # Add forward slash if directory does not have one
    if not mypath.endswith("/"):
        mypath = mypath + "/"
    # Check if directory exists
    if not os.path.isdir(mypath):
        errOut("Data directory not found: {}".format(mypath))
    files = ["{}{}".format(mypath, f) for f in os.listdir(mypath)
             if os.path.isfile(os.path.join(mypath, f)) and
             not f.startswith(".")]
    return files


def readPlate(mypath):
    """Parse plate file into dictionary"""
    plate = {}
    with open(mypath, "r") as fh:
        for l in fh:
            l = l.rstrip()
            # Order of elements are:
            #   1) well
            #   2) mainsource
            #   3) compound
            #   4) concentration
            well, ms, cpd, conc = l.split("\t")
            well = parseWell(well)
            plate[well] = (ms, cpd)
    return plate


def parseWell(well):
    """Convert well string to 2-tuple"""
    well = re.match(r"(\w)(\d+)", well).group(1, 2)
    well = (well[0], int(well[1]))
    return well


def readData(data, f, t0):
    # Get only filename
    fname = os.path.basename(f)
    # Remove file extension
    fname = os.path.splitext(fname)[0]

    # Extract sample name and replicate from file
    m = re.match(r"^([A-Za-z0-9-.]+)_([A-Za-z0-9]+)", fname)
    if m is None:
        errOut("Could not extract name and replicate from filename: "
               "{}".format(f))
    name, rep = m.group(1, 2)
{'fraction_non_alphanumeric': 0.1862476270828939, 'fraction_numerical': 0.013288335794136259, 'mean_word_length': 2.7397476340694005, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5417058', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1417, 'n_words': 452}