private double calcMean(
     HashMap<Integer, String> singleGeneCaseValueMap,
     String groupType,
     String profileStableId) { // group type: altered or unaltered
   switch (groupType) {
     case "altered":
       int _index_altered = 0;
       double[] alteredArray = new double[alteredSampleIds.size()];
       for (Integer alteredSampleId : alteredSampleIds) {
         if (singleGeneCaseValueMap.containsKey(alteredSampleId)) {
           if (profileStableId.indexOf("rna_seq") != -1) {
             try {
               alteredArray[_index_altered] =
                   Math.log(Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId)))
                       / Math.log(2);
             } catch (NumberFormatException e) {
               e.getStackTrace();
             }
           } else {
             try {
               alteredArray[_index_altered] =
                   Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId));
             } catch (NumberFormatException e) {
               e.getStackTrace();
             }
           }
           _index_altered += 1;
         }
       }
       return StatUtils.mean(alteredArray);
     case "unaltered":
       int _index_unaltered = 0;
       double[] unalteredArray = new double[unalteredSampleIds.size()];
       for (Integer unalteredSampleId : unalteredSampleIds) {
         if (singleGeneCaseValueMap.containsKey(unalteredSampleId)) {
           if (profileStableId.indexOf("rna_seq") != -1) {
             try {
               unalteredArray[_index_unaltered] =
                   Math.log(Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId)))
                       / Math.log(2);
             } catch (NumberFormatException e) {
               e.getStackTrace();
             }
           } else {
             try {
               unalteredArray[_index_unaltered] =
                   Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId));
             } catch (NumberFormatException e) {
               e.getStackTrace();
             }
           }
           _index_unaltered += 1;
         }
       }
       return StatUtils.mean(unalteredArray);
     default:
       return Double.NaN; // error
   }
 }
  private double runTTest(HashMap<Integer, String> singleGeneCaseValueMap, String profileStableId)
      throws IllegalArgumentException {

    double[] unalteredArray = new double[unalteredSampleIds.size()];
    double[] alteredArray = new double[alteredSampleIds.size()];
    int _index_unaltered = 0, _index_altered = 0;

    for (Integer alteredSampleId : alteredSampleIds) {
      if (singleGeneCaseValueMap.containsKey(alteredSampleId)) {
        if (profileStableId.indexOf("rna_seq") != -1) {
          try {
            alteredArray[_index_altered] =
                Math.log(Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId)))
                    / Math.log(2);
          } catch (NumberFormatException e) {
            e.getStackTrace();
          }
        } else {
          try {
            alteredArray[_index_altered] =
                Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId));
          } catch (NumberFormatException e) {
            e.getStackTrace();
          }
        }
        _index_altered += 1;
      }
    }
    for (Integer unalteredSampleId : unalteredSampleIds) {
      if (singleGeneCaseValueMap.containsKey(unalteredSampleId)) {
        if (profileStableId.indexOf("rna_seq") != -1) {
          try {
            unalteredArray[_index_unaltered] =
                Math.log(Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId)))
                    / Math.log(2);
          } catch (NumberFormatException e) {
            e.getStackTrace();
          }
        } else {
          try {
            unalteredArray[_index_unaltered] =
                Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId));
          } catch (NumberFormatException e) {
            e.getStackTrace();
          }
        }
        _index_unaltered += 1;
      }
    }

    if (alteredArray.length < 2 || unalteredArray.length < 2) return Double.NaN;
    else {
      double pvalue = TestUtils.tTest(alteredArray, unalteredArray);
      return pvalue;
    }
  }
 private double calcSTDev(
     HashMap<Integer, String> singleGeneCaseValueMap, String groupType, String profileStableId) {
   switch (groupType) {
     case "altered":
       DescriptiveStatistics stats_altered = new DescriptiveStatistics();
       for (Integer alteredSampleId : alteredSampleIds) {
         if (singleGeneCaseValueMap.containsKey(alteredSampleId)) {
           if (profileStableId.indexOf("rna_seq") != -1) {
             try {
               stats_altered.addValue(
                   Math.log(Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId)))
                       / Math.log(2));
             } catch (NumberFormatException e) {
               e.getStackTrace();
             }
           } else {
             try {
               stats_altered.addValue(
                   Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId)));
             } catch (NumberFormatException e) {
               e.getStackTrace();
             }
           }
         }
       }
       return stats_altered.getStandardDeviation();
     case "unaltered":
       DescriptiveStatistics stats_unaltered = new DescriptiveStatistics();
       for (Integer unalteredSampleId : unalteredSampleIds) {
         if (singleGeneCaseValueMap.containsKey(unalteredSampleId)) {
           if (profileStableId.indexOf("rna_seq") != -1) {
             try {
               stats_unaltered.addValue(
                   Math.log(Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId)))
                       / Math.log(2));
             } catch (NumberFormatException e) {
               e.getStackTrace();
             }
           } else {
             try {
               stats_unaltered.addValue(
                   Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId)));
             } catch (NumberFormatException e) {
               e.getStackTrace();
             }
           }
         }
       }
       return stats_unaltered.getStandardDeviation();
     default:
       return Double.NaN; // error
   }
 }
Ejemplo n.º 4
0
  /**
   * Get multiple bonuses
   *
   * @param ssn
   * @param multiplier
   * @return
   */
  private MultipleBonus[] getBonuses(String[] ssn, String[] multiplierStr) {

    // 2 input arrays do not have the same length
    MultipleBonus[] multipleBonus = new MultipleBonus[multiplierStr.length];
    for (int i = 0; i < multiplierStr.length; i++) {

      MultipleBonus b = new MultipleBonus();
      int iSsn = (int) i / 3;
      log.info("(i, iSsn) = (" + i + ", " + iSsn + ")");
      try {
        Ssn _ssn = new Ssn();
        _ssn.setSsn(ssn[iSsn]);
        b.setSsn(new Ssn());
        double m = Double.valueOf(multiplierStr[i]);
        b.setBonus(m);

      } catch (NumberFormatException e) {
        log.error(e.getStackTrace());
        b.setBonus(-1); // if multiplier is not filled correctly

      } finally {
        String msg = String.format("bonus[%d]=%s", i, b.toString());
        log.info(msg);
        multipleBonus[i] = b;
      }
    }
    return multipleBonus;
  }
 /**
  * Thrown to indicate that the application has attempted to convert a string to one of the numeric
  * types, but that the string does not have the appropriate format.
  */
 @ExceptionHandler(NumberFormatException.class)
 public ResponseEntity<Map<String, Object>> handleNumberFormatException(
     NumberFormatException error) {
   Map<String, Object> errorMap = new HashMap<String, Object>();
   errorMap.put("hasErrors", "true");
   errorMap.put("developerMessage", error.getMessage());
   errorMap.put("userMessage", "");
   errorMap.put("moreInfo", error.getStackTrace()[0]);
   errorMap.put("errorCode", HttpStatus.BAD_REQUEST);
   error.printStackTrace();
   return new ResponseEntity<Map<String, Object>>(errorMap, HttpStatus.INTERNAL_SERVER_ERROR);
 }
  /**
   * Get multiple bonuses
   *
   * @param ssn
   * @param multiplier
   * @return
   */
  private Bonus[] getBonuses(String[] ssn, String[] multiplierStr) throws NamingException {

    // If there's any configuration problem, take a look at:
    //
    // EJB invocations from a remote client using JNDI
    // https://docs.jboss.org/author/display/AS71/EJB+invocations+from+a+remote+client+using+JNDI
    //
    // Remote EJB invocations via JNDI - EJB client API or remote-naming project
    // https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project
    //
    // javax.naming.NamingException: Failed instantiate InitialContextFactory
    // org.jboss.naming.remote.client.InitialContextFactory from classloader
    // http://stackoverflow.com/questions/13065025/javax-naming-namingexception-failed-instantiate-initialcontextfactory-org-jboss
    //
    // Cannot connect to Remote EJB deployed to 7.1 instance, from another 7.1 instance
    // https://developer.jboss.org/thread/195516?tstart=0

    Properties props = new Properties();
    props.put(
        Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
    props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
    props.put(Context.PROVIDER_URL, "remote://192.168.1.13:4447");
    props.put(Context.SECURITY_PRINCIPAL, "testuser");
    props.put(Context.SECURITY_CREDENTIALS, "testpassword");
    Context ctx = new InitialContext(props);
    CalculatorRemote calculator = (CalculatorRemote) ctx.lookup(JNDI_BINDING);

    // assume that 2 input arrays have the same length
    Bonus[] bonus = new Bonus[ssn.length];
    for (int i = 0; i < ssn.length; i++) {

      Bonus b = new Bonus();
      try {
        b.setSsn(ssn[i]);
        double m = Double.valueOf(multiplierStr[i]);
        b.setMultiplier(m);
        b.setBonus(Double.toString(calculator.bonus(m)));

      } catch (NumberFormatException e) {
        log.error(e.getStackTrace());
        b.setBonus(null); // if multiplier is not filled correctly

      } finally {
        String msg = String.format("bonus[%d]=%s", i, b.toString());
        log.debug(msg);
        bonus[i] = b;
      }
    }
    return bonus;
  }
Ejemplo n.º 7
0
  /**
   * Check the correct validity of the parameter value
   *
   * @param replacement : the parameter
   * @param parType : the parameter type
   * @param attribute : the attribute
   * @return
   */
  private static String checkParType(String replacement, String parType, String attribute)
      throws NumberFormatException {
    logger.debug("IN");
    String toReturn = replacement;
    // check if numbers are number otherwise throw exception
    try {
      if (parType.equalsIgnoreCase("NUMBER")) {
        toReturn = replacement.replaceAll("\'", "");
        toReturn = replacement.replaceAll(";", ",");
        if (toReturn.indexOf(",") >= 0) {
          // multivalues management
          String[] values = toReturn.split(",");
          for (int i = 0; i < values.length; i++) {
            Double double1 = Double.valueOf(values[i]);
          }
        } else {
          Double double1 = Double.valueOf(toReturn);
        }
      }
    } catch (NumberFormatException e) {
      String me = e.getMessage();
      me += " - attribute " + attribute + " should be of number type";
      NumberFormatException numberFormatException = new NumberFormatException(attribute);
      numberFormatException.setStackTrace(e.getStackTrace());
      throw numberFormatException;
    }

    // check when type is RAW that there are not '' surrounding values (in case remove them)
    // remotion done here in order to not modify SpagoBI Analytical driver of type string handling
    try {
      if (parType.equalsIgnoreCase("RAW")) {
        logger.debug("Parmaeter is Raw type, check if there are '' and remove them");
        if (toReturn.length() > 2) {
          if (toReturn.startsWith("'")) {
            logger.debug("first character is ', remove");
            toReturn = toReturn.substring(1);
          }
          if (toReturn.endsWith("'")) {
            logger.debug("last character is ', remove");
            toReturn = toReturn.substring(0, replacement.length() - 1);
          }
        }
      }
    } catch (Exception e) {
      logger.error("Error in removing the '' in value " + toReturn + " do not substitute them");
    }

    logger.debug("OUT");
    return toReturn;
  }
Ejemplo n.º 8
0
 public Bitmap readMjpegFrame() throws IOException {
   mark(FRAME_MAX_LENGTH);
   int headerLen = getStartOfSequence(this, SOI_MARKER);
   reset();
   byte[] header = new byte[headerLen];
   readFully(header);
   try {
     mContentLength = parseContentLength(header);
   } catch (NumberFormatException nfe) {
     nfe.getStackTrace();
     Log.d(TAG, "catch NumberFormatException hit", nfe);
     mContentLength = getEndOfSeqeunce(this, EOF_MARKER);
   }
   reset();
   byte[] frameData = new byte[mContentLength];
   skipBytes(headerLen);
   readFully(frameData);
   return BitmapFactory.decodeStream(new ByteArrayInputStream(frameData));
 }
Ejemplo n.º 9
0
  private void handleGetDistance(String[] params) {
    if (params.length < 4) {
      return;
    }
    try {

      double lat1 = Double.valueOf(params[0]);
      double lon1 = Double.valueOf(params[1]);
      double lat2 = Double.valueOf(params[2]);
      double lon2 = Double.valueOf(params[3]);

      double distance = -1;

      LatLng p1 = new LatLng(lat1, lon1);
      LatLng p2 = new LatLng(lat2, lon2);
      distance = DistanceUtil.getDistance(p1, p2);

      // // 判断,如果是小距离
      // if ((Math.abs(lat1 - lat2) < MyDistanceUtils.SMALL_DISTANCE_FLAG)
      // && (Math.abs(lon1 - lon2) < MyDistanceUtils.SMALL_DISTANCE_FLAG))
      // {
      // distance = MyDistanceUtils.getShortDistance(lon1, lat1, lon2,
      // lat2);
      // }
      // // 大距离
      // else {
      // distance = MyDistanceUtils.getLongDistance(lon1, lat1, lon2,
      // lat2);
      // }

      jsCallback(EBaiduMapUtils.MAP_FUN_CB_GET_DISTANCE, 0, EUExCallback.F_C_TEXT, "" + distance);

    } catch (NumberFormatException e) {
      e.getStackTrace();
    }
  }
  @Override
  public SampleResult sample(Entry entry) {
    ServiceSocket socket = null;
    SampleResult sampleResult = new SampleResult();
    sampleResult.setSampleLabel(getName());
    sampleResult.setDataEncoding(getContentEncoding());

    StringBuilder errorList = new StringBuilder();
    errorList.append("\n\n[Problems]\n");

    boolean isOK = false;

    String payloadMessage = getRequestPayload();
    sampleResult.setSamplerData(payloadMessage);
    sampleResult.sampleStart();

    try {
      socket = getConnectionSocket();
      if (socket == null) {
        sampleResult.setResponseCode("500");
        sampleResult.setSuccessful(false);
        sampleResult.sampleEnd();
        sampleResult.setResponseMessage(errorList.toString());
        errorList.append(" - Connection couldn't be opened").append("\n");
        return sampleResult;
      }

      if (!payloadMessage.isEmpty()) {
        socket.sendMessage(payloadMessage);
      }

      int responseTimeout = Integer.parseInt(getResponseTimeout());
      socket.awaitClose(responseTimeout, TimeUnit.MILLISECONDS);

      if (socket.getResponseMessage() == null || socket.getResponseMessage().isEmpty()) {
        sampleResult.setResponseCode("204");
      }

      if (socket.getError() != 0) {
        isOK = false;
        sampleResult.setResponseCode(socket.getError().toString());
      } else {
        sampleResult.setResponseCodeOK();
        isOK = true;
      }

      sampleResult.setResponseData(socket.getResponseMessage(), getContentEncoding());

    } catch (URISyntaxException e) {
      errorList
          .append(" - Invalid URI syntax: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    } catch (IOException e) {
      errorList
          .append(" - IO Exception: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    } catch (NumberFormatException e) {
      errorList
          .append(" - Cannot parse number: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    } catch (InterruptedException e) {
      errorList
          .append(" - Execution interrupted: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    } catch (Exception e) {
      errorList
          .append(" - Unexpected error: ")
          .append(e.getMessage())
          .append("\n")
          .append(StringUtils.join(e.getStackTrace(), "\n"))
          .append("\n");
    }

    sampleResult.sampleEnd();
    sampleResult.setSuccessful(isOK);

    String logMessage = (socket != null) ? socket.getLogMessage() : "";
    sampleResult.setResponseMessage(logMessage + errorList);
    return sampleResult;
  }
  private double runFisherExactTest(
      HashMap<Integer, String> singleGeneCaseValueMap, String profileType) {

    int a = 0, // non altered
        b = 0, // x non altered, y altered
        c = 0, // x altered, y non altered
        d = 0; // both alered

    for (Integer alteredSampleId : alteredSampleIds) {
      if (singleGeneCaseValueMap.containsKey(alteredSampleId)) {
        if (profileType.equals(GeneticAlterationType.COPY_NUMBER_ALTERATION.toString())) {
          try {
            Double value = Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId));
            if (copyNumType.equals("del")) {
              if (value == -2.0) {
                d += 1;
              } else {
                c += 1;
              }
            } else if (copyNumType.equals("amp")) {
              if (value == 2.0) {
                d += 1;
              } else {
                c += 1;
              }
            }
          } catch (NumberFormatException e) {
            e.getStackTrace();
          }
        } else if (profileType.equals(GeneticAlterationType.MUTATION_EXTENDED.toString())) {
          String value = singleGeneCaseValueMap.get(alteredSampleId);
          if (value.equals("Non")) {
            c += 1;
          } else {
            d += 1;
          }
        }
      }
    }

    for (Integer unalteredSampleId : unalteredSampleIds) {
      if (singleGeneCaseValueMap.containsKey(unalteredSampleId)) {
        if (profileType.equals(GeneticAlterationType.COPY_NUMBER_ALTERATION.toString())) {
          try {
            Double value = Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId));
            if (copyNumType.equals("del")) {
              if (value == -2.0) {
                b += 1;
              } else {
                a += 1;
              }
            } else if (copyNumType.equals("amp")) {
              if (value == 2.0) {
                b += 1;
              } else {
                a += 1;
              }
            }
          } catch (NumberFormatException e) {
            e.getStackTrace();
          }
        } else if (profileType.equals(GeneticAlterationType.MUTATION_EXTENDED.toString())) {
          String value = singleGeneCaseValueMap.get(unalteredSampleId);
          if (value.equals("Non")) {
            a += 1;
          } else {
            b += 1;
          }
        }
      }
    }

    FisherExact fisher = new FisherExact(a + b + c + d);
    return fisher.getCumlativeP(a, b, c, d);
  }
  private double calcPct(
      HashMap<Integer, String> singleGeneCaseValueMap,
      String profileType,
      String groupType) { // group type: altered or unaltered

    double _result_pct = 0, _count = 0; // altered samples count

    if (profileType.equals(GeneticAlterationType.COPY_NUMBER_ALTERATION.toString())
        && copyNumType.equals("del")) {
      switch (groupType) {
        case "altered":
          for (Integer alteredSampleId : alteredSampleIds) {
            if (singleGeneCaseValueMap.containsKey(alteredSampleId)) {
              try {
                if (Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId)) == -2.0) {
                  _count += 1;
                }
              } catch (NumberFormatException e) {
                e.getStackTrace();
              }
            }
          }
          _result_pct = _count / alteredSampleIds.size();
          break;
        case "unaltered":
          for (Integer unalteredSampleId : unalteredSampleIds) {
            if (singleGeneCaseValueMap.containsKey(unalteredSampleId)) {
              try {
                if (Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId)) == -2.0) {
                  _count += 1;
                }
              } catch (NumberFormatException e) {
                e.getStackTrace();
              }
            }
          }
          _result_pct = _count / unalteredSampleIds.size();
          break;
      }
    } else if (profileType.equals(GeneticAlterationType.COPY_NUMBER_ALTERATION.toString())
        && copyNumType.equals("amp")) {
      switch (groupType) {
        case "altered":
          for (Integer alteredSampleId : alteredSampleIds) {
            if (singleGeneCaseValueMap.containsKey(alteredSampleId)) {
              try {
                if (Double.parseDouble(singleGeneCaseValueMap.get(alteredSampleId)) == 2.0) {
                  _count += 1;
                }
              } catch (NumberFormatException e) {
                e.getStackTrace();
              }
            }
          }
          _result_pct = _count / alteredSampleIds.size();
          break;
        case "unaltered":
          for (Integer unalteredSampleId : unalteredSampleIds) {
            if (singleGeneCaseValueMap.containsKey(unalteredSampleId)) {
              try {
                if (Double.parseDouble(singleGeneCaseValueMap.get(unalteredSampleId)) == 2.0) {
                  _count += 1;
                }
              } catch (NumberFormatException e) {
                e.getStackTrace();
              }
            }
          }
          _result_pct = _count / unalteredSampleIds.size();
          break;
      }
    } else if (profileType.equals(GeneticAlterationType.MUTATION_EXTENDED.toString())) {
      switch (groupType) {
        case "altered":
          for (Integer alteredSampleId : alteredSampleIds) {
            if (singleGeneCaseValueMap.containsKey(alteredSampleId)) {
              if (!singleGeneCaseValueMap.get(alteredSampleId).equals("Non")) {
                _count += 1;
              }
            }
          }
          _result_pct = _count / alteredSampleIds.size();
          break;
        case "unaltered":
          for (Integer unalteredSampleId : unalteredSampleIds) {
            if (singleGeneCaseValueMap.containsKey(unalteredSampleId)) {
              if (!singleGeneCaseValueMap.get(unalteredSampleId).equals("Non")) {
                _count += 1;
              }
            }
          }
          _result_pct = _count / unalteredSampleIds.size();
          break;
      }
    }

    return _result_pct;
  }