code - gdstn/smiles_converter GitHub Wiki

//I DO NOT KNOW HOW TO USE GITHUB YET //Molecules to sSiles; created by Matt Goodstein //WARNING: ALGORITHM IS (PROBABLY) BUG-FREE. CODE AND IMPLEMENTATION MIGHT NOT BE. STILL NEEDS TO REMOVE COMPILE-TIME ERRORS. class node{ boolean visited; node(){ visited=false; } } //End is empty valences; doublesig indicates a double bond taking up another valence slot; hydrogen is hydrogen
node end=new node(); node doublesig=new node(); node hydrogen=new node(); //Total number of cycles; increments after every cycle addition int cyclenumber=0;

class atom extends node{ //if it is int valence; char symbol; node[] bonded=new node[8]; String [] terms=new String[8]; boolean [] cyles=new boolean[8]; atom(int v, char s, node[] atoms){ super(); valence=v; symbol=s; bonded=atoms; terms={"","","","","","","",""} } int search(atom target){ for (int x=0; x<valence; x++){ if (bonded[x]==target){ return x; } } }

//depth first search; creates a list of one term for each bonded String printsmiles(atom parent){ this.visited=true; String ret=""+symbol; for(int x=0; x<start.valence; x++){ //prevents the algorithm from going back or repeating cycles. if(this.cycles[c]||this.bonded[x]==parent){ continue; } //Cycle-handling if(this.bonded[x].visited){ int c=bonded[x].search(this); bonded[x].terms[c]=cyclenumber; bonded[x].cycles[c]=true; this.terms(x)=cyclenumber; this.cycles[x]=true; cyclenumber++; }

if(this.bonded[x]==hydrogen||this.bonded[x]=end){
  continue;
}
//Double/triple bond-handling
else if(this.bonded[x]==doublesig){
  if(terms[x-1]=parent||terms[x-2]==parent){
    continue;
  }
  if(terms[x-1][0]='='){
    terms[x-1][0]='#';
  }
  else{
    terms[x-1]='='+terms[x-1];
  }
}
//recursively prints it 
else{
  terms[x]=bonded[x].printsmiles(this);
}

} //Prints each bonded goup in parentheses. for(int z=0; z<valence; z++){ if(!cycles[z]){ ret=ret + '('; } ret+=terms[z]; if(!cycles[z]){ ret=ret + ')'; } } return ret; } } //how this is specifically going to be implemented is going to take some work. //this is a test; may need to initialize carbons as containing all hydrogens and then modify them atom carbon1=new atom('C', 4, {hydrogen, carbon2, carbon3, doublesig}); atom carbon2=new atom('C', 4, {hydrogen, hydrogen, carbon3, carbon1}); atom carbon3=new atom('C', 4, {hydrogen, carbon2, carbon1, doublesig});