Commit 5226fab8 authored by Deniz Tosoni's avatar Deniz Tosoni

Added all files

parent 72bf6c28
package ch.fhnw;
import ch.fhnw.util.DNAUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
/**
* @author MedEx
*/
@WebServlet(name = "findwordservlet", urlPatterns = { "/FindWord", "/startNewApp" })
public class FindWordServlet extends HttpServlet {
public static String wordResult;
public static List<String> keys;
public static int max;
private final static Logger LOG = LogManager.getLogger(FindWordServlet.class);
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
LOG.info("Retrieved call from: " + req.getRemoteAddr());
String dna = req.getParameter("dnasequence");
String header = "";
if (dna != null && dna.trim().startsWith(">")) {
header = dna.split("\\r?\\n")[0];
dna = dna.substring(header.length());
dna = dna.replace("\n", "").replace("\r", "");
}
LOG.debug(header);
int wordSize = 5;
try {
String length = req.getParameter("wordSize");
if (length != null) {
wordSize = Integer.parseInt(length);
}
} catch (Exception ex) {
LOG.error("FAIL!!!!!!", ex);
}
Map<String, Integer> wordResult = new HashMap<>();
if (dna != null && !dna.trim().isEmpty()) {
wordResult = DNAUtil.getWordFrequency(dna, wordSize);
keys = new ArrayList<>();
max = Collections.max(wordResult.values());
for(Map.Entry<String, Integer> entry : wordResult.entrySet()) {
if (entry.getValue()==max){
keys.add(entry.getKey());
}
}
}
String informationStr = "";
if (dna != null && !dna.isEmpty()) {
try {
informationStr = "Most Frequent Word Count: " + max + "<br/>";
informationStr += "Following words found with this count: <br/>";
informationStr += keys;
} catch (Exception ex) {
LOG.error("Issues on using biojava " + ex.getMessage(), ex);
}
}
req.setAttribute("informationStr", informationStr);
req.setAttribute("getWordFrequencyresult", wordResult);
req.setAttribute("header", header);
RequestDispatcher view = req.getRequestDispatcher("wordResult.jsp");
view.forward(req, resp);
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ page import ="java.util.*,ch.fhnw.model.*,ch.fhnw.util.*" %>
<!DOCTYPE html>
<html>
<head>
<title>DNA Analyzer Application</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />
<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script type="text/javascript" src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
<!--
<link rel="stylesheet" href='<c:url value="/scripts/bootstrap.min.css"/>' />
<script type="text/javascript" src='<c:url value="/scripts/jquery-3.5.1.min.js"/>'></script>
<script type="text/javascript" src='<c:url value="/scripts/bootstrap.min.js"/>'></script>
-->
<style>
.modal-body{
word-break: break-all;
}
</style>
</head>
<body>
<div class="container-md">
<div class="page-header">
<h1>
<img src = "images/fhnw_logo.png" class = "img-rounded" width="60">
MSC Medical Informatics
<br/><img src = "images/dna_icon.jpg" class = "img-rounded" width="60">
<small>DNA Analysis Application</small>
</h1>
</div>
<p class="text-justify">
<textarea placeholder="Place DNA sequence here..." form="addform" name="dnasequence" class="form-control rounded-0" id="exampleFormControlTextarea1" rows="10"></textarea>
</p>
<p class="text-justify">
Word length<br/>
<input class="form-control" name="wordSize" type="number" value="5" form="addform"></input>
</p>
<p class="text-justify">
<form method="post" action="FindWord" id="addform">
<button type="submit" class="btn btn-primary">Find most frequent word</button>
</form>
</p>
<br/>
<% if (!request.getAttribute("getWordFrequencyresult").toString().isEmpty()) { %>
<div class="alert alert-dark" role="alert">
<% out.println(request.getAttribute("informationStr")); %>
</div>
<% } %>
<br/>
</div>
<%
Map<String, Integer> wordMap = (Map) request.getAttribute("getWordFrequencyresult");
StringBuilder htmlBuilder = new StringBuilder();
htmlBuilder.append("<table>");
for (Map.Entry<String, Integer> entry : wordMap.entrySet()) {
htmlBuilder.append(String.format("<tr><td>%s</td><td>%d</td></tr>",
entry.getKey(), entry.getValue()));
}
htmlBuilder.append("</table>");
String html = htmlBuilder.toString();
%>
out.write(html)
<!-- Modal -->
</body>
</html>
\ No newline at end of file
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment