Skip to content

Commit

Permalink
Spectrum search engine implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
psalios committed Feb 27, 2018
1 parent c17972d commit ffcf939
Show file tree
Hide file tree
Showing 10 changed files with 242 additions and 18 deletions.
2 changes: 2 additions & 0 deletions peaks/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# NMR Spectrum Search Engine

This component is currently under development.

## Installation

### Dependencies
Expand Down
4 changes: 3 additions & 1 deletion peaks/peaks.iml
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,10 @@
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-devtools:1.5.10.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.5.10.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:1.5.10.RELEASE" level="project" />
<orderEntry type="library" name="Maven: org.webjars:jquery:3.3.1" level="project" />
<orderEntry type="library" name="Maven: org.webjars:bootstrap:3.3.7" level="project" />
<orderEntry type="library" name="Maven: org.webjars:jquery:1.11.1" level="project" />
<orderEntry type="library" name="Maven: org.webjars:bootstrap-select:1.12.4" level="project" />
<orderEntry type="library" name="Maven: org.webjars.bower:font-awesome:4.7.0" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:8.0.8-dmr" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:24.0-jre" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:1.3.9" level="project" />
Expand Down
16 changes: 15 additions & 1 deletion peaks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,26 @@
<optional>true</optional>
</dependency>

<!-- Optional, for bootstrap -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>3.3.7</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap-select</artifactId>
<version>1.12.4</version>
</dependency>
<dependency>
<groupId>org.webjars.bower</groupId>
<artifactId>font-awesome</artifactId>
<version>4.7.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,67 @@
package com.mp236.controllers;

import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;

import com.mp236.entities.Peak;
import com.mp236.entities.PeakRepository;
import com.mp236.entities.Spectrum;
import com.mp236.entities.SpectrumRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class SearchEngineController {

@Autowired
private SpectrumRepository spectrumRepository;

@Autowired
private PeakRepository peakRepository;

@RequestMapping("/")
public String welcome(Map<String, Object> model) {
public String welcome(
@RequestParam(value = "shift[]", defaultValue = "") double[] shifts,
@RequestParam(value = "multiplicity[]", defaultValue = "") String[] multiplicities,
@RequestParam(value = "deviation[]", defaultValue = "") double[] deviations,
Map<String, Object> model
) {

Map<Spectrum, Integer> spectrumHash = new HashMap<>();
for(int i=0;i<shifts.length;i++) {
List<Double> peaks = peakRepository.findInRange(shifts[i] - deviations[i], shifts[i] + deviations[i])
.stream()
.map(Peak::getPeak)
.collect(Collectors.toList());
List<Spectrum> spectrums = spectrumRepository.findInPeakList(peaks);

for(Spectrum s: spectrums) {
int times = 0;
if(spectrumHash.containsKey(s)) {
times = spectrumHash.get(s);
}
spectrumHash.put(s, times + 1);
}
}

Map<Integer, List<Spectrum>> spectrumMap = new TreeMap<>((o1, o2) -> o2 - o1);
spectrumHash.forEach((spectrum, times) -> {
if(!spectrumMap.containsKey(times)) {
spectrumMap.put(times, new ArrayList<>());
}
spectrumMap.get(times).add(spectrum);
});

List<List<Spectrum>> spectrums = new ArrayList<>();
spectrumMap.values().forEach(spectrums::add);
model.put("spectrums", spectrums);

model.put("shifts", shifts);
model.put("multiplicities", multiplicities);
model.put("deviations", deviations);

model.put("spectrums", spectrumRepository.findAll());
return "welcome";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

@Entity
@Table(name = "peaks")
public class Peaks {
public class Peak {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
Expand Down Expand Up @@ -44,4 +44,5 @@ public void setSpectrumId(Integer spectrumId) {
public String toString() {
return Double.toString(peak);
}

}
13 changes: 13 additions & 0 deletions peaks/src/main/java/com/mp236/entities/PeakRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.mp236.entities;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface PeakRepository extends CrudRepository<Peak, Long>{

@Query("SELECT p FROM Peak p WHERE p.peak BETWEEN :start AND :stop")
List<Peak> findInRange(@Param("start") double start, @Param("stop") double stop);
}
20 changes: 16 additions & 4 deletions peaks/src/main/java/com/mp236/entities/Spectrum.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.google.common.base.Joiner;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Entity
@Table(name = "spectrum")
Expand All @@ -16,7 +17,10 @@ public class Spectrum {

@OneToMany
@JoinColumn(name = "spectrum_id")
private Set<Peaks> peaks = new HashSet<>();
private List<Peak> peaks = new ArrayList<>();

@Column(name = "date")
private Date date;

public Integer getId() {
return id;
Expand All @@ -30,7 +34,15 @@ public String getPeaks() {
return Joiner.on(" ").skipNulls().join(peaks);
}

public void setPeaks(Set<Peaks> peaks) {
public void setPeaks(ArrayList<Peak> peaks) {
this.peaks = peaks;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package com.mp236.entities;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface SpectrumRepository extends CrudRepository<Spectrum, Long> {

@Query("SELECT s FROM Spectrum s JOIN s.peaks p WHERE p.peak IN :peaks ORDER BY s.date DESC")
List<Spectrum> findInPeakList(@Param("peaks") List<Double> peaks);

}
File renamed without changes.
143 changes: 135 additions & 8 deletions peaks/src/main/resources/templates/welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,160 @@
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<link rel="stylesheet" type="text/css" href="webjars/bootstrap/3.3.7/css/bootstrap.min.css" />

<link rel="stylesheet" type="text/css" th:href="@{/css/main.css}" href="../../css/main.css" />
<link rel="stylesheet" type="text/css" href="webjars/bootstrap-select/1.12.4/css/bootstrap-select.min.css" />
<link rel="stylesheet" type="text/css" href="webjars/font-awesome/4.7.0/css/font-awesome.min.css" />
<link rel="stylesheet" type="text/css" th:href="@{/css/main.css}" href="../static/css/main.css" />

</head>
<body>

<div class="container">
<h2 class="page-header"><strong>Chemical Shifts</strong></h2>
<form th:action="@{/}" target="_self" method="get">
<div class="row">
<div class="col-md-offset-1 col-md-10">
<table class="table table-striped table-condensed">
<thead>
<tr>
<th class="col-md-1 text-center"><strong>Line number</strong></th>
<th class="col-md-3 text-center"><strong>Chemical Shift (ppm)</strong></th>
<th class="col-md-4 text-center"><strong>Multiplicity</strong></th>
<th class="col-md-3 text-center"><strong>Deviation (ppm)</strong></th>
<th class="col-md-1 text-center"><strong>Remove</strong></th>
</tr>
</thead>
<tbody id="tableBody">
<tr th:each="shift,iter: ${shifts}">
<td class="text-center"><p class="text-center form-control-static" th:text="${iter.index + 1}"></p></td>
<td class="text-center"><input class="form-control" type="number" name="shift[]" step="0.01" th:value="${shift}"/></td>
<td class="text-center">
<select class="selectpicker" name="multiplicity[]">
<option value="m">m</option>
<option value="s">s</option>
<option value="d">d</option>
<option value="t">t</option>
<option value="q">q</option>
<option value="p">p</option>
<option value="h">h</option>
<option value="hept">hept</option>
<option value="dd">dd</option>
<option value="td">td</option>
<option value="ddt">ddt</option>
<option value="ddd">ddd</option>
</select>
</td>
<td class="text-center"><input class="form-control" type="number" name="deviation[]" step="0.01" th:value="${deviations[iter.index]}"/></td>
<td class="text-center"><button type="button" class="btn btn-danger btn-sm" onclick="remove(this)"><span class="fa fa-times"></span></button></td>
</tr>
</tbody>
</table>
</div>
<div class="col-md-offset-4 col-md-4">
<button type="button" class="btn btn-success btn-block" onclick="add()">Add Chemical Shift</button>
</div>
</div>
<div class="help-block"></div>
<div class="row">
<div class="text-center col-md-offset-1 col-md-10">
<button type="submit" class="btn btn-success">Submit</button>
</div>
</div>
</form>

<div th:unless="${#lists.isEmpty(spectrums)}">
<br/>
<div class="table-responsive">
<table class="table table-hover table-condensed table-striped">
<thead>
<tr>
<th>Peaks</th>
<th>Options</th>
<th class="col-md-9 text-center">Peaks</th>
<th class="col-md-2 text-center">Last Updated</th>
<th class="col-md-1 text-center">Options</th>
</tr>
</thead>
<tbody>
<tr th:each="spectrum: ${spectrums}">
<td th:text="${{spectrum.getPeaks()}}">Peaks</td>
<td>Empty</td>
</tr>
<th:block th:each="spectrumList: ${spectrums}">
<tr th:each="spectrum: ${spectrumList}">
<td th:text="${{spectrum.getPeaks()}}">Peaks</td>
<td class="text-center" th:text="${#dates.format(spectrum.getDate(), 'dd-MM-yyyy hh:mm:ss')}">Date</td>
<td class="text-center"></td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</th:block>
</tbody>
</table>
</div>
</div>
</div>

<script type="text/javascript" src="webjars/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="webjars/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script type="text/javascript" src="webjars/bootstrap-select/1.12.4/js/bootstrap-select.min.js"></script>

<script th:inline="javascript">
/*<![CDATA[*/
var lineNumber = /*[[${#lists.size(shifts) + 1}]]*/ 1;
/*]]>*/
</script>

<script type="text/javascript">
function add() {
$('#tableBody').append(
'<tr>' +
'<td class="text-center"><p class="text-center form-control-static">'+lineNumber+'</p></td>' +
'<td class="text-center"><input class="form-control" type="number" name="shift[]" step="0.01"/></td>' +
'<td class="text-center">' +
'<select class="selectpicker" name="multiplicity[]">' +
'<option value="m">m</option>' +
'<option value="s">s</option>' +
'<option value="d">d</option>' +
'<option value="t">t</option>' +
'<option value="q">q</option>' +
'<option value="p">p</option>' +
'<option value="h">h</option>' +
'<option value="hept">hept</option>' +
'<option value="dd">dd</option>' +
'<option value="td">td</option>' +
'<option value="ddt">ddt</option>' +
'<option value="ddd">ddd</option>' +
'</select>' +
'</td>' +
'<td class="text-center"><input class="form-control" type="number" name="deviation[]" step="0.01"/></td>' +
'<td class="text-center"><button type="button" class="btn btn-danger btn-sm" onclick="remove(this)"><span class="fa fa-times"></span></button></td>' +
'</tr>'
);
lineNumber++;
$('.selectpicker').selectpicker();
}

function remove(el) {
var row = $(el).parent().parent();

recalculateLineNumbers(row.next());
row.remove();
lineNumber--;
}

function recalculateLineNumbers(el) {
while(el[0]) {
var child = el.children(":first").children(":first");
child.html(parseInt(child.html()) - 1);

el = el.next();
}
}


$(function () {
$('.selectpicker').selectpicker();
if(lineNumber === 1) {
add();
} else {
$('.selectpicker').selectpicker();
}
});
</script>
</body>
</html>

0 comments on commit ffcf939

Please sign in to comment.