Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

.xyz - mol file inputter method for moleculer viewer in java+JOGL

  1. Jan 22, 2012 #1
    So basically i am developing a molecular viewer in java with opengl bindings (JOGL) and i am having problems with creating an importFile() method. This method will be responsible for importing all the data from .xyz (mol) files. It is a crucial point because the whole 3D Opengl part will rely on the data imported by this method.
    153

    Si -8.2202000000 -5.6491900000 -1.5237200000
    Si -7.3205700000 -7.6948600000 -0.7860500000
    Si -6.3849500000 -3.8557600000 -4.4003200000
    Si -6.3816400000 -5.7792300000 -3.0557700000
    Si -4.4540900000 -1.9259100000 -7.0791000000

    Here is an example from an .xyz file which contains the number of atoms at the top, then there comes the symbol of each atoms and the x, y, z, coordinates for each atoms of the molecules. OpenGl will create a sphere for each atom in the xyz files and the bonds by the Van der Waals radius values .
    I want to import them for easy usage with OpenGL and to create a basic default table model in java to show the imported data separately.

    I tried to do this method already but without any success.
    Somehow all the coordinates and symbols should be stored in an array which can be accessed by OpenGL and the table.
    I would really appreciate that if someone could help me with a bit of code or advice how i should implement it.

    I dont want to stick to this way of import because i think it is not the best one but looked quite easy at first (i mean with tokenizer) so please share your ideas and codes about this
    Code (Text):
    public void importFile() throws IOException
    {
            //the selected file
            File fileSelected;
            //filenamextensionfilter
            FileNameExtensionFilter fileFilter;
            //create filechooser
            JFileChooser fileChooser = new JFileChooser();
            //arraylist of fileNames
             ArrayList<String> fileNames = new ArrayList<String>();
            //the bufferedreader for importfile method
            BufferedReader inputFile;
            fileFilter = new FileNameExtensionFilter("XYZ", "xyz");
            fileChooser.setFileFilter(fileFilter);
           
            int returnVal = fileChooser.showOpenDialog(fileChooser);
           
            double numberOfAtoms;
            String [] symbols = null;
            Float [] inputX = null;
            Float [] inputY = null;
            Float [] inputZ = null;
           
            if(returnVal == JFileChooser.APPROVE_OPTION)
            {
                fileSelected = fileChooser.getSelectedFile();
                String[] fileName = fileSelected.getName().split("\\.");
                if(fileNames.contains(fileName))
                {
                    int i = fileNames.indexOf(fileName);
                    fileNames.remove(i);
                }
                inputFile = new BufferedReader(new FileReader(fileSelected));
               
                int counter = 0;
               
                StreamTokenizer tokenizer = new StreamTokenizer(inputFile);
               
                //INPUTTING
                if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER) {
                                          throw new IOException("Could not understand format : "
                                           + tokenizer);
                             } else {
                                numberOfAtoms = tokenizer.nval;
                             } // end if
               
                // skip next line  
                         inputFile.readLine();
               
                try {
                   
                for(int i=0; i< numberOfAtoms; i++) {                
                               // a set of four tokens constitute the atom
                   
                    //  d) the symbol  
                    if (tokenizer.nextToken() != StreamTokenizer.TT_WORD) {
                                             throw new IOException("Could not understand format : "
                                               + tokenizer);
                                    } else {
                                          int j = 0;
                                              symbols[j] = tokenizer.sval.toLowerCase();
                                               j++;
                                    } // end if
                                     //  a) the x coordinate              
                                   if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER) {
                                              throw new IOException("Could not understand format : "
                                               + tokenizer);
                                   } else {
                                 int j = 0;
                                     inputX[j] = (float) tokenizer.nval;
                                       j++;
                                   } // end if
                   
                                     //  b) the y coordinate              
                                   if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER) {
                                          throw new IOException("Could not understand format : "
                                               + tokenizer);
                                   } else {
                                            int j = 0;
                        inputY[j] = (float) tokenizer.nval;
                                            j++;
                                   } // end if
                   
                                     //  c) the z coordinate              
                                   if (tokenizer.nextToken() != StreamTokenizer.TT_NUMBER) {
                                             throw new IOException("Could not understand format : "
                                               + tokenizer);
                                     } else {
                                        int j = 0;
                        inputZ[j] = (float) tokenizer.nval;
                                              j++;
                                     } // end if
                   
                                     // and now we can safely add this atom to our list
                                    //molecule.addAtom(symbol, x, y, z, i);
                   
                                    // skip to next line
                                     inputFile.readLine();
                                     tokenizer = new StreamTokenizer(inputFile);
                                      } // end for
                                   } catch (Exception e) {
                                        throw new IOException("Error reading file : " + tokenizer
                                      + "\n Exception is : " + e.toString());
                                 } // end of try .. catch block
               
            //TEST
            System.out.println(numberOfAtoms+ " " + inputX[0] + " " + inputY[0] + " " +inputZ[0] + " "+ symbols[0]);
            // return the "raw" molecule
            return;  
                   
               
            }
        }
     
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?
Draft saved Draft deleted



Similar Discussions: .xyz - mol file inputter method for moleculer viewer in java+JOGL
  1. Emailing Java files (Replies: 2)

  2. File not found in Java (Replies: 0)

Loading...