if  (id1 > id2) hi_id = id1; else hi_id = id2; if  (hi_id > hi_read) { hi_read = hi_id; Read . resize (hi_read + 1); }
short_id1 = Read_Subscript (id1); short_id2 = Read_Subscript (id2);
int  main    (int argc, char * argv [])  {   FILE  * fp;   vector <Unitig_Info_t>  unitig;   vector <Contig_Info_t>  contig;   vector <Contig_Pos_t>  contig_pos;   vector <Uni_Data_t>  uni_data;   vector <Con_Data_t>  con_data;   char  line [MAX_LINE];   bool  found;   float  cov;   short  copy_ct;   int  unitig_ct;   long long int  id1, id2, hi_id;   int  a, b, c;   int  hi_read = 0;   int  i, j, n;   if  (argc < 2)       {        fprintf (stderr, "USAGE:  %s <contig-info-file> [<repeat-coord-file>]\n",            argv [0]);        exit (EXIT_FAILURE);       }   fp = fopen (argv [1], "r");   assert (fp != NULL);   while  (fscanf (fp, "%lld %d %d %s", & id1, & a, & b, line) == 4)     {      Contig_Pos_t  c;      c . contig_id = id1;      c . a = a;      c . b = b;      c . tag = strdup (line);      contig_pos . push_back (c);     }   fclose (fp);   if  (argc > 2)       Output_Repeats (argv [2], contig_pos);   while  (fgets (line, MAX_LINE, stdin) != NULL)     {      char  tag [10], ori [10], typ [10];      double  mean, stddev;      sscanf (line, "%s", tag);      if  (strcmp (tag, "LKG") == 0)          {           sscanf (line + 4, "%lld %lld %lf %lf %s",               & id1, & id2, & mean, & stddev, ori);           if  (id1 > id2)               hi_id = id1;             else               hi_id = id2;           if  (hi_id > hi_read)               {                hi_read = hi_id;                Read . resize (hi_read + 1);               }           if  (ori [0] != 'I')               {                fprintf (stderr, "ERROR:  Unexpected orientation \"%s\"\n",                    ori);                fputs (line, stderr);                exit (EXIT_FAILURE);               }           if  (Read [id1] . mate != -1 || Read [id2] . mate != -1)               {                fprintf (stderr, "ERROR:  Duplicate mate\n");                fprintf (stderr, "id1 = %lld  mate = %d\n", id1, Read [id1] . mate);                fprintf (stderr, "id2 = %lld  mate = %d\n", id2, Read [id2] . mate);                exit (EXIT_FAILURE);               }           Read [id1] . mate = id2;           Read [id2] . mate = id1;           n = Dst . size ();           found = false;           for  (i = 0;  i < n && ! found;  i ++)             if  (Dst [i] . mean == mean && Dst [i] . stddev == stddev)                 {                  found = true;                  break;                 }           if  (! found)               {                Dst_Info_t  new_dst;                new_dst . mean = mean;                new_dst . stddev = stddev;                Dst . push_back (new_dst);               }           Read [id1] . dst = i;           Read [id2] . dst = i;                     }      else if  (strcmp (tag, "UTG") == 0)          {           sscanf (line + 4, "%lld %lld %d %d %s",               & id1, & id2, & a, & b, typ);           if  (id1 > hi_read)               {                hi_read = id1;                Read . resize (hi_read + 1);               }           if  (typ [0] != 'R')               {                fprintf (stderr, "ERROR:  Unexpected type \"%s\"\n",                    ori);                fputs (line, stderr);                exit (EXIT_FAILURE);               }           assert (Read [id1] . unitig_id == -1);           Read [id1] . unitig_id = id2;           Read [id1] . u_a_pos = a;           Read [id1] . u_b_pos = b;          }      else if  (strcmp (tag, "CCO") == 0)          {           sscanf (line + 4, "%lld %lld %d %d %s",               & id1, & id2, & a, & b, typ);           if  (id1 > hi_read)               {                hi_read = id1;                Read . resize (hi_read + 1);               }           if  (typ [0] != 'R')               {                fprintf (stderr, "ERROR:  Unexpected type \"%s\"\n",                    ori);                fputs (line, stderr);                exit (EXIT_FAILURE);               }           assert (Read [id1] . contig_id == -1);           Read [id1] . contig_id = id2;           Read [id1] . c_a_pos = a;           Read [id1] . c_b_pos = b;          }      else if  (strcmp (tag, "UNI") == 0)          {           Uni_Data_t  uni;           sscanf (line + 4, "%lld %f %s %d %d",               & id1, & cov, typ, & a, & b);           uni . unitig_id = id1;           uni . coverage = cov;           uni . typ = typ [0];           uni . num_reads = a;           uni . len = b;           uni_data . push_back (uni);          }      else if  (strcmp (tag, "CON") == 0)          {           Con_Data_t  con;           sscanf (line + 4, "%lld %s %d %d %d",               & id1, typ, & a, & b, & c);           con . contig_id = id1;           con . typ = typ [0];           con . num_reads = a;           con . num_unis = b;           con . len = c;           con_data . push_back (con);          }      else if  (strcmp (tag, "UPS") == 0)          {           Unitig_Info_t  u;           sscanf (line + 4, "%lld %lld %d %d %s",               & id1, & id2, & a, & b, typ);           u . unitig_id = id1;           u . contig_id = id2;           u . c_a_pos = a;           u . c_b_pos = b;           u . typ = typ [0];           unitig . push_back (u);          }      else if  (strcmp (tag, "SCF") == 0)          {           Contig_Info_t  c;           sscanf (line + 4, "%lld %lld %d %s %lf %lf",               & id1, & id2, & a, ori, & mean, & stddev);           assert (ori [0] == 'F' || ori [0] == 'R');           c . contig_id = id1;           c . scaff_id = id2;           c . scaff_place = a;           c . ori = ori [0];           c . gap_mean = mean;           c . gap_stddev = stddev;           contig . push_back (c);          }        else          {           fprintf (stderr, "ERROR:  Unexpected tag\n");           fputs (line, stderr);           exit (EXIT_FAILURE);          }     }   sort (unitig . begin (), unitig . end (), Unitig_Cmp);   sort (contig . begin (), contig . end (), Contig_Cmp);   sort (contig_pos . begin (), contig_pos . end (), Contig_Pos_Cmp);   sort (uni_data . begin (), uni_data . end (), Uni_Data_Cmp);   Print_Colour_Headers ();   // Still Need to Do:   // Add to  contig_pos  contigs that are not there already but   // are in the same scaffold as contigs that are in it.   // Set size of contigs in  contig_pos  to max unitig pos   // in them   // Also set  copy_num  and  copy_total  fields in  unitig  array   n = unitig . size ();fprintf (stderr, "### %d unitigs\n", n);   copy_ct = 0;   for  (i = 0;  i < n;  i ++)     {      j = Search (contig_pos, unitig [i] . contig_id);      unitig [i] . match = j;      if  (j >= 0)          {           if  (unitig [i] . c_a_pos > contig_pos [j] . len)               contig_pos [j] . len = unitig [i] . c_a_pos;           if  (unitig [i] . c_b_pos > contig_pos [j] . len)               contig_pos [j] . len = unitig [i] . c_b_pos;          }      if  (i == 0 || unitig [i] . unitig_id == unitig [i - 1] . unitig_id)          copy_ct ++;        else          {           for  (j = 1;  j <= copy_ct;  j ++)             unitig [i - j] . copy_total = copy_ct;           copy_ct = 1;          }      unitig [i] . copy_num = copy_ct;     }   for  (j = 1;  j <= copy_ct;  j ++)     unitig [n - j] . copy_total = copy_ct;      // Output celamy lines for matching unitigs   unitig_ct = 0;   for  (i = 0;  i < n;  i ++)     {      j = unitig [i] . match;      if  (j >= 0)          {           const char  * colour;           int  k, x, y;           Set_Position (x, y, unitig [i] . c_a_pos, unitig [i] . c_b_pos,               contig_pos [j] . a, contig_pos [j] . b, contig_pos [j] . len);           switch  (unitig [i] . typ)             {              case  'U' :                colour = Unique_Col_Id;                break;              case  'R' :                colour = Rock_Col_Id;                break;              case  'S' :                colour = Surr_Col_Id;                break;              case  's' :                colour = Degen_Col_Id;                break;              default :                fprintf (stderr, "ERROR:  Bad unitig type = \"%c\"\n",                    unitig [i] . typ);                exit (EXIT_FAILURE);             }           k = Uni_Data_Search (uni_data, unitig [i] . unitig_id);           if  (k < 0)               {                fprintf (stderr, "ERROR:  Missing unitig %lld in uni_data\n",                    unitig [i] . unitig_id);                exit (EXIT_FAILURE);               }           unitig_ct ++;           if  (x < y)               printf ("%dUnitig: %d A%s %d R6 # Utg%lld Ctg%lld"                   " cov=%.1f typ=%c nfr=%d len=%d",                   unitig_ct, x, colour, y, unitig [i] . unitig_id,                   unitig [i] . contig_id, uni_data [k] . coverage,                   uni_data [k] . typ, uni_data [k] . num_reads,                   uni_data [k] . len);             else               printf ("%dUnitig: %d A%s %d R6 # Utg%lld Ctg%lld"                   "  cov=%.1f typ=%c nfr=%d len=%d",                   unitig_ct, y, colour, x, unitig [i] . unitig_id,                   unitig [i] . contig_id, uni_data [k] . coverage,                   uni_data [k] . typ, uni_data [k] . num_reads,                   uni_data [k] . len);           if  (unitig [i] . copy_total > 1)               printf (" copy=%d of %d", unitig [i] . copy_num,                    unitig [i] . copy_total);           putchar ('\n');          }     }   // Mark scaffolds in  contig_pos   n = contig . size ();fprintf (stderr, "### %d contigs\n", n);   for  (i = 0;  i < n;  i ++)     {      j = Search (contig_pos, contig [i] . contig_id);      contig [i] . match = j;      if  (j >= 0)          contig_pos [j] . scaff_id = contig [i] . scaff_id;     }   // Output scaffold links   n = contig_pos . size ();   for  (i = 0;  i < n;  i ++)     contig_pos [i] . mark = false;   for  (i = 0;  i < n;  i ++)     if  (! contig_pos [i] . mark)         {          contig_pos [i] . mark = true;          printf ("LNK: %s", contig_pos [i] . tag);          if  (contig_pos [i] . scaff_id == -1)              printf (" A%s # Degenerate scaff\n", Degen_Link_Col_Id);            else              {               for  (j = i + 1;  j < n;  j ++)                 if  (contig_pos [j] . scaff_id == contig_pos [i] . scaff_id)                     {                      contig_pos [j] . mark = true;                      printf (" %s", contig_pos [j] . tag);                     }               printf (" A%s # Scaff %lld\n", Scaff_Link_Col_Id,                   contig_pos [i] . scaff_id);              }         }   // Find reads that are in the picture   n = Read . size ();fprintf (stderr, "### %d reads\n", n);   for  (i = 0;  i < n;  i ++)     {      Read [i] . mark = false;      if  (Read [i] . contig_id >= 0)          {           j = Search (contig_pos, Read [i] . contig_id);           if  (j >= 0)               {                Set_Position (Read [i] . cam_a, Read [i] . cam_b,                    Read [i] . c_a_pos, Read [i] . c_b_pos,                    contig_pos [j] . a, contig_pos [j] . b,                    contig_pos [j] . len);               }          }      else if  (Read [i] . unitig_id >= 0)          {           bool  done;           int  k;           j = Unitig_Search (unitig, Read [i] . unitig_id);           if  (j < 0)               fprintf (stderr, "** Unitig %lld not found\n",                    Read [i] . unitig_id);             else               {                done = false;                for  (k = j;  k < j + unitig [j] . copy_total && ! done;  k ++)                  {                   int  p;                   p = Search (contig_pos, unitig [k] . contig_id);                   if  (p >= 0)                       {                        int  x, y;                        Set_Position (x, y, unitig [k] . c_a_pos,                            unitig [k] . c_b_pos, contig_pos [p] . a,                            contig_pos [p] . b, contig_pos [p] . len);                        Set_Position (Read [i] . cam_a, Read [i] . cam_b,                            Read [i] . u_a_pos, Read [i] . u_b_pos,                            x, y, abs (x - y));                        done = true;                       }                  }               }          }     }   Set_Starting_Rows ();   for  (i = 0;  i < n;  i ++)     {      if  (Read [i] . cam_a > 0 && ! Read [i] . mark)          {           char  tag [200];           j = Read [i] . mate;           if  (j < 0)               {                sprintf (tag, "Single read %d (Utg%lld)", i, Read [i] . unitig_id);                Output_Single_Read (i, Single_Read_Col_Id, Single_Mate_Col_Id,                    SINGLE_READ_LEN, Good_Start_Row, tag);               }             else                {// fprintf (stderr, "%8d has mate %8d  cam_a [j] = %d\n", i, j, Read [j] . cam_a);                if  (Read [j] . cam_a > 0)                    Output_Mates (i, j);                  else                    {                     int  mean;                     sprintf (tag, "Read %d (Utg%lld) mate %d (Utg%lld) not in picture",                         i, Read [i] . unitig_id, j, Read [j] . unitig_id);                     mean = int (Dst [Read [i] . dst] . mean);                     Output_Single_Read (i, Infer_Read_Col_Id, Infer_Mate_Col_Id,                          mean, Missing_Start_Row, tag);                    }                Read [j] . mark = true;               }          }      Read [i] . mark = true;     }   return  0;  }