/**
   * Connect to ftp server
   *
   * @param server the host you connect
   * @param user the user account to login [anonymous as default]
   * @param passwd the password according to you account, and if the account is anonymous "" as
   *     default
   */
  public void initConnection(String server, String user, String passwd) {
    ftp = new FTPClient();
    try {
      ftp.connect(server);
      ftp.login(user, passwd);
      int reply = ftp.getReplyCode();
      if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();
        LogUtils.log("disconnect after login");
      }
      LogUtils.log("Connected to " + server + " with name [" + user + "]");
    } catch (SocketException e) {

      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  /**
   * download file from remote host and display the downloaded percentage
   *
   * @param folder remote directory
   * @param fileName the file you want to download
   * @param destfolder the destination folder you will store the file
   */
  public void downloadFileInProgress(String folder, String fileName, String destfolder) {
    try {
      ftp.enterLocalPassiveMode();
      ftp.changeWorkingDirectory(folder);
      LogUtils.log("Changing to directory:[" + folder + "]");
      String realFile = destfolder + File.separator + fileName;
      File localFile = new File(realFile);
      FileOutputStream fos = new FileOutputStream(localFile);
      LogUtils.log("Start downloading..");
      FTPFile[] fs = ftp.listFiles();
      DecimalFormat df = new DecimalFormat("#.00%");
      for (FTPFile f : fs) {
        if (f.getName().equals(fileName)) {
          InputStream is = ftp.retrieveFileStream(f.getName());
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
          String line = br.readLine();
          long transfered = 0, total = f.getSize();
          double ratio = 0.0;
          while (line != null) {
            byte[] buff = line.getBytes();
            transfered += buff.length;
            if (transfered * 1.0 / total - ratio >= 0.01) {
              ratio = transfered * 1.0 / total;
              LogUtils.log("Download size : " + df.format(ratio));
            }
            fos.write(buff);
            line = br.readLine();
          }
          is.close();
          ftp.logout();
          ftp.disconnect();

          LogUtils.log("Download done!");
        }
      }
      fos.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  /**
   * The actual calculation function
   *
   * @param dataIn
   * @param fileName
   */
  public void simCalculation(HashMap<String, Double> dataIn, String fileName) {
    Object[] objs = pathways.keySet().toArray();
    int npaths = objs.length;

    NetworkOutput.open(fileName);

    for (int i = 0; i < npaths - 1; i++) {
      HashSet<String> source = pathwayOverlap(pathways.get(objs[i].toString()), geneSet);
      for (int j = i + 1; j < npaths; j++) {
        HashSet<String> target = pathwayOverlap(pathways.get(objs[j].toString()), geneSet);
        // 相似性计算
        double sim = geneSetSim(source, target, dataIn);
        // p值计算
        // double pval = permutePval(pathways.get(objs[i].toString())
        // .size(), pathways.get(objs[j].toString()).size(),
        // nperms, sim, dataIn);
        // pval calculation
        HashSet<String> union =
            unionGenes(pathways.get(objs[i].toString()), pathways.get(objs[j].toString()));
        double pval =
            pValuePermuteLabel(
                pathways.get(objs[i].toString()).size(),
                pathways.get(objs[j].toString()).size(),
                union,
                nperms,
                sim,
                dataIn);
        // 存储数据到csv文件中
        String[] contents = {
          objs[i].toString(), objs[j].toString(), String.valueOf(sim), String.valueOf(pval)
        };

        NetworkOutput.writeRecord(contents);
        LogUtils.log(
            objs[i].toString()
                + "["
                + i
                + "]"
                + objs[j].toString()
                + "["
                + j
                + "] ["
                + npaths
                + "]["
                + pval
                + "]");
      }
    }
    NetworkOutput.close();
  }
 /**
  * calculate the similarity of pathways entrance
  *
  * @param type This indicate the category of network[norm,tumor,semantic]
  * @param fileName The name of file to store the pathway similarity and pvals
  */
 public void similarityCalculation(String type, String fileName, double threshold) {
   if (type.equalsIgnoreCase("norm")) {
     HashMap<String, Double> normNet = GeneInteractionNet.geneNetFilter("normnet.out", threshold);
     simCalculation(normNet, fileName);
   } else if (type.equalsIgnoreCase("tumor")) {
     HashMap<String, Double> tumorNet = GeneInteractionNet.geneNetFilter("tumnet.out", threshold);
     simCalculation(tumorNet, fileName);
   } else if (type.equalsIgnoreCase("semantic")) {
     HashMap<String, Double> semNet = GeneInteractionNet.geneNetFilter("semnet.out", threshold);
     simCalculation(semNet, fileName);
   } else {
     LogUtils.log("Type error:[type should be  in [norm,tumor,semantic] ], but given:" + type);
   }
 }
  /**
   * download file from remote host without progress percentage display
   *
   * @param folder remote directory
   * @param fileName the file you want to download
   * @param destfolder the destination folder you will store the file
   */
  public void downloadFile(String folder, String fileName, String destfolder) {
    try {
      ftp.enterLocalPassiveMode();
      ftp.changeWorkingDirectory(folder);
      LogUtils.log("Changing to directory:[" + folder + "]");
      String realFile = destfolder + File.separator + fileName;
      File localFile = new File(realFile);
      FileOutputStream fos = new FileOutputStream(localFile);
      FileInputStream fis = new FileInputStream(localFile);
      LogUtils.log("Start downloading..");
      FTPFile[] fs = ftp.listFiles();
      for (FTPFile f : fs) {
        if (f.getName().equals(fileName)) {
          ftp.retrieveFile(f.getName(), fos);
          LogUtils.log("Download done!");
          break;
        }
      }
      fos.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }