Пример #1
0
def post_annotations(request):
    document_name = request.COOKIES.get('document_name')
    document_id = request.COOKIES.get('document_id')
    data = j.loads(request.body.decode('utf-8'))
    text = data.pop('text')

    tmp = text.split(':-:')

    short = str.strip(tmp[0])
    if len(tmp) > 1:
        ref = str.strip(tmp[1])
    else:
        ref = ''
    data['term_accession'] = ref
    data["annotation_value"] = short
    data["term_source"] = 'xxx'

    if 'quote' in data:
        quote = data.pop('quote')
        data['text'] = quote

    if 'id' in data:
        # EDIT ANNOTATION
        annotation_id = data.pop('id')
        r = Annotation().update_annotation(document_id, annotation_id, data)
    else:
        # CREATE ANNOTATION
        r = Annotation().add_to_annotation(document_id, data)

    r['id'] = r.pop('_id')
    r['text'] = r['annotation_value'] + ' :-: ' + r['term_accession']

    return HttpResponse(j.dumps(r))
Пример #2
0
def refresh_annotations_for_user(request):
    file_id = request.GET["file_id"]
    sheet_name = request.GET["sheet_name"]
    filter = request.GET["filter"]
    uid = request.user.id
    if filter == "all":
        annotations = Annotation().get_terms_for_user_alphabetical(uid)
    elif filter == "by_count":
        annotations = Annotation().get_terms_for_user_ranked(uid)
    elif filter == "by_dataset":
        annotations = Annotation().get_terms_for_user_by_dataset(uid)
    return HttpResponse(json_util.dumps({"annotations": annotations}))
Пример #3
0
def annotate_meta(request, file_id):
    if "ss_data" in request.session:
        del request.session["ss_data"]
    if "ss_sheet_names" in request.session:
        del request.session["ss_sheet_names"]
    df = DataFile().get_record(ObjectId(file_id))
    name = df["name"]
    if name.endswith(('xls', 'xlsx')):
        return render(request, 'copo/copo_annotate_spreadsheet.html',
                      {'file_id': file_id, 'file_name': name, 'file_type': "ss"})
    elif name.endswith("csv"):
        return render(request, 'copo/copo_annotate_spreadsheet.html',
                      {'file_id': file_id, 'file_name': name, 'file_type': "csv"})
    elif name.endswith(("txt", "tsv")):
        return render(request, 'copo/copo_annotate_spreadsheet.html',
                      {'file_id': file_id, 'file_name': name, 'file_type': "tab"})
    elif name.endswith(('pdf')):
        html = ""
        records = Annotation().get_all_records()
        if "annotation_html" not in request.session:
            # if True:
            folder_name = str(uuid.uuid1())
            full_path = os.path.join(settings.MEDIA_ROOT, folder_name)
            os.makedirs(full_path)
            run("ebook-convert  " + df[
                "file_location"] + " " + full_path + " --no-images --pretty-print --insert-blank-line")
            with open(os.path.join(full_path, "index.html"), 'r') as f:
                html = f.read()
            shutil.rmtree(full_path)
            request.session["annotation_html"] = html
        else:
            print("using session text data")
            html = request.session["annotation_html"]
        return render(request, 'copo/copo_annotate_pdf.html',
                      {'html': html, 'file_id': file_id, 'file_name': name, "file_type": "pdf"})
Пример #4
0
def search_all(request):
    document_id = ObjectId(request.COOKIES.get('document_id'))
    data = Annotation().get_annotations_for_page(document_id)
    ant = change_mongo_id_format_to_standard(data['annotation'])
    ant = convert_text(ant)
    d = {"total": len(data), "rows": ant, "annotation_id": data["_id"]}
    return HttpResponse(j.dumps(d))
Пример #5
0
def post_annotations(request):
    document_name = request.COOKIES.get('document_name')
    document_id = request.COOKIES.get('document_id')
    data = j.loads(request.body.decode('utf-8'))
    text = data.pop('text')

    tmp = text.split(':-:')

    short = str.strip(tmp[0])
    if len(tmp) > 1:
        ref = str.strip(tmp[1])
    else:
        ref = ''
    data['@id'] = ref
    data["shortform"] = short


    if 'quote' in data:
        quote = data.pop('quote')
        data['text'] = quote

    if request.method == "DELETE":
        # DELETE ANNOTATION
        annotation_id = data.pop('id')
        r = Annotation().update_annotation(document_id, annotation_id, {}, True)
        response = HttpResponse('')
        response.content = ''
        response.status_code = 204
        return response
    elif 'id' in data:
        # EDIT ANNOTATION
        annotation_id = data.pop('id')
        r = Annotation().update_annotation(document_id, annotation_id, data)
    else:
        # CREATE ANNOTATION
        r = Annotation().add_to_annotation(document_id, data)

    r['id'] = r.pop('_id')
    r['text'] = r['shortform'] + ' :-: ' + r['@id']

    return HttpResponse(j.dumps(r))
Пример #6
0
def delete_annotation(request):
    col_idx = request.GET["col_idx"]
    sheet_name = request.GET["sheet_name"]
    file_id = request.GET["file_id"]
    iri = request.GET["iri"]
    uid = request.user.id

    doc = Annotation().decrement_or_delete_annotation(uid, iri)
    doc = DataFile().delete_annotation(col_idx=col_idx,
                                       sheet_name=sheet_name,
                                       file_id=file_id)
    return HttpResponse("Hello World")
Пример #7
0
def delete_ss_annotation(request):
    # DELETE ANNOTATION
    annotation_id = request.POST.get('annotation_id')
    document_id = request.POST.get('document_id')
    r = Annotation().update_annotation(document_id, annotation_id, {}, True)
    out = dict()
    if r == '':
        out['deleted'] = True

    else:
        out['deleted'] = False

    return HttpResponse(json.dumps(out))
Пример #8
0
def save_ss_annotation(request):
    document_id = request.POST.get('document_id')
    column_header = request.POST.get('column_header')
    annotation_value = request.POST.get('annotation_value')
    term_source = request.POST.get('term_source')
    term_accession = request.POST.get('term_accession')

    fields = {
        'column_header': column_header,
        'annotation_value': annotation_value,
        'term_source': term_source,
        'term_accession': term_accession
    }
    annotation_id = Annotation().add_to_annotation(id=document_id,
                                                   fields=fields)['_id']

    return HttpResponse(annotation_id)
Пример #9
0
def send_file_annotation(request):
    col_idx = request.POST["col_idx"]
    sheet_name = request.POST["sheet_name"]
    col_header = request.POST["col_header"]
    iri = request.POST["iri"]
    label = request.POST["label"]
    id = request.POST["id"]
    obo_id = request.POST.get("obo_id", "")
    ontology_name = request.POST["ontology_name"]
    ontology_prexfix = request.POST["ontology_prefix"]
    short_form = request.POST["short_form"]
    type = request.POST["type"]
    file_id = request.POST["file_id"]
    file_name = request.POST["file_name"]
    description = request.POST["description"]
    data = {
        "column_idx": col_idx,
        "column_header": col_header,
        "sheet_name": sheet_name,
        "iri": iri,
        "obo_id": obo_id,
        "label": label,
        "id": id,
        "ontology_name": ontology_name,
        "ontology_prefix": ontology_prexfix,
        "short_form": short_form,
        "type": type,
        "description": description,
        "uid": request.user.id,
        "file_id": file_id,
        "file_name": file_name
    }
    if Annotation().add_or_increment_term(data):
        annotations = DataFile().update_file_level_metadata(file_id, data)
    else:
        annotations = {"status": 500, "message": "Could not add annotation"}
    return HttpResponse(json_util.dumps({"annotation": annotations}))
Пример #10
0
def export_generic_annotation(request):
    ant_id = request.POST["annotation_id"]
    doc = Annotation().get_annotations_for_page(ant_id)
    out = {"raw": json.loads(doc["raw"]), "annotations": doc["annotation"]}
    return HttpResponse(json_util.dumps(out))
Пример #11
0
def annotate_data(request):
    doc = Annotation().get_record(request.POST.get('target_id'))
    return HttpResponse(j.dumps(doc))
Пример #12
0
def handle_upload(request):
    f = request.FILES['file']

    # TODO - this should be changed to a uuid

    # save file to media location
    save_name = path.join(settings.MEDIA_ROOT, "generic_annotations", f._name)
    # check if path exists
    if not path.exists(path.join(settings.MEDIA_ROOT, "generic_annotations")):
        os.makedirs(path.join(settings.MEDIA_ROOT, "generic_annotations"))

    with open(save_name, 'wb+') as destination:
        for chunk in f.chunks():
            destination.write(chunk)
    f.seek(0)

    file_name = os.path.splitext(f.name)[0]
    file_type = request.POST['file_type']
    skip_rows = request.POST['skip_rows']
    if file_type == "Spreadsheet":
        # load spreadsheet data and return to backend
        try:
            s = read_excel(f, skiprows=int(skip_rows))
        except XLRDError as e:
            s = read_csv(f, skiprows=(int(skip_rows)))

        s = s.replace(NaN, "")
        for column in s.iteritems():
            # check data type
            dt = column[1].dtype
            if str(dt).startswith('datetime'):
                # we are dealing with date objects
                s[column[0]] = s[column[0]].astype(str)

        raw = list()
        raw.append(s.columns.tolist())
        raw.extend(s.values.tolist())
        jraw = j.dumps(raw)
        raw = jraw

    elif file_type == "PDF Document":

        cmd = 'pdftotext -htmlmeta ' + save_name
        resp = pexpect.run(cmd)
        # now open the resulting file, parse and send to frontend
        file_name = os.path.splitext(save_name)[0]
        html_name = file_name + '.html'
        with open(html_name, "r", encoding='utf-8', errors='ignore') as p:
            raw = p.read()
    out = dict()

    if not Annotation().annotation_exists(file_name, str(request.user.id)):
        out['raw'] = raw
        out['type'] = file_type
        out['document_name'] = file_name
        out['profile_id'] = request.session['profile_id']
        out['deleted'] = '0'
        out['date_created'] = datetime.datetime.now()
        out['uid'] = str(request.user.id)
        out['file_location'] = save_name
        out = Annotation(request.session['profile_id']).save_record({}, **out)

    else:
        out = Annotation().get_annotation_by_name(file_name, request.user.id)

    try:
        os.remove(save_name)
        os.remove(html_name)
    except:
        print('cannot find temp file to delete.....no worries')

    return HttpResponse(j.dumps(out))