Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.util

File HMMProbabilityDistributionAnalyser.java

 

Coverage histogram

../../img/srcFileCovDistChart0.png
60% of files have more coverage

Code metrics

116
386
30
1
978
702
103
0.27
12.87
30
3.43

Classes

Class Line # Actions
HMMProbabilityDistributionAnalyser 36 386 103
0.00%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package jalview.util;
2   
3    import jalview.datamodel.AlignmentAnnotation;
4    import jalview.datamodel.HiddenMarkovModel;
5    import jalview.datamodel.SequenceI;
6    import jalview.io.DataSourceType;
7    import jalview.io.FileParse;
8    import jalview.io.HMMFile;
9    import jalview.io.StockholmFile;
10    import jalview.schemes.ResidueProperties;
11   
12    import java.io.BufferedReader;
13    import java.io.File;
14    import java.io.FileNotFoundException;
15    import java.io.FileOutputStream;
16    import java.io.FileReader;
17    import java.io.IOException;
18    import java.io.InputStreamReader;
19    import java.io.PrintWriter;
20    import java.util.ArrayList;
21    import java.util.HashMap;
22    import java.util.List;
23    import java.util.Map;
24    import java.util.Random;
25    import java.util.Scanner;
26    import java.util.Vector;
27   
28    /**
29    * Processes probability data. The file indexes used in this program represent
30    * the index of the location of a family or hmm in their respective files,
31    * starting from 0.
32    *
33    * @author TZVanaalten
34    *
35    */
 
36    public class HMMProbabilityDistributionAnalyser
37    {
38    AlignmentAnnotation reference = null;
39   
40    Vector<SequenceI> sequences;
41   
42    HiddenMarkovModel hmm;
43   
44    // contains the raw data produced
45    List<ArrayList<Double>> raw = new ArrayList<>();
46   
47    // contains binned data
48    Map<String, Double> binned = new HashMap<>();
49   
50    // location of the family file
51    String families = "/media/sf_Shared_Folder/PFAM/Family/SeedFamilies.seed";
52   
53    // location of the file containing the family-clan links
54    final static String FAMILIESTOCLAN = "/media/sf_Shared_Folder/PFAM/Family/Clanlinks.dat";
55   
56    // location of the HMM file
57    String hmms = "/media/sf_Shared_Folder/PFAM/HMMs/Pfam-A.hmm";
58   
59    // suffix for raw file
60    final static String RAW = "/Raw.csv";
61   
62    // suffix for binned file
63    final static String BINNED = "/Binned.csv";
64   
65    // normalisation scale
66    final static double SCALE = 1;
67   
68    // current position in file
69    int currentFilePosition = 0;
70   
71    final static String NL = "\n";
72   
73    Random generator = new Random();
74   
75    // current directory
76    String currentFolder;
77   
78    boolean keepRaw = false;
79   
80    /**
81    * Sets the working directory.
82    *
83    * @param path
84    */
 
85  0 toggle public void setFolder(String path)
86    {
87  0 currentFolder = path;
88    }
89   
90    /**
91    * Moves a buffered reader forward in the file by a certain amount of entries.
92    * Each entry in the file is delimited by '//'.
93    *
94    * @param index
95    * The index of the location in the file.
96    * @param br
97    * @throws IOException
98    */
 
99  0 toggle public void moveLocationBy(int index, BufferedReader br)
100    throws IOException
101    {
102  0 for (int i = 0; i < index; i++)
103    {
104  0 String line = br.readLine();
105  0 while (!"//".equals(line))
106    {
107  0 line = br.readLine();
108   
109    }
110    }
111   
112    }
113   
114    /**
115    * Analyses a specified number of families and then saves the data. Before
116    * analysing the data, the previous saved data will be imported and after
117    * analysing this, the data is exported back into the file. The file must be
118    * in flat file format.
119    *
120    * @param increments
121    * The number of families to read before saving.
122    * @throws IOException
123    */
 
124  0 toggle public void run(int increments, boolean keepRawData) throws IOException
125    {
126  0 keepRaw = keepRawData;
127  0 try
128    {
129  0 readPreviousData(currentFolder);
130  0 BufferedReader posReader = new BufferedReader(
131    new FileReader(currentFolder + "/CurrentPosition.txt"));
132   
133  0 String line = posReader.readLine();
134  0 posReader.close();
135  0 currentFilePosition = Integer.parseInt(line);
136    } catch (Exception e)
137    {
138  0 System.out.println("No previous data found");
139    }
140   
141   
142   
143  0 BufferedReader inputSTO = new BufferedReader(new FileReader(families));
144  0 BufferedReader inputHMM = new BufferedReader(new FileReader(hmms));
145   
146   
147   
148  0 moveLocationBy(currentFilePosition, inputHMM);
149  0 moveLocationBy(currentFilePosition, inputSTO);
150   
151  0 int filesRead = 0;
152  0 int i = 0;
153  0 while (filesRead < increments)
154    {
155   
156  0 readStockholm(inputSTO);
157   
158  0 readHMM(inputHMM);
159   
160  0 int count = countValidResidues();
161  0 processData(count);
162  0 filesRead++;
163   
164  0 currentFilePosition++;
165  0 System.out.println(i);
166  0 i++;
167    }
168   
169  0 PrintWriter p = new PrintWriter(
170    new File(currentFolder + "/CurrentPosition.txt"));
171  0 p.print(currentFilePosition);
172  0 p.close();
173  0 exportData(currentFolder);
174  0 raw.clear();
175  0 binned.clear();
176   
177    }
178   
179    /**
180    * Analyses all families and then saves the data. Before analysing the data,
181    * the previous saved data will be imported and after analysing this, the data
182    * is exported back into the file. The file must be in flat file format.
183    *
184    * @param increments
185    * The number of families to read before saving.
186    * @throws IOException
187    */
 
188  0 toggle public void runToEnd(int minCount, int maxCount, boolean keepRawData,
189    boolean forClans)
190    throws IOException
191    {
192  0 keepRaw = keepRawData;
193  0 BufferedReader inputSTO = null;
194  0 BufferedReader inputHMM = null;
195  0 int size = 0;
196  0 int files = 1;
197  0 try
198    {
199  0 if (forClans)
200    {
201  0 files = 603;
202    }
203  0 int filesRead = 0;
204  0 for (int clan = 0; clan < files; clan++)
205    {
206  0 System.out.println(clan);
207  0 String clanPath = "";
208  0 int numberOfFamilies = 0;
209  0 if (forClans)
210    {
211  0 clanPath = currentFolder + "/Clan" + clan;
212  0 if (!new File(clanPath).exists())
213    {
214  0 continue;
215    }
216  0 BufferedReader famCountReader = new BufferedReader(
217    new FileReader(clanPath + "/NumberOfFamilies.txt"));
218  0 numberOfFamilies = Integer.parseInt(famCountReader.readLine());
219    }
220    else
221    {
222  0 numberOfFamilies = 1;
223    }
224   
225  0 for (int fam = 0; fam < numberOfFamilies; fam++)
226    {
227  0 if (forClans)
228    {
229  0 families = clanPath + "/Families/Fam" + fam + ".sto";
230  0 hmms = clanPath + "/HMMs/HMM" + fam + ".hmm";
231    }
232   
233  0 inputSTO = new BufferedReader(new FileReader(families));
234  0 inputHMM = new BufferedReader(new FileReader(hmms));
235   
236   
237  0 int i = 0;
238  0 boolean endReached = atEnd(inputSTO);
239  0 while (!endReached)
240    {
241  0 readStockholm(inputSTO);
242  0 readHMM(inputHMM);
243   
244  0 int count = countValidResidues();
245  0 if (count >= minCount && count < maxCount)
246    {
247  0 processData(count);
248    }
249  0 filesRead++;
250  0 System.out.println(filesRead);
251  0 endReached = atEnd(inputSTO);
252    }
253    }
254    }
255    } catch (Exception e)
256    {
257  0 e.printStackTrace();
258    } finally
259    {
260  0 exportData(currentFolder);
261  0 raw.clear();
262  0 binned.clear();
263    }
264    }
265   
266    /**
267    * Reads the previous data from both files
268    *
269    * @param source
270    * @throws IOException
271    */
 
272  0 toggle public void readPreviousData(String source) throws IOException
273    {
274  0 readBinned(source);
275  0 if (keepRaw)
276    {
277  0 readRaw(source);
278    }
279    }
280   
281    /**
282    * Reads the previous data from the binned file.
283    *
284    * @param source
285    * @throws IOException
286    */
 
287  0 toggle public void readBinned(String source) throws IOException
288    {
289  0 BufferedReader input = new BufferedReader(
290    new FileReader(source + BINNED));
291  0 String line = input.readLine();
292  0 binned = new HashMap<>();
293  0 while (!("".equals(line) || line == null))
294    {
295  0 Scanner scanner = new Scanner(line);
296  0 scanner.useDelimiter(",");
297  0 String key = scanner.next();
298  0 String value = scanner.next();
299  0 binned.put(key, Double.valueOf(value));
300  0 scanner.close();
301  0 line = input.readLine();
302    }
303   
304  0 input.close();
305    }
306   
307    /**
308    * Reads the previous data from the raw file.
309    *
310    * @param source
311    * @throws IOException
312    */
 
313  0 toggle public void readRaw(String source) throws IOException
314    {
315  0 BufferedReader input = new BufferedReader(new FileReader(source + RAW));
316  0 String line = input.readLine();
317  0 if (line == null)
318    {
319  0 input.close();
320  0 return;
321    }
322  0 Scanner numberScanner = new Scanner(line);
323  0 numberScanner.useDelimiter(",");
324  0 raw = new ArrayList<>();
325  0 while (numberScanner.hasNext())
326    {
327  0 numberScanner.next();
328  0 raw.add(new ArrayList<Double>());
329    }
330  0 numberScanner.close();
331   
332  0 line = input.readLine();
333  0 while (!("".equals(line) || line == null))
334    {
335  0 Scanner scanner = new Scanner(line);
336  0 scanner.useDelimiter(",");
337   
338  0 int i = 0;
339  0 while (scanner.hasNext())
340    {
341  0 String value;
342  0 value = scanner.next();
343  0 if (!value.equals("EMPTY"))
344    {
345  0 raw.get(i).add(Double.parseDouble(value));
346    }
347    else
348    {
349  0 raw.get(i).add(null);
350    }
351   
352  0 i++;
353    }
354  0 scanner.close();
355  0 line = input.readLine();
356    }
357   
358  0 input.close();
359    }
360   
361    /**
362    * Counts the number of valid residues in the sequence.
363    *
364    * @return
365    */
 
366  0 toggle public int countValidResidues()
367    {
368  0 int count = 0;
369   
370  0 for (int width = 0; width < sequences.size(); width++)
371    {
372  0 for (int length = 1; length < hmm.getLength() + 1; length++)
373    {
374  0 char symbol;
375  0 int alignPos;
376  0 alignPos = hmm.getNodeMapPosition(length);
377   
378  0 symbol = sequences.get(width).getCharAt(alignPos);
379  0 if (ResidueProperties.backgroundFrequencies.get("amino")
380    .containsKey(symbol))
381    {
382  0 count++;
383    }
384    }
385    }
386   
387  0 return count;
388    }
389   
390    /**
391    * Processes data, and stores it in both a raw and binned format.
392    *
393    * @param count
394    */
 
395  0 toggle public void processData(int count)
396    {
397  0 int rawPos = 0;
398  0 if (keepRaw)
399    {
400  0 raw.add(new ArrayList<Double>());
401  0 rawPos = raw.size() - 1;
402    }
403  0 Double total = 0d;
404  0 for (int width = 0; width < sequences.size(); width++)
405    {
406  0 for (int length = 1; length < hmm.getLength() + 1; length++)
407    {
408  0 char symbol;
409  0 int alignPos;
410  0 alignPos = hmm.getNodeMapPosition(length);
411   
412  0 symbol = sequences.get(width).getCharAt(alignPos);
413  0 if (ResidueProperties.backgroundFrequencies.get("amino")
414    .containsKey(symbol))
415    {
416  0 Double prob;
417  0 Float bfreq;
418  0 Double llr;
419  0 prob = hmm.getMatchEmissionProbability(alignPos, symbol);
420  0 bfreq = ResidueProperties.backgroundFrequencies.get("amino")
421    .get(symbol);
422  0 if (prob == 0 || bfreq == 0)
423    {
424  0 System.out.println("error");
425    }
426  0 llr = Math.log(prob / bfreq);
427  0 if (keepRaw)
428    {
429  0 raw.get(rawPos).add(llr);
430    }
431   
432  0 String output;
433  0 output = String.format("%.1f", llr);
434  0 total += Double.parseDouble(output);
435  0 if ("-0.0".equals(output))
436    {
437  0 output = "0.0";
438    }
439  0 if (binned.containsKey(output))
440    {
441  0 double prev = binned.get(output);
442  0 prev += (SCALE / count);
443  0 binned.put(output, prev);
444   
445    }
446    else
447    {
448  0 binned.put(output, SCALE / count);
449    }
450    }
451    }
452    }
453  0 System.out.println(total / count);
454    }
455   
456   
457    /**
458    * Reads in the sequence data from a Stockholm file.
459    *
460    * @param source
461    * @throws IOException
462    */
 
463  0 toggle public void readStockholm(BufferedReader inputSTO) throws IOException
464    {
465  0 FileParse parserSTO = new FileParse(inputSTO, "", DataSourceType.FILE);
466  0 StockholmFile file = new StockholmFile(parserSTO);
467  0 Vector<AlignmentAnnotation> annots = file.getAnnotations();
468   
469  0 for (AlignmentAnnotation annot : annots)
470    {
471  0 if (annot.label.contains("Reference"))
472    {
473  0 reference = annot;
474    }
475    }
476  0 sequences = file.getSeqs();
477    }
478   
479    /**
480    * Reads in the HMM data from a HMMer file.
481    *
482    * @param source
483    * @throws IOException
484    */
 
485  0 toggle public void readHMM(BufferedReader inputHMM) throws IOException
486    {
487  0 FileParse parserHMM = new FileParse(inputHMM, "", DataSourceType.FILE);
488  0 HMMFile file = new HMMFile(parserHMM);
489  0 hmm = file.getHMM();
490   
491   
492    }
493   
494    /**
495    * Exports both the binned and raw data into separate files.
496    *
497    * @param location
498    * @throws FileNotFoundException
499    */
 
500  0 toggle public void exportData(String location) throws FileNotFoundException
501    {
502  0 PrintWriter writerBin = new PrintWriter(new File(location + BINNED));
503  0 for (Map.Entry<String, Double> entry : binned.entrySet())
504    {
505  0 writerBin.println(entry.getKey() + "," + entry.getValue());
506    }
507  0 writerBin.close();
508  0 if (keepRaw)
509    {
510   
511  0 PrintWriter writerRaw = new PrintWriter(new File(location + RAW));
512   
513  0 StringBuilder identifier = new StringBuilder();
514   
515  0 for (int i = 1; i < raw.size() + 1; i++)
516    {
517  0 identifier.append("Fam " + i + ",");
518    }
519   
520  0 writerRaw.println(identifier);
521   
522  0 boolean rowIsEmpty = false;
523  0 int row = 0;
524  0 while (!rowIsEmpty)
525    {
526  0 rowIsEmpty = true;
527  0 StringBuilder string = new StringBuilder();
528  0 for (int column = 0; column < raw.size(); column++)
529    {
530  0 if (raw.get(column).size() <= row)
531    {
532  0 string.append("EMPTY,");
533    }
534    else
535    {
536  0 string.append(raw.get(column).get(row) + ",");
537  0 rowIsEmpty = false;
538    }
539    }
540  0 row++;
541  0 writerRaw.println(string);
542    }
543  0 writerRaw.close();
544   
545    }
546   
547    }
548   
549    /**
550    * Prints the specified family on the console.
551    *
552    * @param index
553    * @throws IOException
554    */
 
555  0 toggle public void printFam(int index) throws IOException
556    {
557  0 BufferedReader br = new BufferedReader(new FileReader(families));
558   
559  0 moveLocationBy(index, br);
560   
561  0 String line = br.readLine();
562   
563  0 while (!"//".equals(line))
564    {
565  0 System.out.println(line);
566  0 line = br.readLine();
567    }
568  0 System.out.println(line);
569  0 br.close();
570   
571    }
572   
573    /**
574    * Prints the specified HMM on the console.
575    *
576    * @param index
577    * @throws IOException
578    */
 
579  0 toggle public void printHMM(int index) throws IOException
580    {
581  0 BufferedReader br = new BufferedReader(new FileReader(hmms));
582   
583  0 moveLocationBy(index, br);
584   
585  0 String line = br.readLine();
586   
587  0 while (!"//".equals(line))
588    {
589  0 System.out.println(line);
590  0 line = br.readLine();
591    }
592  0 System.out.println(line);
593  0 br.close();
594   
595    }
596   
597    /**
598    * Prints the specified family to a .sto file.
599    *
600    * @param index
601    * @throws IOException
602    */
 
603  0 toggle public void exportFam(int index, String location) throws IOException
604    {
605  0 BufferedReader br = new BufferedReader(new FileReader(families));
606   
607  0 moveLocationBy(index, br);
608   
609  0 String line = br.readLine();
610  0 PrintWriter writer = new PrintWriter(
611    new FileOutputStream(new File(location), true));
612  0 while (!"//".equals(line))
613    {
614  0 writer.println(line);
615  0 line = br.readLine();
616    }
617  0 writer.println(line);
618  0 writer.close();
619  0 br.close();
620   
621    }
622   
 
623  0 toggle public void exportFile(BufferedReader br, String location, boolean append)
624    throws IOException
625    {
626  0 String line = br.readLine();
627  0 PrintWriter writer = new PrintWriter(
628    new FileOutputStream(location, append));
629  0 while (!"//".equals(line))
630    {
631  0 writer.println(line);
632  0 line = br.readLine();
633    }
634  0 writer.println(line);
635  0 writer.close();
636   
637   
638    }
639   
 
640  0 toggle public String getHMMName(int index) throws IOException
641    {
642  0 String name;
643   
644  0 BufferedReader nameFinder = new BufferedReader(new FileReader(hmms));
645   
646  0 moveLocationBy(index, nameFinder);
647   
648  0 nameFinder.readLine();
649   
650  0 Scanner scanner = new Scanner(nameFinder.readLine());
651  0 name = scanner.next();
652  0 name = scanner.next();
653  0 scanner.close();
654  0 return name;
655    }
656   
 
657  0 toggle public String getFamilyName(int index) throws IOException
658    {
659  0 String name;
660   
661  0 BufferedReader nameFinder = new BufferedReader(
662    new FileReader(families));
663   
664  0 moveLocationBy(index, nameFinder);
665   
666  0 nameFinder.readLine();
667   
668  0 Scanner scanner = new Scanner(nameFinder.readLine());
669  0 name = scanner.next();
670  0 name = scanner.next();
671  0 name = scanner.next();
672  0 scanner.close();
673  0 return name;
674    }
675   
676    /**
677    * Prints the specified family to a .hmm file.
678    *
679    * @param index
680    * @throws IOException
681    */
 
682  0 toggle public void exportHMM(int index, String location) throws IOException
683    {
684   
685   
686  0 BufferedReader br = new BufferedReader(new FileReader(hmms));
687   
688  0 moveLocationBy(index, br);
689   
690  0 String line = br.readLine();
691   
692  0 PrintWriter writer = new PrintWriter(
693    new FileOutputStream(new File(location), true));
694  0 while (!"//".equals(line))
695    {
696  0 writer.println(line);
697  0 line = br.readLine();
698    }
699  0 writer.println(line);
700  0 writer.close();
701  0 br.close();
702   
703    }
704   
705    /**
706    * Clears all raw, binned and current position data in the current directory.
707    *
708    * @throws FileNotFoundException
709    */
 
710  0 toggle public void clear() throws FileNotFoundException
711    {
712  0 PrintWriter pos = new PrintWriter(
713    currentFolder + "/CurrentPosition.txt");
714  0 pos.println("0");
715   
716  0 PrintWriter raw = new PrintWriter(currentFolder + RAW);
717   
718  0 PrintWriter bin = new PrintWriter(currentFolder + BINNED);
719   
720  0 pos.close();
721  0 bin.close();
722  0 raw.close();
723    }
724   
 
725  0 toggle public void sortIntoClans(String directory) throws IOException
726    {
727  0 BufferedReader clanFinder = new BufferedReader(new FileReader(FAMILIESTOCLAN));
728  0 BufferedReader familyReader = new BufferedReader(
729    new FileReader(families));
730  0 BufferedReader hmmReader = new BufferedReader(new FileReader(hmms));
731  0 int families = 0;
732    // moveLocationBy(7000, familyReader);
733    // moveLocationBy(7000, clanFinder);
734    // moveLocationBy(7000, hmmReader);
735  0 HashMap<String, Integer> clanIndexes = new HashMap<>();
736  0 ArrayList<Integer> familyCounts = new ArrayList<>();
737  0 int filePos = 0;
738  0 int clanCount = 0;
739  0 String line;
740  0 line = clanFinder.readLine();
741   
742  0 while (!"".equals(line) && !" ".equals(line) && line != null)
743    {
744  0 String clanName;
745  0 boolean inClan = false;
746  0 while (!(line.indexOf("//") > -1))
747    {
748   
749  0 if (line.indexOf("#=GF CL") > -1)
750    {
751  0 families++;
752  0 System.out.println(families);
753  0 inClan = true;
754  0 Scanner scanner = new Scanner(line);
755  0 scanner.next();
756  0 scanner.next();
757  0 clanName = scanner.next();
758  0 scanner.close();
759   
760  0 if (!clanIndexes.containsKey(clanName))
761    {
762  0 clanIndexes.put(clanName, clanCount);
763  0 clanCount++;
764  0 familyCounts.add(0);
765    }
766   
767   
768  0 Integer clanI = clanIndexes.get(clanName);
769  0 String clanPath = directory + "/Clan" + clanI.toString();
770  0 createFolders(clanPath);
771   
772  0 int index = clanIndexes.get(clanName);
773  0 exportFile(familyReader,
774    clanPath + "/Families/Fam" + familyCounts.get(index)
775    + ".sto",
776    false);
777  0 exportFile(hmmReader,
778    clanPath + "/HMMs/HMM" + familyCounts.get(index) + ".hmm",
779    false);
780   
781  0 int count = familyCounts.get(index);
782  0 count++;
783  0 familyCounts.set(index, count);
784    }
785  0 line = clanFinder.readLine();
786   
787    }
788  0 if (!inClan)
789    {
790  0 moveLocationBy(1, familyReader);
791  0 moveLocationBy(1, hmmReader);
792    }
793  0 filePos++;
794    // System.out.println(filePos + " files read.");
795  0 line = clanFinder.readLine();
796   
797    }
798  0 clanFinder.close();
799   
800  0 for (int clan = 0; clan < clanCount; clan++)
801    {
802  0 PrintWriter writer = new PrintWriter(
803    directory + "/Clan" + clan + "/NumberOfFamilies.txt");
804  0 int count = familyCounts.get(clan);
805  0 writer.print(count);
806  0 writer.close();
807    }
808   
809    }
810   
 
811  0 toggle public String getFamilies()
812    {
813  0 return families;
814    }
815   
 
816  0 toggle public void setFamilies(String families)
817    {
818  0 this.families = currentFolder + families;
819    }
820   
 
821  0 toggle public String getHmms()
822    {
823  0 return hmms;
824    }
825   
 
826  0 toggle public void setHmms(String hmms)
827    {
828  0 this.hmms = currentFolder + hmms;
829    }
830   
 
831  0 toggle public void alignWithinClan(String exportLocation, String clansLocation)
832    throws IOException, InterruptedException
833    {
834  0 int alignmentsExported = 0;
835  0 for (int clan = 0; clan < 604; clan++)
836    {
837  0 System.out.println(clan);
838  0 int famCount = 0;
839  0 String clanPath = clansLocation + "/Clan" + clan;
840  0 int numberOfFamilies;
841  0 BufferedReader br = new BufferedReader(
842    new FileReader(clanPath + "/NumberOfFamilies.txt"));
843  0 String line = br.readLine();
844  0 numberOfFamilies = Integer.parseInt(line);
845  0 br.close();
846  0 if (numberOfFamilies == 1)
847    {
848  0 continue;
849    }
850  0 final String commandExportLocation = exportLocation + "/Clan" + clan;
851  0 createFolders(commandExportLocation);
852  0 for (int family = 0; family < numberOfFamilies; family++)
853    {
854  0 famCount++;
855  0 ArrayList<Integer> indexes = new ArrayList<>();
856  0 for (int i = 0; i < numberOfFamilies; i++)
857    {
858  0 if (i != family)
859    {
860  0 indexes.add(i);
861    }
862    }
863   
864  0 int hmmIndex = getRandom(indexes);
865  0 String famPath = clanPath + "/Families/Fam" + family + ".sto";
866  0 String hmmPath = clanPath + "/HMMs/HMM" + hmmIndex + ".hmm";
867  0 String command = "/media/sf_Shared_Folder/hmmer/binaries/hmmalign --mapali "
868    + clanPath + "/Families/Fam" + hmmIndex + ".sto"
869    + " --trim ";
870  0 command += hmmPath + " ";
871  0 command += famPath;
872  0 final int familyIndex = family;
873  0 final Process p = Runtime.getRuntime().exec(command);
874   
875  0 new Thread(new Runnable()
876    {
 
877  0 toggle @Override
878    public void run()
879    {
880  0 BufferedReader input = new BufferedReader(
881    new InputStreamReader(p.getInputStream()));
882  0 String line = null;
883   
884  0 try
885    {
886  0 PrintWriter writer = new PrintWriter(commandExportLocation
887    + "/Families/Fam" + familyIndex + ".sto");
888  0 String lastLine = "";
889  0 boolean dataFound = false;
890  0 while ((line = input.readLine()) != null)
891    {
892  0 if (line.contains("#=GR") && !dataFound)
893    {
894  0 writer.println(lastLine);
895  0 dataFound = true;
896    }
897  0 if (line.contains("#") || dataFound || " ".equals(line)
898    || "".equals(line) || "//".equals(line))
899    {
900  0 writer.println(line);
901    }
902  0 lastLine = line;
903    }
904  0 writer.close();
905    } catch (IOException e)
906    {
907  0 e.printStackTrace();
908    }
909    }
910    }).start();
911   
912  0 p.waitFor();
913   
914  0 BufferedReader hmmExporter = new BufferedReader(
915    new FileReader(hmmPath));
916   
917  0 exportFile(hmmExporter,
918    commandExportLocation + "/HMMs/HMM" + family + ".hmm",
919    false);
920   
921  0 alignmentsExported++;
922   
923   
924    }
925  0 PrintWriter writer = new PrintWriter(
926    commandExportLocation + "/NumberOfFamilies.txt");
927  0 writer.print(famCount);
928  0 writer.close();
929    }
930   
931    }
932   
 
933  0 toggle public boolean atEnd(BufferedReader br) throws IOException
934    {
935  0 boolean end = false;
936  0 br.mark(80);
937  0 String line = br.readLine();
938  0 if ("".equals(line) || line == null)
939    {
940  0 end = true;
941    }
942  0 br.reset();
943  0 return end;
944    }
945   
 
946  0 toggle public int getRandom(ArrayList<Integer> list)
947    {
948  0 if (!(list.size() > 0))
949    {
950  0 System.out.println("Error - size = " + list.size());
951    }
952  0 int index = generator.nextInt(list.size());
953  0 int value = list.get(index);
954  0 list.remove(index);
955  0 return value;
956    }
957   
 
958  0 toggle public void createFolders(String clanPath)
959    {
960  0 File clanFolder = new File(clanPath);
961  0 if (!clanFolder.exists())
962    {
963  0 clanFolder.mkdir();
964    }
965   
966  0 File famFolder = new File(clanPath + "/Families");
967  0 File hmmFolder = new File(clanPath + "/HMMs");
968  0 if (!famFolder.exists())
969    {
970  0 famFolder.mkdir();
971  0 hmmFolder.mkdir();
972    }
973    }
974    }
975   
976   
977   
978