簡體   English   中英

我試圖將我的代碼從 Python 轉換為 JS,因為我想在我的 JavaScript c 中使用 HTML。 我的問題是,如何將此代碼轉換為 java

[英]I was trying to convert my code from Python to JS, because I want to use HTML with my JavaScript c. My question is, how do I convert this code to java

import random
import os
import collections
from collections import OrderedDict
from matplotlib import pyplot as plt
from matplotlib import cm
import pylab
import math
import numpy as np
import pandas as pd

h1 = input("Enter subsequent: ")

Test = []
for n in range(1):

    # Function to convert

    def listToString(s):

        # initialize an empty string
        str1 = ""
        # traverse in the string
        for ele in s:
            str1 += ele
        # return string
        return str1

    # Driver code
    S = listToString(h2)

    def count_kmers(sequence, k):
        d = collections.defaultdict(int)
        for i in range(len(S) - (k - 1)):
            d[sequence[i : i + k]] += 1

        return d

    def probabilities(kmer_count, k):
        probabilities = collections.defaultdict(float)
        N = len(S)
        for key, value in kmer_count.items():
            probabilities[key] = float(value) / (N - k + 1)
        return probabilities

    def chaos_game_representation(probabilities, k):
        array_size = int(math.sqrt(4 ** k))
        chaos = []
        for i in range(array_size):
            chaos.append([0] * array_size)
        maxx = array_size
        maxy = array_size
        posx = 1
        posy = 1
        for key, value in probabilities.items():
            for char in key:
                if char == "T":
                    posx += maxx // 2
                elif char == "C":
                    posy += maxy // 2
                elif char == "G":
                    posx += maxx // 2
                    posy += maxy // 2
                maxx = maxx // 2
                maxy //= 2
            chaos[posy - 1][posx - 1] = value
            maxx = array_size
            maxy = array_size
            posx = 1
            posy = 1

        return chaos

    p = count_kmers(S, 5)

    F = probabilities(p, 5)

    chaos_k3 = chaos_game_representation(F, 5)

    pylab.title("Chaos game representation for 3-mers")
    pylab.imshow(chaos_k3, interpolation="nearest", cmap=cm.gray_r)

    def flatten_list(_2d_list):
        flat_list = []
        # Iterate through the outer list
        for element in _2d_list:
            if type(element) is list:
                # If the element is of type list, iterate through the sublist
                for item in element:
                    flat_list.append(item)
            else:
                flat_list.append(element)
        return flat_list

    Test.append(flatten_list(chaos_k3))
print(Test)


import csv
import cv2

file = open("test.csv", "w+", newline="")


# writing the data into the file

with file:

    write = csv.writer(file)

    write.writerows(Test)
test_u = pd.read_csv("test.csv", header=None)
test_u

X_train = (test_u.iloc[:1, :].values).astype("float32")

F11 = X_train.reshape(test_u.shape[0], 32, 32, 1)
F11.shape

為什么需要將這整個轉換為 JS (!= java)? 有一些有趣的 web 框架可以使用 python 與 HTML 元素交互並以 python 本地語言導入你的函數。

我建議深入研究 Flask。 這個簡單的片段允許您呈現一個 html 頁面 (main.html),您可以在其中返回Python 函數甚至變量以在代碼中使用。 這也更有效,因為允許您分離 BE 和 FE 函數。 如果需要,您甚至可以添加與 JS 腳本的進一步交互。 然后,您還可以使用 PythonAnywhere 或 Heroku 輕松地在線托管它。

from flask import Flask, request, render_template

app = Flask(__name__)
app.config["DEBUG"] = True

@app.route('/', methods = ["GET"])
def input_page():
    return render_template("main.html")
    
if __name__ == '__main__':
    app.run(use_reloader=False)

(對不起,這應該更適合作為評論,但我沒有足夠的聲譽。但是我不想錯過幫助的機會:))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM