package signature.chemistry;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class MoleculeReader {
    
    public static Molecule readMolfile(String filename) {
        Molecule molecule = null;
        try {
            File file = new File(filename);
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            List<String> block = new ArrayList<String>();
            while ((line = reader.readLine()) != null) {
                block.add(line);
            }
            molecule = makeMolecule(block);
        } catch (IOException ioe) {
            System.err.println(ioe.toString());
        } catch (Exception e) {
            System.err.println(e.toString());
        }
        return molecule;
    }
    
    public static List<Molecule> readSDFFile(String filename) {
        List<Molecule> molecules = new ArrayList<Molecule>();
        try {
            File file = new File(filename);
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line;
            int i = 0;
            List<String> block = new ArrayList<String>();
            while ((line = reader.readLine()) != null) {
                if (line.startsWith("$$$$")) {
                    Molecule molecule;
                    try {
                        molecule = MoleculeReader.makeMolecule(block); 
                        molecules.add(molecule);
                    } catch (Exception e) {
                        System.err.println("failed for block " + i + " " + e);
                        e.printStackTrace();
                    }
                    block.clear();
                    i++;
                } else {
                    block.add(line);
                }
            }
        } catch (IOException ioe) {
            System.err.println(ioe.toString());
        } 
        return molecules;
    }
    
    private static Molecule makeMolecule(List<String> block) throws Exception {
        Molecule molecule = new Molecule();
        // counts are on the fourth line
        String countLine = block.get(3);
        int atomCount = Integer.parseInt(countLine.substring(0, 3).trim());
        int bondCount = Integer.parseInt(countLine.substring(3, 6).trim());
        
        // atom block starts on the fifth line (4th index)
        int atomLineStart = 4;
        int atomLineEnd   = atomCount + atomLineStart; 
        for (int i = atomLineStart; i < atomLineEnd; i++) {
            String symbol = block.get(i).substring(30, 33).trim();
            int atomIndex = i - atomLineStart;
            molecule.addAtom(atomIndex, symbol);
        }
        
        if ( atomCount > 1) {
        	// bond block starts right after the atom block
        	int bondLineStart = atomLineEnd;
        	int bondLineEnd   = bondLineStart + bondCount;
        	for (int i = bondLineStart; i < bondLineEnd; i++) {
        		String bondLine = block.get(i);
        		int atomNumberA = Integer.parseInt(bondLine.substring(0, 3).trim());
        		int atomNumberB = Integer.parseInt(bondLine.substring(3, 6).trim());
        		int order = Integer.parseInt(bondLine.substring(7, 10).trim());
        		molecule.addBond(atomNumberA - 1, atomNumberB - 1, order);
        	}
        }
        return molecule;
    }

}

@@ -98,42 +98,174 @@
 File
-Reader;%0Aimport java.io.IOException
+InputStream;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileReader;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.InputStreamReader
 ;%0Aim
@@ -344,20 +344,16 @@
 eader %7B%0A
-    
 %0A    pub
@@ -1020,64 +1020,716 @@
     
-public static List%3CMolecule%3E readSDFFile(String filename
+%0A%09/**%0A%09 * Read a list of Molecule from SDFile%0A%09 * @param filename path to SDFile%0A%09 * @return List of Molecules%0A%09 */%0A    public static List%3CMolecule%3E readSDFFile(String filename) %7B%0A%0A        File file = new File(filename);%0A        FileInputStream fi;%0A        try %7B%0A            fi = new FileInputStream(file);%0A            return readSDFfromStream(fi);%0A        %7D catch (FileNotFoundException e) %7B%0A            System.err.println(e.toString());%0A        %7D%0A        return null;%0A    %7D%0A    %0A%09/**%0A%09 * Read a list of Molecule from an InputStream, providing SDFile contents%0A%09 * @param stream InputStream to read from%0A%09 * @return List of Molecules%0A%09 */%0A    public static List%3CMolecule%3E readSDFfromStream(InputStream stream
 ) %7B%0A
@@ -1820,40 +1820,73 @@
     
-File file = new File(filen
+InputStreamReader ir=new InputStreamReader(stre
 am
-e
 );%0A
+            %0A
     
@@ -1936,36 +1936,18 @@
 dReader(
-new FileReader(file)
+ir
 );%0A     
@@ -3580,17 +3580,16 @@
     if (
- 
 atomCoun
@@ -3605,17 +3605,20 @@
         
-%09
+    
 // bond 
@@ -3657,33 +3657,36 @@
 m block%0A        
+  
-%09
+  
 int bondLineStar
@@ -3710,17 +3710,20 @@
         
+    
-%09
 int bond
@@ -3730,18 +3730,16 @@
 LineEnd 
-  
 = bondLi
@@ -3767,17 +3767,20 @@
         
-%09
+    
 for (int
@@ -3831,18 +3831,24 @@
         
-%09%09
+        
 String b
@@ -3871,34 +3871,40 @@
 get(i);%0A        
-%09%09
+        
 int atomNumberA 
@@ -3905,16 +3905,37 @@
 mberA = 
+%0A                    
 Integer.
@@ -3981,26 +3981,32 @@
 ));%0A        
-%09%09
+        
 int atomNumb
@@ -4011,16 +4011,37 @@
 mberB = 
+%0A                    
 Integer.
@@ -4091,18 +4091,24 @@
         
-%09%09
+        
 int orde
@@ -4175,10 +4175,16 @@
     
-%09%09
+        
 mole

Patch from Ola