This file is indexed.

/usr/bin/altree-convert is in altree 1.3.1-4+b2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
#!/usr/bin/perl
# This program trasforms output files from different haplotype reconstructions programs (Phase, FAMHAP, ...) into input files for phylogeny reconstruction programs (PAUP or Phylip)

use strict;
use diagnostics;
use warnings;
use Getopt::Long; # qw(:config permute);
use Pod::Usage;
#use Getopt::Std;

our($opt_h, $opt_r, $opt_i, $opt_j, $opt_o, $opt_t, $opt_p, $opt_c, $opt_q, $opt_s);

our $VERSION;
$VERSION = sprintf "0.%03d", q$Revision: 290 $ =~ /(\d+)/g;


##### FONCTIONS ANNEXES GLOBALES POUR TOUS LES INPUTS ####

sub print_hash_ind
{
    my($hash_ind)=shift;
    my($clef);
    print "AFFICHAGE\n";
    foreach $clef (keys %{$hash_ind}) {
	print $clef ," ", $hash_ind->{$clef}->{"statut"}, " ", $hash_ind->{$clef}->{"haplo1"}, " ", $hash_ind->{$clef}->{"haplo2"}, "\n";
    }
}

#Verification that 
# - the hash contains 2 haplo per individual
# - the statut is defined
sub VerifyHashInd
{
    my($hash_ind)=shift;
    my($nb_loci)=shift;
    my($ind);
    
    foreach $ind (keys %{$hash_ind}) {
	if ((!exists $hash_ind->{$ind}->{"statut"}) && (!$opt_j)) {
	    die "Statut are not available in the input file: You should either provide a second input file (option -j ) or add it in the input file of phase and use the -c-1 option of phase";
	} elsif ((!exists $hash_ind->{$ind}->{"haplo1"}) 
		 || (!exists $hash_ind->{$ind}->{"haplo2"})) {
	    die "Undefined haplotypes for individual $ind\n";
	} elsif ((length($hash_ind->{$ind}->{"haplo1"}) != $nb_loci) 
		 || (length($hash_ind->{$ind}->{"haplo2"}) != $nb_loci)) {
	    my($long)=length($hash_ind->{$ind}->{"haplo1"});
	    die "$nb_loci, $long, Bad haplotype sequence length for individual: $ind\n";
	}
    }
}

############################

##### READ PHASE OUTPUT  #####

#Read information in the phase outfile (option -c)
#The info are stored in a hash{num_ind}->{"info"} 
#  (info=statut[if available], haplo1, haplo2)

sub ReadPhase
{   
    my($file)=shift;
    # Do we have to read statut in INPUT1 or not (use INPUT2)?
    my($read_statut)=shift;

    my($mark)=0;
    my(%hash_ind);
    my($passe_par_la)=0; #indicates if a section is read in the input file
    my($ligne, $nb_loci);
    my($file_has_statut)=0; #=1 when we are sure that info about statut are in the file

    open(INPUT1, '<', $file) || die "Unable to open '$file': $!\n";

    while ($ligne=<INPUT1>) {
	chomp($ligne);
	if ($ligne =~ /^BEGIN (OUTFILE_LIST|BESTPAIRS1|INPUT_SUMMARY)$/) {
	    if ($mark != 0) {
		die "Nested sections ! \n";
	    }
	    if ($1 eq "OUTFILE_LIST") {
		$mark=2;
	    } elsif ($1 eq "INPUT_SUMMARY") {
		$mark=3;
	    } else {
		$mark=1;
	    }
	    if ($passe_par_la & (1<<($mark-1)) ) {
		die "Double section 'BEGIN $1' !\n";
	    }
	    $passe_par_la |= (1<<($mark-1));
	    next;
	}
	if ($ligne =~ /^END (OUTFILE_LIST|BESTPAIRS1|INPUT_SUMMARY)$/) {
	    if ($mark == 0) {
		die "Section $1 ends before start ! \n";
	    }
	    $mark=0;
	    next;
	}
	if ($mark==1) {
	    my ($ligne2, $ligne3, $statut, $ind);
	    if ($ligne !~ /^([01])\s+([0-9A-Za-z\#_-]+)$/) {
		print STDERR "Skipping strange line : $ligne\n";
	    } else {
		$statut=$1;
		$ind=$2;
		my $i;
		for ($i=1; $i<=2; $i++) {
		    $ligne2=<INPUT1>;
		    chomp($ligne2);
		    $ligne2 =~ tr/()[] //d; # faire aussi un s/(-1)/?/;
		    $ligne2 =~ s/-1/?/g;
		    $hash_ind{$ind}->{"haplo".$i}=$ligne2; 
		}
		if ($read_statut) {
		    $hash_ind{$ind}->{"statut"}=$statut;
		}
	    }
	}
	if ($mark==2) {
	    if ($ligne =~ / : p-value for testing cases vs controls$/) {
		$file_has_statut=1;
	    }
	}
	if ($mark==3){
	    if ($ligne =~ /^Number of Loci: ([0-9]+)$/) {
		$nb_loci=$1;
	    }
	}
    }
    close(INPUT1);

    if ($passe_par_la != 7) {
	print "PASSE PAR LA=$passe_par_la \n";
	die "The program has either read too many sections or not enough!\n";
    }
    if ($file_has_statut == 0 && $read_statut == 1) {
	die "Statut are not available in the input file: You should either provide a second input file (option -j ) or add it in the input file of phase and use the -c-1 option of phase"
	}
    if  (%hash_ind) { #not empty
	return \%hash_ind, $nb_loci;
    } else {
	die "Error in reading Input 1: info have not been stored correctly in the hash. Check your input file?\n";
    }    
}

#To add statut info in the hash when it's not available in INPUT1
sub ReadStatut
{
    my ($file)=shift;
    my ($hash_ind)=shift;
    my(@tableau, $ligne);

    open(INPUT2, '<', $file) || die "Unable to open '$file': $!\n";

    while ($ligne=<INPUT2>) {
	chomp($ligne);
    	@tableau=split(/\s+/, $ligne);
	if (!exists($hash_ind->{$tableau[0]})) {
	    die "This individual doesn't exist in $opt_i: $tableau[0] !\n";
	}
	$hash_ind->{$tableau[0]}->{"statut"}=$tableau[1];
    }

    close(INPUT2);

    my($ind);
    foreach $ind (keys %{$hash_ind}) {
	if (!exists($hash_ind->{$tableau[0]}->{"statut"})) {
	    die "No statut was found for individual $ind !\n";
	}
    }
}

##########################################

#### READ FAMHAP OUTPUT ##################
#Read information in the FAMHAP outfile ( outfile=file1) 
#The info are stored in a hash{haploID}=haplotype_sequence
sub ReadFAMHAPOutfile
{
    my($file1)=shift;
    my($ligne, , $ligne2, @tableau, %hashHaploID);
    my($haplo, $HaploID);
    my($nb_loci, $nb_haplo);
    
    open(FAMHAPOUT, '<', $file1) || die "Unable to open '$file1': $!\n";
    
    while ($ligne=<FAMHAPOUT>) {
	chomp($ligne);
	if ($ligne =~ /^Selected loci:/) {
	    chomp($ligne);
	    @tableau=split(/\s+/, $ligne);
	    $nb_loci=$#tableau-1;
	}
	if ($ligne =~ /HaploiD: ([0-9]+)/) {
	    $HaploID=$1;
	    @tableau=split(/\s+/, $ligne);
	    $haplo="";
	    my($i);
	    if (not defined $nb_loci) {
		die "Problem in the input file: nb_loci could not be read: $nb_loci\n";
	    } else {
		for ($i=0; $i<$nb_loci; $i++) {
		    $haplo.=$tableau[$i];
		    # Je ne vérifie pas que les alleles font bien un seul 
		    # caractère (une seuk chiffre ou une seule lettre...
		}
	    }
	    #hash dont clef=HaploID, et valeur = la seq de l'haplotype
	    if ($HaploID ne "") {
		$hashHaploID{$HaploID}=$haplo;
		#DEBUG print "haplo=$haplo, hashHaploID=$HaploID hashHaploID{haplo}=	$hashHaploID{$HaploID}\n";
	    } else {
		die "Problem in the input file, HaploID could not be correctly read: $HaploID\n";
	    }
	}
    }
    close(FAMHAPOUT);
    return (\%hashHaploID, $nb_loci);
}

#Read information in the FAMHAP Hx_MOSTLIKELI (file2) 
#uses the hashHaploID 
sub ReadFAMHAPMostLikeli
{
    my($file2)=shift;
    my($hashHaploID)=shift;
    my($ligne, $FT, $FID, $FNT, $MT, $MNT);
    my(%hash_ind);
    
    open(OUTHAPLO, '<', $file2) || die "Unable to open '$file2': $!\n";
    my($index)=0;
    while ($ligne=<OUTHAPLO>) {
	chomp($ligne);
	if ($ligne =~ /^\s*FID\s+PID\s+HAPLO1\s+HAPLO2\s+HAPLO3\s+HAPLO4\s+LIKELIHOOD_WEIGHT/) {
	    $index=1;
	    next;
	}
	if ($index==1) {
	    if ($ligne =~ /^\s*(\d+)\s+(.+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(.+)/) { 
		$FID=$1;
		$FT=$3;
		$FNT=$4;
		$MT=$5;
		$MNT=$6;
		# print "hashHaploID3= $hashHaploID->{$3}\n";
		$hash_ind{$FID."0"}->{"haplo1"}=$hashHaploID->{$FNT};
		$hash_ind{$FID."0"}->{"haplo2"}=$hashHaploID->{$MNT};
		$hash_ind{$FID."0"}->{"statut"}=0;
		$hash_ind{$FID."1"}->{"haplo1"}=$hashHaploID->{$FT};
		$hash_ind{$FID."1"}->{"haplo2"}=$hashHaploID->{$MT};
		$hash_ind{$FID."1"}->{"statut"}=1;
	    } elsif ($ligne =~ /^$/) {
		next;
	    } else {
		print "strange line $ligne\n";
	    }
	}
    }
    close(OUTHAPLO);
    return(\%hash_ind);
}




# call the different function involved in the reading of FAMHAP input file and storage of the infoin a hash{num_ind}->{"info"} (info=statut[if available], haplo1, haplo2)
sub ReadFAMHAP
{
    my($file1)=shift;
    my($file2)=shift;
    my($hashHaploID, $hash_ind);
    my($haplo);
    my($nb_loci);
    ($hashHaploID, $nb_loci)=ReadFAMHAPOutfile($file1);
    ($hash_ind)= ReadFAMHAPMostLikeli($file2, $hashHaploID);   
#print "nb_haplo= $nb_haplo, nb_loci=$nb_loci\n";
  #  foreach $haplo (keys %{$hashHaploID}) {
  #  	print "haplo: $haplo hash=$hashHaploID->{$haplo}\n";
  #  }
   # print_hash_ind($hash_ind);
    return($hash_ind, $nb_loci);
}

##########################################

#### DATA TRANSFO ########################
sub TransfoData
{
    my($hash_ind)=shift;
    my($data_quali)=shift;
    my($j)=1;
    my($ind, %hash_haplo);
    

     
    foreach $ind (keys %{$hash_ind}) {
	if ($data_quali eq "quali") {
	    if (($hash_ind->{$ind}->{"statut"} != 0) 
		&& ($hash_ind->{$ind}->{"statut"} !=1)) {
		die "Invalid disease statut for individual $ind: ", 
		$hash_ind->{$ind}->{"statut"}, "\n";
	    } else {
		my($i);
		for ($i=1; $i<=2; $i++) {
		    $hash_haplo{$hash_ind->{$ind}->{"haplo".$i}}->[$hash_ind->{$ind}->{"statut"}]++;
		    $hash_haplo{$hash_ind->{$ind}->{"haplo".$i}}->[0]+=0; # moche mais pour eviter les non défini dans affichage
		    $hash_haplo{$hash_ind->{$ind}->{"haplo".$i}}->[1]+=0;
		    
		    if (!exists $hash_haplo{$hash_ind->{$ind}->{"haplo".$i}}->[2]) {
			$hash_haplo{$hash_ind->{$ind}->{"haplo".$i}}->[2]=$j;
			$j++;
		    }
		}
	    }
	} else {
	    # In this case, $hash_haplo is a hash with $haplo as keys and a 
            # ref on a tab containing the quantitative values as values.
	    for (my $i=1; $i<=2; $i++) {
		push (@{$hash_haplo{$hash_ind->{$ind}->{"haplo".$i}}->{"valeurs"}}, $hash_ind->{$ind}->{"statut"});
		if (!exists $hash_haplo{$hash_ind->{$ind}->{"haplo".$i}}->{"name"}) {
		    $hash_haplo{$hash_ind->{$ind}->{"haplo".$i}}->{"name"}=$j;
		    $j++;
		}
		
	    }
	}
    }
    my($nb_haplo);
    $nb_haplo=keys %hash_haplo;
   # print "Nb_haplo= $nb_haplo\n";
    return \%hash_haplo, $nb_haplo;
}      
###########################################

#### BUILDING OF DIFFERENT OUTPUT FILE ####

#Affiche juste les haplo. Surtout pour debug
sub AfficheHashHaplo
{
    my($hash_haplo)=shift;
    my($haplo);
    foreach $haplo (keys %{$hash_haplo}) {
	printf "H%.3i_c%.3i_m%.3i\t%s\n", $hash_haplo->{$haplo}->[2],$hash_haplo->{$haplo}->[0], $hash_haplo->{$haplo}->[1], $haplo;
#	print "hash_haplo->{$haplo}->[1]", $hash_haplo->{$haplo}->[1], "\n";
    }
} 


# Prepare a file (correspond.txt) in which the number of cases and the number of controls are specified for each haplotype.
sub MakeCorrespondanceFile
{
    my($hash_haplo)=shift;
    my($file_corres)=shift;
    my $data_quali = shift;
    my $seuil = shift;
    my($haplo);
    open (CORRESP, '>', $file_corres) || die "Unable to open correspond.txt: $!\n";
    my $hash_haplo_sorted; # ref on tab containing the haplotypes sorted on the number of individuals carrying them
	
    if ($data_quali eq "quali") {
	$hash_haplo_sorted = SortNbhaploQuali($hash_haplo, $seuil);
    } else {
	$hash_haplo_sorted = SortNbhaploQuanti($hash_haplo, $seuil);
    }
    
#    foreach $haplo (keys %{$hash_haplo}) {
    foreach $haplo (@{$hash_haplo_sorted}) { #new version with sorted sequences
	if ($data_quali eq "quali") {
	    printf CORRESP "H%.3i\tm%.3i\tc%.3i\n", $hash_haplo->{$haplo}->[2],$hash_haplo->{$haplo}->[1], $hash_haplo->{$haplo}->[0];
	} else {
	    printf CORRESP "H%.3i\t", $hash_haplo->{$haplo}->{"name"};  
	    foreach my $valeurs (@{$hash_haplo->{$haplo}->{"valeurs"}}) {
		print CORRESP "$valeurs\t";
	    }
	    print CORRESP "\n";
	}
    }
    
    close(CORRESP);
}


# Select and sort the haplotypes that will appear in the paup file and in the correspondance file. Does not work with phylip yet

sub SortNbhaploQuali
{
    my $hash_haplo = shift;
    my $seuil = shift;
 
    my @hash_haplo_sorted = sort {
	$hash_haplo->{$a}->[0]+$hash_haplo->{$a}->[1] <=>
	    $hash_haplo->{$b}->[0]+$hash_haplo->{$b}->[1]
	} (grep { 
	    $hash_haplo->{$_}->[0]+$hash_haplo->{$_}->[1]>$seuil
	} (keys %{$hash_haplo}));
    
    return \@hash_haplo_sorted;
}



sub SortNbhaploQuanti
{
    my $hash_haplo = shift;
    my $seuil =shift;
    
    my @hash_haplo_sorted = sort  {
	$#$hash_haplo->{$a}->{"valeurs"} <=> 
	    $#$hash_haplo->{$b}->{"valeurs"} 
    }  (grep { 
	$hash_haplo->{$_}->[0]+$hash_haplo->{$_}->[1]>$seuil
	}   (keys %{$hash_haplo}));
    
    return \@hash_haplo_sorted;
    
}





# Read the opt_t and define the character to add to the ancestral sequence ($anc) and to the other haplotypes ($der). Also define the "format symbol for paup.
sub ReadDataType
{
    my($data_type)=shift;
#    my ($der, $anc);
    my($write_data_type);
    if ($data_type eq "DNA") {
	$write_data_type="ATGCU";
#	$anc="G";
#	$der="C";
    } elsif ($data_type eq "NUM") {
	$write_data_type="0123456789";
#	$anc="1";
#	$der="0";
    } else {
	die "Invalid data type specified in opt_t: $opt_t. You should use DNA or NUM\n";
    }
    return ($write_data_type);
#   return ($anc, $der, $write_data_type);
}



#Build a PAUP OUTFILE
sub BuildPAUP
{ 
    my($file)=shift;
    my($hash_haplo)=shift;
    my($nb_haplo)=shift;
    my($nb_loci)=shift;
    my($data_type)=shift;
    my($file_corres)= shift;
    my $data_quali=shift;
    my $seuil = shift;
#my($der, $anc);
    my($write_data_type);
    
    MakeCorrespondanceFile($hash_haplo, $file_corres, $data_quali, $seuil);
    #($anc, $der, $write_data_type)=ReadDataType($data_type);
    ($write_data_type)=ReadDataType($data_type);
    my $hash_haplo_sorted;
    if ($data_quali eq "quali") {
	$hash_haplo_sorted = SortNbhaploQuali($hash_haplo, $seuil);
    } else {
	$hash_haplo_sorted = SortNbhaploQuanti($hash_haplo, $seuil);
    }

    open(OUTPAUP, '>', $file) || die "Unable to open '$file': $!\n";
    print OUTPAUP "#Nexus\n";
    print OUTPAUP "Begin data;\n";
    print OUTPAUP "dimension ntax=",$#$hash_haplo_sorted+1," nchar=", $nb_loci,";\n";
    print OUTPAUP "format symbols=\"$write_data_type\" missing=?;\n";
    print OUTPAUP "matrix\n";
    my($haplo);
#    foreach $haplo (keys %{$hash_haplo}) {
#	printf OUTPAUP "H%.3i_m%.3i_c%.3i\t%s%s\n", $hash_haplo->{$haplo}->[2],$hash_haplo->{$haplo}->[1], $hash_haplo->{$haplo}->[0], $haplo, $der; # a modifier si je fais un deuxième fichier de sortie...
    foreach $haplo (@{$hash_haplo_sorted}) {
    if ($data_quali eq "quali") {
	    printf OUTPAUP "H%.3i\t%s\n", $hash_haplo->{$haplo}->[2], $haplo;#, $der; 
	} else {
	    printf OUTPAUP "H%.3i\t%s\n",  $hash_haplo->{$haplo}->{"name"}, $haplo;
	}
    }
    print OUTPAUP "H000_ancetre [add ancestral haplotype]\n";#$anc\n";
    print OUTPAUP ";\n";
    print OUTPAUP "end;\n";
    print OUTPAUP "begin assumptions;\n";
    print OUTPAUP "ancstates *anc vector = [add ancestral haplotype];\n";
    print OUTPAUP "end;\n";
    print OUTPAUP "begin paup;\n";
    print OUTPAUP "set nowarnreset autoclose maxtrees = [2000] increase=[no - Auto AutoInc = 100] monitor = no taxlabels = full\n";
    print OUTPAUP "root=lundberg warnroot=no opt=[deltran - acctran] ancstates=anc;\n";
    print OUTPAUP "hsearch;\n";
    print OUTPAUP "savetrees [from=1 to=1] file=[test.tree] root=yes format=altnexus;\n";
    print OUTPAUP "cleartrees nowarn=yes;\n";
    print OUTPAUP "gettrees rooted=yes file=[test.tree];\n";
    print OUTPAUP "log file = [test.res.log] replace=[yes - no];\n"; 
    print OUTPAUP "describetrees [all] /plot=[cladogram - phylogram] brlens=yes rootmethod=lundberg apolist=yes;\n";
    print OUTPAUP "log stop;\n";
    print OUTPAUP "end;\n";
    print OUTPAUP "quit;\n";
    print OUTPAUP "[WARNING, this file is designed for bi-allelic data. If it is not the case, you may have to modify this input file, for example by specifying the ordered option in the typeset command in the assumption block]\n";
    close(OUTPAUP);

}

#Build a PHYLIP OUTFILE
sub BuildPHYLIP
{ 
    my ($file)=shift;
    my($hash_haplo)=shift;
    my($nb_haplo)=shift;
    my($nb_loci)=shift;
    my($data_type)=shift;
    my($file_corres)=shift;
  #  my ($der, $anc);
    my($write_data_type);

#### SI ON UTILISE PHYLIP? FAIRE UN TEST: on doit avoir du 0/1 ou du ATGCU... SInon, marche pas!


    MakeCorrespondanceFile($hash_haplo, $file_corres);
#    ($anc, $der, $write_data_type)=ReadDataType($data_type);
    ($write_data_type)=ReadDataType($data_type);
    open(OUTPHYLIP, '>', $file) || die "Unable to open '$file': $!\n";
    print OUTPHYLIP  "\t", $nb_haplo+1, "\t",  $nb_loci+1, "\n";
    my($haplo);
    foreach $haplo (keys %{$hash_haplo}) {#### SI ON UTILISE PHYLIP? FAIRE UN TEST: on doit avoir du 0/1 ou du ATGCU... SInon, marche pas!
	printf OUTPHYLIP "H%.3i%s\t%s\n", $hash_haplo->{$haplo}->[2],"      ", $haplo;#, $der;
    }
   # print OUTPHYLIP "H000_anc  ", "\t", "[ancestral sequence]\n";#$anc\n";
    close(OUTPHYLIP);
   # print STDERR "Don't forget to prepare the ancestor file containing the ancestor sequence followed by the character $anc\n";
}
############################################


sub main
{
    my($progname, $rec_program);
    my %options= (
		  "first-input-file" => \$opt_i,
		  "second-input-file" => \$opt_j,
		  "output-file" => \$opt_o,
		  "case-control-output" => \$opt_c,
		  "data-type" => \$opt_t,
		  "phylo-prog" => \$opt_p,
		  "reconstruct-prog" => \$opt_r,
		  "data-quali" => \$opt_q,
		  "nbind-threshold" => \$opt_s,	  
		  );
    	
    #getopts('hr:i:j:o:t:p:');
    GetOptions (\%options,
		"version",
		"short-help|h",
		"help",
		"man",
		"first-input-file|i=s",
                "second-input-file|j=s",
                "output-file|o=s",
		"case-control-output|c=s",
		"data-type|t=s",
		"phylo-prog|p=s",
		"reconstruct-prog|r=s",
		"data-quali|q=s",
		"nbind-threshold|s=i",
		) or pod2usage(2);
    if (defined($options{"version"})) {
	print $0, " version ", $VERSION, "\n";
	print "(Perl version ", $], ")\n";
	exit 0;
    }
    if (defined($options{"short-help"})) {
	pod2usage(-exitstatus => 0, -verbose => 0);
    }
    if (defined($options{"help"})) {
	pod2usage(-exitstatus => 0, -verbose => 1);
    }
    if (defined($options{"man"})) {
	pod2usage(-exitstatus => 0, -verbose => 2);
    }
    
    
#    if ($opt_h) {
#	($progname = $0) =~ s|^.*/([^/]*)$|$1|;
#	print STDERR "usage : $progname \n";
#	print STDERR "\t-r Haplotype reconstruction program\n";
#	print STDERR "\t-i Input file 1\n";
#	print STDERR "\t[-j Input file 2]\n";
#	print STDERR "\t-o Output file  \n";
#	print STDERR "\t-t Type of data: DNA (ATGCU) or NUM (0-9)\n";
#	print STDERR "\t-p Phylogeny reconstruction program\n";
#	print STDERR "\t-h: this help\n";
#	exit (1);
#    }
    if ($opt_r){
	$rec_program = $opt_r;
    } else {
	die "Name of the haplotype reconstruction program used missing: opt_r\n";
    }    
    if (!$opt_i) { # Principal outfile of haplotypic reconstruction program
	die "No input file\n";
    }
    if (!$opt_o) { # Output file for a phylogeny reconstruction program
	die "No output file\n";
    }
    if (!$opt_c) { # Output file for a phylogeny reconstruction program
	die "No case-control statut output file\n";
    }
    my($phylo_program);
    if ($opt_p){
	$phylo_program = $opt_p;
    } else {
	die "Name of the phylogeny reconstruction program used missing: opt_p\n";
    }  
    if (!$opt_t){
	die "Type of data: opt_t (DNA or NUM) not specified!\n"; #il faudra peut-etre vérifier que les données sont bien du bon type??? 
    }
    my $data_quali;
    if (! $opt_q){
	die "Data quality: opt_q, (qualitative or quantitative) not specified!\n";
    }
    if (not defined $opt_s){
	die "Nb ind threshold: opt_s  not specified!\n";
    }
    
    my($hash_ind, $hash_statut, $hash_haplo);
    my($nb_haplo, $nb_loci);

    if ($rec_program =~ /^[Pp][Hh][Aa][Ss][Ee]$/) {
	($hash_ind, $nb_loci)=ReadPhase($opt_i, !$opt_j);
	VerifyHashInd($hash_ind, $nb_loci);
	if ($opt_j) {
	    ReadStatut($opt_j, $hash_ind);
	}
    } elsif ($rec_program =~ /^[Ff][Aa][Mm][Hh][Aa][Pp]$/) {
	if (!$opt_j) {
	    die "No file H1_MOSTLIKELI or H0_MOSTLIKELI provided\n";
	} else {
	   ($hash_ind, $nb_loci)=ReadFAMHAP($opt_i, $opt_j);
	    VerifyHashInd($hash_ind, $nb_loci);
	}
    } else {
	die "Unknown software. Check the -r option! \n";
    }

    if ($opt_q =~ /^[Qq][Uu][Aa][Ll][Ii][Tt][Aa][Tt][Ii][Vv][Ee]$/) {
	$data_quali="quali";
    } elsif ($opt_q =~ /^[Qq][Uu][Aa][Nn][Tt][Ii][Tt][Aa][Tt][Ii][Vv][Ee]$/) {
	$data_quali="quanti";
    } else {
	die "Unknown data_qual. Check the -q option! \n";
    }
	
    
    #print_hash_ind($hash_ind);       
    
    ($hash_haplo, $nb_haplo)=TransfoData($hash_ind, $data_quali);
   # AfficheHashHaplo($hash_haplo); # pour verifier
   
    if ($phylo_program =~ /^[Pp][Aa][Uu][Pp]$/) {
	BuildPAUP($opt_o, $hash_haplo, $nb_haplo, $nb_loci, $opt_t, $opt_c, $data_quali, $opt_s)
    } elsif ($phylo_program =~ /^[Pp][Hh][Yy][Ll][Ii][Pp]$/) {
	BuildPHYLIP($opt_o, $hash_haplo, $nb_haplo, $nb_loci, $opt_t, $opt_c);
    } else {
	die "Unknown phylogeny software. Check the -p option! \n";
    }
    
}

main;

__END__
    
=head1 NAME

altree-convert - Title...

=head1 SYNOPSIS

altree-convert [options]

 Options:
    --version             program version
    --short-help|h        brief help message
    --help                help message with options descriptions
    --man                 full documentation
    --first-input-file|i  input file 1
    --second-input-file|j input file 2 (not mandatory)
    --output-file|o       output file
    --case-control-output|c  output containing the nb cases/controls
    --data-type|t         DNA|NUM
    --phylo-prog|p        PAUP|PHYLIP
    --reconstruct-prog|r  PHASE|FAMHAP
    --data-quali|q        Type of data: qualitative or quantitative
    --nbind-threshold|s   Minimum number of individuals ecquired to keep an haplotype

=head1 OPTIONS

=over 8

=item B<--version>

Print the program version and exits.

=item B<--short-help>

Print a brief help message and exits.

=item B<--help>

Print a help message with options descriptions and exits.

=item B<--man>

Prints the manual page and exits.

=item B<--first-input-file|i>

Input file 1 (output of the haplotype reconstruction program)

=item B<--second-input-file|j>

Input file 2 (second output of famhap or file containig the disease status)

=item B<--output-file|o>

Output file

=item B<--case-control-output|c>

Output file containing the number of cases and controls carrying each haplotype

=item B<--data-type|t> C<DNA>|C<SNP>

Type of data: DNA (ATGCU) or SNP (0-1)

=item B<--phylo-prog|p> C<phylip>|C<paup>

Phylogeny reconstruction program

=item B<reconstruct-prog|r> C<famhap|phase>

Haplotype reconstruction program

=item B<data-quali|q> C<qualitative|quantitative>

Type of data analyzed

=item B<nbind-threshold|s> 

Minimal number of individuals carrying an haplotype recquired to keep it for further analysis. If you want to keep all haplotypes, you must affect 0 to this variable

=back

=head1 DESCRIPTION

B<This program> will read the given input file(s) and  generate an input file for the phylogenetic reconstruction software paup or phylip/paml

=cut