SGD2AttrTable.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.OutputStreamWriter;
  7. import java.util.HashMap;
  8. import java.util.Iterator;
  9. /*
  10. * This program starts by creating an intermediate table and then will load the function from Dr. Brett McKinney to create the attribute table.
  11. */
  12. /**
  13. *
  14. * @author DTian
  15. */
  16. public class SGD2AttrTable {
  17. public void createIntermediateTable(String inputFile, String outputFile) {
  18. HashMap geneToGODict = new HashMap();
  19. try {
  20. FileReader fr = new FileReader(inputFile);
  21. BufferedReader br = new BufferedReader(fr);
  22. // strRow is used to read line from file(skip first row)
  23. String strRow = br.readLine();
  24. // The while loop read the data from data file to vvf
  25. while ((strRow = br.readLine()) != null) {
  26. // Check: skip the line if it is a comment line
  27. if (strRow.trim().charAt(0) != 'S' ) {
  28. continue;
  29. }
  30. String [] strArray = strRow.trim().split("\\t");
  31. String key = toKey(strArray[10].toUpperCase());
  32. if (key.compareToIgnoreCase("") == 0) {
  33. continue;
  34. }
  35. String value = toValue(strArray[4]);
  36. if (geneToGODict.containsKey(key)) {
  37. geneToGODict.put(key, geneToGODict.get(key)+ "\t" + value);
  38. } else {
  39. geneToGODict.put(key, value);
  40. }
  41. }
  42. br.close();
  43. fr.close();
  44. // Write to output file
  45. FileOutputStream stream; // provides file access
  46. OutputStreamWriter writer; // writes to the file
  47. stream = new FileOutputStream(new File(outputFile), true);
  48. writer = new OutputStreamWriter(stream);
  49. Iterator it = geneToGODict.keySet().iterator();
  50. while(it.hasNext()){
  51. String key = it.next().toString();
  52. String value = geneToGODict.get(key).toString();
  53. writer.write(key + "\t" + value + "\n");
  54. }
  55. writer.flush();
  56. writer.close();
  57. stream.close();
  58. } catch (IOException e) {
  59. // Catch possible io errors from readLine()
  60. System.out.println("IOException error in 'class SGD2AttrTable, method createIntermediateTable'");
  61. }
  62. }
  63. public void createAttrTable(String intermediaFile, String outputFile){
  64. HashMap geneToGODict = new HashMap();
  65. try {
  66. FileReader fr = new FileReader(intermediaFile);
  67. BufferedReader br = new BufferedReader(fr);
  68. // strRow is used to read line from file(skip first row)
  69. String strRow = br.readLine();
  70. // The while loop read the data from data file to vvf
  71. while ((strRow = br.readLine()) != null) {
  72. //check: skip the line if it is a comment line
  73. if (strRow.trim().charAt(0) != 'S' ) {
  74. continue;
  75. }
  76. String [] strArray = strRow.trim().split("\\t");
  77. String key = toKey(strArray[10].toUpperCase());
  78. if (key.compareToIgnoreCase("") == 0) {
  79. continue;
  80. }
  81. String value = toValue(strArray[4]);
  82. if (geneToGODict.containsKey(key)) {
  83. geneToGODict.put(key, geneToGODict.get(key)+ "\t" + value);
  84. } else {
  85. geneToGODict.put(key, value);
  86. }
  87. }
  88. br.close();
  89. fr.close();
  90. // Write to output file
  91. FileOutputStream stream; // provides file access
  92. OutputStreamWriter writer; // writes to the file
  93. stream = new FileOutputStream(new File(outputFile), true);
  94. writer = new OutputStreamWriter(stream);
  95. Iterator it = geneToGODict.keySet().iterator();
  96. while (it.hasNext()) {
  97. String key = it.next().toString();
  98. String value = geneToGODict.get(key).toString();
  99. writer.write(key + "\t" + value + "\n");
  100. }
  101. writer.flush();
  102. writer.close();
  103. stream.close();
  104. } catch (IOException e) {
  105. // Catch possible io errors from readLine()
  106. System.out.println("IOException error in 'class SGD2AttrTable, method createIntermediateTable'");
  107. }
  108. }
  109. /**
  110. *
  111. * @param raw the string need to be get rid of the "GO:0s"
  112. * @return the string without "GO:00"
  113. */
  114. private String toValue(String raw) {
  115. String result = raw.toUpperCase(); //raw should be like: "GO:0005739"
  116. // Delete "GO:"
  117. result = result.substring(3);
  118. // Delete "lead zeros"
  119. while (result.charAt(0) == '0') {
  120. result =result.substring(1);
  121. }
  122. return result;
  123. }
  124. private String toKey(String raw) {
  125. String result = raw.toUpperCase(); // raw should be like: "GO:0005739"
  126. // Find the '|'
  127. int end = result.indexOf('|');
  128. // Get the sub string
  129. if (end < 0) {
  130. return result;
  131. } else {
  132. return result.substring(0, end);
  133. }
  134. }
  135. }