/test/model/VertexStatsRecorderUnitTest.java

https://github.com/massimo-nocentini/my-undergraduatethesis-java · Java · 689 lines · 496 code · 190 blank · 3 comment · 13 complexity · b1ffd270a770254237c77d7933b6efdf MD5 · raw file

  1. package model;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.Writer;
  7. import java.util.HashMap;
  8. import java.util.Locale;
  9. import java.util.Map;
  10. import java.util.Map.Entry;
  11. import java.util.SortedMap;
  12. import java.util.TreeMap;
  13. import javax.xml.stream.XMLStreamException;
  14. import junit.framework.Assert;
  15. import org.junit.Test;
  16. import org.sbml.jsbml.Model;
  17. import org.sbml.jsbml.SBMLDocument;
  18. import org.sbml.jsbml.SBMLReader;
  19. import org.sbml.jsbml.Species;
  20. import piping.PipeFilter;
  21. import piping.PipeFilterFactory;
  22. import piping.PlainTextInfoComputationListener;
  23. import util.IntegerCounter;
  24. import dotInterface.DotFileUtilHandler;
  25. import dotInterface.DotFileUtilHandler.DotUtilAction;
  26. public class VertexStatsRecorderUnitTest {
  27. @Test
  28. public void record_stats_information_from_simple_vertices() {
  29. VertexStatsRecorder recorder = new VertexStatsRecorder();
  30. Vertex simple = VertexFactory.makeSimpleVertex();
  31. Vertex simple2 = VertexFactory.makeSimpleVertex();
  32. Vertex simple3 = VertexFactory.makeSimpleVertex();
  33. Vertex simple4 = VertexFactory.makeSimpleVertex();
  34. simple.addNeighbour(simple2).addNeighbour(simple3)
  35. .addNeighbour(simple4);
  36. simple2.addNeighbour(simple3).addNeighbour(simple4);
  37. simple3.addNeighbour(simple4);
  38. simple.publishYourStatsOn(recorder);
  39. simple2.publishYourStatsOn(recorder);
  40. simple3.publishYourStatsOn(recorder);
  41. simple4.publishYourStatsOn(recorder);
  42. Map<PlainTextStatsComponents, Integer> expected = new HashMap<PlainTextStatsComponents, Integer>();
  43. expected.put(PlainTextStatsComponents.NOfVertices, 4);
  44. expected.put(PlainTextStatsComponents.NOfEdges, 6);
  45. expected.put(PlainTextStatsComponents.NOfSources, 1);
  46. expected.put(PlainTextStatsComponents.NOfSinks, 1);
  47. expected.put(PlainTextStatsComponents.NOfWhites, 2);
  48. Assert.assertTrue(recorder.isSimpleVerticesVotesEquals(expected));
  49. Assert.assertFalse(recorder
  50. .isSimpleVerticesVotesEquals(new HashMap<PlainTextStatsComponents, Integer>()));
  51. }
  52. @Test
  53. public void record_average_with_one_model_stats_information_from_simple_vertices_should_return_the_same() {
  54. VertexStatsRecorder recorder = new VertexStatsRecorder();
  55. Vertex simple = VertexFactory.makeSimpleVertex();
  56. Vertex simple2 = VertexFactory.makeSimpleVertex();
  57. Vertex simple3 = VertexFactory.makeSimpleVertex();
  58. Vertex simple4 = VertexFactory.makeSimpleVertex();
  59. simple.addNeighbour(simple2).addNeighbour(simple3)
  60. .addNeighbour(simple4);
  61. simple2.addNeighbour(simple3).addNeighbour(simple4);
  62. simple3.addNeighbour(simple4);
  63. simple.publishYourStatsOn(recorder);
  64. simple2.publishYourStatsOn(recorder);
  65. simple3.publishYourStatsOn(recorder);
  66. simple4.publishYourStatsOn(recorder);
  67. Map<PlainTextStatsComponents, Integer> expected = new HashMap<PlainTextStatsComponents, Integer>();
  68. expected.put(PlainTextStatsComponents.NOfVertices, 4);
  69. expected.put(PlainTextStatsComponents.NOfEdges, 6);
  70. expected.put(PlainTextStatsComponents.NOfSources, 1);
  71. expected.put(PlainTextStatsComponents.NOfSinks, 1);
  72. expected.put(PlainTextStatsComponents.NOfWhites, 2);
  73. Assert.assertTrue(recorder.average(1).isSimpleVerticesVotesEquals(
  74. expected));
  75. Assert.assertFalse(recorder.average(1).isSimpleVerticesVotesEquals(
  76. new HashMap<PlainTextStatsComponents, Integer>()));
  77. }
  78. @Test
  79. public void record_average_with_two_model_stats_information_from_simple_vertices_should_change() {
  80. VertexStatsRecorder recorder = new VertexStatsRecorder();
  81. Vertex simple = VertexFactory.makeSimpleVertex();
  82. Vertex simple2 = VertexFactory.makeSimpleVertex();
  83. Vertex simple3 = VertexFactory.makeSimpleVertex();
  84. Vertex simple4 = VertexFactory.makeSimpleVertex();
  85. simple.addNeighbour(simple2).addNeighbour(simple3)
  86. .addNeighbour(simple4);
  87. simple2.addNeighbour(simple3).addNeighbour(simple4);
  88. simple3.addNeighbour(simple4);
  89. simple.publishYourStatsOn(recorder);
  90. simple2.publishYourStatsOn(recorder);
  91. simple3.publishYourStatsOn(recorder);
  92. simple4.publishYourStatsOn(recorder);
  93. Map<PlainTextStatsComponents, Integer> expected = new HashMap<PlainTextStatsComponents, Integer>();
  94. int counter = 2;
  95. expected.put(PlainTextStatsComponents.NOfVertices,
  96. VertexVoteAccepter.DivideAndRound(4, counter));
  97. expected.put(PlainTextStatsComponents.NOfEdges,
  98. VertexVoteAccepter.DivideAndRound(6, counter));
  99. expected.put(PlainTextStatsComponents.NOfSources,
  100. VertexVoteAccepter.DivideAndRound(1, counter));
  101. expected.put(PlainTextStatsComponents.NOfSinks,
  102. VertexVoteAccepter.DivideAndRound(1, counter));
  103. expected.put(PlainTextStatsComponents.NOfWhites,
  104. VertexVoteAccepter.DivideAndRound(2, counter));
  105. Assert.assertTrue(recorder.average(counter)
  106. .isSimpleVerticesVotesEquals(expected));
  107. Assert.assertFalse(recorder.average(counter)
  108. .isSimpleVerticesVotesEquals(
  109. new HashMap<PlainTextStatsComponents, Integer>()));
  110. }
  111. @Test
  112. public void check_divide_and_round_utility_method() {
  113. Assert.assertEquals(1, VertexVoteAccepter.DivideAndRound(4, 4));
  114. Assert.assertEquals(2, VertexVoteAccepter.DivideAndRound(4, 2));
  115. Assert.assertEquals(1, VertexVoteAccepter.DivideAndRound(1, 4));
  116. Assert.assertEquals(0, VertexVoteAccepter.DivideAndRound(0, 4));
  117. Assert.assertEquals(4, VertexVoteAccepter.DivideAndRound(16, 4));
  118. Assert.assertEquals(4, VertexVoteAccepter.DivideAndRound(16, 5));
  119. }
  120. @Test
  121. public void record_average_with_one_model_stats_information_from_connected_components() {
  122. VertexStatsRecorder recorder = new VertexStatsRecorder();
  123. ConnectedComponentWrapperVertex simple = VertexFactory
  124. .makeConnectedComponentWrapperVertex();
  125. ConnectedComponentWrapperVertex simple2 = VertexFactory
  126. .makeConnectedComponentWrapperVertex();
  127. ConnectedComponentWrapperVertex simple3 = VertexFactory
  128. .makeConnectedComponentWrapperVertex();
  129. ConnectedComponentWrapperVertex simple4 = VertexFactory
  130. .makeConnectedComponentWrapperVertex();
  131. simple.includeMember(VertexFactory.makeSimpleVertex());
  132. simple.includeMember(VertexFactory.makeSimpleVertex());
  133. simple.includeMember(VertexFactory.makeSimpleVertex());
  134. simple.addNeighbour(simple2);
  135. simple2.includeMember(VertexFactory.makeSimpleVertex());
  136. simple2.addNeighbour(simple3).addNeighbour(simple4);
  137. simple3.includeMember(VertexFactory.makeSimpleVertex());
  138. simple3.includeMember(VertexFactory.makeSimpleVertex());
  139. simple3.addNeighbour(simple4);
  140. simple4.includeMember(VertexFactory.makeSimpleVertex());
  141. simple4.includeMember(VertexFactory.makeSimpleVertex());
  142. simple4.includeMember(VertexFactory.makeSimpleVertex());
  143. simple.publishYourStatsOn(recorder);
  144. simple2.publishYourStatsOn(recorder);
  145. simple3.publishYourStatsOn(recorder);
  146. simple4.publishYourStatsOn(recorder);
  147. Map<PlainTextStatsComponents, Integer> expectedFor3members = new HashMap<PlainTextStatsComponents, Integer>();
  148. expectedFor3members.put(PlainTextStatsComponents.NOfComponents, 2);
  149. expectedFor3members.put(PlainTextStatsComponents.NOfSources, 1);
  150. expectedFor3members.put(PlainTextStatsComponents.NOfSinks, 1);
  151. expectedFor3members.put(PlainTextStatsComponents.NOfWhites, 0);
  152. expectedFor3members.put(PlainTextStatsComponents.NOfEdges, 1);
  153. Map<PlainTextStatsComponents, Integer> expectedFor2members = new HashMap<PlainTextStatsComponents, Integer>();
  154. expectedFor2members.put(PlainTextStatsComponents.NOfComponents, 1);
  155. expectedFor2members.put(PlainTextStatsComponents.NOfSources, 0);
  156. expectedFor2members.put(PlainTextStatsComponents.NOfSinks, 0);
  157. expectedFor2members.put(PlainTextStatsComponents.NOfWhites, 1);
  158. expectedFor2members.put(PlainTextStatsComponents.NOfEdges, 1);
  159. Map<PlainTextStatsComponents, Integer> expectedFor1members = new HashMap<PlainTextStatsComponents, Integer>();
  160. expectedFor1members.put(PlainTextStatsComponents.NOfComponents, 1);
  161. expectedFor1members.put(PlainTextStatsComponents.NOfSources, 0);
  162. expectedFor1members.put(PlainTextStatsComponents.NOfSinks, 0);
  163. expectedFor1members.put(PlainTextStatsComponents.NOfWhites, 1);
  164. expectedFor1members.put(PlainTextStatsComponents.NOfEdges, 2);
  165. int counter = 1;
  166. Assert.assertTrue(recorder.average(counter).isComponentsVotesEquals(3,
  167. expectedFor3members));
  168. Assert.assertTrue(recorder.average(counter).isComponentsVotesEquals(2,
  169. expectedFor2members));
  170. Assert.assertTrue(recorder.average(counter).isComponentsVotesEquals(1,
  171. expectedFor1members));
  172. }
  173. @Test
  174. public void record_average_with_two_model_stats_information_from_connected_components() {
  175. VertexStatsRecorder recorder = new VertexStatsRecorder();
  176. ConnectedComponentWrapperVertex simple = VertexFactory
  177. .makeConnectedComponentWrapperVertex();
  178. ConnectedComponentWrapperVertex simple2 = VertexFactory
  179. .makeConnectedComponentWrapperVertex();
  180. ConnectedComponentWrapperVertex simple3 = VertexFactory
  181. .makeConnectedComponentWrapperVertex();
  182. ConnectedComponentWrapperVertex simple4 = VertexFactory
  183. .makeConnectedComponentWrapperVertex();
  184. simple.includeMember(VertexFactory.makeSimpleVertex());
  185. simple.includeMember(VertexFactory.makeSimpleVertex());
  186. simple.includeMember(VertexFactory.makeSimpleVertex());
  187. simple.addNeighbour(simple2);
  188. simple2.includeMember(VertexFactory.makeSimpleVertex());
  189. simple2.addNeighbour(simple3).addNeighbour(simple4);
  190. simple3.includeMember(VertexFactory.makeSimpleVertex());
  191. simple3.includeMember(VertexFactory.makeSimpleVertex());
  192. simple3.addNeighbour(simple4);
  193. simple4.includeMember(VertexFactory.makeSimpleVertex());
  194. simple4.includeMember(VertexFactory.makeSimpleVertex());
  195. simple4.includeMember(VertexFactory.makeSimpleVertex());
  196. simple.publishYourStatsOn(recorder);
  197. simple2.publishYourStatsOn(recorder);
  198. simple3.publishYourStatsOn(recorder);
  199. simple4.publishYourStatsOn(recorder);
  200. int counter = 1;
  201. Map<PlainTextStatsComponents, Integer> expectedFor3members = new HashMap<PlainTextStatsComponents, Integer>();
  202. expectedFor3members.put(PlainTextStatsComponents.NOfComponents,
  203. VertexVoteAccepter.DivideAndRound(2, counter));
  204. expectedFor3members.put(PlainTextStatsComponents.NOfSources,
  205. VertexVoteAccepter.DivideAndRound(1, counter));
  206. expectedFor3members.put(PlainTextStatsComponents.NOfSinks,
  207. VertexVoteAccepter.DivideAndRound(1, counter));
  208. expectedFor3members.put(PlainTextStatsComponents.NOfWhites,
  209. VertexVoteAccepter.DivideAndRound(0, counter));
  210. expectedFor3members.put(PlainTextStatsComponents.NOfEdges,
  211. VertexVoteAccepter.DivideAndRound(1, counter));
  212. Map<PlainTextStatsComponents, Integer> expectedFor2members = new HashMap<PlainTextStatsComponents, Integer>();
  213. expectedFor2members.put(PlainTextStatsComponents.NOfComponents,
  214. VertexVoteAccepter.DivideAndRound(1, counter));
  215. expectedFor2members.put(PlainTextStatsComponents.NOfSources,
  216. VertexVoteAccepter.DivideAndRound(0, counter));
  217. expectedFor2members.put(PlainTextStatsComponents.NOfSinks,
  218. VertexVoteAccepter.DivideAndRound(0, counter));
  219. expectedFor2members.put(PlainTextStatsComponents.NOfWhites,
  220. VertexVoteAccepter.DivideAndRound(1, counter));
  221. expectedFor2members.put(PlainTextStatsComponents.NOfEdges,
  222. VertexVoteAccepter.DivideAndRound(1, counter));
  223. Map<PlainTextStatsComponents, Integer> expectedFor1members = new HashMap<PlainTextStatsComponents, Integer>();
  224. expectedFor1members.put(PlainTextStatsComponents.NOfComponents,
  225. VertexVoteAccepter.DivideAndRound(1, counter));
  226. expectedFor1members.put(PlainTextStatsComponents.NOfSources,
  227. VertexVoteAccepter.DivideAndRound(0, counter));
  228. expectedFor1members.put(PlainTextStatsComponents.NOfSinks,
  229. VertexVoteAccepter.DivideAndRound(0, counter));
  230. expectedFor1members.put(PlainTextStatsComponents.NOfWhites,
  231. VertexVoteAccepter.DivideAndRound(1, counter));
  232. expectedFor1members.put(PlainTextStatsComponents.NOfEdges,
  233. VertexVoteAccepter.DivideAndRound(2, counter));
  234. Assert.assertTrue(recorder.average(counter).isComponentsVotesEquals(3,
  235. expectedFor3members));
  236. Assert.assertTrue(recorder.average(counter).isComponentsVotesEquals(2,
  237. expectedFor2members));
  238. Assert.assertTrue(recorder.average(counter).isComponentsVotesEquals(1,
  239. expectedFor1members));
  240. }
  241. @Test
  242. public void record_stats_information_from_connected_components() {
  243. VertexStatsRecorder recorder = new VertexStatsRecorder();
  244. ConnectedComponentWrapperVertex simple = VertexFactory
  245. .makeConnectedComponentWrapperVertex();
  246. ConnectedComponentWrapperVertex simple2 = VertexFactory
  247. .makeConnectedComponentWrapperVertex();
  248. ConnectedComponentWrapperVertex simple3 = VertexFactory
  249. .makeConnectedComponentWrapperVertex();
  250. ConnectedComponentWrapperVertex simple4 = VertexFactory
  251. .makeConnectedComponentWrapperVertex();
  252. simple.includeMember(VertexFactory.makeSimpleVertex());
  253. simple.includeMember(VertexFactory.makeSimpleVertex());
  254. simple.includeMember(VertexFactory.makeSimpleVertex());
  255. simple.addNeighbour(simple2);
  256. simple2.includeMember(VertexFactory.makeSimpleVertex());
  257. simple2.addNeighbour(simple3).addNeighbour(simple4);
  258. simple3.includeMember(VertexFactory.makeSimpleVertex());
  259. simple3.includeMember(VertexFactory.makeSimpleVertex());
  260. simple3.addNeighbour(simple4);
  261. simple4.includeMember(VertexFactory.makeSimpleVertex());
  262. simple4.includeMember(VertexFactory.makeSimpleVertex());
  263. simple4.includeMember(VertexFactory.makeSimpleVertex());
  264. simple.publishYourStatsOn(recorder);
  265. simple2.publishYourStatsOn(recorder);
  266. simple3.publishYourStatsOn(recorder);
  267. simple4.publishYourStatsOn(recorder);
  268. Map<PlainTextStatsComponents, Integer> expectedFor3members = new HashMap<PlainTextStatsComponents, Integer>();
  269. expectedFor3members.put(PlainTextStatsComponents.NOfComponents, 2);
  270. expectedFor3members.put(PlainTextStatsComponents.NOfSources, 1);
  271. expectedFor3members.put(PlainTextStatsComponents.NOfSinks, 1);
  272. expectedFor3members.put(PlainTextStatsComponents.NOfWhites, 0);
  273. expectedFor3members.put(PlainTextStatsComponents.NOfEdges, 1);
  274. Map<PlainTextStatsComponents, Integer> expectedFor2members = new HashMap<PlainTextStatsComponents, Integer>();
  275. expectedFor2members.put(PlainTextStatsComponents.NOfComponents, 1);
  276. expectedFor2members.put(PlainTextStatsComponents.NOfSources, 0);
  277. expectedFor2members.put(PlainTextStatsComponents.NOfSinks, 0);
  278. expectedFor2members.put(PlainTextStatsComponents.NOfWhites, 1);
  279. expectedFor2members.put(PlainTextStatsComponents.NOfEdges, 1);
  280. Map<PlainTextStatsComponents, Integer> expectedFor1members = new HashMap<PlainTextStatsComponents, Integer>();
  281. expectedFor1members.put(PlainTextStatsComponents.NOfComponents, 1);
  282. expectedFor1members.put(PlainTextStatsComponents.NOfSources, 0);
  283. expectedFor1members.put(PlainTextStatsComponents.NOfSinks, 0);
  284. expectedFor1members.put(PlainTextStatsComponents.NOfWhites, 1);
  285. expectedFor1members.put(PlainTextStatsComponents.NOfEdges, 2);
  286. Assert.assertTrue(recorder.isComponentsVotesEquals(3,
  287. expectedFor3members));
  288. Assert.assertTrue(recorder.isComponentsVotesEquals(2,
  289. expectedFor2members));
  290. Assert.assertTrue(recorder.isComponentsVotesEquals(1,
  291. expectedFor1members));
  292. }
  293. @Test
  294. public void check_consistency_for_connected_components_vertices_votes() {
  295. VertexStatsRecorder recorder = new VertexStatsRecorder();
  296. ConnectedComponentWrapperVertex simple = VertexFactory
  297. .makeConnectedComponentWrapperVertex();
  298. ConnectedComponentWrapperVertex simple2 = VertexFactory
  299. .makeConnectedComponentWrapperVertex();
  300. ConnectedComponentWrapperVertex simple3 = VertexFactory
  301. .makeConnectedComponentWrapperVertex();
  302. ConnectedComponentWrapperVertex simple4 = VertexFactory
  303. .makeConnectedComponentWrapperVertex();
  304. simple.includeMember(VertexFactory.makeSimpleVertex());
  305. simple.includeMember(VertexFactory.makeSimpleVertex());
  306. simple.includeMember(VertexFactory.makeSimpleVertex());
  307. simple.addNeighbour(simple2);
  308. simple2.includeMember(VertexFactory.makeSimpleVertex());
  309. simple2.addNeighbour(simple3).addNeighbour(simple4);
  310. simple3.includeMember(VertexFactory.makeSimpleVertex());
  311. simple3.includeMember(VertexFactory.makeSimpleVertex());
  312. simple3.addNeighbour(simple4);
  313. simple4.includeMember(VertexFactory.makeSimpleVertex());
  314. simple4.includeMember(VertexFactory.makeSimpleVertex());
  315. simple4.includeMember(VertexFactory.makeSimpleVertex());
  316. simple.publishYourStatsOn(recorder);
  317. simple2.publishYourStatsOn(recorder);
  318. simple3.publishYourStatsOn(recorder);
  319. simple4.publishYourStatsOn(recorder);
  320. Assert.assertTrue(recorder
  321. .isConnectedComponentsVoteAccepterConsistent());
  322. }
  323. @Test
  324. public void check_consistency_for_simple_vertices_votes() {
  325. VertexStatsRecorder recorder = new VertexStatsRecorder();
  326. Vertex simple = VertexFactory.makeSimpleVertex();
  327. Vertex simple2 = VertexFactory.makeSimpleVertex();
  328. Vertex simple3 = VertexFactory.makeSimpleVertex();
  329. Vertex simple4 = VertexFactory.makeSimpleVertex();
  330. simple.addNeighbour(simple2).addNeighbour(simple3)
  331. .addNeighbour(simple4);
  332. simple2.addNeighbour(simple3).addNeighbour(simple4);
  333. simple3.addNeighbour(simple4);
  334. simple.publishYourStatsOn(recorder);
  335. simple2.publishYourStatsOn(recorder);
  336. simple3.publishYourStatsOn(recorder);
  337. simple4.publishYourStatsOn(recorder);
  338. Assert.assertTrue(recorder.isSimpleVerticesVoteAccepterConsistent());
  339. }
  340. public void scan_all_standard_sbml_models_should_produce_consistent_informations() {
  341. DotUtilAction<File> action = new DotUtilAction<File>() {
  342. @Override
  343. public void apply(File element) {
  344. PipeFilter firstPlainTextStatsPipeFilter = PipeFilterFactory
  345. .MakePlainTextStatsPipeFilter();
  346. PipeFilter tarjanPipeFilter = PipeFilterFactory
  347. .MakeTarjanPipeFilter();
  348. PipeFilter secondPlainTextStatsPipeFilter = PipeFilterFactory
  349. .MakePlainTextStatsPipeFilter();
  350. tarjanPipeFilter.pipeAfter(firstPlainTextStatsPipeFilter);
  351. secondPlainTextStatsPipeFilter.pipeAfter(tarjanPipeFilter);
  352. PlainTextInfoComputationListener plainTextInfoComputationListener = new PlainTextInfoComputationListener();
  353. String pipeline_name = "massive-stats-info-check-consistency-"
  354. .concat(element.getName().substring(0,
  355. element.getName().lastIndexOf(".")));
  356. secondPlainTextStatsPipeFilter.applyWithListener(pipeline_name,
  357. OurModel.makeOurModelFrom(element.getAbsolutePath()),
  358. plainTextInfoComputationListener);
  359. // no output is produced.
  360. Assert.assertTrue(plainTextInfoComputationListener
  361. .arePlainTextInfoConsistent());
  362. }
  363. };
  364. DotFileUtilHandler.mapOnFilesInFolderFilteringByExtension(
  365. DotFileUtilHandler.getSbmlExampleModelsFolder(),
  366. DotFileUtilHandler.getSBMLFileExtension(), action, false);
  367. }
  368. public void check_model_presence_in_various_sbml_models_contained_in_BioCyc_folder() {
  369. final SortedMap<String, IntegerCounter> count_by_models = new TreeMap<String, IntegerCounter>();
  370. DotUtilAction<File> action = new DotUtilAction<File>() {
  371. @Override
  372. public void apply(File element) {
  373. Model model = null;
  374. SBMLDocument document = null;
  375. try {
  376. document = (new SBMLReader()).readSBML(element);
  377. } catch (FileNotFoundException e) {
  378. } catch (XMLStreamException e) {
  379. } catch (Exception e) {
  380. }
  381. if (document != null) {
  382. model = document.getModel();
  383. String model_name = model.getName();
  384. if (model_name == null) {
  385. Assert.fail("Impossible to have a model without a name");
  386. }
  387. if (count_by_models.containsKey(model_name) == false) {
  388. count_by_models.put(model_name, new IntegerCounter());
  389. }
  390. count_by_models.get(model_name).increment();
  391. }
  392. }
  393. };
  394. DotFileUtilHandler.mapOnAllFilesInFolder(DotFileUtilHandler
  395. .getSbmlExampleModelsFolder().concat("BioCyc15.0/"), action,
  396. true);
  397. // now we can generate the output file
  398. Writer writer;
  399. try {
  400. writer = new FileWriter(
  401. DotFileUtilHandler
  402. .dotOutputFolderPathName()
  403. .concat("maps-of-models-presence-among-multiple-models-in-ByoCyc-folder")
  404. .concat(DotFileUtilHandler
  405. .getPlainTextFilenameExtensionToken()));
  406. writer.write(count_by_models.toString());
  407. writer.close();
  408. } catch (IOException e) {
  409. e.printStackTrace();
  410. }
  411. Assert.assertEquals(92, count_by_models.size());
  412. int exploded_count = 0;
  413. int models_splitted_in_more_than_three_files = 0;
  414. for (Entry<String, IntegerCounter> entry : count_by_models.entrySet()) {
  415. exploded_count = exploded_count + entry.getValue().getCount();
  416. if (entry.getValue().getCount() > 2) {
  417. models_splitted_in_more_than_three_files = models_splitted_in_more_than_three_files + 1;
  418. }
  419. }
  420. Assert.assertEquals(167, exploded_count);
  421. Assert.assertTrue(models_splitted_in_more_than_three_files > 1);
  422. }
  423. public void check_species_presence_in_various_sbml_models_contained_in_curated_folder() {
  424. this.internal_check_species_presence_in_sbml_models(
  425. "maps-of-species-presence-among-multiple-new-curated-models",
  426. DotFileUtilHandler.getSbmlExampleModelsFolder().concat(
  427. "curated/"), false);
  428. }
  429. public void check_species_presence_in_various_sbml_models_contained_in_aae_folder() {
  430. this.internal_check_species_presence_in_sbml_models(
  431. "maps-of-species-presence-among-multiple-models-in-aae-folder",
  432. DotFileUtilHandler.getSbmlExampleModelsFolder().concat("aae/"),
  433. false);
  434. }
  435. public void check_species_presence_in_a_huge_number_of_sbml_models_contained_in_kyoto_database() {
  436. IntegerCounter analyzedModels = this
  437. .internal_check_species_presence_in_sbml_models(
  438. "maps-of-species-presence-among-a-huge-number-of-models-in-kyoto-folder",
  439. DotFileUtilHandler.getSbmlExampleModelsFolder().concat(
  440. "KEGG_R47-SBML_L2V1_nocd-20080728/"), true);
  441. Assert.assertEquals((Integer) 72095, analyzedModels.getCount());
  442. }
  443. public void check_species_presence_in_standard_sbml_models() {
  444. this.internal_check_species_presence_in_sbml_models(
  445. "maps-of-species-presence-among-multiple-old-models",
  446. DotFileUtilHandler.getSbmlExampleModelsFolder(), false);
  447. }
  448. private IntegerCounter internal_check_species_presence_in_sbml_models(
  449. String outputFilename, String modelsContainingDirectory,
  450. boolean recursively) {
  451. final SortedMap<String, Integer> countBySpecies = new TreeMap<String, Integer>();
  452. final IntegerCounter analyzedModels = new IntegerCounter();
  453. DotUtilAction<File> action = new DotUtilAction<File>() {
  454. @Override
  455. public void apply(File element) {
  456. Model model = null;
  457. SBMLDocument document = null;
  458. try {
  459. document = (new SBMLReader()).readSBML(element);
  460. } catch (FileNotFoundException e) {
  461. } catch (XMLStreamException e) {
  462. } catch (Exception e) {
  463. }
  464. if (document != null) {
  465. analyzedModels.increment();
  466. model = document.getModel();
  467. for (Species species : model.getListOfSpecies()) {
  468. String id = (species.getId() + "-(" + species.getName()
  469. + ")" + "-(" + species.getCompartment() + ")")
  470. .toUpperCase(new Locale("(all)"));
  471. if (countBySpecies.containsKey(id)) {
  472. int value = countBySpecies.get(id);
  473. countBySpecies.remove(id);
  474. countBySpecies.put(id, value + 1);
  475. } else {
  476. countBySpecies.put(id, 1);
  477. }
  478. }
  479. }
  480. }
  481. };
  482. DotFileUtilHandler.mapOnAllFilesInFolder(modelsContainingDirectory,
  483. action, recursively);
  484. // now we can generate the output file
  485. Writer writer;
  486. try {
  487. writer = new FileWriter(DotFileUtilHandler
  488. .dotOutputFolderPathName()
  489. .concat(outputFilename)
  490. .concat(DotFileUtilHandler
  491. .getPlainTextFilenameExtensionToken()));
  492. writer.write(countBySpecies.toString());
  493. writer.close();
  494. } catch (IOException e) {
  495. e.printStackTrace();
  496. }
  497. return analyzedModels;
  498. }
  499. }