Пример #1
0
def save_coverages(contigs, coverage_filename):
    """
    :param contigs: A dict contig_name -> contig_id.
    :param coverage_filename: The name of the dsv file.
    """
    coverage_file = utils.parse_dsv(coverage_filename)

    # Determine if the file has a header.
    fields = next(coverage_file)
    has_header = not utils.is_number(fields[1])

    def add_coverages(contig_name, _coverages):
        try:
            contig_id = contigs.pop(contig_name)
        except KeyError:
            return
        for i, cov in enumerate(_coverages):
            db.session.add(Coverage(value=cov, name=header[i], contig_id=contig_id))

    header = fields[1:]
    if not has_header:
        header = ['cov_{}'.format(i) for i, _ in enumerate(fields[1:], 1)]
        contig_name, *_coverages = fields
        add_coverages(contig_name, _coverages)

    for contig_name, *_coverages in coverage_file:
        add_coverages(contig_name, _coverages)

    db.session.commit()
Пример #2
0
def save_coverages(contigs, coverage_filename):
    """
    :param contigs: A dict contig_name -> contig_id.
    :param coverage_filename: The name of the dsv file.
    """
    coverage_file = utils.parse_dsv(coverage_filename)

    # Determine if the file has a header.
    fields = next(coverage_file)
    has_header = not utils.is_number(fields[1])

    def add_coverages(contig_name, _coverages):
        try:
            contig_id = contigs.pop(contig_name)
        except KeyError:
            return
        for i, cov in enumerate(_coverages):
            db.session.add(
                Coverage(value=cov, name=header[i], contig_id=contig_id))

    header = fields[1:]
    if not has_header:
        header = ['cov_{}'.format(i) for i, _ in enumerate(fields[1:], 1)]
        contig_name, *_coverages = fields
        add_coverages(contig_name, _coverages)

    for contig_name, *_coverages in coverage_file:
        add_coverages(contig_name, _coverages)

    db.session.commit()
Пример #3
0
def handle_on_message(client, userdata, msg, app, db):
    try:
        msg.payload = bytes_to_json(msg.payload)
        print("Received message '" + str(msg.payload) + "' on topic '" +
              msg.topic + "' with QoS " + str(msg.qos),
              flush=True)
    except:
        print("Received invalid message '" + str(msg.payload) +
              "' on topic '" + msg.topic + "' with QoS " + str(msg.qos),
              flush=True)
        return
    if msg.topic.endswith("/"):
        msg.topic = msg.topic[:-1].encode()
    topic = msg.topic.split("/")
    if len(topic) >= 2 and topic[1] == "server":
        t, sender_id = topic[0].split(":")
        if t == "d" and is_number(sender_id):
            if len(topic) == 2:
                _save_device_pk(int(sender_id), msg, app, db)
            elif len(topic) == 3:
                if topic[2] in ["save_data", "remove_data"]:
                    _edit_device_data(int(sender_id), topic[2], msg, app, db)
                else:
                    print(f"Invalid topic: {msg.topic}", flush=True)
        else:
            print(f"Invalid Device type or ID", flush=True)
Пример #4
0
 def can_use_device(cls, user, device_id):
     if not is_number(device_id):
         return False
     q = db.session.query(
         db.session.query(User).
         join(UserDevice).
         filter(User.id == user.id).
         filter(UserDevice.device_id == device_id).
         exists()
     )
     return q.scalar()
Пример #5
0
def find_project_id_from_id(project_id):
    """Find the project in the database or return None"""

    if is_number(project_id):
        project_id = int(project_id)

        project = Project.query.filter_by(id=project_id).first()

        return project.id

    else:
        return None
Пример #6
0
def filter_contigs(attr, value):
    _value = value
    value = value.rstrip('e').rstrip('l').rstrip('g')
    if not utils.is_number(value):
        return
    value = float(value)
    if _value.endswith('l'):
        filter = attr < value
    elif _value.endswith('le'):
        filter = attr <= value
    elif _value.endswith('g'):
        filter = attr > value
    elif _value.endswith('ge'):
        filter = attr >= value
    else:
        filter = attr == value
    return filter
Пример #7
0
def read_coverages(filename):
    coverage_file = utils.parse_dsv(filename)
    coverages = {}

    # Determine if the file has a header.
    fields = next(coverage_file)
    has_header = not utils.is_number(fields[1])
    if has_header:
        samples = fields[1:]
    else:
        samples = ['sample_{}'.format(i) for i, _ in enumerate(fields[1:], 1)]
        contig_name, *_coverages = fields
        coverages[contig_name] = {samples[i]: _coverages[i] for i, _ in enumerate(samples)}

    for contig_name, *_coverages in coverage_file:
        coverages[contig_name] = {samples[i]: _coverages[i] for i, _ in enumerate(samples)}

    os.remove(filename)
    return samples, coverages
Пример #8
0
def get_data_by_num_range():
    lower_bound = request.args.get("lower", "")
    upper_bound = request.args.get("upper", "")
    device_name_bi = request.args.get("device_name_bi", None)
    user = User.get_using_jwt_token(request.headers.get("Authorization", ""))

    arg_check = check_missing_request_argument(
        (device_name_bi, DEVICE_NAME_BI_MISSING_ERROR_MSG))
    if arg_check is not True:
        return arg_check

    device = Device.get_by_name_bi(device_name_bi)

    if device is None:
        return http_json_response(
            False, 400, **{"error": DEVICE_NAME_BI_INVALID_ERROR_MSG})

    if not User.can_use_device(user, device.id):
        return http_json_response(False, 400,
                                  **{"error": UNAUTHORIZED_USER_ERROR_MSG})

    if not is_number(lower_bound) and not is_number(upper_bound):
        return http_json_response(False, 400,
                                  **{"error": DATA_RANGE_MISSING_ERROR_MSG})

    with suppress(ValueError):
        lower_bound = int(lower_bound)
    with suppress(ValueError):
        upper_bound = int(upper_bound)

    data = []
    if isinstance(lower_bound, int) and isinstance(upper_bound, int):
        if -214748364800 <= lower_bound < upper_bound <= 214748364700:
            data = db.session.query(DeviceData).join(Device).filter(
                and_(DeviceData.num_data > lower_bound,
                     DeviceData.num_data < upper_bound,
                     Device.name_bi == device_name_bi)).all()
        else:
            return http_json_response(
                False, 400, **{"error": DATA_OUT_OF_OUTPUT_RANGE_ERROR_MSG})
    elif not isinstance(upper_bound, int) and isinstance(lower_bound, int):
        if -214748364800 <= lower_bound <= 214748364700:
            data = db.session.query(DeviceData).join(Device).filter(
                and_(DeviceData.num_data > lower_bound,
                     Device.name_bi == device_name_bi)).all()
        else:
            return http_json_response(
                False, 400, **{"error": DATA_OUT_OF_OUTPUT_RANGE_ERROR_MSG})

    elif not isinstance(lower_bound, int) and isinstance(upper_bound, int):
        if -214748364800 <= upper_bound <= 214748364700:
            data = db.session.query(DeviceData).join(Device).filter(
                and_(DeviceData.num_data < upper_bound,
                     Device.name_bi == device_name_bi)).all()
        else:
            return http_json_response(
                False, 400, **{"error": DATA_OUT_OF_OUTPUT_RANGE_ERROR_MSG})

    result = []
    for row in data:
        r = row.as_dict()
        for k, v in r.items():
            if isinstance(v, bytes):
                r[k] = v.decode()
        result.append(r)
    return http_json_response(**{'device_data': result})