#Fragment_Guided_Graph_Assembly.pl

  This pipeline is designed to connect the contigs and fill their gaps by long reads, bac, fosmid or other contigs. Taking fosmid as an example, firstly, we align the fosmids to contigs with BWA MEM or Blasr. Secondly, filter the alignments with your limiting criteria. Thirdly, construct the graph taking contigs as nodes,fosmids as edges and the overlaping score as weight. Fourthly, find the path in graph with the highest scores. Finally, connect the contigs and fill their gaps by fosmid.

USAGE:
perl Fragment_Guided_Graph_Assembly.pl -Align=file1 -Ref=file2 -Query=file3 -MinIden=Num1 -MaxHang=Num2 -MinLap=Num3 -MinExtend=Num4 -Queue=String1 -Mem=String2 -Script=Dir1 -OurDir=Dir2
	
 [ -Align ]             The Alignment Between Query and Ref
 [ -Ref   ]             The Sequence of Ref
 [ -Query ]             The Sequence of Query Used to Connect Ref
 [ -MinIden ]           The MinIdentity of Alignment
 [ -MaxHang ]           The MaxOverhang for Ref and Query of Alignment
 [ -MinLap ]            The MinOverlap of Alignment
 [ -MinExtend ]         The MinExtend for Ref Extension
 [ -Queue ]             The Queue for PBS
 [ -Mem ]               The Memory for PBS
 [ -Script ]            The Dir of Scripts
 [ -OutDir ]            The OutPut Dir of Results

Note:
1.With the Fragment_Guided_Graph_Assembly.pl, you will get a pbs or shell script containing all steps for submitting,   or you can run the pipeline step by step
2.The input format of alignment is blasr m1(https://github.com/PacificBiosciences/blasr/wiki/Blasr-Output-Format)
  Blasr m1 format:
  qName tName qStrand tStrand score percentSimilarity tStart tEnd tLength qStart qEnd qLength nCells
3.The -Align is the alignment between reference and query. The format is blasr m1
  Reference is the contigs assembled by software
  Query is the long fragments like fosmid 
4.The -Ref is the contigs assembled by software
5.The -Query is the long fragments like fosmid
6.The -OutDir is the folder of output results
  The results contain the connected super-contigs named "Final_Connected.fasta", the non-connected contigs named "non-connected-ctg.fasta" and many temporary files with connecting information(these files will be illustrated below).
 


Introduction of scripts in the pipeline:
1.sam2blasr.pl
  Function: If the alignment softwore you use is BWA, you can change the format of sam into blasr m1 by this script 
  USAGE:    perl $0 <input.sam> <output.txt>
  Input:    The file in sam format
  Output:   The file in blasr m1 format

2.01-BLASRDeOverlap.pl
  Function: If the contigs have overlaps with each other, you can connect the contigs by self-alignment
  USAGE:    perl $0 <BlasrAlignFile> <ContigFile> <MergedContigFile> MinOverhangLength MinOverlapLength MinIdentity
            [ BlasrAlignFile ]    The Alignment File of Blasr m1 format
            [ ContigFile ]        The Contig Sequence Used to connected
            [ MergedContigFile ]  The Output Connected Contig Sequence
            [ MinOverhangLength ] The Max Overhang Length
            [ MinOverlapLength ]  The Min Overlap Length
            [ MinIdentity ]       The Min Identity 
  Input1:  The self-alignments of all contigs with blasr m1 format
  Input2:  The contigs fasta
  Output:  The connected super-contigs fasta

3.02-PacbioGAPFilter.pl
  Function: Filtering the alignment between contigs and fosmid by conditions which include: MinOverhang, 
            MinIdentity,MinOverlap,MinExtend and so on.
  USAGE:    perl $0 <$alignment_file> $MaxOverhang $MinIdentity $MinOverlap $MinExtend ><$outdir/02-PacbioGAPFilter.txt>
  Input:    The alignments between contigs and fosmid with blasr m1 format
  Output:   The filtered alignment with Blasr m1 format

3.03-PacbioGAPLinker.pl
  Function: Determining the orientations of contigs and fosmid based on the alignment. conditions including 
            MinOverhang and MinExtend
  USAGE:    perl $0 <$outdir/02-PacbioGAPFilter.txt> $MaxOverhang $MinExtend > $outdir/03-PacbioGAPLinker.txt
  Input:    The filtered alignment with Blasr m1 format
  Output:   The alignment between contigs and fosmid with their orientations
            Format like:
            Ctg_Ori chain identity overlap Ctg Fosmid Fosmid_Ori Start End Len Ctg_Start Ctg_End Ctg_Len	
            right 1 99.34 11385 Ctg Fosmid right 3315 14772 14772 0 11460 16550
        
4.construct_graph_final.pl
  Function: Constructing graph by the alignment of contig and fosmid, and the nodes are contigs and the edges are 
            fosmids. Then finding contigs paths with the correct orientatios based on graphs
  USAGE:    perl $0 <$outdir/03-PacbioGAPLinker.txt> $outdir
  Input:    The alignment between contigs and fosmid with their orientations
  Output1:  <cluster_ori.txt> contains the contigs'orientations of the path like:
            Ctg1 chain1 orientation chain2 orientation Ctg2 
            Ctg1 1 right 0 right Ctg2 
  Output2:  <ctg_pairs.txt> contains the pairs of contigs
  Output3:  <ctg_ctg_ori.txt> contains the orientation of both contigs in pairs

5.make_ctg_line.pl
  Function: Making the contig and fosmid into the same chain for connection.
            the method:Firstly determining the path chain by first contig, Then determining the next contig chain
            by the previous contig chain until all the contig chains and orientation are sure
  USAGE:    perl $0 <cluster_ori.txt> <cluster_ori_same_chain.txt>
  Input:    <cluster_ori.txt> contains the contigs' orientations of the path
  Output:   <cluster_ori_same_chain.txt> contains the orientations of all contigs in the same chain

6.make_junction_by_pos.pl
  Function: Extracting the alignment positions of contigs and fosmids for next connection
  USAGE:    perl $0 <ctg_pairs.txt> <ctg_ctg_ori.txt> <cluster_ori_same_chain.txt> <cluster_ori_same_chain_pos.txt>
  Input1:   <ctg_pairs.txt> contains the pairs of Contigs
  Input2:   <ctg_ctg_ori.txt> contains the orientation of both Contigs in pairs
  Input3:   <cluster_ori_same_chain.txt> contains the orientation of all Contigs in the same chain
  Output:   <cluster_ori_same_chain_pos.txt> contains the alignment information of all Contigs in the same chain

7.extract_ctg_infor_for_seq.pl
  Function: Extracting the alignment information of contig and fosmid for extracting the final sequence
  USAGE:    perl $0 <cluster_ori_same_chain_pos.txt> <cluster_ori_same_chain_pos_for_seq.txt>
  Input:    <cluster_ori_same_chain_pos.txt> contains the alignment information of all Contigs in the same chain
  Output:   <cluster_ori_same_chain_pos_for_seq.txt> contains the final position of every contig which will be used to            construct the final super-contigs

8.extract_seq_by_pos.pl
  Function: Extracting the final sequence of contig and fosmid by using the position supported 
            by previous results
  USAGE:    perl $0 <cluster_ori_same_chain_pos_for_seq.txt> <$ref_file> <$query_file> <Final_Connected.fasta>
  Input1:   <cluster_ori_same_chain_pos_for_seq.txt> contains the final position of every contig
            which will be used to construct the final super-contigs
  Input2:   <$ref_file> is the contig sequence
  Input3:   <$query_file> is the fosmid sequence
  Output1:  <Final_Connected.fasta> contains the final connected super-contigs and spliced contigs
  Output2:  <non-connected-ctg.fasta> contains the non-changed contigs

The final results contain the Connected sequence and Non-Connected sequence. Besides, the temporary files of contig-pairs, paths, and alignment information are also existed. All final results are stored in -OurDir which you have set.
