Example usage for java.io DataInputStream readLine

List of usage examples for java.io DataInputStream readLine

Introduction

In this page you can find the example usage for java.io DataInputStream readLine.

Prototype

@Deprecated
public final String readLine() throws IOException 

Source Link

Document

See the general contract of the readLine method of DataInput.

Usage

From source file:org.apache.hadoop.hive.sql.QTestUtil.java

public void addFile(File qf) throws Exception {

    FileInputStream fis = new FileInputStream(qf);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    StringBuilder qsb = new StringBuilder();

    // Look for a hint to not run a test on some Hadoop versions
    Pattern pattern = Pattern.compile("-- EXCLUDE_HADOOP_MAJOR_VERSIONS(.*)");

    // Read the entire query
    boolean excludeQuery = false;
    String hadoopVer = ShimLoader.getMajorVersion();
    while (dis.available() != 0) {
        String line = dis.readLine();

        // While we are reading the lines, detect whether this query wants to be
        // excluded from running because the Hadoop version is incorrect
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            String group = matcher.group();
            int start = group.indexOf('(');
            int end = group.indexOf(')');
            assert end > start;
            // versions might be something like '0.17, 0.19'
            String versions = group.substring(start + 1, end);

            Set<String> excludedVersionSet = new HashSet<String>();
            for (String s : versions.split("\\,")) {
                s = s.trim();//from   w w  w  .j a  va 2 s .  co  m
                excludedVersionSet.add(s);
            }
            if (excludedVersionSet.contains(hadoopVer)) {
                excludeQuery = true;
            }
        }
        qsb.append(line + "\n");
    }
    qMap.put(qf.getName(), qsb.toString());
    if (excludeQuery) {
        System.out.println("Due to the Hadoop Version (" + hadoopVer + "), " + "adding query " + qf.getName()
                + " to the set of tests to skip");
        qSkipSet.add(qf.getName());
    }
    dis.close();
}

From source file:org.apache.hadoop.hive.ql.QTestUtil2.java

public void addFile(File qf) throws Exception {

    FileInputStream fis = new FileInputStream(qf);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    StringBuilder qsb = new StringBuilder();

    // Look for a hint to not run a test on some Hadoop versions
    Pattern pattern = Pattern.compile("-- EXCLUDE_HADOOP_MAJOR_VERSIONS(.*)");

    // Read the entire query
    boolean excludeQuery = false;
    String hadoopVer = ShimLoader.getMajorVersion();
    while (dis.available() != 0) {
        String line = dis.readLine();

        // While we are reading the lines, detect whether this query wants
        // to be/*from  w  ww  .  j av  a2s  .  c  o  m*/
        // excluded from running because the Hadoop version is incorrect
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            String group = matcher.group();
            int start = group.indexOf('(');
            int end = group.indexOf(')');
            assert end > start;
            // versions might be something like '0.17, 0.19'
            String versions = group.substring(start + 1, end);

            Set<String> excludedVersionSet = new HashSet<String>();
            for (String s : versions.split("\\,")) {
                s = s.trim();
                excludedVersionSet.add(s);
            }
            if (excludedVersionSet.contains(hadoopVer)) {
                excludeQuery = true;
            }
        }
        qsb.append(line + "\n");
    }
    qMap.put(qf.getName(), qsb.toString());
    if (excludeQuery) {
        System.out.println("Due to the Hadoop Version (" + hadoopVer + "), " + "adding query " + qf.getName()
                + " to the set of tests to skip");
        qSkipSet.add(qf.getName());
    }
    dis.close();
}

From source file:com.polyvi.xface.extension.advancedfiletransfer.FileUploader.java

/**
 * ??????id?/*from ww w . j  a va 2 s .co  m*/
 *
 * @return true:??false:?
 */
private boolean handleShake() {
    HttpURLConnection httpConnection = null;
    DataInputStream dataInputStream = null;
    String souceid = mFileTransferRecorder.getSourceId(mFilePath, "" + mUploadFileSize);
    try {
        httpConnection = getHttpConnection(mServer);
        // ?
        httpConnection.setRequestProperty("Charset", "UTF-8");
        httpConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpConnection.setRequestProperty("ACTIONNAME", ACTION_NAME_HAND);
        httpConnection.setRequestProperty("RESOURCEID", souceid);
        httpConnection.setRequestProperty("FILENAME", getUploadFileName());
        httpConnection.setRequestProperty("FILESIZE", "" + mUploadFileSize);
        if (HttpURLConnection.HTTP_OK == httpConnection.getResponseCode()) {
            // ????? RESOURCEID=?;BFFORE=?
            dataInputStream = new DataInputStream(httpConnection.getInputStream());
            // ???response?
            handleResponse(dataInputStream.readLine());
            // souceid?
            setSourceId(souceid);
        } else {
            onError(INVALID_URL_ERR);
        }
    } catch (Exception e) {
        onError(INVALID_URL_ERR);
        e.printStackTrace();
        return false;
    } finally {
        if (null != httpConnection) {
            httpConnection.disconnect();
            httpConnection = null;
        }
        // ??
        try {
            if (null != dataInputStream) {
                dataInputStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return true;
}

From source file:android.core.SSLSocketTest.java

/**
 * Does a number of HTTPS requests on some host and consumes the response.
 * We don't use the HttpsUrlConnection class, but do this on our own
 * with the SSLSocket class. This gives us a chance to test the basic
 * behavior of SSL./*from  w  ww .j  a  va  2 s  .  co  m*/
 *
 * @param host      The host name the request is being sent to.
 * @param port      The port the request is being sent to.
 * @param path      The path being requested (e.g. "/index.html").
 * @param outerLoop The number of times we reconnect and do the request.
 * @param innerLoop The number of times we do the request for each
 *                  connection (using HTTP keep-alive).
 * @param delay     The delay after each request (in seconds).
 * @throws IOException When a problem occurs.
 */
private void fetch(SSLSocketFactory socketFactory, String host, int port, boolean secure, String path,
        int outerLoop, int innerLoop, int delay, int timeout) throws IOException {
    InetSocketAddress address = new InetSocketAddress(host, port);

    for (int i = 0; i < outerLoop; i++) {
        // Connect to the remote host
        Socket socket = secure ? socketFactory.createSocket() : new Socket();
        if (timeout >= 0) {
            socket.setKeepAlive(true);
            socket.setSoTimeout(timeout * 1000);
        }
        socket.connect(address);

        // Get the streams
        OutputStream output = socket.getOutputStream();
        PrintWriter writer = new PrintWriter(output);

        try {
            DataInputStream input = new DataInputStream(socket.getInputStream());
            try {
                for (int j = 0; j < innerLoop; j++) {
                    android.util.Log.d("SSLSocketTest", "GET https://" + host + path + " HTTP/1.1");

                    // Send a request
                    writer.println("GET https://" + host + path + " HTTP/1.1\r");
                    writer.println("Host: " + host + "\r");
                    writer.println("Connection: " + (j == innerLoop - 1 ? "Close" : "Keep-Alive") + "\r");
                    writer.println("\r");
                    writer.flush();

                    int length = -1;
                    boolean chunked = false;

                    String line = input.readLine();

                    if (line == null) {
                        throw new IOException("No response from server");
                        // android.util.Log.d("SSLSocketTest", "No response from server");
                    }

                    // Consume the headers, check content length and encoding type
                    while (line != null && line.length() != 0) {
                        //                    System.out.println(line);
                        int dot = line.indexOf(':');
                        if (dot != -1) {
                            String key = line.substring(0, dot).trim();
                            String value = line.substring(dot + 1).trim();

                            if ("Content-Length".equalsIgnoreCase(key)) {
                                length = Integer.valueOf(value);
                            } else if ("Transfer-Encoding".equalsIgnoreCase(key)) {
                                chunked = "Chunked".equalsIgnoreCase(value);
                            }

                        }
                        line = input.readLine();
                    }

                    assertTrue("Need either content length or chunked encoding", length != -1 || chunked);

                    // Consume the content itself
                    if (chunked) {
                        length = Integer.parseInt(input.readLine(), 16);
                        while (length != 0) {
                            byte[] buffer = new byte[length];
                            input.readFully(buffer);
                            input.readLine();
                            length = Integer.parseInt(input.readLine(), 16);
                        }
                        input.readLine();
                    } else {
                        byte[] buffer = new byte[length];
                        input.readFully(buffer);
                    }

                    // Sleep for the given number of seconds
                    try {
                        Thread.sleep(delay * 1000);
                    } catch (InterruptedException ex) {
                        // Shut up!
                    }
                }
            } finally {
                input.close();
            }
        } finally {
            writer.close();
        }
        // Close the connection
        socket.close();
    }
}

From source file:com.curso.listadapter.net.RESTClient.java

/**
 * upload multipart/*from  w  w  w.  j a va  2 s  .  c om*/
 * this method receive the file to be uploaded
 * */
@SuppressWarnings("deprecation")
public String uploadMultiPart(Map<String, File> files) throws Exception {
    disableSSLCertificateChecking();
    Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
    HttpURLConnection conn = null;
    DataOutputStream dos = null;
    DataInputStream inStream = null;
    try {
        URL endpoint = new URL(url);
        conn = (HttpURLConnection) endpoint.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

        dos = new DataOutputStream(conn.getOutputStream());
        String post = "";

        //WRITE ALL THE PARAMS
        for (NameValuePair p : params)
            post += writeMultipartParam(p);
        dos.flush();
        //END WRITE ALL THE PARAMS

        //BEGIN THE UPLOAD
        ArrayList<FileInputStream> inputStreams = new ArrayList<FileInputStream>();
        for (Entry<String, File> entry : files.entrySet()) {
            post += lineEnd;
            post += twoHyphens + boundary + lineEnd;
            String NameParamImage = entry.getKey();
            File file = entry.getValue();
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1 * 1024 * 1024;
            FileInputStream fileInputStream = new FileInputStream(file);

            post += "Content-Disposition: attachment; name=\"" + NameParamImage + "\"; filename=\""
                    + file.getName() + "\"" + lineEnd;
            String mimetype = getMimeType(file.getName());
            post += "Content-Type: " + mimetype + lineEnd;
            post += "Content-Transfer-Encoding: binary" + lineEnd + lineEnd;

            dos.write(post.toString().getBytes("UTF-8"));

            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0) {
                dos.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                inputStreams.add(fileInputStream);
            }

            Log.d("Test", post);
            dos.flush();
            post = "";
        }

        //END THE UPLOAD

        dos.writeBytes(lineEnd);

        dos.writeBytes(twoHyphens + boundary + twoHyphens);
        //            for(FileInputStream inputStream: inputStreams){
        //               inputStream.close();
        //            }
        dos.flush();
        dos.close();
        conn.connect();
        Log.d("upload", "finish flush:" + conn.getResponseCode());
    } catch (MalformedURLException ex) {
        Log.e("Debug", "error: " + ex.getMessage(), ex);
    } catch (IOException ioe) {
        Log.e("Debug", "error: " + ioe.getMessage(), ioe);
    }
    try {
        String response_data = "";
        inStream = new DataInputStream(conn.getInputStream());
        String str;
        while ((str = inStream.readLine()) != null) {
            response_data += str;
        }
        inStream.close();
        return response_data;
    } catch (IOException ioex) {
        Log.e("Debug", "error: " + ioex.getMessage(), ioex);
    }
    return null;
}

From source file:MiGA.StatsSelection.java

public void getImPerfectCompoundSSRs(String[] organisms, int length, boolean flag, int gap)
        throws SQLException, ClassNotFoundException, FileNotFoundException, IOException {

    String statsfile = "";
    for (int i = 0; i < organisms.length; i++) {
        boolean found = false;
        String buffer = new String();
        int seekstart = 0;
        int seekend = 0;
        List<String> ssrs = new ArrayList<String>();
        // 18/11/2013 added starting here
        String filetype = "";
        String filepro = "";

        if (flag) {
            filetype = "organisms";
            filepro = "organisms/" + organisms[i] + "/data/";
            int ret = getOrganismStatus(organisms[i]);
            if (ret == -1)
                indexer = new Indexer(chromosomelist);
            else//from   w  w w.java2 s .c om
                indexer = new Indexer(ret);

        } else {
            filetype = "local";
            filepro = "local/" + organisms[i] + "/data/";
            String indexfile = "local/" + organisms[i] + "/index.txt";
            indexer = new Indexer(indexfile);
        }
        //List<String> files = getFiles(organisms[i], minlen, flag);

        // 18/11/2013 added ending here
        PrintWriter out;
        PrintWriter stats;
        PrintWriter html;
        DataOutputStream lt = null;
        if (filetype.contains("organism")) {

            File f = new File("organisms/" + organisms[i] + "/stats/");
            if (!f.exists()) {
                f.mkdir();
            }

            stats = new PrintWriter(
                    new FileWriter("organisms/" + organisms[i] + "/stats/" + "summary_statistics"
                            + now.toString().replace(':', '_').replace(' ', '_') + ".txt", true));
            lt = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("organisms/" + organisms[i]
                    + "/data/" + now.toString().replace(':', '_').replace(' ', '_') + ".compim")));
            html = new PrintWriter(new FileWriter("organisms/" + organisms[i] + "/stats/" + "summary_statistics"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".html", true));

            File fi = new File("organisms/" + organisms[i] + "/results/");
            if (!fi.exists()) {
                fi.mkdir();
            }

            String toopen = "organisms/" + organisms[i] + "/results/allCompImPerfect_"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".txt";
            statsfile = toopen;
            out = new PrintWriter(toopen);
            out.println("Results for organism: " + organisms[i]
                    + "\t Search Parameters --> Maximum Inter-repeat Region for Imperfect Compound SSRs(bp) : "
                    + gap + " - minimum SSR length(bp): " + length);

        } else {

            File f = new File("local/" + organisms[i] + "/stats/");
            if (!f.exists()) {
                f.mkdir();
            }

            stats = new PrintWriter(new FileWriter("local/" + organisms[i] + "/stats/" + "summary_statistics"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".txt", true));
            lt = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("local/" + organisms[i]
                    + "/data/" + now.toString().replace(':', '_').replace(' ', '_') + ".compim")));
            html = new PrintWriter(new FileWriter("local/" + organisms[i] + "/stats/" + "summary_statistics"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".html", true));

            File fi = new File("local/" + organisms[i] + "/results/");
            if (!fi.exists()) {
                fi.mkdir();
            }
            Calendar calendar = Calendar.getInstance();
            Date now = calendar.getTime();
            String toopen = "local/" + organisms[i] + "/results/allCompImPerfect_"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".txt";
            statsfile = toopen;
            out = new PrintWriter(toopen);
            out.println("Results for project: " + organisms[i]
                    + "\t Search Parameters --> Maximum Inter-repeat Region for Imperfect Compound SSRs(bp) : "
                    + gap + " - minimum SSR length(bp): " + length);

        }

        int countpc = 0;
        int bpcount = 0, Aperc = 0, Tperc = 0, Gperc = 0, Cperc = 0;

        while (indexer.hasNext()) {
            String files = filepro + indexer.getNextFileName();
            DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(files)));
            //PrintWriter out = new PrintWriter(files + "-minlentgh_" + length + "_ImPerfect.stats");
            boolean eof = false;
            while (!eof) {
                try {
                    SSR = new ArrayList<String>();
                    repeats = new ArrayList<Integer>();
                    EndOfSsr = new ArrayList<Integer>();
                    start = new ArrayList<Integer>();

                    int len = in.readInt();
                    int line = in.readInt();
                    for (int k = 0; k < len; k++) {
                        //THIS
                        String temp = in.readUTF();
                        if (!temp.contains("N")) {
                            SSR.add(temp);
                            EndOfSsr.add(in.readInt());
                            repeats.add(in.readInt());

                            start.add(EndOfSsr.get(k) - (SSR.get(k).length() * repeats.get(k)));
                        } else {
                            int junk = in.readInt();
                            junk = in.readInt();
                        }
                    }

                    List<String> SSRlen = new ArrayList<String>();
                    List<Integer> Endlen = new ArrayList<Integer>();
                    List<Integer> repslen = new ArrayList<Integer>();
                    List<Integer> startlen = new ArrayList<Integer>();

                    for (int k = 0; k < SSR.size(); k++) {
                        if (SSR.get(k).length() * repeats.get(k) >= length) {
                            SSRlen.add(SSR.get(k));
                            Endlen.add(EndOfSsr.get(k));
                            repslen.add(repeats.get(k));
                            startlen.add(start.get(k));
                        }
                    }

                    List<Integer> sortedstart = new ArrayList<Integer>();

                    List<Integer> sortedend = new ArrayList<Integer>();
                    for (int t = 0; t < startlen.size(); t++) {
                        sortedstart.add(startlen.get(t));
                        sortedend.add(Endlen.get(t));
                    }

                    Collections.sort(sortedstart);
                    Collections.sort(sortedend);

                    //List<String> tofile = new ArrayList<String>();
                    for (int k = 0; k < sortedstart.size() - 2; k++) {
                        found = false;
                        ssrs.clear();
                        ssrs = new ArrayList<String>();
                        if (sortedstart.get(k + 1) - sortedend.get(k) <= gap
                                && sortedstart.get(k + 1) - sortedend.get(k) >= 0) {
                            seekstart = sortedstart.get(k);
                            while (k < sortedstart.size() - 1
                                    && sortedstart.get(k + 1) - sortedend.get(k) <= gap
                                    && sortedstart.get(k + 1) - sortedend.get(k) >= 0) {
                                for (int c = 0; c < startlen.size(); c++) {
                                    if (sortedstart.get(k) == startlen.get(c)) {
                                        ssrs.add(SSRlen.get(c));
                                    }
                                    if (sortedstart.get(k + 1) == startlen.get(c)) {
                                        ssrs.add(SSRlen.get(c));
                                        seekend = Endlen.get(c);
                                        found = true;
                                    }
                                }
                                k++;
                            }
                            k--;
                        }
                        boolean check = checkallsame(ssrs);
                        boolean check2 = checkalldiff(ssrs);
                        if (found && !check && !check2) {
                            BufferedReader stdin = null;
                            if (flag) {
                                String[] temp = files.split("/");
                                boolean type = CheckForKaryotype(organisms[i]);
                                String newdir = "";
                                if (type) {
                                    newdir = temp[0] + "/" + temp[1] + "/chrom-"
                                            + temp[3].substring(0, temp[3].lastIndexOf('.')) + "-slices.txt";
                                } else {
                                    newdir = temp[0] + "/" + temp[1] + "/slice-"
                                            + temp[3].substring(0, temp[3].lastIndexOf('.')) + ".txt";
                                }

                                stdin = new BufferedReader(new FileReader(newdir));

                            } else {
                                String[] temp = files.split("data/");
                                String newdir = temp[0] + "/" + temp[1].substring(0, temp[1].lastIndexOf('.'))
                                        + ".txt";
                                stdin = new BufferedReader(new FileReader(newdir));
                            }
                            buffer = "";
                            for (int c = 0; c < line; c++) {
                                buffer = stdin.readLine();
                            }
                            //System.out.println(buffer.length() + "\t" + seekstart + "\t" + seekend);
                            int real_end = (line - 1) * 20000 + seekend;
                            int real_start = (line - 1) * 20000 + seekstart;
                            //tofile.add("SSR: "+buffer.substring(seekstart, seekend) + "start-end: "+ real_start + "-" +real_end );
                            countpc++;
                            if (seekstart < 0)
                                seekstart++;
                            String tmp = buffer.substring(seekstart, seekend);
                            bpcount += tmp.length();
                            if (tmp.contains("A")) {
                                Aperc += StringUtils.countMatches(tmp, "A");
                            }
                            if (tmp.contains("T")) {
                                Tperc += StringUtils.countMatches(tmp, "T");
                            }
                            if (tmp.contains("G")) {
                                Gperc += StringUtils.countMatches(tmp, "G");
                            }
                            if (tmp.contains("C")) {
                                Cperc += StringUtils.countMatches(tmp, "C");
                            }
                            out.println("SSR: " + tmp + " start-end: " + real_start + "-" + real_end
                                    + " Path(../data/chromosome): "
                                    + files.substring(0, files.lastIndexOf('.')));
                            stdin.close();

                        }

                    }

                } catch (EOFException e) {
                    eof = true;
                }
            }
            in.close();
        }
        out.close();
        Runtime.getRuntime().exec("notepad " + statsfile);
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StatsSelection.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection con = null;
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306", "biouser", "thesis2012");
        } catch (SQLException ex) {
            Logger.getLogger(StatsSelection.class.getName()).log(Level.SEVERE, null, ex);
        }
        Statement st = null;
        try {
            st = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(StatsSelection.class.getName()).log(Level.SEVERE, null, ex);
        }
        st.executeUpdate("use lobid");

        int seqcount = 0;
        if (filetype.contains("organisms")) {
            ResultSet rs = st.executeQuery(
                    "SELECT end FROM slices INNER JOIN organism WHERE slices.org_id=organism.org_id AND organism.name='"
                            + organisms[i] + "'");
            while (rs.next()) {
                seqcount += Long.parseLong(rs.getString(1));
            }
        } else if (filetype.contains("local")) {
            BufferedReader in = new BufferedReader(new FileReader("local/" + organisms[i] + "/index.txt"));
            int count = countlines("local/" + organisms[i] + "/index.txt");
            for (int c = 0; c < count; c++) {
                String temp = in.readLine();
                BufferedReader tmp = new BufferedReader(
                        new FileReader("local/" + organisms[i] + "/" + temp + ".txt"));

                boolean eof = false;
                while (!eof) {
                    String s = tmp.readLine();
                    if (s != null) {
                        seqcount += s.length();
                    } else {
                        eof = true;
                    }
                }
                tmp.close();
            }
        }

        DecimalFormat round = new DecimalFormat("#.###");

        html.println("<html><h1>******* Compound Imperfect SSRs *******</h1>");
        html.println("<h4>Results for project: " + organisms[i]
                + "</h4><h4>Search Parameters --> Maximum Inter-repeat Region for Imperfect Compound SSRs (bp) : "
                + gap + "</h4><h4>minimum SSR length (bp): " + length + "</h4>");
        html.println(
                "<table border=\"1\"><tr><td> </td><td><b>count</b></td><td><b>bp</b></td><td><b>A%</b></td><td><b>T%</b></td><td><b>C%</b></td><td><b>G%</b></td><td><b>Relative Frequency</b></td><td><b>Abundance</b></td><td><b>Relative Abundance</b></td></tr>");
        html.println("<tr><td><b>Compound Imperf.</b></td><td>" + countpc + "</td><td>" + bpcount + "</td><td>"
                + round.format((float) Aperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Tperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Cperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Gperc * 100 / bpcount) + "</td><td>"
                + round.format((float) countpc / countpc) + "</td><td>"
                + round.format((float) bpcount / seqcount) + "</td><td>"
                + round.format((float) bpcount / bpcount) + "</td></tr>");
        html.println("<tr><td><b>TOTAL</b></td><td>" + countpc + "</td><td>" + bpcount + "</td><td>"
                + round.format((float) Aperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Tperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Cperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Gperc * 100 / bpcount) + "</td><td>"
                + round.format((float) countpc / countpc) + "</td><td>"
                + round.format((float) bpcount / seqcount) + "</td><td>"
                + round.format((float) bpcount / bpcount) + "</td></tr></table></html>");
        html.close();

        stats.println("******* Compound Imperfect SSRs *******");
        stats.println("Results for project: " + organisms[i]
                + "\nSearch Parameters --> Maximum Inter-repeat Region for Imperfect Compound SSRs(bp) : " + gap
                + "\nminimum SSR length(bp): " + length);

        stats.println(
                " ____________________________________________________________________________________________________________________ ");
        stats.println(
                "|               |       |            |       |       |       |       |   Relative    |               |   Relative    |");
        stats.println(
                "|               | count |     bp     |   A%  |   T%  |   C%  |   G%  |   Frequency   |   Abundance   |   Abundance   |");
        stats.println(
                "|===============|=======|============|=======|=======|=======|=======|===============|===============|===============|");
        stats.printf(
                "|Compound Imper.|" + cell(Integer.toString(countpc), 7) + "|"
                        + cell(Integer.toString(bpcount), 12) + "|%s|%s|%s|%s|"
                        + cell((float) countpc / countpc, 15) + "|" + cell((float) bpcount / seqcount, 15) + "|"
                        + cell((float) bpcount / bpcount, 15) + "|\n",
                cell((float) Aperc * 100 / bpcount, 7), cell((float) Tperc * 100 / bpcount, 7),
                cell((float) Cperc * 100 / bpcount, 7), cell((float) Gperc * 100 / bpcount, 7));
        stats.println(
                "|---------------|-------|------------|-------|-------|-------|-------|---------------|---------------|---------------|");

        lt.writeLong(seqcount);
        lt.writeInt(countpc);
        lt.writeInt(bpcount);
        lt.writeInt(Aperc);
        lt.writeInt(Tperc);
        lt.writeInt(Gperc);
        lt.writeInt(Cperc);

        stats.println("|TOTAL          |" + cell(Integer.toString(countpc), 7) + "|"
                + cell(Long.toString(bpcount), 12) + "|" + cell((float) Aperc * 100 / bpcount, 7) + "|"
                + cell((float) Tperc * 100 / bpcount, 7) + "|" + cell((float) Cperc * 100 / bpcount, 7) + "|"
                + cell((float) Gperc * 100 / bpcount, 7) + "|" + cell((float) countpc / countpc, 15) + "|"
                + cell((float) bpcount / seqcount, 15) + "|" + cell((float) bpcount / bpcount, 15) + "|");
        stats.println(
                "|_______________|_______|____________|_______|_______|_______|_______|_______________|_______________|_______________|");
        stats.println("Genome length (bp): " + seqcount);
        stats.println("Relative Frequency: Count of each motif type / total SSR count");
        stats.println("Abundance: bp of each motif type / total sequence bp");
        stats.println("Relative Abundance: bp of each motif type / total microsatellites bp");
        stats.println();
        stats.println();
        stats.close();
        lt.close();

    }

}

From source file:MiGA.StatsSelection.java

public void getCompoundPerfectSSRs(String[] organisms, int length, boolean flag, int gap)
        throws SQLException, ClassNotFoundException, FileNotFoundException, IOException {
    String statsfile = "";
    for (int i = 0; i < organisms.length; i++) {
        boolean found = false;
        String buffer = new String();
        int seekstart = 0;
        int seekend = 0;
        List<String> ssrs = new ArrayList<String>();
        // 18/11/2013 added starting here
        String filetype = "";
        String filepro = "";

        if (flag) {
            filetype = "organisms";
            filepro = "organisms/" + organisms[i] + "/data/";
            int ret = getOrganismStatus(organisms[i]);
            if (ret == -1)
                indexer = new Indexer(chromosomelist);
            else/*from  w w w . j  av a2  s.  co m*/
                indexer = new Indexer(ret);

        } else {
            filetype = "local";
            filepro = "local/" + organisms[i] + "/data/";
            String indexfile = "local/" + organisms[i] + "/index.txt";
            indexer = new Indexer(indexfile);
        }
        //List<String> files = getFiles(organisms[i], minlen, flag);

        // 18/11/2013 added ending here

        PrintWriter stats = null;
        PrintWriter html = null;

        PrintWriter out;
        DataOutputStream lt = null;
        if (filetype.contains("organism")) {

            File f = new File("organisms/" + organisms[i] + "/stats/");
            if (!f.exists()) {
                f.mkdir();
            }

            stats = new PrintWriter(
                    new FileWriter("organisms/" + organisms[i] + "/stats/" + "summary_statistics"
                            + now.toString().replace(':', '_').replace(' ', '_') + ".txt", true));
            lt = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("organisms/" + organisms[i]
                    + "/data/" + now.toString().replace(':', '_').replace(' ', '_') + ".compp")));
            html = new PrintWriter(new FileWriter("organisms/" + organisms[i] + "/stats/" + "summary_statistics"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".html", true));

            File fi = new File("organisms/" + organisms[i] + "/results/");
            if (!fi.exists()) {
                fi.mkdir();
            }
            String toopen = "organisms/" + organisms[i] + "/results/allCompPerfect_"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".txt";
            statsfile = toopen;
            out = new PrintWriter(toopen);
            out.println("Results for organism: " + organisms[i]
                    + "\t Search Parameters --> Maximum Inter-repeat Region for Perfect Compound SSRs (bp) : "
                    + gap + " - minimum SSR length (bp): " + length);
        } else {
            File f = new File("local/" + organisms[i] + "/stats/");
            if (!f.exists()) {
                f.mkdir();
            }

            stats = new PrintWriter(new FileWriter("local/" + organisms[i] + "/stats/" + "summary_statistics"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".txt", true));
            lt = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("local/" + organisms[i]
                    + "/data/" + now.toString().replace(':', '_').replace(' ', '_') + ".compp")));
            html = new PrintWriter(new FileWriter("local/" + organisms[i] + "/stats/" + "summary_statistics"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".html", true));

            File fi = new File("local/" + organisms[i] + "/results/");
            if (!fi.exists()) {
                fi.mkdir();
            }
            String toopen = "local/" + organisms[i] + "/results/allCompPerfect_"
                    + now.toString().replace(':', '_').replace(' ', '_') + ".txt";
            statsfile = toopen;
            out = new PrintWriter(toopen);
            out.println("Results for project: " + organisms[i]
                    + "\t Search Parameters --> Maximum Inter-repeat Region for Perfect Compound SSRs (bp) : "
                    + gap + " - minimum SSR length (bp): " + length);
        }

        int countpc = 0;
        int bpcount = 0, Aperc = 0, Tperc = 0, Gperc = 0, Cperc = 0;

        while (indexer.hasNext()) {
            String files = filepro + indexer.getNextFileName();
            DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(files)));

            boolean eof = false;
            while (!eof) {
                try {
                    SSR = new ArrayList<String>();
                    repeats = new ArrayList<Integer>();
                    EndOfSsr = new ArrayList<Integer>();
                    start = new ArrayList<Integer>();

                    int len = in.readInt();
                    int line = in.readInt();
                    for (int k = 0; k < len; k++) {
                        String temp = in.readUTF();
                        if (!temp.contains("N")) {
                            SSR.add(temp);
                            EndOfSsr.add(in.readInt());
                            repeats.add(in.readInt());
                            int st = EndOfSsr.get(k) - (SSR.get(k).length() * repeats.get(k));
                            if (st >= 0)
                                start.add(st);
                            else
                                start.add(0);
                        } else {
                            int junk = in.readInt();
                            junk = in.readInt();
                        }

                        /*
                        int real_end = end+(line-1)*20000;
                                
                        int start = real_end - (ssr.length()*reps);
                                
                        //SSR.add(ssr);
                        repeats.add(in.readInt());
                        EndOfSsr.add(real_end);
                        this.start.add(start);
                         * 
                         */
                    }

                    List<String> SSRlen = new ArrayList<String>();
                    List<Integer> Endlen = new ArrayList<Integer>();
                    List<Integer> repslen = new ArrayList<Integer>();
                    List<Integer> startlen = new ArrayList<Integer>();

                    for (int k = 0; k < SSR.size(); k++) {
                        if (SSR.get(k).length() * repeats.get(k) >= length) {
                            SSRlen.add(SSR.get(k));
                            Endlen.add(EndOfSsr.get(k));
                            repslen.add(repeats.get(k));
                            startlen.add(start.get(k));
                        }
                    }

                    List<Integer> sortedstart = new ArrayList<Integer>();

                    List<Integer> sortedend = new ArrayList<Integer>();
                    for (int t = 0; t < startlen.size(); t++) {
                        sortedstart.add(startlen.get(t));
                        sortedend.add(Endlen.get(t));
                    }

                    Collections.sort(sortedstart);
                    Collections.sort(sortedend);

                    for (int k = 0; k < sortedstart.size() - 2; k++) {
                        found = false;

                        ssrs = new ArrayList<String>();
                        if (sortedstart.get(k + 1) - sortedend.get(k) <= gap
                                && sortedstart.get(k + 1) - sortedend.get(k) >= 0) {
                            seekstart = sortedstart.get(k);
                            while (k < sortedstart.size() - 1
                                    && sortedstart.get(k + 1) - sortedend.get(k) <= gap
                                    && sortedstart.get(k + 1) - sortedend.get(k) >= 0) {
                                for (int c = 0; c < startlen.size(); c++) {
                                    if (sortedstart.get(k) == startlen.get(c)) {
                                        ssrs.add(SSRlen.get(c));
                                    }
                                    if (sortedstart.get(k + 1) == startlen.get(c)) {
                                        ssrs.add(SSRlen.get(c));
                                        seekend = Endlen.get(c);
                                        found = true;
                                    }
                                }
                                k++;
                            }
                            k--;
                        }
                        boolean check = checkalldiff(ssrs);
                        if (found && check) {
                            BufferedReader stdin = null;
                            String newdir = "";
                            if (flag) {
                                String[] temp = files.split("/");

                                boolean type = CheckForKaryotype(organisms[i]);
                                newdir = "";
                                if (type) {
                                    newdir = temp[0] + "/" + temp[1] + "/chrom-"
                                            + temp[3].substring(0, temp[3].lastIndexOf('.')) + "-slices.txt";
                                } else {
                                    newdir = temp[0] + "/" + temp[1] + "/slice-"
                                            + temp[3].substring(0, temp[3].lastIndexOf('.')) + ".txt";
                                }
                                stdin = new BufferedReader(new FileReader(newdir));

                            } else {
                                String[] temp = files.split("data/");
                                newdir = temp[0] + "/" + temp[1].substring(0, temp[1].lastIndexOf('.'))
                                        + ".txt";
                                stdin = new BufferedReader(new FileReader(newdir));
                            }

                            buffer = "";
                            String prebuf = "";
                            for (int c = 0; c < line; c++) {
                                buffer = stdin.readLine();
                            }
                            stdin.close();
                            //System.out.println(buffer.length() + "\t" + seekstart + "\t" + seekend);
                            int real_end = (line - 1) * 20000 + seekend;
                            int real_start = (line - 1) * 20000 + seekstart;
                            //tofile.add("SSR: "+buffer.substring(seekstart, seekend) + "start-end: "+ real_start + "-" +real_end );
                            countpc++;
                            String tmp = "";
                            if (seekstart < 0) {
                                stdin = new BufferedReader(new FileReader(newdir));
                                for (int c = 0; c < line - 1; c++) {
                                    prebuf = stdin.readLine();
                                }
                                stdin.close();
                                tmp += prebuf.substring(prebuf.length() + seekstart);
                                tmp += buffer.substring(0, seekend);
                            } else
                                tmp = buffer.substring(seekstart, seekend);

                            bpcount += tmp.length();
                            if (tmp.contains("A")) {
                                Aperc += StringUtils.countMatches(tmp, "A");
                            }
                            if (tmp.contains("T")) {
                                Tperc += StringUtils.countMatches(tmp, "T");
                            }
                            if (tmp.contains("G")) {
                                Gperc += StringUtils.countMatches(tmp, "G");
                            }
                            if (tmp.contains("C")) {
                                Cperc += StringUtils.countMatches(tmp, "C");
                            }

                            out.println("SSR: " + tmp + " start-end: " + real_start + "-" + real_end
                                    + " Path(../data/chromosome): "
                                    + files.substring(0, files.lastIndexOf('.')));

                        }
                    }
                } catch (EOFException e) {
                    eof = true;
                }
            }
            in.close();
        }
        out.close();
        Runtime.getRuntime().exec("notepad " + statsfile);

        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(StatsSelection.class.getName()).log(Level.SEVERE, null, ex);
        }
        Connection con = null;
        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306", "biouser", "thesis2012");
        } catch (SQLException ex) {
            Logger.getLogger(StatsSelection.class.getName()).log(Level.SEVERE, null, ex);
        }
        Statement st = null;
        try {
            st = con.createStatement();
        } catch (SQLException ex) {
            Logger.getLogger(StatsSelection.class.getName()).log(Level.SEVERE, null, ex);
        }
        st.executeUpdate("use lobid");

        int seqcount = 0;

        if (filetype.contains("organisms")) {
            ResultSet rs = st.executeQuery(
                    "SELECT end FROM slices INNER JOIN organism WHERE slices.org_id=organism.org_id AND organism.name='"
                            + organisms[i] + "'");
            while (rs.next()) {
                seqcount += Long.parseLong(rs.getString(1));
            }
        } else if (filetype.contains("local")) {
            BufferedReader in = new BufferedReader(new FileReader("local/" + organisms[i] + "/index.txt"));
            int count = countlines("local/" + organisms[i] + "/index.txt");
            for (int c = 0; c < count; c++) {
                String temp = in.readLine();
                BufferedReader tmp = new BufferedReader(
                        new FileReader("local/" + organisms[i] + "/" + temp + ".txt"));

                boolean eof = false;
                while (!eof) {
                    String s = tmp.readLine();
                    if (s != null) {
                        seqcount += s.length();
                    } else {
                        eof = true;
                    }
                }
                tmp.close();
            }
        }

        DecimalFormat round = new DecimalFormat("#.###");

        html.println("<html><h1>******* Compound Perfect SSRs *******</h1>");
        html.println("<h4>Results for project: " + organisms[i]
                + "</h4><h4>Search Parameters --> Maximum Inter-repeat Region for Perfect Compound SSRs (bp) : "
                + gap + "</h4><h4>minimum SSR length (bp): " + length + "</h4>");
        html.println(
                "<table border=\"1\"><tr><td> </td><td><b>count</b></td><td><b>bp</b></td><td><b>A%</b></td><td><b>T%</b></td><td><b>C%</b></td><td><b>G%</b></td><td><b>Relative Frequency</b></td><td><b>Abundance</b></td><td><b>Relative Abundance</b></td></tr>");
        html.println("<tr><td><b>Compound Perf.</b></td><td>" + countpc + "</td><td>" + bpcount + "</td><td>"
                + round.format((float) Aperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Tperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Cperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Gperc * 100 / bpcount) + "</td><td>"
                + round.format((float) countpc / countpc) + "</td><td>"
                + round.format((float) bpcount / seqcount) + "</td><td>"
                + round.format((float) bpcount / bpcount) + "</td></tr>");
        html.println("<tr><td><b>TOTAL</b></td><td>" + countpc + "</td><td>" + bpcount + "</td><td>"
                + round.format((float) Aperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Tperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Cperc * 100 / bpcount) + "</td><td>"
                + round.format((float) Gperc * 100 / bpcount) + "</td><td>"
                + round.format((float) countpc / countpc) + "</td><td>"
                + round.format((float) bpcount / seqcount) + "</td><td>"
                + round.format((float) bpcount / bpcount) + "</td></tr></table></html>");
        html.close();

        stats.println("******* Compound Perfect SSRs *******");
        stats.println("Results for project: " + organisms[i]
                + "\nSearch Parameters --> Maximum Inter-repeat Region for Perfect Compound SSRs (bp) : " + gap
                + "\nminimum SSR length (bp): " + length);

        stats.println(
                " ___________________________________________________________________________________________________________________ ");
        stats.println(
                "|              |       |            |       |       |       |       |   Relative    |               |   Relative    |");
        stats.println(
                "|              | count |     bp     |   A%  |   T%  |   C%  |   G%  |   Frequency   |   Abundance   |   Abundance   |");
        stats.println(
                "|==============|=======|============|=======|=======|=======|=======|===============|===============|===============|");
        stats.printf(
                "|Compound Perf.|" + cell(Integer.toString(countpc), 7) + "|"
                        + cell(Integer.toString(bpcount), 12) + "|%s|%s|%s|%s|"
                        + cell((float) countpc / countpc, 15) + "|" + cell((float) bpcount / seqcount, 15) + "|"
                        + cell((float) bpcount / bpcount, 15) + "|\n",
                cell((float) Aperc * 100 / bpcount, 7), cell((float) Tperc * 100 / bpcount, 7),
                cell((float) Cperc * 100 / bpcount, 7), cell((float) Gperc * 100 / bpcount, 7));
        stats.println(
                "|--------------|-------|------------|-------|-------|-------|-------|---------------|---------------|---------------|");

        lt.writeLong(seqcount);
        lt.writeInt(countpc);
        lt.writeInt(bpcount);
        lt.writeInt(Aperc);
        lt.writeInt(Tperc);
        lt.writeInt(Gperc);
        lt.writeInt(Cperc);

        stats.println("|TOTAL         |" + cell(Integer.toString(countpc), 7) + "|"
                + cell(Long.toString(bpcount), 12) + "|" + cell((float) Aperc * 100 / bpcount, 7) + "|"
                + cell((float) Tperc * 100 / bpcount, 7) + "|" + cell((float) Cperc * 100 / bpcount, 7) + "|"
                + cell((float) Gperc * 100 / bpcount, 7) + "|" + cell((float) countpc / countpc, 15) + "|"
                + cell((float) bpcount / seqcount, 15) + "|" + cell((float) bpcount / bpcount, 15) + "|");
        stats.println(
                "|______________|_______|____________|_______|_______|_______|_______|_______________|_______________|_______________|");
        stats.println("Genome length (bp): " + seqcount);
        stats.println("Relative Frequency: Count of each motif type / total SSR count");
        stats.println("Abundance: bp of each motif type / total sequence bp");
        stats.println("Relative Abundance: bp of each motif type / total microsatellites bp");
        stats.println();
        stats.println();
        stats.close();
        lt.close();

    }

}