Question: (Closed) How To I Exclude Some Search Terms In Javascript
0
gravatar for AP
7.3 years ago by
AP60
AP60 wrote:

Hi,

I have an xml code which searches for a drug names from a kegg in an article. the list of drug names are given in the http://rest.kegg.jp/list/drug . the code is working fine and gives me the desired output. but now problem is the list of drug mentioned in kegg has some names which arent drug and need to be excluded like the first name in the list is water. how could i remove these names during search. am sharing ma code.

<Content type="html" view="profile">

    //This will hold all the drug code
    //The key/index will be the drug names
    var drugReference = [];        
    //The url using which master list of drugs can be downloaded
    var drugListUrl = "http://rest.kegg.jp/list/drug";
    //Prefix of the url using which specifications of a particular drug
    //can be fetched
    var drugSpecsUrlPrefix = "http://www.kegg.jp/entry/"
    //This will hold the entire article content
    var articleContent = "";
    //This will hold the drugs to be ignored
    var ignoredDrugNames = [ "water","1-","15-","4-" ];

    //This method ensures the button and cursor
    //are in appropriate states when the drug find
    //action is in progress
    //While in progress disables the div that shows
    //the drug names that are found in the article
    function drugFindInProgress() {
        document.getElementById("listterms").style.display="none";            
        document.getElementById("submit").disabled=true;
        document.getElementById("submit").value="Finding Drugs..";            
        document.body.style.cursor = 'wait';            
    }

    //This method ensures that the button and cursor
    //are in appropriate states after the drug find
    //action is completed
    //After completion the div that shows
    //the drug names that are found in the article
    function drugFindCompleted() {
        document.getElementById("listterms").style.display="block";
        document.getElementById("submit").value="Find Drugs";
        document.getElementById("submit").disabled=false;
        document.body.style.cursor = 'default';            
    }

    //This method is used to make a call to retrieve the article content
    function getContent(){
       drugFindInProgress();           
       gadgets.sciverse.getArticleContent(getContentCallback);           
    }

    //The getContent callback function 
    //After retrieving the article content make a call to the function
    //that will link the drug names
    function getContentCallback(response){
        if (response != null){            
            articleContent = response.toUpperCase();                
            linkText();
        }else{
            drugFindCompleted();
            alert("Sorry!!!. Unable to retrieve the article content");
        }            
    }

    //This method is used to make a request to get the entire list of drug names
    function linkText(){             
        var params ;
        gadgets.sciverse.makeRequest(drugListUrl, requestCallBack, params);

    }

    //The makeRequest callback function
    function requestCallBack (response){            
        //Get the contents of the drug listing
        var relevantData = response.text;
        //Each line represents a particular drug. 
        //Split and get each drug into an array
        var drugs = relevantData.split("\n");
        //Array to hold the terms to be linked in the article
        var terms  = [];

        for(var i=0; i < drugs.length; i++) {                
            //Each line will have a drug code and multiple drug names
            //seperated by a tab. Split the drug code and names first
            var drug = drugs[i].split('\t');
            //This check is to avoid invalid entries like empty lines
            //Ideally a drug should have a drug code and name
            //seperated by a tab
            if(drug.length != 2){
                continue;
            }
            var drugCode = drug[0];    
            var drugNames = drug[1].split(";");

            //Split the multiple drug names and map each name to the drug code
            for(var j = 0; j < drugNames.length ; j++){                        
                var temp = drugNames[j].toUpperCase();
                //From the drug names, extract only the names
                //and exclude the words in the paranthesis
                //Ex : dr:D07380    Letosteine (INN); Viscotiol (TN)
                //we will extract only the  words LETOSTEINE
                //and VISCOTIOL and then map these to the drug code dr:D07380
                //we do not consider (INN) and (TN) which are part of the
                //drug names. This is because in an article we may not
                //find the terms such as the ones in the paranthesis
                var index = temp.lastIndexOf("(");                    
                if(index > 0){
                    temp = temp.substring(0,index);
                }
                //Trim any extra whitespace characters and get the drug name
                var actDrugName = $.trim(temp);                    
                //Store the drug code with the drug name as the key to it
                drugReference[actDrugName] = drugCode;
                //If the drug name is found in the article content
                //then add it to the list of terms to be linked                    
                if(jQuery.inArray(actDrugName,ignoredDrugNames) > -1){
                    continue;
                } else if(jQuery.inArray(actDrugName,terms) == -1 
                  && articleContent.indexOf(actDrugName) > -1){
                    //Following code is to consider the term
                    //only if it matches as a whole word
                    var drugNameRegExp = "\\b(" + actDrugName + ")\\b"; 
                    if(articleContent.search(drugNameRegExp) > -1){                            
                        terms.push(actDrugName);
                    }                        
                }
            }
        }

        //other required parameters to be used while linking
        var categories = ["all"];
        var frequency = "every";
        var occurrence = 1;

        // Link the drug names that have been added to the terms array
        gadgets.sciverse.linkText(terms,categories,frequency,occurrence,'linkTextCallback',linkTextCallback);

        //Following code is used to show the drugs that are present in the article
        //in the drug finder window
        if(terms.length > 0){                

            var message="Following drugs were found in the article :";
            var divContent = "
" + message +"
    "; for(var i = 0; i < terms.length; i++){ divContent = divContent+"
  • "+terms[i]+"
  • "; } divContent=divContent + "
"; document.getElementById("listterms").innerHTML=divContent; } //The drug find action has completed drugFindCompleted(); } // The linkText callback function. function linkTextCallback(term, posx, posy){ openDrugSpecs(term); } //This method is used to open a new window //with specifications of the drug on which //the user clicked function openDrugSpecs(term){ var drugName = term.toUpperCase(); //Get the drug code for the drug that has been selected var drugCode = drugReference[drugName] //Open window with the specifications for the drug that is selected var drugSpecsUrl = drugSpecsUrlPrefix + drugCode; window.open(drugSpecsUrl,'DrugSpecifications','fullscreen=1,resizable=1,scrollbars=1'); } //Resize the gadget on load gadgets.util.registerOnLoadHandler(function() {gadgets.window.adjustHeight();}); </script>
Find Drug Names from the current article.



]]>

</Content>

</Module>

kegg • 3.2k views
ADD COMMENTlink modified 7.3 years ago by Damian Kao15k • written 7.3 years ago by AP60

Wasn't this question closed this morning? This question is cross-posted here: http://www.codingforums.com/showthread.php?t=267705

ADD REPLYlink written 7.3 years ago by Leonor Palmeira3.7k

I closed the question instead of deleting it, so that the content of the answer can be of use to readers.

ADD REPLYlink modified 7.3 years ago • written 7.3 years ago by Leonor Palmeira3.7k
0
gravatar for Damian Kao
7.3 years ago by
Damian Kao15k
USA
Damian Kao15k wrote:

This is really more of a programming question than bioinformatics....

Your callback function already has a bunch of statements for checking the data. The variable drugNames is an array of drug Names. You can just loop through drugNames and see if the items in the array match items in your ignoredDrugName array.

Here is a rough way to do it:(untested)

var ignore = false;
for (var x = 0, countX = drugNames.length ; i < countX ; x ++ ) {
   for (var y = 0, countY = ignoredDrugName.length ; i < countY ; y++) {
      if (drugNames[x] == ignoredDrugName[y]) {
         ignore = true;
         break;
      }
   }
}

if (ignore) continue;
ADD COMMENTlink modified 7.3 years ago • written 7.3 years ago by Damian Kao15k
Please log in to add an answer.
The thread is closed. No new answers may be added.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2313 users visited in the last hour