/*
***************************************************************************
Biomolecular Interaction Network Database (BIND)
The Blueprint Initiative
522 University Avenue, 9th Floor, Suite 900
Toronto, Ontario, Canada, M5G 1W7
Hogue Lab - University of Toronto Biochemistry Department
Samuel Lunenfeld Research Institute, Mount Sinai Hospital
Publication to cite:
Bader GD, Betel D, Hogue CW. (2003) BIND: the Biomolecular
Interaction Network Database. Nucleic Acids Res. 31(1): 248-50
PMID: 12519993
Copyright Notice:
Copyright 2003, 2004 Mount Sinai Hospital (MSH)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of
the License, or any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the
Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA
02111-1307 USA
or visit http://www.gnu.org/copyleft/gpl.html
***************************************************************************/
package org.blueprint.seqhound;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
/**
* Contains miscellaneous functions such as printing arrays,
* copying arrays, merging 2 arrays, etc...
*
* @author hao lieu (hlieu@blueprint.org)
*/
public class SLRIMisc
{
/**
* Saves an array of intergers as a comma delimited string.
* Eg. If intList
contains the integers 6 7 4,
* then this function will return: 6,7,4 as a string.
* If intList is an empty array, returns ""
* @param intList an array of integers.
* @return a comma delimited string of integers.
*/
public static String SaveArrAsString (int[] intList)
{
if(intList.length == 0)
{
return "";
}
StringBuffer s = new StringBuffer();
Integer j;
for(int i = 0; i < intList.length; i++)
{
try {
j = new Integer(intList[i]);
s.append(j.toString().concat(","));
} catch (NullPointerException e){
continue;
}
}
if(s.length() == 0) return "";
return s.substring(0, s.length() - 1);
}
/**
* Saves an array of strings as a comma delimited string.
* Eg if strList
contains "hi" "there",
* this function will return "hi,there"
* @param strList an array of strings
* @return a comma delimited string
*/
public static String SaveArrAsString (String[] strList)
{
if(strList.length == 0)
{
return "";
}
StringBuffer s = new StringBuffer();
for(int i = 0; i < strList.length; i++)
{
try{
s.append(strList[i].concat(","));
} catch (NullPointerException e){
continue;
}
}
if(s.length() == 0) return "";
return s.substring(0, s.length() - 1);
}
/**
* Splits a string according to the regex
string.
* @param s the string to be split
* @param regex the delimiter to split s
by
* @return an array of integers
*/
public static int[] StrInt2IntArr(String s, String regex)
{
Integer i;
String[] strArr = s.split(regex);
int[] intArr = new int[strArr.length];
for(int j = 0; j < strArr.length; j++)
{
i = new Integer(strArr[j]);
intArr[j] = i.intValue();
}
return intArr;
}
/**
* Splits a string according to regex
.
* @param s the string to be split
* @param regex the delimiter to split s
by
* @return an array of strings
*/
public static String[] StrStr2StrArr(String s, String regex)
{
return s.split(regex);
}
/**
* Copies an array of integers into a vector.
* @param numArr the array to be copied.
* @return a vector of integers that contains the exact same keys
* in the same order as numArr
*/
public static Vector CopyArray(int[] numArr)
{
Vector v = new Vector();
for(int i = 0; i < numArr.length; i++){
v.add(new Integer(numArr[i]));
}
return v;
}
/**
* Copies an array of strings into a vector.
* @param strArr the array to be copied
* @return a vector of strings that contains teh exact same keys
* in the same order as strArr
*/
public static Vector CopyArray(String[] strArr)
{
Vector v = new Vector();
for(int i = 0; i < strArr.length; i++){
v.add(strArr[i]);
}
return v;
}
/**
* Moves the integers in v
into a new array and returns the array.
* The vector input must contain only integers. The vector shrinks
* for each element moved into the returned array.
* @param v a vector containing integers that will be moved into the array
* @return an array of integers
*/
public static int[] MaxSizeIntList(Vector v)
{
int lim = Math.min(v.size(), MAXQ);
int[] iArr = new int[lim];
for(int i = 0; !v.isEmpty(); i++){
iArr[i] = ((Integer)v.remove(0)).intValue();
if(i+1 == MAXQ) break;
}
return iArr;
}
/**
* Moves the string elements in v
into a new array and returns the array.
* The vector input must contain only strings. The vector shrinks
* for each element moved into the returned array.
* @param v a vector containing only strings that will be moved into the array.
* @return an array of strings.
*/
public static String[] MaxSizeStrList(Vector v)
{
int lim = Math.min(v.size(), MAXQ);
String[] iArr = new String[lim];
for(int i = 0; !v.isEmpty(); i++){
iArr[i] = (String)v.remove(0);
if( i+1 == MAXQ) break;
}
return iArr;
}
/**
* Merges to arrays of integers into one new array.
* @param i2 an array of integers
* @param i2 an array of integers
* @return an new array of integers containing the elements in i1 then i2.
*/
public static int[] MergeLists(int[] i1, int[] i2)
{
if(i1.length == 0){
return i2;
}
if(i2.length == 0){
return i1;
}
int[] iRet = new int[i1.length + i2.length];
for(int i = 0; i < i1.length; i++){
iRet[i] = i1[i];
}
for(int i = 0; i < i2.length; i++){
iRet[i + i1.length] = i2[i];
}
return iRet;
}
/**
* Merges two arrays of strings into a new array.
* @param i1 an array of strings.
* @param i2 an array of strings.
* @return a new array of strings containing the elements of i1 then i2.
*/
public static String[] MergeLists(String[] i1, String[] i2)
{
if(i1.length == 0){
return i2;
}
if(i2.length == 0){
return i1;
}
String[] iRet = new String[i1.length + i2.length];
for(int i = 0; i < i1.length; i++){
iRet[i] = i1[i];
}
for(int i = 0; i < i2.length; i++){
iRet[i + i1.length] = i2[i];
}
return iRet;
}
/**
* Prints an array of strings separated by separator
* to System.out
* @param strList an array of strings.
* @param separator separates each string in strList
*/
public static void PrintList(String[] strList, String separator)
{
/* TODO provide a handle to an OutputStream or Writer which is
where output is directed
*/
if(strList == null){
return;
}
if(strList.length == 0){
return;
}
for(int i = 0; i < strList.length; i++)
{
System.out.print(strList[i]); System.out.print(separator);
}
System.out.println();
}
/**
* Prints an array of integers into System.out.
* @param iList an array of integers.
* @param separator separates each integer in iList
*/
public static void PrintList(int[] iList, String separator)
{
/* TODO Provide a handle to an OutStream or Writer which
is where the output is directed
*/
if(iList == null){
return;
}
if(iList.length == 0){
return;
}
for(int i = 0; i < iList.length; i++)
{
System.out.print(iList[i]); System.out.print(separator);
}
System.out.println();
}
/**
* Converts an string array to hashtable
* a helper function for API list function handling
* each entry in the sting array is separated by sep
* split into two fields and then stored as key and element in the hash
* @param strArr an array to be converted to hash
* @param h is the hashtable
* @param sep is the delimiter to split s
by
* @return a hashtable contains the information in strArr
*/
public static Hashtable StrArr2Hash(String[] strArr, Hashtable h, String sep)
{
String[] pairs = new String[2];
for(int j = 0; j < strArr.length; j++)
{
pairs = strArr[j].split(sep);
h.put(pairs[0], pairs[1]);
}
return h;
}
/**
* print out the contents of a hashtable
* @param h is the hashtable
*/
public static void PrintHashTable(Hashtable h)
{
Enumeration e = h.keys();
Enumeration e1 = h.elements();
while (e.hasMoreElements())
{
System.out.println(e.nextElement());
System.out.println("=>");
System.out.println(e1.nextElement());
}
}
private static final int MAXQ = 700;
}