Commit b701f0ba authored by Anubhuti MISHRA's avatar Anubhuti MISHRA

Git comments incorporated

parent 5fd1aef4
Pipeline #10583 failed with stage
in 42 seconds
......@@ -4,12 +4,21 @@ import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.List;
//import javax.xml.ws.Response;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.jboss.logging.Logger;
import org.lcsb.dapi.dao.DrugDao;
import org.lcsb.dapi.model.Drug;
import org.lcsb.dapi.service.DrugService;
import org.lcsb.dapi.service.Parser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -22,33 +31,49 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
@RestController
public class DrugController {
private static Logger logger = Logger.getLogger(DrugController.class);
@Autowired
private DrugService drugService;
@Autowired
DrugDao drugDao;
@PostMapping("/drugs/")
Drug addDrug(@RequestBody Drug newDrug) {
return drugService.save(newDrug);
}
@GetMapping("/api/database/drugbank/releases/{releaseName}/drugs/{id}")
Drug findById(@PathVariable("id") String id, @PathVariable("releaseName") String releaseName)
public ResponseEntity<?> findById(@PathVariable("id") String id, @PathVariable("releaseName") String releaseName)
throws DrugNotFoundException {
try {
return (drugService.findById(id, releaseName));
} catch (Exception e) {
throw new DrugNotFoundException(id);
boolean drugIdApproved = validDrugId(id);
if (!drugIdApproved) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Please enter a valid Drug-Id (eg:DB00001) ");
}
Drug drug = drugService.findById(id, releaseName);
if (drug == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("The Drug-Id you're looking for is not found");
}
return new ResponseEntity<>(drug, HttpStatus.OK);
}
private boolean validDrugId(String id){
if(id.length()!=7)return false;
for(int i=0;i<2;i++) {
if(!Character.isLetter(id.charAt(i))) {
return false;
}
}
for(int i=2;i<7;i++) {
if(!Character.isDigit(id.charAt(i))) {
return false;
}
}
return true;
}
@GetMapping("/api/database/drugbank/releases/{releaseName}/drugs")
List<Drug> getReleaseList(@PathVariable("releaseName") String releaseName) {
List<Drug> result = drugService.getReleaseList(releaseName);
List<Drug> getDrugsByRelease(@PathVariable("releaseName") String releaseName) {
List<Drug> result = drugService.findByRelease(releaseName);
return result;
}
......@@ -56,18 +81,12 @@ public class DrugController {
public UploadFileResponse uploadFile(@RequestParam("file") MultipartFile file) throws Exception {
String fileName = file.getOriginalFilename();
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath().path("/downloadFile/")
.path(fileName).toUriString();
InputStream inputStream = new BufferedInputStream(file.getInputStream());
List<Drug> druglist = new Parser().extractDrugs(inputStream);
drugService.save(druglist.get(0));
drugService.flush();
return new UploadFileResponse(fileName, fileDownloadUri, file.getContentType(), file.getSize());
}
......
......@@ -14,12 +14,8 @@ public interface DrugDao {
public Drug getDrugById(String id, Release release);
public List<Drug> getDrugByIdentifierType(IdentifierType drugBank);
public List<Drug> getAllDrugsByRelease(Release release);
public void deleteDrug(Drug entity);
public List<Drug> list();
public void evict(Drug drug);
......
......@@ -45,15 +45,6 @@ public class DrugDaoImp implements DrugDao {
}
}
@Override
public List<Drug> getDrugByIdentifierType(IdentifierType id) {
String hql = "FROM Drug d where d.sourceIdentifier.databaseIdentifier = :id";
Query<Drug> query = this.sessionFactory.getCurrentSession().createQuery(hql, Drug.class);
query.setParameter("id", id);
List<Drug> results = query.getResultList();
return results;
}
@Override
public List<Drug> getAllDrugsByRelease(Release release) {
String hql = "FROM Drug d where d.release = :release";
......@@ -63,11 +54,6 @@ public class DrugDaoImp implements DrugDao {
return results;
}
@Override
public void deleteDrug(Drug entity) {
this.sessionFactory.getCurrentSession().delete(entity);
}
@Override
@SuppressWarnings("unchecked")
public List<Drug> list() {
......
......@@ -17,12 +17,12 @@ public class ExternalDatabase {
@Id @GeneratedValue
private int id;
String externalDatabaseName;
private String externalDatabaseName;
@JsonManagedReference
@OneToMany(mappedBy="externalDatabase",fetch = FetchType.EAGER)
@Cascade({org.hibernate.annotations.CascadeType.ALL})
List<Release> releases;
private List<Release> releases;
public String getName() {
return externalDatabaseName;
......
......@@ -40,7 +40,7 @@ public class Target {
@JsonBackReference
@ManyToOne(cascade = CascadeType.ALL, optional=false,fetch = FetchType.EAGER)
public Drug drug;
private Drug drug;
public Drug getDrug() {
......
......@@ -11,7 +11,6 @@ public interface DrugService {
Drug findById(String id,String releaseName);
List<Drug>getReleaseList(String releaseName);
List<Drug>findByRelease(String releaseName);
void flush();
}
......@@ -14,8 +14,6 @@ import org.springframework.transaction.annotation.Transactional;
@Service
public class DrugServiceImp implements DrugService {
private static Logger logger = LogManager.getLogger(DrugServiceImp.class);
@Autowired
private DrugDao drugDao;
......@@ -29,12 +27,6 @@ public class DrugServiceImp implements DrugService {
return drug;
}
@Override
@Transactional
public void flush() {
drugDao.flush();
}
@Transactional(readOnly = true)
public List<Drug> list() {
......@@ -51,7 +43,7 @@ public class DrugServiceImp implements DrugService {
@Override
@Transactional
public List<Drug> getReleaseList(String releaseName) {
public List<Drug> findByRelease(String releaseName) {
Release release=releaseDao.get(releaseName);
return drugDao.getAllDrugsByRelease(release);
......
......@@ -9,10 +9,10 @@ import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyHandler extends DefaultHandler {
List<String> drugData = new ArrayList<>();
public String releaseTimestamp;
public String releaseName;
public String externalDatabaseName;
private List<String> drugData = new ArrayList<>();
private String releaseTimestamp;
private String releaseName;
private String externalDatabaseName;
public String getReleaseTimestamp() {
return releaseTimestamp;
......
......@@ -26,75 +26,64 @@ import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class Parser {
Logger logger = LogManager.getLogger(Parser.class);
List<Release>releaseList=new ArrayList<>();
public Drug sampleDrug;
String releaseTimestamp;
String releaseName;
String externalDatabaseName;
private List<Release> releaseList = new ArrayList<>();
private Drug sampleDrug;
private String releaseTimestamp;
private String releaseName;
private String externalDatabaseName;
ExternalDatabase extDatabase;
public List<Drug> extractDrugs(InputStream filename) throws Exception {
public List<Drug> extractDrugs(InputStream filename)
throws ParserConfigurationException, ParseException, SAXException, IOException, ParserException {
List<Drug> druglist = new ArrayList<>();
SAXDrugParser saxDrugParser = new SAXDrugParser();
List<String> drugData = saxDrugParser.parsedDrugs(filename);
releaseTimestamp = saxDrugParser.getReleaseTimestamp();
releaseName=saxDrugParser.getReleaseName();
externalDatabaseName=saxDrugParser.getExternalDatabaseName();
extDatabase=new ExternalDatabase();
releaseName = saxDrugParser.getReleaseName();
externalDatabaseName = saxDrugParser.getExternalDatabaseName();
extDatabase = new ExternalDatabase();
extDatabase.setName(externalDatabaseName);
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
for (int i = 0; i < drugData.size(); i++) {
InputStream stream = new ByteArrayInputStream(drugData.get(i).getBytes(StandardCharsets.UTF_8));
Document doc = db.parse(stream);
Element e = doc.getDocumentElement();
TargetFields fillTargets=new TargetFields();
sampleDrug=new Drug();
sampleDrug.setName(extractDrugName(e));
sampleDrug.setDescription(extractDrugDescription(e));
sampleDrug.setApproved(extractDrugApproved(e));
sampleDrug.addSynonyms(extractSynonyms(e));
sampleDrug.addBrandNames(extractBrandNames(e));
sampleDrug.setTargets(fillTargets.extractTargets(e,sampleDrug));
sampleDrug.setSourceIdentifier(extractSourceIdentifier(e,sampleDrug));
sampleDrug.setBloodBrainBarrier(extractBloodBrainBarrier(e));
sampleDrug.setRelease(extractRelease(sampleDrug));
druglist.add(sampleDrug);
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
for (int i = 0; i < drugData.size(); i++) {
InputStream stream = new ByteArrayInputStream(drugData.get(i).getBytes(StandardCharsets.UTF_8));
Document doc = db.parse(stream);
Element e = doc.getDocumentElement();
TargetFields fillTargets = new TargetFields();
sampleDrug = new Drug();
sampleDrug.setName(extractDrugName(e));
sampleDrug.setDescription(extractDrugDescription(e));
sampleDrug.setApproved(extractDrugApproved(e));
sampleDrug.addSynonyms(extractSynonyms(e));
sampleDrug.addBrandNames(extractBrandNames(e));
sampleDrug.setTargets(fillTargets.extractTargets(e, sampleDrug));
sampleDrug.setSourceIdentifier(extractSourceIdentifier(e, sampleDrug));
sampleDrug.setBloodBrainBarrier(extractBloodBrainBarrier(e));
sampleDrug.setRelease(extractRelease(sampleDrug));
druglist.add(sampleDrug);
}
catch(ParserConfigurationException|IOException|SAXException e){
logger.debug("something is wrong");
throw e;
}
//extDatabase.setReleases(new ArrayList<Release>(releaseList.values()));
extDatabase.setReleases(releaseList);
return druglist;
}
......@@ -109,27 +98,25 @@ public class Parser {
}
return drugName;
}
private String extractDrugDescription(Element e) {
NodeList nodeList = e.getElementsByTagName("description");
String description = "";
if (nodeList != null) {
if (nodeList.getLength() > 0 && !nodeList.item(0).getTextContent().isEmpty()) {
description = nodeList.item(0).getChildNodes().item(0).getNodeValue();
}
}
return description;
}
/**
*
* @param e
* three cases arise here:
* 1->the only field is approved then return true
* 2->more than 1 field and one of them is approved then situation is ambigous return null
* 3->more than 1 field and none of which is approved then return false
* @param e three cases arise here: 1->the only field is approved then return
* true 2->more than 1 field and one of them is approved then situation
* is ambigous return null 3->more than 1 field and none of which is
* approved then return false
*/
private Boolean extractDrugApproved(Element e) {
NodeList nodeList = e.getElementsByTagName("groups");
......@@ -137,17 +124,15 @@ public class Parser {
Element nextEle = (Element) nodeList.item(j);
NodeList secondNodeList = nextEle.getElementsByTagName("group");
boolean approvedFlag = false;
if (secondNodeList.getLength() == 1) {
if(secondNodeList.item(0).getTextContent().equalsIgnoreCase("approved")) {
if (secondNodeList.item(0).getTextContent().equalsIgnoreCase("approved")) {
return true;
}
else {
} else {
return false;
}
}
else if (secondNodeList.getLength() > 1) {
for (int k = 0; k < secondNodeList.getLength(); k++) {
if (secondNodeList.item(k).getTextContent().equalsIgnoreCase("approved")) {
......@@ -158,18 +143,17 @@ public class Parser {
if (!approvedFlag) {
return false;
}
}
else {
} else {
return false;
}
}
return false;
}
private List<String> extractSynonyms(Element e) {
List<String> listOfSynonyms = new ArrayList<>();
NodeList nodeList = e.getElementsByTagName("synonyms");
if (nodeList != null && nodeList.getLength() > 0) {
for (int i = 0; i < nodeList.getLength(); i++) {
Element SynEle = (Element) nodeList.item(i);
......@@ -201,16 +185,16 @@ public class Parser {
List<String> listOfBrandNames = new ArrayList<>(hs);
return listOfBrandNames;
}
private Identifier extractSourceIdentifier(Element e,Drug drug) {
private Identifier extractSourceIdentifier(Element e, Drug drug) {
Identifier sampleIdentifier = new Identifier();
NodeList resourceIdentifierList = e.getElementsByTagName("drugbank-id");
sampleIdentifier.setDatabaseIdentifier(IdentifierType.DRUG_BANK);
if (resourceIdentifierList != null && resourceIdentifierList.getLength() > 0) {
sampleIdentifier.setResourceIdentifier(resourceIdentifierList.item(0).getChildNodes().item(0).getNodeValue());
sampleIdentifier
.setResourceIdentifier(resourceIdentifierList.item(0).getChildNodes().item(0).getNodeValue());
}
return sampleIdentifier;
}
......@@ -218,31 +202,28 @@ public class Parser {
private Boolean extractBloodBrainBarrier(Element e) {
return null;
}
private Release extractRelease(Drug sampleDrug) throws ParseException{
Release release=null;
boolean flag=false;
for(Release releaseItem:releaseList){
if(releaseItem.getName().equals(releaseName)) {
release=releaseItem;
flag=true;
release.getDrugList().add(sampleDrug);
}
}
if(!flag){
release=new Release();
release.setTimestamp(new SimpleDateFormat("yyyy-dd-MM").parse(releaseTimestamp));
release.setName(releaseName);
release.setExternalDatabase(extDatabase);
List<Drug> list=new ArrayList<>();
list.add(sampleDrug);
release.setDrugList(list);
releaseList.add(release);
private Release extractRelease(Drug sampleDrug) throws ParseException {
Release release = null;
boolean flag = false;
for (Release releaseItem : releaseList) {
if (releaseItem.getName().equals(releaseName)) {
release = releaseItem;
flag = true;
release.getDrugList().add(sampleDrug);
}
return release;
}
if (!flag) {
release = new Release();
release.setTimestamp(new SimpleDateFormat("yyyy-dd-MM").parse(releaseTimestamp));
release.setName(releaseName);
release.setExternalDatabase(extDatabase);
List<Drug> list = new ArrayList<>();
list.add(sampleDrug);
release.setDrugList(list);
releaseList.add(release);
}
return release;
}
}
......@@ -12,7 +12,7 @@ import org.xml.sax.SAXException;
public class SAXDrugParser {
MyHandler handler;
private MyHandler handler;
public List<String> parsedDrugs(InputStream stream) throws ParserException {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
handler = new MyHandler();
......
......@@ -40,9 +40,6 @@ public class ControllerTest {
@Autowired
private DrugService drugServiceMock;
@Autowired
private ReleaseDao releaseDao;
@Autowired
DrugController drugController;
......@@ -56,102 +53,34 @@ public class ControllerTest {
public void tearDown() {
drugDao.clear();
}
@Test
public void oneDrugTest() throws Exception {
assertEquals(0, drugDao.list().size());
String fileName = "testFiles/Lepirudin.txt";
InputStream inputStream = new FileInputStream(new File(fileName));
List<Drug> druglist = new Parser().extractDrugs(inputStream);
drugServiceMock.save(druglist.get(0));
Drug lepirudin = druglist.get(0);
assertEquals("expected only one drug", 1, druglist.size());
assertEquals("Expected was Lepirudin", "Lepirudin", lepirudin.getName());
assertEquals(1, druglist.size());
assertEquals("Expected was Lepirudin", "Lepirudin", lepirudin.getName());
assertEquals(1, drugDao.list().size());
}
@Test
public void threeDrugTest() throws Exception {
public void findByIdTest() throws Exception {
String fileName = "testFiles/ThreeDrug.txt";
String fileName = "testFiles/Lepirudin.txt";
InputStream inputStream = new FileInputStream(new File(fileName));
List<Drug> druglist = new Parser().extractDrugs(inputStream);
drugServiceMock.save(druglist.get(0));
Drug leuprolide = druglist.get(1);
assertEquals(leuprolide.getName(), drugDao.getDrugById("DB00007", leuprolide.getRelease()).getName());
assertEquals(leuprolide.getTargets().size(), 1);
Drug lepirudin = (Drug) drugController.findById("DB00001", "5.1").getBody();
assertEquals("Lepirudin", lepirudin.getName());
assertEquals(2, lepirudin.getBrandNames().size());
Drug result = drugDao.getDrugById("DB00007", leuprolide.getRelease());
assertNotNull(result);
}
@Test
public void checkRelease() throws Exception {
public void findByReleaseTest() throws Exception {
String fileName = "testFiles/ThreeDrug.txt";
InputStream inputStream = new FileInputStream(new File(fileName));
List<Drug> druglist = new Parser().extractDrugs(inputStream);
drugServiceMock.save(druglist.get(0));
// check release for Bivalirudin
Release rel1 = druglist.get(0).getRelease();
assertEquals(rel1.getName(), "5.1");
assertEquals(rel1.getExternalDatabase().getName(), "drugbank");
assertEquals(rel1.getDrugList().size(), 3);
// check release for Leuprolide
Release rel2 = druglist.get(1).getRelease();
assertEquals(rel2.getName(), "5.1");
assertEquals(rel2.getExternalDatabase().getName(), "drugbank");
assertEquals(rel2.getDrugList().size(), 3);
// check release for Peginterferon
Release rel3 = druglist.get(2).getRelease();
assertEquals(rel3.getName(), "5.1");
assertEquals(rel3.getExternalDatabase().getName(), "drugbank");
assertEquals(rel3.getDrugList().size(), 3);
assertEquals(3, druglist.size());
assertTrue(druglist.get(0).getRelease() == druglist.get(1).getRelease());
assertTrue(druglist.get(2).getRelease() == druglist.get(1).getRelease());
assertEquals(3, drugDao.list().size());
}
@Test
public void checkBrandNames() throws Exception {
String fileName = "testFiles/ThreeDrug.txt";
InputStream inputStream = new FileInputStream(new File(fileName));
List<Drug> druglist = new Parser().extractDrugs(inputStream);
drugController.addDrug(druglist.get(0));
Drug leuprolide = druglist.get(1);
assertEquals(leuprolide.getBrandNames().size(), 19);
assertEquals(leuprolide.getSynonyms().size(), 21);
}
@Test
public void checkTarget() throws Exception {
String fileName = "testFiles/MultipleDrugs.txt";
InputStream inputStream = new FileInputStream(new File(fileName));
List<Drug> druglist = new Parser().extractDrugs(inputStream);
drugController.addDrug(druglist.get(0));
assertEquals("Alteplase", druglist.get(0).getName());
assertEquals(4, druglist.get(0).getTargets().size());
assertEquals("Sermorelin", druglist.get(1).getName());
assertEquals(1, druglist.get(1).getTargets().size());
List<Drug> listOfDrugs = drugController.getDrugsByRelease("5.1");
assertEquals(3, listOfDrugs.size());
assertEquals("Bivalirudin", listOfDrugs.get(0).getName());
assertEquals("Leuprolide", listOfDrugs.get(1).getName());
assertEquals("Peginterferon alfa-2a", listOfDrugs.get(2).getName());
}
}
\ No newline at end of file
......@@ -12,6 +12,7 @@ import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lcsb.dapi.model.Drug;
......@@ -37,6 +38,11 @@ public class DrugDaoImpTest {
Logger logger = LogManager.getLogger(DrugDaoImpTest.class);
@Autowired
DrugDao drugDao;
@After
public void tearDown() {
drugDao.clear();
}
@Test
public void testAdd() {
......@@ -90,7 +96,7 @@ public class DrugDaoImpTest {
drugDao.addDrug(drug);
assertEquals(count + 1, drugDao.list().size());
drugDao.deleteDrug(drug);
drugDao.delete(drug);
assertEquals(count, drugDao.list().size());
}
......@@ -118,7 +124,6 @@ public class DrugDaoImpTest {
assertEquals("Aspirin", drugDao.getDrugById("DB00001", release).getName());
assertEquals("used to treat headache", drugDao.getDrugById("DB00001", release).getDescription());
assertEquals("version.1.0", drugDao.getDrugById("DB00001", release).getRelease().getName());
assertEquals(count + 1, drugDao.getDrugByIdentifierType(IdentifierType.DRUG_BANK).size());
}
@Test
......
......@@ -10,6 +10,7 @@ import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -40,7 +41,7 @@ public class DrugServiceTest {
DrugService drugService;
@Mock
DrugDao dao;
DrugDao drugDao;
@Mock
ReleaseDao releasedao;
......@@ -50,39 +51,41 @@ public class DrugServiceTest {
MockitoAnnotations.initMocks(this);
}
@After
public void tearDown() {
drugDao.clear();
}